首页 > 解决方案 > 从 numpy 数组生成的热图给出不准确的结果

问题描述

我正在尝试使用边界框坐标生成热图,以显示检测到的对象进入房间的区域。为此,我创建了一个 numpy 数组,并使用坐标创建了一个掩码,并为所有边界框增加 1。之后,我使用 min_max_scaler 来规范化 numpy 数组并使用 matplotlib.cm 创建一个颜色图。

import cv2
import csv
import numpy as np
from PIL import Image
from matplotlib import cm
from sklearn import preprocessing

min_max_scaler = preprocessing.MinMaxScaler()

with open(filename, newline = '\n') as file:
            lines = csv.reader(file, delimiter = "\t")            
            lines_list = [x for x in lines]

im = np.zeros((1080,1920,3), np.uint8)      
heatmap = np.zeros_like(im[:,:,0]).astype(float)
        
for li in lines_list:
    if li[1] == "valid":
        heatmap[int(li[3]):int(li[5]),int(li[2]):int(li[4])] += 1
        
result = min_max_scaler.fit_transform(heatmap)        
        
newim = Image.fromarray(np.uint8(cm.Wistia(result)*255)) 
opencvImage = cv2.cvtColor(np.array(newim), cv2.COLOR_RGB2BGR) 
#gaussian blur added to smoothen the edges of the boxes
opencvImage = cv2.GaussianBlur(opencvImage,(11,11),cv2.BORDER_DEFAULT)

一切似乎都很好,但是当我看到颜色图的结果时,代表 np.array 最大值的某些区域似乎不准确。由于对象足够大,因此具有最高值的区域的大小需要以某种方式相似,但是有些颜色最深的部分小于平均边界框大小的 10%。例如,在下图中,该区域实际上是一堵墙,因此为了使对象多次穿过该区域,它也应该穿过右侧或顶部的区域(相等或更多)。我检查了边界框,它们是正确的,所以那部分没有问题。

在此处输入图像描述

我从中读取数据的文件:https ://gofile.io/d/QaIgE8

任何帮助将非常感激。非常感谢 !

标签: pythonnumpyopencvmatplotlibheatmap

解决方案


推荐阅读