quite well when there is only one camera to pick (see Chapter 4 for more details). Camera capture from file
or from camera is demonstrated in Example 2-10.
Example 2-10: The same object can load videos from a camera or a file
#include <opencv2/opencv.hpp>
#include <iostream>
int main( int argc, char** argv ) {
cv::namedWindow( "Example2_10", cv::WINDOW_AUTOSIZE );
cv::VideoCapture cap;
if (argc==1) {
cap.open(0); // open the default camera
} else {
cap.open(argv[1]);
}
if( !cap.isOpened() ) { // check if we succeeded
std::cerr << "Couldn't open capture." << std::endl;
return -1;
}
// The rest of program proceeds as in Example 2-3
…
In Example 2-10, if a filename is supplied, it opens that file just like in Example 2-3, and if no filename is
given, it attempts to open camera zero (0). We have added a check that something actually opened that
will report an error if not.
Writing to an AVI File
In many applications, we will want to record streaming input or even disparate captured images to an
output video stream, and OpenCV provides a straightforward method for doing this. Just as we are able to
create a capture device that allows us to grab frames one at a time from a video stream, we are able to
create a writer device that allows us to place frames one by one into a video file. The object that allows us
to do this is cv::VideoWriter ...