Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Issues with opencv python VideoWriter

Tags:

python

opencv

I have been trying to get the openCV python VideoWriter object to work with no success. What I am trying to do is read a video, grab the frame, do some processing and write it back to a video file. The error that I get is:

Traceback (most recent call last):
File "detect.py", line 35, in <module>
    out_video.write(processed)
cv2.error: /tmp/opencv-z9Pa/opencv-2.4.9/modules/imgproc/src/color.cpp:4419: 
error: (-215) src.depth() == dst.depth() in function cvCvtColor

The code I wrote is as follows:

video = VideoCapture(args["video"])
num_frames = video.get(CV_CAP_PROP_FRAME_COUNT)
width = video.get(CV_CAP_PROP_FRAME_WIDTH)
height = video.get(CV_CAP_PROP_FRAME_HEIGHT)

fps = video.get(CV_CAP_PROP_FPS)

out_video = VideoWriter()
fourcc = CV_FOURCC('m', 'p', '4', 'v')
out_video.open(args["out"], fourcc, int(fps), (int(width), int(height)),True)

while (video.isOpened()):
    ret, frame = video.read()
    # This simply takes the frame and does some image processing on it
    segments = compute_superpixels(frame, num_pixels=100)
    processed = mark_boundaries(frame, segments)
    out_video.write(processed)

Does anyone have any idea what I might be doing wrong here?

[EDIT]

I tried something which might shed some light (or not). So, if I want to write the original frame i.e. replace

out_video.write(processed)

with

out_video.write(frame)

I get my original video back. However, the frame and processed object have the same size and type! So, now I am completely baffled as to what is going on. The output of processed and frame shape and types are:

frame: (576, 720, 3)
processed: (576, 720, 3)
frame: <type 'numpy.ndarray'>
processed: <type 'numpy.ndarray'>
like image 945
Luca Avatar asked Jun 26 '26 19:06

Luca


1 Answers

I figured out what was wrong. The line

processed = mark_boundaries(frame, segments)

was actually normalising the images between 0 and 1, so it was not 8 bit depth, which was an issue. The fix was doing something like:

processed = (processed * 255.0).astype('u1')

and then passing this on to the VideoWriter.write_frame().

like image 161
Luca Avatar answered Jun 28 '26 09:06

Luca



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!