Writing numpy arrays using cv2 VideoWriter

I had a problem writing a toy example using opencv2.3.1 VideoWriter, here's how I do it:

writer = cv2.VideoWriter('test1.avi',cv.CV_FOURCC('P','I','M','1'),25,(640,480)) for i in range(1000): x = np.random.randint(10,size=(480,640)).astype('uint8') writer.write(x) #del writer (with or without tested) 

I tried every possible combination as a result of a file with 0 bytes if the extension was mpg and 5.5kb if it was avi. I have to say that some indicated that I should build the ffmpeg library from the source and not apt-get it. Well, I did it on a new machine based on the help of this site http://vinayhacks.blogspot.com/2011/11/installing-opencv-231-with-ffmpeg-on-64.html . which also presented an error when compiling opencv (the error was related to ffmpeg). Now I'm really out of ideas, How to generate video using OPENCV?

Thanks in advance

+4
source share
3 answers

VideoWriter has the last argument isColor with the default value True . Therefore, if you change it to False , 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) 
+3
source

What OS are you using? Are you sure the PIM1 codec is installed on your system?

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

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

+1
source

Hi, I am new to opencv and I had the same problem. It seems like writer.write (x) requires x to be an array with RGB values, not scalars. I solved the problem by following these steps:

 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 guess there are cleaner ways to do this, but I have not found them.

+1
source

Source: https://habr.com/ru/post/1396433/


All Articles