如何在opencv中的图像中找到形状的角点?

时间:2018-06-22 08:52:21

标签: image opencv image-processing edge-detection corner-detection

我必须在图像中找到形状的角。我已经使用哈里斯角点检测算法来找到角点,但是它给出了图像中存在的总角点,并且无法为该图像中的特定形状找到角点。请提出其他方法。enter image description here

2 个答案:

答案 0 :(得分:6)

您可以使用哈里斯角点检测算法。角是两个边缘的交汇点,其中边缘是图像亮度的突然变化。该算法直接参考方向将角点得分的差异考虑在内(维基百科)。函数cornerSubPix()改进了角的位置-迭代查找角或径向鞍点的亚像素准确位置(opencv文档)。

代码示例:

import cv2
import numpy as np


img = cv2.imread('edges.png')
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
gray = np.float32(gray)
dst = cv2.cornerHarris(gray,5,3,0.04)
ret, dst = cv2.threshold(dst,0.1*dst.max(),255,0)
dst = np.uint8(dst)
ret, labels, stats, centroids = cv2.connectedComponentsWithStats(dst)
criteria = (cv2.TERM_CRITERIA_EPS + cv2.TERM_CRITERIA_MAX_ITER, 100, 0.001)
corners = cv2.cornerSubPix(gray,np.float32(centroids),(5,5),(-1,-1),criteria)
for i in range(1, len(corners)):
    print(corners[i])
img[dst>0.1*dst.max()]=[0,0,255]
cv2.imshow('image', img)
cv2.waitKey(0)
cv2.destroyAllWindows

enter image description here

enter image description here

要检查它们是否是真实值,可以添加:

for i in range(1, len(corners)):
    print(corners[i,0])
    cv2.circle(img, (int(corners[i,0]), int(corners[i,1])), 7, (0,255,0), 2)

结果:

enter image description here

编辑:

如果要为每种形状分别提取角,可以先搜索轮廓,然后对每个轮廓应用Harris角检测(可以使用cv2.fillPolly()将其绘制在蒙版上)。您甚至可以根据其特征(例如旋转角度,拐角数量等)定义它们的形状。我已经编写了一个示例代码来帮助您理解,但请注意,还有其他形状可以符合我制定的标准,您可能还会制定其他标准(梯形,圆形等)。这只是一个简单的例子:

import cv2
import numpy as np


img = cv2.imread('edges.png')
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
ret,thresh = cv2.threshold(gray,150,255,cv2.THRESH_BINARY)
im2, contours, hierarchy = cv2.findContours(thresh,cv2.RETR_TREE,cv2.CHAIN_APPROX_NONE)

for i in contours:
    img = cv2.imread('edges.png')
    size = cv2.contourArea(i)
    rect = cv2.minAreaRect(i)
    if size <10000:
        gray = np.float32(gray)
        mask = np.zeros(gray.shape, dtype="uint8")
        cv2.fillPoly(mask, [i], (255,255,255))
        dst = cv2.cornerHarris(mask,5,3,0.04)
        ret, dst = cv2.threshold(dst,0.1*dst.max(),255,0)
        dst = np.uint8(dst)
        ret, labels, stats, centroids = cv2.connectedComponentsWithStats(dst)
        criteria = (cv2.TERM_CRITERIA_EPS + cv2.TERM_CRITERIA_MAX_ITER, 100, 0.001)
        corners = cv2.cornerSubPix(gray,np.float32(centroids),(5,5),(-1,-1),criteria)
        if rect[2] == 0 and len(corners) == 5:
            x,y,w,h = cv2.boundingRect(i)
            if w == h or w == h +3: #Just for the sake of example
                print('Square corners: ')
                for i in range(1, len(corners)):
                    print(corners[i])
            else:
                print('Rectangle corners: ')
                for i in range(1, len(corners)):
                    print(corners[i])
        if len(corners) == 5 and rect[2] != 0:
            print('Rombus corners: ')
            for i in range(1, len(corners)):
                print(corners[i])
        if len(corners) == 4:
            print('Triangle corners: ')
            for i in range(1, len(corners)):
                print(corners[i])
        if len(corners) == 6:
            print('Pentagon corners: ')
            for i in range(1, len(corners)):
                print(corners[i])
        img[dst>0.1*dst.max()]=[0,0,255]
        cv2.imshow('image', img)
        cv2.waitKey(0)
        cv2.destroyAllWindows

enter image description here

输出(在检测到所有形状之后):

enter image description here

答案 1 :(得分:0)

对于每种形状,都跟踪轮廓,对于轮廓的每个像素,检查是否找到了附近的角(例如在3x3或5x5邻域中)。