0

I am trying to convert my code that works on images to video. My program takes in an image, works out the mean RGB of each 9*9 window and outputs an image:

Input Image: Input Image

Output Image: Output Image

Here is my code that has an image as input/output:

import numpy as np
import cv2

#Read in image
img = cv2.imread('images/0021.jpg')

scale = 9
#Get x and y components of image
y_len,x_len,_ = img.shape

mean_values = []
for y in range(scale):
    for x in range(scale):
        #Crop image 3*3 windows
        cropped_img=img[(y*y_len)/scale:((y+1)*y_len)/scale,
                          (x*x_len)/scale:((x+1)*x_len)/scale]

        mean_val=cv2.mean(cropped_img)
        mean_val=mean_val[:3]
        cropped_img[:,:,:] = mean_val

print img.shape     
cv2.imshow('mean_RGB',img)
cv2.waitKey(0)

When trying to use the same code on a video I get a video output but it is empty (0 bytes).

Here is the code:

import numpy as np
import cv2

cap = cv2.VideoCapture('videos/kondo2.avi')

fourcc = cv2.cv.CV_FOURCC(*'DIVX')
out = cv2.VideoWriter('videos/output.avi',fourcc, 15.0, (800,600),True)

while(cap.isOpened()):
    ret, frame = cap.read()
    if ret == True:
        y_len,x_len,_ = frame.shape
        scale = 9
        for y in range(scale):
            for x in range(scale):
                cropped_frame=frame[(y*y_len)/scale:((y+1)*y_len)/scale,
                                        (x*x_len)/scale:((x+1)*x_len)/scale]

                mean_val=cv2.mean(cropped_frame)
                mean_val=mean_val[:3]
                cropped_frame[:,:,:] = mean_val
                out.write(frame)

cap.release()
out.release()
cv2.destroyAllWindows()

Thank you for reading :)

7
  • try it with double backslash (\) path names, also try with an absolute path name to ensure that is not the problem. Commented Mar 3, 2015 at 9:25
  • @GPPK the path shouldn't be the problem as the 'output.avi' video is saved to the right location, it is just empty with no frames. Commented Mar 3, 2015 at 9:30
  • Fair enough. I always find it best to start with static path names just to reduce the error possibilities. The amount of times that's been the problem. Commented Mar 3, 2015 at 9:33
  • Can you visualize the frames that you add to the videowriter? Add cv2.imshow("frame", frame) to see if the frame you want to add is there? Commented Mar 3, 2015 at 9:56
  • Are you potentially re-writing over the VideoWriter every frame so when you release there is only ever one frames stored and written? Commented Mar 3, 2015 at 14:59

1 Answer 1

1

I tried your code. Three things I had to change:

  • The codec of the output video. I changed it to mp4 and it works.
  • The indentation of the line out.write(frame).
  • Resize the input frame, just to make sure it has the right size.

Here is what works for me:

import numpy as np
import cv2

cap = cv2.VideoCapture('videos/kondo2.avi')
w=800
h=600

fourcc = cv2.cv.CV_FOURCC('m', 'p', '4', 'v')
out = cv2.VideoWriter('videos/output.avi',fourcc, 25, (w,h),True)
count = 0
while(cap.isOpened()):
    count = count + 1
    print "processing frame ", count
    ret, frame = cap.read()
    if ret == True:
        frame = cv2.resize(frame,(w,h), interpolation = cv2.INTER_CUBIC)
        y_len,x_len,_ = frame.shape

        scale = 9
        for y in range(scale):
            for x in range(scale):
                cropped_frame=frame[(y*y_len)/scale:((y+1)*y_len)/scale,
                                    (x*x_len)/scale:((x+1)*x_len)/scale]

                mean_val=cv2.mean(cropped_frame)
                mean_val=mean_val[:3]
                cropped_frame[:,:,:] = mean_val

        out.write(frame)


cap.release()
out.release()
cv2.destroyAllWindows()
Sign up to request clarification or add additional context in comments.

2 Comments

That works, something else that I had to do was copy the ffmpeg 3rd party binaries from the opencv folder to my python directory. After the last frame has been processed I get a traceback error, File "C:\Python27\video.py", line 16, in <module> frame = cv2.resize(frame,(w,h), interpolation = cv2.INTER_CUBIC) error: ..\..\..\..\opencv\modules\imgproc\src\imgwarp.cpp:1968: error: (-215) ssize.area() > 0 in function cv::resize . I guess this is because it is trying to operate on a frame that isn't there? How can I make an exception for this case?
@NeilVicker: Move that line to inside the scope of if ret == True, then you will be fine. See my edited answer.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.