Opencv python VideoWriter problems
I am trying to get the openCV python VideoWriter object to work without success. What I am trying to do is read the video, grab the frame, process it and write it to a video file. The error I am getting:
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 looks like this:
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 know what I might be doing wrong here?
[EDIT]
I tried something that might shed some light (or not). So if I want to write the original frame, that is, replace
out_video.write(processed)
from
out_video.write(frame)
I am returning my original video. However, the frame and the processed object are the same size and type! So now I am completely puzzled with what is going on. Output of processed shape and frame type:
frame: (576, 720, 3)
processed: (576, 720, 3)
frame: <type 'numpy.ndarray'>
processed: <type 'numpy.ndarray'>
source to share
I understood what had happened. Line
processed = mark_boundaries(frame, segments)
actually normalizes images between 0 and 1, so it wasn't 8 bits deep, which was a problem. The fix was doing something like:
processed = (processed * 255.0).astype('u1')
and then pass that to VideoWriter.write_frame ().
source to share