简体   繁体   English

带有热图颜色的图像上的 Plot 边界框注释

[英]Plot bounding box annotation on image with heatmap color

I have bounding box annotation data as df like我有边界框注释数据作为df

x           y          width       height
1028.119141,449.497467,667.6237793,62.45513916
737.3796997,352.5843506,297.2002563,49.53338623
730.9188232,399.9641113,148.6001587,58.14785767
671.157959,463.3088684,1020.751343,43.715271
1084.267212,517.9529419,72.13012695,34.972229
729.9176025,359.7701416,303.7685547,37.2722168
1026.231812,456.6779175,661.5819092,50.31750488
662.06073,457.2356262,1032.41156,99.91079712
668.9989624,411.4431152,191.4957275,43.01715088
677.0771484,567.5809937,464.8626709,337.1990356
659.2854614,353.1618652,373.277771,48.56771851
2626.677246,353.6801758,241.1516113,50.53591919
1026.98584,450.5015869,673.2525635,60.3218689
651.710144,349.5532837,405.8690796,57.69006348
230.9644012,29.21221352,277.5276031,70.96037865
724.7105103,359.6932983,328.6080322,26.96270752
689.3218994,427.1000671,957.1763916,165.146637
761.086853,268.6202087,287.673645,158.2123718
1022.51825,453.3616333,766.8713989,93.41070557
2149.582031,223.365921,0.854980469,2.565200806
735.6414185,363.6863098,286.78125,25.07974243
369.3438416,241.1960144,1196.013336,615.5481873
1357.483154,451.0677185,326.9484863,67.52200317
289.6882935,22.07415199,120.0834045,14.12745857
236.7103271,502.4077148,204.8481445,900.6254883
321.4750977,424.7066956,35.31863403,395.5688171
649.9384766,456.4934692,748.755249,169.52948
596.9605103,467.0890808,1193.770203,98.8921814
1010.315857,447.121582,666.9611206,68.6998291
679.3789673,514.437439,492.6141968,48.35473633
674.8457031,411.6835632,211.552124,43.82150269
679.3789673,460.0383301,1016.961121,46.84368896

for an image like this:对于这样的图像: 在此处输入图像描述 I have plotted this data on an image using the following python code我使用以下 python 代码在图像上绘制了这些数据

import matplotlib.pyplot as plt
from matplotlib.patches import Rectangle
from PIL import Image

# Display the image 
plt.imshow(Image.open('subject_49251684.png'))

# Display Bounding boxes
for row in df.itertuples():
    x = float(row.x)
    y = float(row.y)
    w = float(row.width)
    h = float(row.height)
    plt.gca().add_patch(Rectangle((x,y),w,h,linewidth=1,edgecolor='auto',facecolor='none'))

I got the output something like我得到了 output 之类的东西

在此处输入图像描述

Instead of just red bounding boxes, I want to plot all the bounding boxes as a heatmap where overlapping bounding boxes are dense in the color than non-overlapping bounding boxes.除了红色边界框,我还想将所有边界框 plot 作为热图,其中重叠边界框的颜色比非重叠边界框密集。

Any help will be much appreciated.任何帮助都感激不尽。

In order to replicate this example, you can use this string s :为了复制这个例子,你可以使用这个字符串s

s = """1028.119141,449.497467,667.6237793,62.45513916
737.3796997,352.5843506,297.2002563,49.53338623
730.9188232,399.9641113,148.6001587,58.14785767
671.157959,463.3088684,1020.751343,43.715271
1084.267212,517.9529419,72.13012695,34.972229
729.9176025,359.7701416,303.7685547,37.2722168
1026.231812,456.6779175,661.5819092,50.31750488
662.06073,457.2356262,1032.41156,99.91079712
668.9989624,411.4431152,191.4957275,43.01715088
677.0771484,567.5809937,464.8626709,337.1990356
659.2854614,353.1618652,373.277771,48.56771851
2626.677246,353.6801758,241.1516113,50.53591919
1026.98584,450.5015869,673.2525635,60.3218689
651.710144,349.5532837,405.8690796,57.69006348
230.9644012,29.21221352,277.5276031,70.96037865
724.7105103,359.6932983,328.6080322,26.96270752
689.3218994,427.1000671,957.1763916,165.146637
761.086853,268.6202087,287.673645,158.2123718
1022.51825,453.3616333,766.8713989,93.41070557
2149.582031,223.365921,0.854980469,2.565200806
735.6414185,363.6863098,286.78125,25.07974243
369.3438416,241.1960144,1196.013336,615.5481873
1357.483154,451.0677185,326.9484863,67.52200317
289.6882935,22.07415199,120.0834045,14.12745857
236.7103271,502.4077148,204.8481445,900.6254883
321.4750977,424.7066956,35.31863403,395.5688171
649.9384766,456.4934692,748.755249,169.52948
596.9605103,467.0890808,1193.770203,98.8921814
1010.315857,447.121582,666.9611206,68.6998291
679.3789673,514.437439,492.6141968,48.35473633
674.8457031,411.6835632,211.552124,43.82150269
679.3789673,460.0383301,1016.961121,46.84368896"""

One way to see this problem is we can start from a 2d image of zeros, then using filled rectangles, adding to a counter at each pixel.查看这个问题的一种方法是,我们可以从零的二维图像开始,然后使用填充的矩形,在每个像素处添加一个计数器。 Then, by replacing all 0s with np.nans we can remove them from the final result.然后,通过用np.nans替换所有 0,我们可以将它们从最终结果中删除。

import matplotlib.pyplot as plt
import numpy as np
from PIL import Image

img = Image.open('subject_49251684.png')
rectangles = np.zeros_like(img)[:, :, 0].astype('float32')

for row in s.splitlines():
    x, y, w, h = map(float, row.split(','))
    rectangles[int(y):int(y + h), int(x):int(x + w)] += 1

rectangles[rectangles == 0] = np.nan

# Plotting the main image
plt.imshow(img)

# Overlaying the rectangles on top of original image
# with transparency = 50%
plt.imshow(rectangles, alpha=0.5)

This is the rectangles only on sort of heatmap style:这是仅在某种热图样式上的矩形: 矩形

And this is the output rectangles on top of the original image:这是原始图像顶部的 output 矩形: 叠加图像

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM