OpenCV feature detection

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 . For each pixel, the gradient covariance matrix of nearby blocksize pixels will be calculated.
  • ksize  -Aperture parameter of Sobel derivative used. Convolution kernel size of Sobel operator
  • k  -Harris detector free parameter in the equation. Free parameter?

example:

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. Sometimes, you may need to find the corners with maximum accuracy.  So OpenCV A cv.cornerSubPix() function is provided to get sub-pixel accuracy.

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

 

image Input single-channel, 8-bit or float image. The input image must be 8-bit or float image
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. Definition Half the size of the search window, for example, if you set winSize=(5,5), then the search window is (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.

 

example:

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)

In the above figure, the red one is the Harris Corners algorithm, and the green one is modified by the Sub-pixel algorithm. It can be seen that sub-pixel is more accurate. 

Guess you like

Origin blog.csdn.net/weixin_40244676/article/details/104319135