카메라와 동영상으로부터 프레임(frame)을 받아오는 작업 수행
VideoCapture::VideoCapture(int index, int apiPreference = CAP_ANY)
VideoCapture::VideoCapture(const String& filename, int apiPreference = CAP_ANY)
VideoCapture::open(int index, int apiPreference = CAP_ANY)
VideoCapture::open(const String& filename, int apiPreference = CAP_ANY)
현재프레임 받아오기
bool VideoCapture::read(OutputArray image);
VideoCapture& VideoCapture::operator >> (Mat& image);
int main() {
videoCapture cap;
cap.open(0);
if(!cap.isOpened()) return -1;
Mat frame, edge;
while(1) {
cap >> frame;
if(frame.empty()) break;
Canny(frame, edge, 50, 150);
imshow("frame", frame);
imshow("edge", edge);
if(waitKey(10) >= 0) break;
}
}
double VideoCapture::get(int propID) const;
bool VideoCapture::set(int propId, double value);
get: 참조
set: 설정
propId: 속성 플래그
value: 속성값. 필요한 경우 정수형 사용.
int main() {
videoCapture cap;
cap.open(0);
if(!cap.isOpened()) return -1;
cap.set(CAP_PROP_FRAME_WIDTH, 1280);
cap.set(CAP_PROP_FRAME_HEIGHT, 720);
int w = cvRound(cap.get(CAP_PROP_FRAME_WIDTH)); //cvRound() -> 정수로 반올림
int h = cvRound(cap.get(CAP_PROP_FRAME_HEIGHT));
double fps = cap.get(CAP_PROP_FPS);
cout << "wxh" << w << "x" << h << endl;
cout << "fps: " << fps << endl;
Mat frame, edge;
while(1) {
cap >> frame;
if(frame.empty()) break;
Canny(frame, edge, 50, 150);
imshow("frame", frame);
imshow("edge", edge);
if(waitKey(10) >= 0) break;
}
}
VideoWriter::VideoWriter(const String& filename, int fourcc, double fps,
Size frameSize, bool isColor=true);
VideoWriter::open(const String& filename, int fourcc, double fps,
Size frameSize, bool isColor=true);
int main() {
VideoCapture cap(0);
if (!cap.isOpened()) {
cerr << "Camera open failed!" << endl;
return -1;
}
int fourcc = VideoWriter::fourcc('X', 'V', 'I', 'D');
double fps = 30;
Size sz((int)cap.get(CAP_PROP_FRAME_WIDTH), (int)cap.get(CAP_PROP_FRAME_HEIGHT));
cout << "FPS = " << fps << endl;
cout << "Size = " << sz << endl;
VideoWriter output("output.avi", fourcc, fps, sz);
if (!output.isOpened()) {
cerr << "output.avi open failed!" << endl;
return -1;
}
int delay = cvRound(1000 / fps);
Mat frame, edge;
while (true) {
cap >> frame;
if (frame.empty())
break;
Canny(frame, edge, 50, 150);
cvtColor(edge, edge, COLOR_GRAY2BGR);
output << edge;
imshow("frame", frame);
imshow("edge", edge);
if (waitKey(delay) == 27)
break;
}
cout << "output.avi file is created!!!" << endl;
output.release();
cap.release();
destroyAllWindows();
}