#include <opencv2/core/core.hpp>
#include <opencv2/imgproc/imgproc.hpp>
#include <opencv2/highgui/highgui.hpp>
using namespace cv;
#include <iostream>
using namespace std;
int main(int argc, const char *argv[])
{
// Get filename to the source image:
if (argc != 2) {
cout << "usage: " << argv[0] << " <video_name>" << endl;
return -1;
}
// Build video capturer
VideoCapture cap(argv[1]); // read from file
//VideoCapture cap(0); // read from camera
// Check if video is open
if(!cap.isOpened()){
cout << "Cannot open video/camera!" << endl;
return -1;
}
// Set up window
const char* WINDOW = "frame";
namedWindow(WINDOW, WINDOW_AUTOSIZE);
// Read frames
Mat frame;
int frameNum = 0;
for(;;){
// read frame and check
cap >> frame;
if(frame.empty()){
cout << "frame " << frameNum << " is empty!" << endl;
break;
}
// process here...
// update frame number
frameNum++;
// show image
imshow(WINDOW, frame);
int delay = 30; // ms
int key = waitKey(delay);
if(27 == key || 'Q' == key || 'q' == key)
break;
}
}
参考
1. http://docs.opencv.org/master/modules/highgui/doc/reading_and_writing_images_and_video.html?highlight=imwrite
2. http://docs.opencv.org/doc/tutorials/highgui/video-input-psnr-ssim/video-input-psnr-ssim.html