Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

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

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)

    # Display the resulting frame

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

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

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)

    # Display the resulting frame

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