Bug in created video with cv2.VideoWriter (Python 2.7)

asked 2014-09-07 16:33:03 -0600

Renan gravatar image

updated 2014-09-08 02:10:54 -0600

berak gravatar image

I'm having an issue with VideoWriter class in opencv module (version 2.4.9) for python 2.7

I'm reading a video file (MP4), performing some math transformations in each frame and writing the result as a new video (AVI), with cv2.VideoWriter.

The resulting video is being generated without any warning or error, but the last 2 columns of pixels of the video are all filled with black pixels (see the images below).

Picture 1: First frame of the original video (MP4): image description

Picture 2: First frame of the generated video (AVI) image description

Picture 3: Zoom of top-right corner of picture 2: image description

Picture 4: Zoom of the bottom-right corner of picture 2: image description

Only the last 2 pixels of the right columns in Picture 4 aren't filled with black. All the othem pixels of the columns are filled with RGB=(0,0,0)

My code is this:

cap = cv2.VideoCapture('C:\\inputVideo.mp4')
wrt = cv2.VideoWriter('C:\\outputVideo.avi',cv2.cv.CV_FOURCC('X','V','I','D'),\

while cap.get(1)<5: #catching only the first 5 frames of the video
    flag, img = cap.read()

    #I didn't put here the math transformations,
    #because the problem occurs also without the math part.

    #I'll generate a picture here for testing my code
    if cap.get(1)==1:
        cv2.imwrite('C:\\input.png', img) #Picture 1, attached above


With the following procedure I can generate "Picture 2":

flag, img = cap.read()
cv2.imwrite('C:\\output.png', img)

Anyone here have ever seen that bug? Is that a problem with the opencv library or with the codecs that I used? I don't have any possible idea of what can it be...

edit retag flag offensive close merge delete


What is the size of your video? Maybe there is some contraint that the column count has to be a multiple of four or something. BTW: Do you track chickens?

FooBar gravatar imageFooBar ( 2014-09-08 02:20:43 -0600 )edit

My video has 300x530 px. I'm making a mathmatical model to infer the welfare of broiler chickens based on images. Maybe i'll perform some tracking to do that. I'll try to grab only 528 columns to see if the black pixels' columns disappears...

Renan gravatar imageRenan ( 2014-09-29 23:46:12 -0600 )edit