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.
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).
Moving body frame = | Current frame - Background |
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()
The original image
** Moving object extraction image **
background image
I was able to successfully extract the walking people.
Recommended Posts