[我试图用numpy数组创建视频,但出现错误

问题描述 投票:0回答:1

我试图从NumPy数组创建视频,但是我一直都遇到此错误:

cv2.error: OpenCV(4.2.0) c:\projects\opencv-python\opencv\modules\imgproc\src\color.simd_helpers.hpp:94: error: (-2:Unspecified error) in function '__thiscall cv::impl::`anonymous-namespace'::CvtHelper<struct cv::impl::`anonymous namespace'::Set<3,4,-1>,struct cv::impl::A0xe227985e::Set<3,4,-1>,struct cv::impl::A0xe227985e::Set<0,2,5>,2>::CvtHelper(const class cv::_InputArray &,const class cv::_OutputArray &,int)'
> Unsupported depth of input image:
>     'VDepth::contains(depth)'
> where
>     'depth' is 4 (CV_32S)

链接到完整代码-https://drive.google.com/file/d/1qqat43eJw0Ql46TlFRiGMjqROs2QjCFj/view?usp=sharing

这是代码的结构:

import cv2 , time , numpy
frame = numpy.asarray(the long list of NumPy array, if you want, it is in the link above)
fourcc = cv2.VideoWriter_fourcc(*'mp4v')
out = cv2.VideoWriter('output.mp4v' , fourcc , 20.0 , (640 , 480))
out.write(frame)
out.release()

如果在它之前添加了frame = frame.astype(numpy.uint8),则会创建一个视频,但实际上它只是第一帧的一张照片的视频!!!

请帮我解决这个问题。这真的很重要。

python numpy opencv cv2
1个回答
0
投票

我只能在您的链接中找到单个RGB图像。您是否要使用尺寸为640x480x3的单个图像制作视频?如果可以确保此处的帧数组具有多个RGB图像,则以下代码应足够:

import cv2 , time , numpy
frame = numpy.asarray(the long list of NumPy array, if you want, it is in the link above)
fourcc = cv2.VideoWriter_fourcc(*'XVID')
writer = cv2.VideoWriter('output.avi', fourcc, 1, (frame_height, frame_width)) 
i=0
for img in (frame):
    print("writing frame # {}".format(i))
    writer.write(img.astype('uint8'))
    i+=1

writer.release()
© www.soinside.com 2019 - 2024. All rights reserved.