Saved video using openCV shows nothing

Viewed 42

I have an array of images which are binary, either 1 or 0. To give a bit more context, these images are masks and so the image is mostly black and it shows some white spots. Each image is part of a sequence and I would like to save the sequence as a video to see how the evolution of the spots progresses. Right now I have few images, between 7 or 20 and I have the following code:

# predictedMasks is a list containing each mask that I want to see in the video
videoName = sequenceName + '.avi'
fourcc = cv2.VideoWriter_fourcc('X', 'V', 'I', 'D')
fps = 1
frame_width = np.size(predictedMasks[0], 1)
frame_height = np.size(predictedMasks[0], 0)
incolor = False
out = cv2.VideoWriter(videoName, fourcc, fps, (frame_width, frame_height), incolor)
for mask in predictedMasks:
    out.write(mask)
out.release()

Now when I try to open the video from my folder it shows nothing, only a black screen although it should be showing the white spots from what I understand. I am working on Windows 10, and I have also tried changing the codec using:

videoName = sequenceName + '.mp4'
fourcc = cv2.VideoWriter_fourcc(*'mp4v')

Which results in the same issue. Is there something I am doing wrong?

EDIT:

Thanks to the answer below I have discovered that the issue was that I had to change boolean values to 'uint8' and then change all 1's in the image to 255! The edited code is shown below:

   out = cv2.VideoWriter(videoName, fourcc, fps, (frame_width, frame_height), incolor)
    for mask in predictedMasks:
        mask = mask.astype('uint8')
        mask = np.where(mask == 1, 255, mask)
        out.write(mask)
    out.release()
1 Answers

I tried your code with some sample data values(frame) and its works well. Its shows the frames accordingly in the video:

# sample values
data = np.array([[255,0,255,0],[255,0,255,0],[255,0,255,0]])
sequenceName = "test"

videoName = sequenceName + '.avi'
fourcc = cv2.VideoWriter_fourcc('X', 'V', 'I', 'D')
fps = 1
frame_width = np.size(data, 1)
frame_height = np.size(data, 0)
incolor = False
out = cv2.VideoWriter(videoName, fourcc, fps, (frame_width, frame_height), incolor)
for i in range(100): # for 100 frames
    out.write(np.uint8(data))
out.release()

We may be able to detect the problem clearly if you share you predicted masks as well.

Related