50行代码利用Python-OpenCV绘制视频中运动轨迹热力图

一、环境配置

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

二、算法步骤:

核心思路是,通过高斯混合差值算法,计算相邻帧图像的差值,得到二值图像,利用二值图像进行累积求和,得到累积二值图,并将累计二值图转为伪彩色图像,与原图像进行融合,得到运动轨迹热力图。

step1.构建视频流

cap = cv2.VideoCapture('TownCentreXVID.avi'),用于读取视频的每一帧

step2.初始化初始参数

初始化累积二值图像accum_image,用于累积每一帧的背景差分二值图的和

step3.差值计算

filter = background_subtractor.apply(frame),用于计算差值,去除背景

step4.累积二值图,并赋予伪彩色,和原图进行融合
# 1.二值化
ret, th1 = cv2.threshold(filter, threshold, maxValue, cv2.THRESH_BINARY) 
# 2.累积二值图
accum_image = cv2.add(accum_image, th1)
# 3.赋予伪彩色
color_image_video = cv2.applyColorMap(accum_image, cv2.COLORMAP_HOT)
# 4.图像融合
video_frame = cv2.addWeighted(frame, 0.7, color_image_video, 0.7, 0)
step5.显示与保存

使用cv2.imshow()cv2.imwrite()显示和保存图像

三、完整代码

只需要更改第五行中的视频文件路径

import numpy as np
import cv2
import copy

def main():
    capture = cv2.VideoCapture('TownCentreXVID.avi')
    background_subtractor = cv2.bgsegm.createBackgroundSubtractorMOG()  # 基于高斯混合的背景差分算法,原理可参考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)
        # 第一帧作为初始化
        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)
            # 差分图的累积计算图,用于绘制热力背景
            accum_image = cv2.add(accum_image, th1)
            # 为二值图添加伪色彩
            color_image_video = cv2.applyColorMap(accum_image, cv2.COLORMAP_HOT)
            # 图像融合
            video_frame = cv2.addWeighted(frame, 0.7, color_image_video, 0.7, 0)
            cv2.imshow('frame',frame)   # 原图
            cv2.imshow('diff-bkgnd-frame',filter)   # 背景差分图,通过高斯混合差分算法得到的差分图
            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)

    # 保存最终图
    cv2.imwrite('diff-overlay.jpg', result_overlay)

    # 释放
    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

你可能感兴趣的:(小项目,opencv,python,opencv,python,计算机视觉)