从热图数据生成边界框 [英] Generating bounding boxes from heatmap data

查看:42
本文介绍了从热图数据生成边界框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有我正在进行的车辆检测项目的热图数据,但我不知道下一步要去哪里.我想围绕图像的最热"部分绘制一个广义边界框.我的第一个想法是在所有重叠的部分上画一个框,但有些东西告诉我有一种更准确的方法来做到这一点.任何帮助,将不胜感激!不幸的是,我的声誉使我无法发布图片.下面是我创建热图的方法:

#正预测窗口坐标结构:((x1, y1), (x2, y2))def create_heatmap(bounding_boxes_list):# 创建与输入数据大小相同的黑色图像热图 = np.zeros(shape=(375, 1242))# 遍历测试图像中的边界框位置列表对于 bounding_boxes_list 中的 bounding_box:热图[bounding_box[0][1]:bounding_box[1][1], bounding_box[0][0]:bounding_box[1][0]] += 1返回热图

导入 cv2# 灰度然后大津的阈值图像 = cv2.imread('1.png')灰色 = cv2.cvtColor(图像,cv2.COLOR_BGR2GRAY)thresh = cv2.threshold(gray, 0, 255, cv2.THRESH_BINARY + cv2.THRESH_OTSU)[1]# 查找轮廓cnts = cv2.findContours(thresh, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)cnts = cnts[0] 如果 len(cnts) == 2 else cnts[1]对于 cnts 中的 c:x,y,w,h = cv2.boundingRect(c)cv2.rectangle(图像, (x, y), (x + w, y + h), (36,255,12), 2)cv2.imshow('thresh', thresh)cv2.imshow('图像', 图像)cv2.waitKey()

I have the heatmap data for a vehicle detection project I'm working on but I'm at a loss for where to go next. I want to draw a generalized bounding box around the 'hottest' portions of the image. My first thought was to just draw a box over all portions that overlap but something's telling me there is a more accurate way to do this. Any help would be appreciated! Unfortunately my reputation prevents me from posting images. Here's how I'm creating the heatmap:

# Positive prediction window coordinate structure: ((x1, y1), (x2, y2))
def create_heatmap(bounding_boxes_list):
     # Create a black image the same size as the input data
     heatmap = np.zeros(shape=(375, 1242))
     # Traverse the list of bounding box locations in test image
     for bounding_box in bounding_boxes_list:
          heatmap[bounding_box[0][1]:bounding_box[1][1], bounding_box[0][ 0]:bounding_box[1][0]] += 1

return heatmap

Here's the link to the heatmap I have

Here's a general idea of what I had in mind

解决方案

Otsu's threshold and contour detection on the binary image should do it. Using this screenshotted image without the axis lines:

import cv2

# Grayscale then Otsu's threshold
image = cv2.imread('1.png')
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
thresh = cv2.threshold(gray, 0, 255, cv2.THRESH_BINARY + cv2.THRESH_OTSU)[1]

# Find contours
cnts = cv2.findContours(thresh, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
cnts = cnts[0] if len(cnts) == 2 else cnts[1]
for c in cnts:
    x,y,w,h = cv2.boundingRect(c)
    cv2.rectangle(image, (x, y), (x + w, y + h), (36,255,12), 2)

cv2.imshow('thresh', thresh)
cv2.imshow('image', image)
cv2.waitKey()

这篇关于从热图数据生成边界框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆