OpenCV特征检测

Harris Corner Detection

dst=cv.cornerHarris(src, blockSize, ksize, k)

  • img - Input image, it should be grayscale and float32 type.应该输入灰度图像
  • blockSize - It is the size of neighbourhood considered for corner detection  考虑要检测的附近的块大小,for each pixel (x,y) it calculates a 2×2 gradient covariance matrix M(x,y) over a blockSize×blockSize neighborhood.,对于每一个像素,都会计算附近blocksize大小的像素点的梯度covariance矩阵。
  • ksize - Aperture parameter of Sobel derivative used. Sobel算子的卷积核大小
  • k - Harris detector free parameter in the equation. 自由参数?

例子:

import numpy as np
import cv2 as cv
filename = '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 important
dst = 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()

Corner with SubPixel Accuracy

Sometimes, you may need to find the corners with maximum accuracy. OpenCV comes with a function cv.cornerSubPix() which further refines the corners detected with sub-pixel accuracy. 有时候,我们可能会需要得到更加精确的点,所以OpenCV提供了一个cv.cornerSubPix()函数得到sub-pixel accuracy。

corners=cv.cornerSubPix(image, corners, winSize, zeroZone, criteria)

image Input single-channel, 8-bit or float image.输入的图像,需要是8比特或者float的类型
corners Initial coordinates of the input corners and refined coordinates provided for output.得到的corners坐标
winSize Half of the side length of the search window. For example, if winSize=Size(5,5) , then a (5∗2+1)×(5∗2+1)=11×11 search window is used.定义搜索窗口的一半大小,比如如果设置winSize=(5,5),那么搜索窗口为 (5∗2+1)×(5∗2+1)=11×11
zeroZone Half of the size of the dead region in the middle of the search zone over which the summation in the formula below is not done. It is used sometimes to avoid possible singularities of the autocorrelation matrix. The value of (-1,-1) indicates that there is no such a size.
criteria Criteria for termination of the iterative process of corner refinement. That is, the process of corner position refinement stops either after criteria.maxCount iterations or when the corner position moves by less than criteria.epsilon on some iteration.

例子:

import numpy as np
import cv2 as cv
filename = 'chessboard2.jpg'
img = cv.imread(filename)
gray = cv.cvtColor(img,cv.COLOR_BGR2GRAY)
# find Harris corners
gray = 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 centroids
ret, labels, stats, centroids = cv.connectedComponentsWithStats(dst)
# define the criteria to stop and refine the corners
criteria = (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 them
res = 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)

上图结果,红色的是Harris Corners 算法,绿色的是经过Sub-pixel算法修正的。可以看出,sub-pixel更加精确。 

猜你喜欢

转载自blog.csdn.net/weixin_40244676/article/details/104319135
今日推荐