繁体   English   中英

OpenCV Python:无法将图像从阵列保存到视频中

[英]OpenCV Python: Unable to save images from an array into a video

我正在尝试使用 opencv 将 3D np.array(来自内存)写入视频。 现在代码将运行,但没有任何反应。 我很困惑为什么这实际上并没有保存 MP4。 有任何想法吗? 这些是我存储在 arrays 中的灰度图像,因此没有 RGB 值。

def convert_array_to_video(array, pathOut, fps):

    size = (array.shape[2], array.shape[1]) #size = (width, height)

    fourcc = cv.VideoWriter_fourcc('m', 'p', '4', 'v')
    out = cv.VideoWriter(pathOut, fourcc, fps, size, False)

    for i in range(array.shape[0]):
        # writing to a image array
        out.write(array[i])
    out.release()


def main():
    array = JTT_DXR_images_clip # shape = (232, 349, 888)
    pathOut = processed_image_directory + 'video.mp4'
    fps = 25.0
    convert_array_to_video(array, pathOut, fps)


if __name__== "__main__":
    main()

这是错误:

---------------------------------------------------------------------------
error                                     Traceback (most recent call last)
<ipython-input-36-bdb5968613b8> in <module>
     20 
     21 if __name__== "__main__":
---> 22     main()
     23 
     24 

<ipython-input-36-bdb5968613b8> in main()
     16     pathOut = processed_image_directory + 'video.mp4'
     17     fps = 25.0
---> 18     convert_array_to_video(array, pathOut, fps)
     19 
     20 

<ipython-input-36-bdb5968613b8> in convert_array_to_video(array, pathOut, fps)
      8     for i in range(array.shape[0]):
      9         # writing to a image array
---> 10         out.write(array[i])
     11     out.release()
     12 

error: OpenCV(3.4.2) /opt/concourse/worker/volumes/live/9523d527-1b9e-48e0-7ed0-a36adde286f0/volume/opencv-suite_1535558719691/work/modules/videoio/src/cap_ffmpeg.cpp:296: error: (-215:Assertion failed) image.depth() == 0 in function 'write'

我认为“(-215:Assertion failed) image.depth() == 0 in function 'write'”是这个问题的关键,但在网上没有找到太多运气

这就是我最终使用的。

# convert array into video to be used by createBackgroundSubtractorMOG2

video_filename = processed_image_directory + 'video_full.mp4'


def convert_array_to_video(array, pathOut, fps):

    size = (array.shape[2], array.shape[1]) #size = (width, height)

    fourcc = cv.VideoWriter_fourcc('m', 'p', '4', 'v')
    out = cv.VideoWriter(pathOut, fourcc, fps, size, isColor=False)

    for i in range(array.shape[0]):
        # writing to a image array
        out.write(array[i])
    out.release()

    
def main():
    array = JTT_DXR_images_uint8
    pathOut = video_filename
    fps = 25.0
    convert_array_to_video(array, pathOut, fps)


if __name__== "__main__":
    main()

暂无
暂无

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

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