OPENCV Learning-read a USB camera or video and save it as a frame

Source: Internet
Author: User
Tags sleep sprintf
read the USB camera and save by frame

1. After running, press "s" in the "USB Video" window to start saving the image by frame, and press "Q" to finish.
2. The device number is set according to the actual situation, my Computer default camera is 0,USB camera is 3.
3. Since the USB camera initialization is slower than the computer default camera, the sleep () statement is added before the loop, otherwise the "USB Video" window may not be able to read the gray screen. Use the sleep () statement to include the header file windows.h.

void Videocap ()
    videocapture usbcap (3);
    if (!usbcap.isopened ())
        cout << "Cannot open the camera" << Endl;
    int i = 0;
    Char imgname[100];
    Namedwindow ("USB video", window_normal);
    bool start = false;
    bool stop = false;
    Sleep (+);
    while (!stop)
        Mat frame;
        Usbcap >> frame;
        Imshow ("USB video", frame);
        if ((waitkey () = = ' s ') | | start)
            start = true;
            sprintf (Imgname, "%s%d%s", "img", ++i, ". jpg");
            Imwrite (imgname, frame);
            if (waitkey () = = ' Q ')
                stop = True;}}
read video and save by frame
void Videoread () {Videocapture capture ("1.avi");
    if (!capture.isopened ()) cout << "Error reading" << Endl;
    Char imgname [100];
    int i = 0;
    Mat frame;
    Cvnamedwindow ("video", Cv_window_normal);
    bool stop = FALSE;
        while (!stop) {if (! (frame)) is break;
        Imshow ("video", frame);
        int key = Waitkey (30);
        sprintf (Imgname, "%s%d%s", "img", ++i, ". jpg");
    Imwrite (imgname, frame); }   

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.