I am having problems saving processed video, raw video saves fine in Python.

asked 2019-04-02 00:11:40 -0600

wgb-2019 gravatar image

updated 2019-04-02 12:32:41 -0600

I am trying to save a processed video as an MPEG4 file. The original video is a file. It loads fine and I can also save it as an MPEG4. So, I assume the cv2.VideoWriter params are fine. However when I try to do a background subtraction, followed by erosion and dilation, I am unable to save the modified video, even though it will correctly display on the screen. The code is shown below:

import cv2

#declare I/O
video_capture = cv2.VideoCapture('C:\\Users\\good.avi')
 out = cv2.VideoWriter('output.mp4',cv2.VideoWriter_fourcc(*'MP4V'), 30.0, (800,600))

#background subtraction configruation
subtractor = cv2.createBackgroundSubtractorMOG2(history=10, varThreshold=25)

while True:
    # Capture frame-by-frame
    ret, frame = video_capture.read()

    if ret != True:
        break

    #video processing
    mask = subtractor.apply(frame)
    img_erosion = cv2.erode(mask, None, iterations=3)
    img_dilate = cv2.dilate(img_erosion, None, iterations=3)

    #show Videos
    cv2.imshow("Original", frame)
    cv2.imshow("Subtractor", mask)
    cv2.imshow("erosion", img_dilate)

    #write video DOES NOT WORK.  It can saves "frame" but not img_dilate
    out.write(img_dilate)

    if cv2.waitKey(300) & 0xFF == ord('q'):
        break 
    #When everything is done, release the capture
    video_capture.release()
    cv2.destroyAllWindows()
    out.release
edit retag flag offensive close merge delete

Comments

1

are you writing single channel, grayscale images now ? you probably have to toggle the resp. flag in th e VideoWriter

berak gravatar imageberak ( 2019-04-02 00:58:16 -0600 )edit

That is the plan, I would like to write out the grayscale video. Could you be more specific on the resp. flag

wgb-2019 gravatar imagewgb-2019 ( 2019-04-02 12:31:56 -0600 )edit