【发布时间】:2021-09-18 04:05:51
【问题描述】:
我试图用中心点和边界框绘制我的图像。在变量rect 中是高度、宽度、角度和中心点的信息,所以我假设轮廓和所有内容都正确找到。但是为什么剧情中没有显示出来呢?
hierachy, img_threshold_32bit = cv2.threshold(img_hr, 100, 255, cv2.THRESH_BINARY)
img_8bit = np.array(img_threshold_32bit,dtype=np.uint8)
contours,_ = cv2.findContours(img_8bit, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
cv2.drawContours(img_8bit, contours, -1, (0, 255, 0), 2, cv2.LINE_AA)
for cnt in contours:
rect = cv2.minAreaRect(cnt)
box = cv2.boxPoints(rect)
box = np.int0(box)
cv2.drawContours(img_8bit,[box],0,(0,0,255),2)
cv2.circle(img_8bit,(int(rect[0][0]),int(rect[0][1])),5,(255,0,0),-1)
plt.imshow(img_8bit)
感谢您的帮助
【问题讨论】:
-
您正在绘制一个黑色矩形,而您可能只是没有看到它。尝试使用更亮的灰色调:
cv2.drawContours(img_8bit,[box],0,(200,0,0),2) -
不,
(0,0,255)是红色,(255,0,0)是蓝色 -
由于您在单通道图像上绘图,因此您应该只考虑第一个通道。所以 (0, ..., ...) 是黑色的,而 (255, ..., ...) 是白色的。 (200,...,...) 是浅灰色。
-
但是
drawContours独立于我的图片频道。我只用括号中的值缩放边界框线的颜色。检查 docs.opencv.org/3.4/d6/d6e/…。我也尝试过你的方法,但它不起作用。
标签: python opencv bounding-box