问题描述
我正在从事一个项目,该项目在图像分类任务中围绕获得的预测异常图像的热图绘制边界框。虽然,我目前的算法绘制了一个边界框,但并不理想。事实上,边界框比我预期的要大。这是我的代码:
myclass1
我为 orig_img = (heatmap.permute(1,2,0).cpu().numpy() * 255).astype(np.uint8)
orig_img = cv2.cvtColor(orig_img,cv2.COLOR_RGB2BGR)
grey_img = cv2.cvtColor(orig_img,cv2.COLOR_BGR2GRAY)
thresh = cv2.threshold(grey_img,50,255,cv2.THRESH_BINARY + cv2.THRESH_OTSU)[1]
contours = cv2.findContours(thresh,cv2.RETR_EXTERNAL,cv2.CHAIN_APPROX_SIMPLE)
contours = contours[0] if len(contours) == 2 else contours[1]
for cnt in contours:
if len(cnt) > 100:
x,y,w,h = cv2.boundingRect(cnt)
or_img = (invTrans(inputs[j].cpu()).permute(1,0).cpu().numpy() * 255).astype(np.uint8)
or_img = cv2.cvtColor(or_img,cv2.COLOR_RGB2BGR)
cv2.rectangle(or_img,(x,y),(x+w,y+h),(250,0),4)
plt.imshow(or_img)
cv2.waitKey(0)
plt.show()
尝试了不同的值,但无法获得最佳值。这是我得到的图像:
这是我想要的图片:
我很感激你们的帮助,伙计们。
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)