查找矩形内部角点

3
我有一个二进制图像,如下所示,我需要找到矩形的内部角点。我尝试使用OpenCVfindcontours函数获取矩形边界和角点,但它没有提供我要寻找的精确点位置(它提供的是外部角点)。有什么想法或方法可以解决这个问题吗? input

2
尝试使用CV_RETR_CCOMP标志,它应该会给你一个2级层次列表,一个是外部的,另一个是“孔洞”的。 - api55
谢谢@appi55。它起作用了。 - User
1
@用户,您可以通过使用“层次结构”来完成更多操作,例如查找相同级别的轮廓、具有相同父级的轮廓、具有相同兄弟姐妹的轮廓等等! - Jeru Luke
1个回答

3

如评论中所述,您需要使用cv2.RETR_CCOMP。以下是Python的实现:

img = cv2.imread(os.path.join(r'C:\Users\Jackson\Desktop\stack\contour', FILE_NAME))
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) # Don't use magic numbers
thresh = cv2.threshold(gray, thresh=BLACK_THRESHOLD, maxval=255, type=cv2.THRESH_BINARY)[1]

#--- Find the contours ---
_, contours, hierarchy = cv2.findContours(thresh, cv2.RETR_CCOMP, cv2.CHAIN_APPROX_SIMPLE)

#--- get hierarchy descriptions ---
print(hierarchy)

返回:

[[-1 -1  1 -1]
 [-1 -1 -1  0]]

这是什么意思?

层次结构有助于在轮廓之间建立父子关系。 子轮廓是指在称为父级的外部轮廓内的轮廓。

层次结构返回一个具有以下含义的数组:

[下一个,上一个,第一个子元素,父级]

这份文档对这个主题进行了详细介绍。

#--- creating a copy of original image ---
img2 = img.copy()

#--- checking whether the contour in hierarchy is a child and if it is then draw it ---
for i in hierarchy:
    print(i)
    if i[2] > 0:      
        cv2.drawContours(img2, [contours[i[2]]], 0, (0,255,0), 2)

cv2.imshow('img2', img2)
cv2.waitKey(0)
cv2.destroyAllWindows()

enter image description here


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接