我正在尝试使用opencv将3D np.array(从内存中)写入视频。现在,代码将运行,但是什么也没有发生。我很困惑为什么这实际上没有保存MP4。有任何想法吗?这些是我存储在数组中的灰度图像,因此没有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:断言失败)image.depth()== 0在函数'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()
关于python - OpenCV Python:无法将数组中的图像保存到视频中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62648980/