[PYTHON] Implemented inter-frame difference method with OpenCV

Background subtraction using OpenCV

The inter-frame difference method is one of the methods for detecting moving objects. It is the result made with Python3 + OpenCV.

Click here for the original data (It's hard to understand, but it's a video that feels like splashing water on the beach) スクリーンショット 2020-06-17 22.54.43.jpg

** Click here for the binarized mask image ** スクリーンショット 2020-06-17 22.39.08.jpg

** This is a video ** スクリーンショット 2020-06-17 22.40.01.jpg

** Display background image ** スクリーンショット 2020-06-17 22.40.50.jpg

Then it is a sample program.

movieSample.py


import cv2
import numpy as np
import time

i = 0      #Count variable
th = 30    #Difference image threshold

#Video file capture
cap = cv2.VideoCapture("/Users/.../.../.../movies/movieSample.mp4")

#Set the first frame as the background image
ret, bg = cap.read()

#Grayscale conversion
bg = cv2.cvtColor(bg, cv2.COLOR_BGR2GRAY)

while(cap.isOpened()):
    #Get frame
    ret, frame = cap.read()

    #Grayscale conversion
    gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)

    #Calculate the absolute value of the difference
    mask = cv2.absdiff(gray, bg)

    #Binarize the difference image and mask image(Monochrome)Calculate
    mask[mask < th] = 0
    mask[mask >= th] = 255

    #Display mask image
    cv2.imshow("Mask", mask)
    #Display frame image (monochrome)
    cv2.imshow("Flame", gray)
    #Display background image (monochrome)
    cv2.imshow("Background", bg)

    #Wait(0.03sec)
    time.sleep(0.03)
    i += 1    #Increase the count by 1

    #Background image update (at regular intervals)
    if(i > 30):
        ret, bg = cap.read()
        bg = cv2.cvtColor(bg, cv2.COLOR_BGR2GRAY)
        i = 0  #Initialization of count variable

    #Stops halfway when the x key is pressed
    if cv2.waitKey(1) & 0xFF == ord('x'):
        break

cap.release()
cv2.destroyAllWindows()

You may need to install OpenCV separately as a trial. Please try according to the environment. If you are using a Mac, from the terminal

$ python3 -m pip install opencv-python [ENTER] I think I can go there.

** K Igarashi ** has introduced the installation. Install OpenCV with pip

Recommended Posts

Implemented inter-frame difference method with OpenCV
Detect stoop with OpenCV
Kernel Method with Python
Rotate sprites with OpenCV
Data Augmentation with openCV
Easy TopView with OpenCV
Stumble with homebrew opencv3
Face recognition with Python's OpenCV
"Apple processing" with OpenCV3 + Python3
Try edge detection with OpenCV
Image editing with python OpenCV
Camera capture with Python + OpenCV
Method chain with `return self`
Implemented word2vec with Theano + Keras
[Python] Using OpenCV with Python (Basic)
Implemented SMO with Python + NumPy
Graph drawing method with matplotlib
Binarize photo data with OpenCV
I implemented VQE with Blueqat
Real-time edge detection with OpenCV
Face detection with Python + OpenCV
Implemented Conditional GAN with chainer
Get image features with OpenCV
Implemented Efficient GAN with keras
Face recognition / cutting with OpenCV
Try OpenCV with Google Colaboratory
Implemented SmoothGrad with Chainer v2
Using OpenCV with Python @Mac
Image recognition with Keras + OpenCV
Anime face detection with OpenCV
Automatic image interpolation with OpenCV and Python (Fast Marching Method, Navier-Stokes)
Draw optical flow in real time with OpenCV (Shi-Tomasi method, Lucas-Kanade method)