使用 cv2 在热图上绘制边界框

时间:2021-01-29 01:44:04

标签: python-3.x image-processing heatmap cv2 bounding-box

我正在从事一个项目,该项目在图像分类任务中围绕获得的预测异常图像的热图绘制边界框。虽然,我目前的算法绘制了一个边界框,但并不理想。事实上,边界框比我预期的要大。这是我的代码:

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,2,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,0),4) plt.imshow(or_img) cv2.waitKey(0) plt.show() 尝试了不同的值,但无法获得最佳值。这是我得到的图像:

Current image

这是我想要的图片:

Desired image

我很感激你们的帮助,伙计们。

0 个答案:

没有答案