Given this image OpenCV findContours函数检索this result线的内部和外部轮廓。我需要检测每个盒子的一个轮廓。我尝试过其他检索方法,如列表和外部,但需要层次结构和内部元素。我还编写了一些代码,试图忽略第一个孩子的轮廓,它的宽度和高度非常接近,但层次结构修复是一个混乱。我还重新绘制了轮廓,以最大限度地提高对比度,但功能是给我两个轮廓,内部和外部具有完全相同的宽度和高度。你知道吗
PD:有可能有各种层次结构like this,这是因为只检查父级或子级的特殊解决方案不起作用。你知道吗
这是打印所有轮廓的基本代码(Python3.7和OpenCV 4.1.1):
import cv2
import numpy as np
import imutils
image = 255-cv2.imread('images/3boxes.png',0) #Inverted image to detect white lines
image = imutils.resize(image, width=600)
contourns, hierarchy = cv2.findContours(image.copy(), cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)
#Twice because grab_contours and hierarchy retrieval doesn't work properly
contourns = cv2.findContours(image.copy(), cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)
contourns = imutils.grab_contours(contourns)
image = cv2.cvtColor(image,cv2.COLOR_GRAY2RGB)
ocurr = 0
for c in contourns:
ocurr = ocurr + 1
# Compute the center of the contour
M = cv2.moments(c)
cX = int((M["m10"] / M["m00"]))
cY = int((M["m01"] / M["m00"]))
x,y,w,h = cv2.boundingRect(c)
cv2.rectangle(image,(x,y),(x+w,y+h),(0,255,0),1)
label = "E" + str(ocurr) + " (" + str(w) + ", " + str(h) + ")"
cv2.putText(image, label, (cX, cY), cv2.FONT_HERSHEY_SIMPLEX, 0.5, (255, 255, 255), 1)
cv2.imshow("Result", image)
cv2.waitKey(0)
谢谢你的帮助!你知道吗
目前没有回答
相关问题 更多 >
编程相关推荐