Python cv2.VideoWriter() produces distorted video when writing .avi from numpy array

Viewed 297

I'm trying to write out a 3D grayscale f32 numpy array (outputStack) as a video (e.g. .avi, .mp4) in python. However, when I play the video in the default Windows 10 app ("Films & TV") the video plays back highly banded, ugly, and unrecognisable. See image here

Importantly, my output .avi has the expected dimensions, FPS, and time length. File size is 252,584 KB. Here's my code:

(H,W) = np.shape(outputStack[0, :, :])
FPS = 10
seconds = int((np.shape(outputStack[:, 0, 0])[0]) / FPS)
fourcc = cv2.VideoWriter_fourcc('M', 'J', 'P', 'G')
titleTemp = './' + fileName.split('.')[0] + '.avi'
vid = cv2.VideoWriter(titleTemp, cv2.VideoWriter_fourcc('M', 'J', 'P', 'G'), FPS, (W, H), isColor = False)
 
for i in range(FPS * seconds):
    frameTemp = outputStack[i, :, :]
    vid.write(frameTemp)
 
vid.release()

I've also tried converting my array to 3-channel BGR using cv2.normalize() and cv2.merge(); here I call cv2.VideoWriter() with isColor = True, which produces similar results. In all cases the frames display fine in matplotlib.

I'm not at all sure what's going wrong. Is it that cv2.VideoWriter doesn't support f32 arrays with 0 to 1 values? Is my codec somehow compressing my array incorrectly? Is it that Windows 10 Films & TV doesn't support the MJPG codec?

0 Answers
Related