The difference between the frames of the video shot with OpenCV using Python is output as a black and white video, and the black and white video file is generated, but it cannot be played. I've seen that the default OpenCV can only play uncompressed avi files, but I don't know how to deal with it, so please let me know.
-- codig: utf-8 --
import cv2
import time
import numpy as np
def flame_sub(im1,im2,im3,th,blur):
d1 = cv2.absdiff(im3, im2) d2 = cv2.absdiff(im2, im1) diff = cv2.bitwise_and(d1, d2)
mask = diff < th
im_mask = np.empty((im1.shape[0],im1.shape[1]),np.uint8) im_mask[:][:]=255
im_mask[mask]=0
im_mask = cv2.medianBlur(im_mask,blur) return im_mask
if name == 'main':
cam = cv2.VideoCapture("toukasoku_movie.mp4")
im1 = cv2.cvtColor(cam.read()[1], cv2.COLOR_RGB2GRAY) im2 = cv2.cvtColor(cam.read()[1], cv2.COLOR_RGB2GRAY) im3 = cv2.cvtColor(cam.read()[1], cv2.COLOR_RGB2GRAY)
out = cv2.VideoWriter('output_toukasoku.avi',-1, 20.0, (640,480))
while True:
ret,frame = cam.read()
im_fs = flame_sub(im1,im2,im3,5,7) im1 = im2 im2 = im3 im3 = cv2.cvtColor(cam.read()[1], cv2.COLOR_RGB2GRAY) out.write(frame) cv2.imshow("Motion Mask2",im_fs)
if cv2.waitKey(100) & 0xFF == ord('q'): cam.release() out.release() cv2.destroyAllWindows() break
def flame_sub2(im4,im5,im6,th2,blur2):
d3 = cv2.absdiff(im6, im5) d4 = cv2.absdiff(im5, im4) diff2 = cv2.bitwise_and(d3, d4)
mask2 = diff2 < th2
im_mask2 = np.empty((im4.shape[0],im4.shape[1]),np.uint8) im_mask2[:][:]=255
im_mask2[mask]=0
im_mask2 = cv2.medianBlur(im_mask2,blur2) return im_mask2
if name == 'main':
cam2 = cv2.VideoCapture("Frame_detection.MP4") im4 = cv2.cvtColor(cam2.read()[1], cv2.COLOR_RGB2GRAY) im5 = cv2.cvtColor(cam2.read()[1], cv2.COLOR_RGB2GRAY) im6 = cv2.cvtColor(cam2.read()[1], cv2.COLOR_RGB2GRAY)
while True:
cv2.imshow("Motion Mask3",cam2) im4 = im5 im5 = im6 im6 = cv2.cvtColor(cam2.read()[1], cv2.COLOR_RGB2GRAY) key2 = cv2.waitKey(1)
if cam == True: frame = cv2.flip(frame,0) out.write(out)
if cv2.waitKey(100) & 0xFF == ord('q'): cam.release() out.release() cv2.destroyAllWindows() break
if name == 'main': main()
Recommended Posts