[英]Plot bounding box annotation on image with heatmap color
我有邊界框注釋數據作為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
對於這樣的圖像: 我使用以下 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'))
我得到了 output 之類的東西
除了紅色邊界框,我還想將所有邊界框 plot 作為熱圖,其中重疊邊界框的顏色比非重疊邊界框密集。
任何幫助都感激不盡。
為了復制這個例子,你可以使用這個字符串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"""
查看這個問題的一種方法是,我們可以從零的二維圖像開始,然后使用填充的矩形,在每個像素處添加一個計數器。 然后,通過用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)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.