程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式師世界 >> 編程語言 >  >> 更多編程語言 >> Python

50 lines of code using Python OpenCV to draw the thermal diagram of motion track in video

編輯:Python

One 、 Environment configuration

opencv-python == 3.4.2.16
opencv-contrib-python == 3.4.2.16
numpy == 1.19.3

Two 、 Algorithm steps :

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 .

step1. Build a video stream

cap = cv2.VideoCapture('TownCentreXVID.avi'), Used to read every frame of video

step2. Initialize initial parameters

Initialize cumulative binary image accum_image, It is used to accumulate the sum of the background difference binary image of each frame

step3. Difference calculation

filter = background_subtractor.apply(frame), Used to calculate the difference , Remove the background

step4. Cumulative binary graph , And give false colors , Merge with the original image
# 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)
step5. Display and save

Use cv2.imshow() and cv2.imwrite() Display and save images

3、 ... and 、 Complete code

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

Reference resources

1.https://towardsdatascience.com/build-a-motion-heatmap-videousing-opencv-with-python-fd806e8a2340
2.https://blog.csdn.net/qq_30815237/article/details/87120195


  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式師世界 All Rights Reserved