class VideoCapture
{
public:
VideoCapture(); // 아무런 인자를 받지 않는 기본적인 생성자
// 여러개의 인자를 받는 생성자
VideoCapture(const String& filename, int apiPreference = CAP_ANY); // 문자열 받는 형태
VideoCapture(int index, int apiPreference = CAP_ANY); // 정수값 받는 형태
virtual ~VideoCapture(); // 소멸자
virtual bool open(const String& filename, int apiPreference = CAP_ANY); // 문자열을 받는 형태
virtual bool open(int index, int apiPreference = CAP_ANY); // 정수값을 받는 형태
vitrual void release();
// 문자열은 받는 형태의 함수들은 동영상 파일들을 오픈할때 사용하는 함수
// 정수값을 받는 형태는 카메라 장치를 열때 사용하는 함수
//프레임 받아오기
virtual VideoCapture& operator >> (Mat& image);
virtual bool VideoCapture::read(OutputArray image);
// 속성 설정과 참조
virtual bool set(int propId, double value);
virtual double get(int propId) const;
}
VideoCapture::VideoCapture(int index, int apiPreference = CAP_ANY);
bool VideoCapture::open(int index, int apiPreference = CAP_ANY);
VideoCapture::VideoCapture(const String& filename, int apiPreference = CAP_ANY);
bool VideoCapture::open(const String& filename, int apiPreference = CAP_ANY);
bool VideoCapture::read(OutputArray image);
VideoCapture& VideoCapture::operator >> (Mat& image);
VideoCapture& VideoCapture::operator >> (Mat& image)
{
read(image);
return *this;
}
int main()
{
// VideoCapture 객체 생성 후 0번 카메라 열기.
VideoCapture cap;
cap.open(0); // VideoCapture cap(0); 과 같음
if (!cap.isOpened()) {
cerr << "Camera open failed!" << endl;
return -1;
}
Mat frame, edge;
while (true) {
// VideoCapture 객체로부터 한 프레임을 받아 frame 변수에 저장
cap >> frame;
if (frame.empty()) {
break;
}
// 받아온 프레임 처리 및 화면 출력
Canny(frame, edge, 50, 150);
imshow("frame", frame);
imshow("edge", edge);
// 일정 시간(10ms) 기다린 후 다음 프레임을 처리.
// 만약 키 입력이 있으면 프로그램을 종료
if (waitKey(10) == 0)
break;
}
}
int main()
{
// VideoCapture 객체 생성 후 test_video.mp4 파일 열기
VideoCapture cap;
cap.open("test_video.mp4"); // VideoCapture cap("test_video.mp4");와 같음
if (!cap.isOpened()) {
cerr << "Camera open failed!" << endl;
return -1;
}
Mat frame, edge;
while (true) {
// VideoCapture 객체로부터 한 프레임을 받아 frame 변수에 저장
cap >> frame;
if (frame.empty()) {
break;
}
// 받아온 프레임 처리 및 화면 출력
Canny(frame, edge, 50, 150);
imshow("frame", frame);
imshow("edge", edge);
// 일정 시간(10ms) 기다린 후 다음 프레임을 처리.
// 만약 키 입력이 있으면 프로그램을 종료
if (waitKey(10) == 0)
break;
}
}
double VideoCapture::get(int propId) const;
bool VideoCapture::set(int propId, double value);
propId: 속성 플래그. cv::VideoCaptureProperties
또는 Additional flags for video I/O API backends
상수 중 선택
value: 속성 값. 필요한 경우 정수형으로 형변환하여 사용
1280 x 720 프레임으로 받아오고 싶을 경우
cap.set(CAP_PROP_FRAME_WIDTH,1280);
cap.set(CAP_PROP_FRAME_HEIGHT,720);
fps 값 받아와서 출력하기
double fps = cap.get(CAP_PROP_FPS)
class VideoWriter
{
public:
VideoWriter();
// 저장할 동영상 파일 열기
VideoWriter(const String& filename, int fourcc, double fps, Size frameSize, bool isColor = true);
virtual ~VideoCapture();
// 저장할 동영상 파일 열기
virtual bool open(const String& filename, int fourcc, double fps, Size frameSize, bool isColor = true);
virtual void release();
// 프레임 저장
virtual VideoCapture& operator << (const Mat& image);
// 정보 설정하기 & 가져오기
virtual bool set(int propId, double value);
virtual double get(int propId) const;
}
VideoWriter::VideoWriter(const String& filename, int fourcc, double fps, Size frameSize, bool isColor = true);
bool VideoWriter::open(const String& filename, int fourcc, double fps, Size frameSize, bool isColor = true);
int main()
{
VideoCapture cap(0);
int fourcc = VideoWriter::fourcc('X', 'V', 'I', 'D');
double fps = 15;
Size sz((int)cap.get(CAP_PROP_FRAME_WIDTH), (int)cap.get(CAP_PROP_FRAME_HEIGHT));
VideoWriter output("output.avi", fourcc, fps, sz);
int delay = cvRound(1000 / fps);
Mat frame;
while (true) {
cap >> frame;
outputVideo << frame;
imshow("frame", frame);
if (waitKey(delay) == 27)
break;
}
}