The avi file output by OpenCV cannot be played

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)

True if the difference is less than the threshold

mask = diff < th

Array generation of the same size as the background image

im_mask = np.empty((im1.shape[0],im1.shape[1]),np.uint8) im_mask[:][:]=255

True part (background) is painted black

im_mask[mask]=0

Sesame salt noise removal

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()

Inter-frame difference calculation

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)

End of difference between frames

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)

True if the difference is less than the threshold

mask2 = diff2 < th2

Array generation of the same size as the background image

im_mask2 = np.empty((im4.shape[0],im4.shape[1]),np.uint8) im_mask2[:][:]=255

True part (background) is painted black

im_mask2[mask]=0

Sesame salt noise removal

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:

Difference calculation of inter-frame difference

cv2.imshow("Motion Mask3",cam2) im4 = im5 im5 = im6 im6 = cv2.cvtColor(cam2.read()[1], cv2.COLOR_RGB2GRAY) key2 = cv2.waitKey(1)

Finished calculation of difference between frames

if cam == True: frame = cv2.flip(frame,0) out.write(out)

When the q key is pressed

if cv2.waitKey(100) & 0xFF == ord('q'): cam.release() out.release() cv2.destroyAllWindows() break

if name == 'main': main()

Recommended Posts

The avi file output by OpenCV cannot be played
How to switch the configuration file to be read by Python
If you are told cannot by Python import, review the file name
[OpenCV] About the array returned by imread
Read the file line by line in Python
Read the file line by line in Python
Read the file by specifying the character code.
Read the image posted by flask so that it can be handled by opencv
Check the operation of OpenCV3 installed by Anaconda
When Html cannot be output with Jupyter Notebook
How to erase the characters output by Python
Put the file uploaded by django into MinIO
Download the csv file created by Google Colaboratory
[Django] css in the project cannot be read
PIL cannot be installed by virtualenv with pyenv
The problem that the ifconfig command cannot be used
[Python] Save the video data imported by OpenCV as a serial number jpg file
The material covered by the module name cannot be called correctly at the import destination / source.