官术网_书友最值得收藏!

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).
主站蜘蛛池模板: 舞阳县| 吉林市| 诏安县| 安陆市| 乃东县| 梓潼县| 连云港市| 延长县| 湖北省| 广河县| 定兴县| 来凤县| 措勤县| 江门市| 吐鲁番市| 长治市| 得荣县| 彩票| 鲜城| 湟源县| 靖远县| 白山市| 龙泉市| 汉川市| 灌南县| 武宣县| 乐清市| 平罗县| 阿图什市| 萨嘎县| 马关县| 温宿县| 博爱县| 同德县| 克什克腾旗| 永和县| 永安市| 西林县| 息烽县| 湖南省| 澳门|