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

  • OpenCV Essentials
  • Oscar Deniz Suarez等
  • 473字
  • 2021-04-02 10:03:16

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).
主站蜘蛛池模板: 中江县| 苍南县| 邛崃市| 手游| 百色市| 疏附县| 嘉荫县| 惠州市| 平安县| 南宫市| 河池市| 德江县| 娱乐| 秦皇岛市| 临泉县| 灵宝市| 华坪县| 曲水县| 景宁| 乐昌市| 壤塘县| 青田县| 高青县| 广丰县| 富锦市| 灵台县| 盖州市| 普定县| 遂川县| 嵊州市| 阜新| 天全县| 青浦区| 石阡县| 溧水县| 汶川县| 兴隆县| 嘉禾县| 安国市| 安丘市| 双牌县|