【问题标题】:I was trying to create a video out of a numpy array but i was getting an error我试图从一个 numpy 数组中创建一个视频,但我遇到了一个错误
【发布时间】:2020-05-12 00:42:17
【问题描述】:

我试图从 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】:

    我只能在您的链接中找到一个 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()
    

    【讨论】:

    • 非常感谢。我注意到了我的错误。请同时查看我的最新问题。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-11-28
    • 2021-07-24
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多