Writing numpy arrays using cv2 VideoWriter

不想你离开。 提交于 2019-12-05 14:19:50

VideoWriter has last argument isColor with default value True. So if you change it to False then you can write your 2D arrays.

import cv2
import numpy as np

writer = cv2.VideoWriter('test1.avi', cv2.VideoWriter_fourcc(*'PIM1'), 25, (640, 480), False)
for i in range(100):
    x = np.random.randint(255, size=(480, 640)).astype('uint8')
    writer.write(x)

Which OS are you using? Are you sure your system have PIM1 codec installed?

I use windows, and I can use cv.FOURCC(*"DIB ") for uncompressed video, or use -1 to show a codec dialog.

After install ffdshow, I can use cv.FOURCC(*"ffds") to encode the video by MPEG-4.

Hello I am new to opencv and I had this same problem. It appears that the writer.write(x) Needs x to be an array whit RGB values and not scalars. I solved the issue by doing this:

import cv2
import cv2.cv as cv
import numpy as np

writer = cv2.VideoWriter('test1.avi',cv.CV_FOURCC('P','I','M','1'),25,(640,480))
for i in range(1000):
    x = np.random.randint(255,size=(480,640)).astype('uint8')
    x = np.repeat(x,3,axis=1)
    x = x.reshape(480, 640, 3)
    writer.write(x)

I assume there are cleaner ways to do it but I haven't found any.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!