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?
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.
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)
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With