【问题标题】:No video output OpenCV Python没有视频输出 OpenCV Python
【发布时间】:2015-05-03 20:54:28
【问题描述】:

我正在尝试将适用于图像的代码转换为视频。我的程序接收一张图片,计算出每个 9*9 窗口的平均 RGB 并输出一张图片:

输入图像:

输出图像:

这是我的代码,其中有一个图像作为输入/输出:

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)

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

代码如下:

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

感谢您的阅读:)

【问题讨论】:

  • 尝试使用双反斜杠 (\) 路径名,也尝试使用绝对路径名以确保不是问题。
  • @GPPK 路径应该不是问题,因为“output.avi”视频保存在正确的位置,它只是空的,没有帧。
  • 很公平。我总是发现最好从静态路径名开始,以减少错误的可能性。出现问题的次数。
  • 你能想象你添加到 videowriter 的帧吗?添加 cv2.imshow("frame", frame) 看看你要添加的框架有没有?
  • 您是否可能每帧都重写 VideoWriter,所以当您发布时,只有一帧存储和写入?

标签: python opencv video image-processing computer-vision


【解决方案1】:

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

  • 输出视频的编解码器。我把它改成 mp4 就可以了。
  • out.write(frame) 行的缩进。
  • 调整输入帧的大小,以确保其大小合适。

这对我有用:

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

【讨论】:

  • 那行得通,我必须做的其他事情是将 ffmpeg 3rd 方二进制文件从 opencv 文件夹复制到我的 python 目录。处理完最后一帧后,我收到回溯错误 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 。我想这是因为它试图在一个不存在的框架上操作?我怎样才能为这种情况破例?
  • @NeilVicker:将该行移到 if ret == True 的范围内,那么你会没事的。请参阅我编辑的答案。
猜你喜欢
  • 2023-01-02
  • 2017-10-23
  • 2021-06-05
  • 1970-01-01
  • 2018-02-18
  • 2020-10-28
  • 2020-05-11
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多