OpenCV calls the camera video and saves it

Source: Internet
Author: User

 

Many online users say that the recorded video file is 0 kb or cannot be opened.

In fact, this is mainly due to two reasons:

1. The encoding format of the videos generated by OpenCV is related.

2. It is related to illegal withdrawal (I will solve it in 200 cycles. Or you can use Timer. Or use another key)

 

 

The solution I personally recommend is as follows:

1. Download an XviD encoder and install it.

2. Use the following code for testing:

1 # include "cv. h"
2 # include "cxcore. h"
3 # include "highgui. h"
4 # include <iostream>
5
6 using namespace std;
7 int main ()
8 {
9 CvCapture * capture = cvCaptureFromCAM (-1 );
10 CvVideoWriter * video = NULL;
11 IplImage * frame = NULL;
12 int n;
13 if (! Capture) // if the camera cannot be turned on, give a warning
14 {
15 cout <"Can not open the camera." <endl;
16 return-1;
17}
18 else
19 {
20 frame = cvQueryFrame (capture); // obtain a frame in the camera.
21 video = cvCreateVideoWriter ("camera. avi", CV_FOURCC ('x', 'V', 'I', 'D'), 25,
22 cvSize (frame-> width, frame-> height); // create a CvVideoWriter object and allocate space
23 // The saved file name is camera. avi, which must be selected during the program running. The video size is the video size of the camera and the frame frequency is 32.
24 if (video) // if you can create a CvVideoWriter object, it indicates that the object is successfully created.
25 {
26 cout <"VideoWriter has created." <endl;
27}
28
29 cvNamedWindow ("Camera Video", 1); // create a window
30 int I = 0;
31 while (I <= 200) // Let it automatically stop admission 200 times
32 {
33 frame = cvQueryFrame (capture); // obtain a frame from CvCapture
34 if (! Frame)
35 {
36 cout <"Can not get frame from the capture." <endl;
37 break;
38}
39 n = cvWriteFrame (video, frame); // determines whether the write is successful. If 1 is returned, the write is successful.
40 cout <n <endl;
41 cvShowImage ("Camera Video", frame); // displays the image of the Video content
42 I ++;
43 if (cvWaitKey (2)> 0)
44 break; // exit if there are other keyboard responses
45}
46
47 cvReleaseVideoWriter (& video );
48 cvReleaseCapture (& capture );
49 cvDestroyWindow ("Camera Video ");
50}
51 return 0;
52}

 

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: info-contact@alibabacloud.com 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.