窗口函数可以是矩形窗口,也可以是高斯窗口(它为下面的像素赋予权重)。
我们需要最大化这个函数E(u,v)来检测拐角。这意味着,我们要使第二项最大化。将泰勒展开应用于上述方程,并使用一些数学步骤(如需完整推导,请参考您喜欢的任何标准课本),得到最终方程为:
这里的 Ix 和 Iy 分别是图像在 x 和 y 上的导数。(可以很容易的通过函数 cv.Sobel()得到)。
接下来是主要部分。在这之后,他们创建了一个分数,基本上是一个方程,它将决定一个窗口是否可以包含一个角。
其中:
因此,这些特征值决定了该区域是角,边沿还是平台区。
可以用下图 λ1 和 λ2 坐标图来表示:
因此,Harris角检测的结果是一个带评分的灰度图像。适当的阈值设置会得到图像中的角。我们将用一个简单的图像来做测试。
OpenCV的Harris角点检测实现
OpenCV 有一个函数 cv.cornerHarris() 就是用于此目的,包括一下参数:
举例说明该函数的使用:
import numpy as npimport cv2 as cvfilename = 'chessboard.png'img = cv.imread(filename)gray = cv.cvtColor(img,cv.COLOR_BGR2GRAY)gray = np.float32(gray)dst = cv.cornerHarris(gray,2,3,0.04)#result is dilated for marking the corners, not importantdst = cv.dilate(dst,None)# Threshold for an optimal value, it may vary depending on the image.img[dst>0.01*dst.max()]=[0,0,255]cv.imshow('dst',img)if cv.waitKey(0) & 0xff == 27: cv.destroyAllWindows()
以下就是检测结果:
亚像素精度的角点检测
有时候,你可能需要找到最精确的角。OpenCV附带一个函数cv.cornersubpix(),它进一步细化了以亚像素精度检测到的角。下面是一个例子。像往常一样,我们需要先找到哈里斯角。然后我们通过这些角的质心(可能在一个角上有一堆像素,我们取它们的质心)来细化它们。Harris角用红色像素标记,精制角用绿色像素标记。对于这个函数,我们必须定义何时停止迭代的条件。我们在指定的迭代次数或达到一定的精度后停止它,无论先发生什么。我们还需要定义搜索拐角的邻域的大小。
import numpy as npimport cv2 as cvfilename = 'chessboard2.jpg'img = cv.imread(filename)gray = cv.cvtColor(img,cv.COLOR_BGR2GRAY)# find Harris cornersgray = np.float32(gray)dst = cv.cornerHarris(gray,2,3,0.04)dst = cv.dilate(dst,None)ret, dst = cv.threshold(dst,0.01*dst.max(),255,0)dst = np.uint8(dst)# find centroidsret, labels, stats, centroids = cv.connectedComponentsWithStats(dst)# define the criteria to stop and refine the cornerscriteria = (cv.TERM_CRITERIA_EPS + cv.TERM_CRITERIA_MAX_ITER, 100, 0.001)corners = cv.cornerSubPix(gray,np.float32(centroids),(5,5),(-1,-1),criteria)# Now draw themres = np.hstack((centroids,corners))res = np.int0(res)img[res[:,1],res[:,0]]=[0,0,255]img[res[:,3],res[:,2]] = [0,255,0]cv.imwrite('subpixel5.png',img)
以下是结果,每个角经过缩放后显示红绿不同位置标识,该函数明显提升了精度: