首页 > 解决方案 > 使用 OpenCV 和 Python 在视频上应用热图

问题描述

我想编写一个代码,将热图应用于有运动的地方的视频。我写了一个检测运动的代码,带有轮廓,但我不知道如何制作热图。

这是我拥有的代码

import cv2
import numpy as np

# upload video
cap = cv2.VideoCapture('test_video.mp4')

#reading two frames
ret, frame1 = cap.read()
ret, frame2 = cap.read()

while cap.isOpened():

     # get diference between two frames
     diff = cv2.absdiff(frame1, frame2)

     # convert diference in gray
     gray = cv2.cvtColor(diff, cv2.COLOR_BGR2GRAY)

     # bluring and treshold
     blur = cv2.GaussianBlur(gray, (5,5), 0)
     _, thresh = cv2.threshold(blur, 20, 255, cv2.THRESH_BINARY)

     dilated = cv2.dilate(thresh, None, iterations = 3)

     # define contours
     contours, _ = cv2.findContours(dilated, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)
     
     # draw contours
     cv2.drawContours(frame1, contours, -1, (255,0,0), 1)

     # show frames
     cv2.imshow('frame', frame1)
     frame1 = frame2
     ret, frame2 = cap.read()

     if cv2.waitKey(60) == 60:
          break

cv2.destroyAllWindows()
cap.release()

我看过这个链接:Build a Motion Heatmap Video Using OpenCV With Python。我想重现代码,但是很多东西,如fourcc,image_folderimages没有定义,所以我试着用另一种方式来做。

你能帮我解决这个问题吗?基本上,我想将热图应用于有运动的视频。

标签: pythonopencvimage-processingheatmap

解决方案


这是一个想法。您知道如何使用 opencv 循环播放视频的帧,对吧?好吧,对于while循环所在的每一帧,将当前帧之后的帧存储在一个变量中,并比较当前帧和未来帧之间的差异。

通过两帧之间的差异,您可以检测运动的轮廓。假设我们用绿色在图像上绘制轮廓。

while在循环之前定义一个空白数组作为热图;循环的每次迭代,在热图的每个坐标上添加一定量,其中坐标在框架上为绿色,并从热图中移除一定量,其中坐标在图像上不是绿色。

  1. 导入必要的模块:
import cv2
import numpy as np
  1. 定义一个函数来处理两个帧之间的差异,以便更好地检测我们案例的轮廓。这些值可以根据您的需要进行调整:
def process(img):
    img_gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
    img_blur = cv2.GaussianBlur(img_gray, (5, 5), 25)
    img_canny = cv2.Canny(img_blur, 5, 50)
    kernel = np.ones((3, 3))
    img_dilate = cv2.dilate(img_canny, kernel, iterations=4)
    img_erode = cv2.erode(img_dilate, kernel, iterations=1)
    return img_erode
  1. 定义一个函数,该函数将接收两帧之间差异的处理图像,以及要在其上绘制轮廓的图像。它将返回带有轮廓(绿色)的图像:
def get_contours(img, img_original):
    img_contours = img_original.copy()
    contours, hierarchies = cv2.findContours(img, cv2.RETR_TREE, cv2.CHAIN_APPROX_NONE)
    cv2.drawContours(img_contours, contours, -1, (0, 255, 0), -1) 
    return img_contours
  1. while定义您的捕获设备,并在循环之前读取两帧;第一个是循环每次迭代的当前帧,while第二个是循环每次迭代的未来帧while。此外,为热图定义空白图像heat_map
cap = cv2.VideoCapture("Bar fight security cam.mp4")

success, img1 = cap.read()
success, img2 = cap.read()
heat_map = np.zeros(img1.shape[:-1])
  1. while循环中,找到两帧之间的差异,并从绘制的差异中获取每个循环的当前帧以及轮廓:
while success:
    diff = cv2.absdiff(img1, img2)
    img_contours = get_contours(process(diff), img1)
  1. 在前面定义的函数返回的图像上绿色3的位置的每个坐标上添加一个数字,比如,并从图像上不是绿色的位置的每个坐标中减去 3。要确保没有颜色导致通道值小于和大于,请将边界应用于热图:heat_mapget_contoursheat_map0255
    heat_map[np.all(img_contours == [0, 255, 0], 2)] += 3
    heat_map[np.any(img_contours != [0, 255, 0], 2)] -= 3
    heat_map[heat_map < 0] = 0
    heat_map[heat_map > 255] = 255
  1. heat_map数组转换为灰度图像,然后转换为热图:
    img_mapped = cv2.applyColorMap(heat_map.astype('uint8'), cv2.COLORMAP_JET)
  1. 最后,显示帧并检索下一次迭代的帧:
    cv2.imshow("Original", img1)
    cv2.imshow("Heat Map", img_mapped)
    
    img1 = img2
    success, img2 = cap.read()
    
    if cv2.waitKey(1) == ord('q'):
        break

共:

import cv2
import numpy as np

def process(img):
    img_gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
    img_blur = cv2.GaussianBlur(img_gray, (5, 5), 25)
    img_canny = cv2.Canny(img_blur, 5, 50)
    kernel = np.ones((3, 3))
    img_dilate = cv2.dilate(img_canny, kernel, iterations=4)
    img_erode = cv2.erode(img_dilate, kernel, iterations=1)
    return img_erode

def get_contours(img, img_original):
    img_contours = img_original.copy()
    contours, hierarchies = cv2.findContours(img, cv2.RETR_TREE, cv2.CHAIN_APPROX_NONE)

    cv2.drawContours(img_contours, contours, -1, (0, 255, 0), -1) 
    # If you want to omit smaller contours, loop through the detected contours, and only draw them on the image if they are at least a specific area. Don't forget to remove the line above if you choose the below block of code.
    # for cnt in contours: 
    #     if cv2.contourArea(cnt) > 500:
    #         cv2.drawContours(img_contours, [cnt], -1, (0, 255, 0), -1) 

    return img_contours

cap = cv2.VideoCapture("Bar fight security cam.mp4")

success, img1 = cap.read()
success, img2 = cap.read()
heat_map = np.zeros(img1.shape[:-1])

while success:
    diff = cv2.absdiff(img1, img2)
    img_contours = get_contours(process(diff), img1)

    heat_map[np.all(img_contours == [0, 255, 0], 2)] += 3 # The 3 can be tweaked depending on how fast you want the colors to respond
    heat_map[np.any(img_contours != [0, 255, 0], 2)] -= 3
    heat_map[heat_map < 0] = 0
    heat_map[heat_map > 255] = 255

    img_mapped = cv2.applyColorMap(heat_map.astype('uint8'), cv2.COLORMAP_JET)

#    img1[heat_map > 160] = img_mapped[heat_map > 160] Use this line to draw the heat map on the original video at a specific temperature range. For this it's where ever the temperature is above 160 (min is 0 and max is 255)

    cv2.imshow("Original", img1)
    cv2.imshow("Heat Map", img_mapped)
    
    img1 = img2
    success, img2 = cap.read()
    
    if cv2.waitKey(1) == ord('q'):
        break

推荐阅读