Try to separate the background and moving object of the video with OpenCV

Introduction

OpenCV (Open Source Computer Vision Library) is a collection of BSD-licensed video / image processing libraries. There are many algorithms for image filtering, template matching, object recognition, video analysis, machine learning, and more.

Example of motion tracking using OpenCV (OpenCV Google Summer of Code 2015) https://www.youtube.com/watch?v=OUbUFn71S4s

Click here for installation and easy usage Install OpenCV 3 (core + contrib) in Python 3 environment & Difference between OpenCV 2 and OpenCV 3 & simple operation check

Click here for still image filtering Try edge detection with OpenCV

Click here for processing video files Try converting videos in real time with OpenCV Try converting webcam / camcorder video in real time with OpenCV

This time, I will try to separate the background and the moving object using a video taken with a fixed point camera. It can be used as a method of extracting people when tracking people passing by with a surveillance camera.

algorithm

If you stack the frames while weighting them, the parts that do not move will emerge. Instead of simply adding, the current frame is added and subtracted from the background frame. OpenCV uses cv2.absdiff (). Also, in order to reduce the error of weighting calculation, it is calculated using floating point (np.float32).

  1. Prepare a zero-filled background frame
  2. New background = background x (1-weight) + current frame x weight
  3. Repeat step 2

Moving body frame = | Current frame - Background |

program

diff&accum.py


import cv2
import numpy as np

#Constant definition
ESC_KEY = 27     #Esc key
INTERVAL= 33     #interval
FRAME_RATE = 30  # fps

WINDOW_ORG = "org"
WINDOW_BACK = "back"
WINDOW_DIFF = "diff"

FILE_ORG = "org_768x576.avi"

#Window preparation
cv2.namedWindow(WINDOW_ORG)
cv2.namedWindow(WINDOW_BACK)
cv2.namedWindow(WINDOW_DIFF)

#Read original video file
mov_org = cv2.VideoCapture(FILE_ORG)

#First frame read
has_next, i_frame = mov_org.read()

#Background frame
back_frame = np.zeros_like(i_frame, np.float32)

#Conversion processing loop
while has_next == True:
    #Convert input image to floating point type
    f_frame = i_frame.astype(np.float32)

    #Difference calculation
    diff_frame = cv2.absdiff(f_frame, back_frame)

    #Background update
    cv2.accumulateWeighted(f_frame, back_frame, 0.025)

    #Frame display
    cv2.imshow(WINDOW_ORG, i_frame)
    cv2.imshow(WINDOW_BACK, back_frame.astype(np.uint8))
    cv2.imshow(WINDOW_DIFF, diff_frame.astype(np.uint8))

    #Exit with Esc key
    key = cv2.waitKey(INTERVAL)
    if key == ESC_KEY:
        break
    
    #Read next frame
    has_next, i_frame = mov_org.read()

#End processing
cv2.destroyAllWindows()
mov_org.release()

Execution result

org.png The original image

diff.png ** Moving object extraction image **

back.png background image

I was able to successfully extract the walking people.

Recommended Posts

Try to separate the background and moving object of the video with OpenCV
Try to get the contents of Word with Golang
How to loop and play gif video with openCV
Try to measure the position of the object on the desk (real coordinate system) from the camera image with Python + OpenCV
Try to automate the operation of network devices with Python
Convert video to black and white with ffmpeg + python + opencv
Try to extract the features of the sensor data with CNN
Try blurring the image with opencv2
Color extraction with Python + OpenCV solved the mystery of the green background
[Cloudian # 9] Try to display the metadata of the object in Python (boto3)
Try to solve the N Queens problem with SA of PyQUBO
To improve the reusability and maintainability of workflows created with Luigi
I want to check the position of my face with OpenCV!
Try moving the servo motor with RasPi (360 degree round and round version)
[Python] Try to recognize characters from images with OpenCV and pyocr
Try using the camera with Python's OpenCV
Put Cabocha 0.68 on Windows and try to analyze the dependency with Python
How to crop the lower right part of the image with Python OpenCV
Read the graph image with OpenCV and get the coordinates of the final point of the graph
Try to react only the carbon at the end of the chain with SMARTS
Get and estimate the shape of the head using Dlib and OpenCV with python
Recognize the contour and direction of a shaped object with OpenCV3 and Python3 (Principal component analysis: PCA, eigenvectors)
Try to solve the fizzbuzz problem with Keras
Try to solve the man-machine chart with Python
Extract the color of the object in the image with Mask R-CNN and K-Means clustering
Try to communicate with EV3 and PC! (MQTT)
How to try the friends-of-friends algorithm with pyfof
Specify the start and end positions of files to be included with qiitap
Script to tweet with multiples of 3 and numbers with 3 !!
The easiest way to use OpenCV with python
I compared the moving average of IIR filter type with pandas and scipy
Save the object to a file with pickle
Even in the process of converting from CSV to space delimiter, seriously try to separate input / output and rules
Wavelet transform of images with PyWavelets and OpenCV
Try to detect an object with Raspberry Pi ~ Part 1: Comparison of detection speed ~
[Verification] Try to align the point cloud with the optimization function of pytorch Part 1
Try to simulate the movement of the solar system
The story of moving from Pipenv to Poetry
Try to detect fish with python + OpenCV2.4 (unfinished)
An introduction to cross-platform GUI software made with Python / Tkinter! (And many Try and Error)! (In the middle of writing)
[Blender] How to get the selection order of vertices, edges and faces of an object
Try to create a battle record table with matplotlib from the data of "Schedule-kun"
Extract images and tables from pdf with python to reduce the burden of reporting
I tried to automate the article update of Livedoor blog with Python and selenium.
The story of trying to contribute to COVID-19 analysis with AWS free tier and failing
It's Christmas, so I'll try to draw the genealogy of Jesus Christ with Cabocha
I just wanted to extract the data of the desired date and time with Django
I tried to compare the processing speed with dplyr of R and pandas of Python
Try to solve the programming challenge book with python3
Add the attribute of the object of the class with the for statement
I tried to extract features with SIFT of OpenCV
Add information to the bottom of the figure with Matplotlib
Try to visualize the room with Raspberry Pi, part 1
Try projective transformation of images using OpenCV with Python
Try to operate DB with Python and visualize with d3
Try to estimate the number of likes on Twitter
Try moving the servo motor with RasPi (180 degree version)
Fill the background with a single color with OpenCV2 + Python
Visualize the appreciation status of art works with OpenCV
Try to specify the axis with PyTorch's Softmax function
A server that returns the number of people in front of the camera with bottle.py and OpenCV