📜 ⬆️ ⬇️

OpenCV. Video from the camera. We write to the file

image
Greetings

In past lessons:
OpenCV. Video output
OpenCV (computer vision). Installation under MSVS 2008. "Hello World"

We learned how to install OpenCV, wrote the first program and read the video from the file.

Now I want to show you how easy it is to capture video from a camera and learn how to save video to a file.
Go!

')

Video from the camera.


In the last lesson, it was described how to read a video from a file. But hardly OpenCV should be used to process the already recorded video. More extensive use of this library can be found in the processing of real-time video.

Let's look at the code:

#include "highgui.h"

int main( int argc, char** argv )
{
cvNamedWindow( "CAM Capture", CV_WINDOW_AUTOSIZE );
CvCapture* capture = cvCreateCameraCapture(-1);
assert(capture != NULL);
IplImage* frame;
while(1)
{
frame = cvQueryFrame( capture );
if( !frame ) break;
cvShowImage( "CAM Capture", frame );
char c = cvWaitKey(33);
if( c == 27 ) break;
}
cvReleaseCapture( &capture );
cvDestroyWindow( "CAM Capture" );
}


In comparison with the code of the previous lesson, the differences are not noticeable.
Only the cvCreateFileCapture() function is replaced by
cvCreateCameraCapture() , as a parameter, we give it not the path to the file, but the camera ID - if you have one camera, then safely set the value to -1.
And then everything happens as with the video file.

We write to the file.


In some situations it is required to record the received video to a file, including after its processing. OpenCV library allows you to do this. When we have already created a capture device that allows us to take frames from a video stream, then we will need to create a recording device in order to record the received frames in a video file. For this we need the function cvCreateVideoWriter() .
Next, for each frame, we have to use cvWriteFrame() and in conclusion, we will use cvReleaseVdeoWriter() .

#include "highgui.h"
#include "cv.h"

int main(int argc, char** argv[])
{
CvCapture* capture = cvCreateFileCapture("f.avi");

if( !capture )
{
return -1;
}

IplImage* bgr_frame = cvQueryFrame( capture );
double fps = cvGetCaptureProperty( capture, CV_CAP_PROP_FPS);
CvSize size = cvSize((int)cvGetCaptureProperty( capture, CV_CAP_PROP_FRAME_WIDTH),
(int)cvGetCaptureProperty( capture, CV_CAP_PROP_FRAME_HEIGHT));
CvVideoWriter* writer = cvCreateVideoWriter("1.avi", CV_FOURCC('M','J','P','G'), fps, size);
IplImage* logpolar_frame = cvCreateImage( size, IPL_DEPTH_8U, 3);

while( (bgr_frame=cvQueryFrame(capture)) != NULL )
{
cvLogPolar( bgr_frame, logpolar_frame, cvPoint2D32f(bgr_frame->width/2, bgr_frame->height/2),
40, CV_INTER_LINEAR+CV_WARP_FILL_OUTLIERS );
cvWriteFrame( writer, logpolar_frame );
}

cvReleaseVideoWriter( &writer );
cvReleaseImage( &logpolar_frame );
cvReleaseCapture( &capture );
return(0);
}


Difficulties, I think, should arise.
In this program, we perform a small conversion (log polar), and more simply, we make our video black and white, or more precisely, in shades of gray. :)
The width, height and fps of the output video will be the same as the input one, for this we use the lines:
double fps = cvGetCaptureProperty( capture, CV_CAP_PROP_FPS);
CvSize size = cvSize((int)cvGetCaptureProperty( capture, CV_CAP_PROP_FRAME_WIDTH),
(int)cvGetCaptureProperty( capture, CV_CAP_PROP_FRAME_HEIGHT));
CvVideoWriter* writer = cvCreateVideoWriter("1.avi", CV_FOURCC('M','J','P','G'), fps, size);


CV_FOURCC ('M', 'J', 'P', 'G') - here we set the MJPG codec ( motion jpeg ).

A cycle:

while( (bgr_frame=cvQueryFrame(capture)) != NULL )
{
cvLogPolar( bgr_frame, logpolar_frame, cvPoint2D32f(bgr_frame->width/2, bgr_frame->height/2),
40, CV_INTER_LINEAR+CV_WARP_FILL_OUTLIERS );
cvWriteFrame( writer, logpolar_frame );
}

directly performs the conversion.

Later we will examine the cvLogPolar function and all its parameters in more detail.
This is another lesson is over, I will be glad to all your comments and suggestions!

Source: https://habr.com/ru/post/78150/


All Articles