Schütze Schütze - 1 year ago 210
C++ Question

OpenCV VideoWriter size issue

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?

Answer Source

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.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download