简体   繁体   English

没有视频输出OpenCV Python

[英]No video output OpenCV Python

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: 我的程序获取图像,计算出每个9 * 9窗口的平均RGB并输出图像:

Input 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). 尝试在视频上使用相同的代码时,我得到了视频输出,但它为空(0字节)。

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 :) 谢谢您的阅读:)

I tried your code. 我尝试了您的代码。 Three things I had to change: 我必须更改的三件事:

  • The codec of the output video. 输出视频的编解码器。 I changed it to mp4 and it works. 我将其更改为mp4,并且可以正常工作。
  • The indentation of the line out.write(frame). 行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()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM