opencv-python == 3.4.2.16
opencv-contrib-python == 3.4.2.16
numpy == 1.19.3
The core idea is , Through Gaussian mixture difference algorithm , Calculate the difference between adjacent frames , Get a binary image , Use binary image to accumulate and sum , Get the cumulative binary graph , The cumulative binary image is transformed into a pseudo color image , Fuse with the original image , Get the thermal diagram of the motion track .
cap = cv2.VideoCapture('TownCentreXVID.avi')
, Used to read every frame of video
Initialize cumulative binary image accum_image
, It is used to accumulate the sum of the background difference binary image of each frame
filter = background_subtractor.apply(frame)
, Used to calculate the difference , Remove the background
# 1. Two valued
ret, th1 = cv2.threshold(filter, threshold, maxValue, cv2.THRESH_BINARY)
# 2. Cumulative binary graph
accum_image = cv2.add(accum_image, th1)
# 3. Give false color
color_image_video = cv2.applyColorMap(accum_image, cv2.COLORMAP_HOT)
# 4. Image fusion
video_frame = cv2.addWeighted(frame, 0.7, color_image_video, 0.7, 0)
Use cv2.imshow()
and cv2.imwrite()
Display and save images
Just change the video file path in line 5
import numpy as np
import cv2
import copy
def main():
capture = cv2.VideoCapture('TownCentreXVID.avi')
background_subtractor = cv2.bgsegm.createBackgroundSubtractorMOG() # Background difference algorithm based on Gaussian mixture , The principle can refer to https://blog.csdn.net/qq_30815237/article/details/87120195
length = int(capture.get(cv2.CAP_PROP_FRAME_COUNT))
first_iteration_indicator = 1
for i in range(0, length):
ret, frame = capture.read()
frame = cv2.resize(frame,dsize=None,fx=0.3,fy=0.3)
# The first frame is used as initialization
if first_iteration_indicator == 1:
first_frame = copy.deepcopy(frame)
height, width = frame.shape[:2]
accum_image = np.zeros((height, width), np.uint8)
first_iteration_indicator = 0
else:
filter = background_subtractor.apply(frame)
threshold = 2
maxValue = 2
ret, th1 = cv2.threshold(filter, threshold, maxValue, cv2.THRESH_BINARY)
# Cumulative calculation diagram of difference diagram , Used to draw thermal background
accum_image = cv2.add(accum_image, th1)
# Add false colors to the binary graph
color_image_video = cv2.applyColorMap(accum_image, cv2.COLORMAP_HOT)
# Image fusion
video_frame = cv2.addWeighted(frame, 0.7, color_image_video, 0.7, 0)
cv2.imshow('frame',frame) # Original picture
cv2.imshow('diff-bkgnd-frame',filter) # Background difference diagram , The difference graph obtained by Gaussian mixture difference algorithm
cv2.imshow('mask',accum_image)
cv2.imshow('result',video_frame)
if cv2.waitKey(1) & 0xFF == ord('q'):
break
color_image = cv2.applyColorMap(accum_image, cv2.COLORMAP_HOT)
result_overlay = cv2.addWeighted(first_frame, 0.7, color_image, 0.7, 0)
# Save the final drawing
cv2.imwrite('diff-overlay.jpg', result_overlay)
# Release
capture.release()
cv2.destroyAllWindows()
if __name__ == '__main__':
main()
1.https://towardsdatascience.com/build-a-motion-heatmap-videousing-opencv-with-python-fd806e8a2340
2.https://blog.csdn.net/qq_30815237/article/details/87120195