cv2.VideoWriter saving corrupted video after applying MOG

Hi everyone,

I tried execute cv2.VideoWriter and save frame per frame after use Background Subtraction with MOG, however, when tried open the final video, not executed.
below, the code:

cap = cv2.VideoCapture('images/RodoviaCoreia.mp4')

# Get the height and width of the frame (required to be an interger)
w = int(cap.get(3)) 
h = int(cap.get(4))

# Definindo o CODEC e o video de saida
out = cv2.VideoWriter('Rodovia-Coreia-MOG.mp4', cv2.VideoWriter.fourcc('M','P','4','V'), 30, (w, h))

# Aplicando o MIXTURE OF GAUSSIANS
foreground_background = cv2.bgsegm.createBackgroundSubtractorMOG()

# aplicando para cada frame
while True:
    
    ret, frame = cap.read()

    if ret: 
      # Apply background subtractor to get our foreground mask
      foreground_mask = foreground_background.apply(frame)
      
      imshow("Foreground Mask", foreground_mask)
      out.write(foreground_mask)
    else:
      break

cap.release()
out.release()

foreground_mask.dtype and foreground_mask.shape please

foreground_mask.dtype - uint8
foreground_mask.shape - (1080, 1920)

Is not a np.array, It must be because of this, right?

what do you mean? did it have a dtype and shape, or did it not have those?

the issue here is that it’s a grayscale image but you didn’t tell VideoWriter that.

read about the isColor argument.

1 Like

Thank you, i needed this argument