Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

OpenCV VideoWriter size issue

Tags:

c++

opencv

I am trying to read a video file, process it, and write the processed frames as an output video file. However, I get the following error:

    OpenCV Error: Assertion failed (img.cols == width && img.rows == height && channels == 3) in write, file /.../opencv-cpp/modules/videoio/src/cap_mjpeg_encoder.cpp, line 829
    terminate called after throwing an instance of 'cv::Exception'
      what():  /.../opencv-cpp/modules/videoio/src/cap_mjpeg_encoder.cpp:829: error: (-215) img.cols == width && img.rows == height && channels == 3 in function write

I am sure I have 3 channels (I checked it with .channels()) before the write() function takes place:

//generate video
cout<<finalOutputRGB.channels()<<endl;
outputCapRGB.write(finalOutputRGB);

So the problem is not here. Perhaps the way I initialized?

// Setup output videos
VideoWriter outputCapRGB(rgbVideoOutputPath, captureRGB.get(CV_CAP_PROP_FOURCC), captureRGB.get(CV_CAP_PROP_FPS),
Size(captureRGB.get(CV_CAP_PROP_FRAME_WIDTH), captureRGB.get(CV_CAP_PROP_FRAME_HEIGHT)));

What could it be? One thing came to my mind is that during the processing of the frames, they are being cropped, so the resolutions are not the same. Maybe this could be the reason. But then again, it would be stupid for OpenCV to not allow any modified video to be recorded.

So I tried to create the videowriter objects with the cropped sizes of my frames as follows:

// Sizes of the videos to be written (after the processing)
Size irFrameSize = Size(449, 585);
Size rgbFrameSize = Size(488, 694);

// Setup output videos
VideoWriter outputCapRGB(rgbVideoOutputPath, captureRGB.get(CV_CAP_PROP_FOURCC), captureRGB.get(CV_CAP_PROP_FPS), rgbFrameSize);
VideoWriter outputCapIR(irVideoOutputPath, captureIR.get(CV_CAP_PROP_FOURCC), captureIR.get(CV_CAP_PROP_FPS), irFrameSize);

However, I still get the same damn error.

Alternatively, I also appreciate any suggestion of software which can crop video files conveniently, on Ubuntu. This would also solve the problem. I would crop the videos and feed them in.

Any thoughts?

like image 750
Schütze Avatar asked Feb 05 '23 15:02

Schütze


2 Answers

The exception says the image frame that you want to write have a different size from the size in your videowriter. You should check if every image frame you are writing has the same width/height as in your videowriter.

like image 62
Yancey Avatar answered Feb 08 '23 17:02

Yancey


Just a shot in the dark: from the error, since you're sure that you have 3 channels, is it possible that you inverted width and height? In OpenCV, the Size is defined has (width, height), whereas Mat are defined as (rows, cols), what effectively correspond to (height, width).

like image 40
Olivier Avatar answered Feb 08 '23 15:02

Olivier