没有视频输出OpenCV Python [英] No video output OpenCV Python

查看:282
本文介绍了没有视频输出OpenCV Python的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想将我的代码转换为视频。我的程序接收一个图像,计算每个9 * 9窗口的平均RGB并输出一个图像:

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:

输出图片:

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)


b $ b

当试图在视频上使用相同的代码时,我得到一个视频输出,但它是空的(0字节)。

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

这里是代码:

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()

谢谢阅读:)

推荐答案

我试过你的代码。我必须改变三件事:

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


  • 输出视频的编解码器。

  • 调整输入框的大小,只是为了调整输入框的大小。

以下是适合我的方法:

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()

这篇关于没有视频输出OpenCV Python的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆