how to save thresholding video in python opencv [closed]

asked 2018-08-28 03:18:07 -0600

usuf gravatar image

updated 2018-08-28 03:20:10 -0600

import numpy as np

import cv2

cap = cv2.VideoCapture(0)

fourcc = cv2.VideoWriter_fourcc(*'XVID')

out = cv2.VideoWriter('output.avi', fourcc, 20.0, (640,480))

frame1 = None

while(cap.isOpened()):

ret, frame = cap.read()

if ret==True:

    #frame = cv2.flip(frame,0)

    gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)

    if frame1 is None:

        frame1 = gray

    frameT = cv2.absdiff(frame1, gray)

    _ ,thresh = cv2.threshold(frameT, 25, 255, cv2.THRESH_BINARY)



    out.write(thresh)

    cv2.imshow('thresh',thresh)

    cv2.imshow('frame',frame)

    if cv2.waitKey(1) & 0xFF == ord('q'):

        break

else:

    break

cap.release()

out.release()

cv2.destroyAllWindows()

by using above code i can save the video as frame by using this "out.write(frame)" but not able to save video "out.write(thresh)" when i use this any idea please?

edit retag flag offensive reopen merge delete

Closed for the following reason duplicate question by usuf
close date 2018-08-28 04:44:51.596111

Comments

just for the record, if you want to write single channel, grayscale images, use:

out = cv2.VideoWriter('output.avi', fourcc, 20.0, (640,480), False)

(last arg !)

berak gravatar imageberak ( 2018-08-28 04:49:43 -0600 )edit