OpenCV Essentials
上QQ阅读APP看书,第一时间看更新

Live input from a camera

Usually, the computer vision problems we face are related with processing live video input from one or several cameras. In this section, we will describe the recLiveVid example, which grabs a video stream from a webcam (connected to our computer), displays the stream in a window, and records it in a file (recorded.avi). By default, in the following example, the video capture is taken from the camera with cam_id=0. However, it is possible to handle a second camera (cam_id=1) and grab the video from it, setting an argument at the command line:

//… (omitted for brevity)
int main(int argc, char *argv[])
{
    Mat frame;
    const char win_name[]="Live Video...";
    const char file_out[]="recorded.avi";
    int cam_id=0; // Webcam connected to the USB port
    double fps=20;

    if (argc == 2)
        sscanf(argv[1], "%d", &cam_id);

    VideoCapture inVid(cam_id); // Open camera with cam_id
    if (!inVid.isOpened())
        return -1;

    int width = (int)inVid.get(CV_CAP_PROP_FRAME_WIDTH);
    int height = (int)inVid.get(CV_CAP_PROP_FRAME_HEIGHT);
    VideoWriter recVid(file_out, CV_FOURCC('F','F','D','S'), fps, Size(width, height));
    if (!recVid.isOpened()) 
        return -1;

    namedWindow(win_name);
    while (1) {
        inVid >> frame; // Read frame from camera
        recVid << frame; // Write frame to video file
        imshow(win_name, frame); // Show frame
        if (waitKey(1000/fps) >= 0)
            break;
    }
    inVid.release(); // Close camera
    return 0;
}

The code explanation is given as follows:

  • VideoCapture::VideoCapture(int device) – This class constructor initializes a VideoCapture object to receive a video from a camera rather than a file. In the following code example, it is used with a camera identifier:
    VideoCapture inVid(cam_id); // Open camera with cam_id
  • VideoWriter::VideoWriter(const string& filename, int fourcc, double fps, Size frameSize, bool isColor=true) – This class constructor creates an object to write a video stream to a file with the name passed as the first argument. The second argument identifies the video codec with a code of four single characters (for example, in the previous sample code, FFDS stands for ffdshow). Obviously, only codecs actually installed in the local system can be used. The third argument indicates the frames per second of the recording. This property can be obtained from the VideoCapture object with the VideoCapture::get method, although it may return 0 if the property is not supported by the backend. The frameSize argument indicates the total size for each frame of the video that is going to be written. This size should be the same as the input video grabbed. Finally, the last argument allows writing the frame in color (default) or in grayscale. In the example code, the constructor is used with the ffdshow codec and the size of the video capture is as follows:
    int width = (int)inVid.get(CV_CAP_PROP_FRAME_WIDTH);
    int height = (int)inVid.get(CV_CAP_PROP_FRAME_HEIGHT);
    VideoWriter recVid(file_out, CV_FOURCC('F','F','D','S'), fps,Size(width, height));
    
  • void VideoCapture::release() – This method closes the capturing device (webcam) or the video file. This method is always called implicitly at the end of the program. However, in the preceding example, it is called explicitly to avoid wrong termination of the output file (only noticeable when playing the recorded video).