Angular point

Features of corner points: the intersection between contours for the same scene, even if the Angle of view changes, it usually has stable characteristics. The pixels in the area near this point have great changes in both gradient direction and gradient amplitudeCopy the code

Basic principle of corner detection

Using a fixed window to slide the image in any direction, comparing the two situations before sliding and after sliding, the change degree of pixel gray in the window, if there is sliding in any direction, there is a large change of gray, then we can think that there are corners in the window.Copy the code

Harris corner detection in OpenCV

cv2.cornerHarris(src, blockSize, ksize, k, dst, borderType)
Copy the code
SRC: input image whose data type is float32 blockSize: field size to be considered in corner detection ksize: window size used in Sobel derivation k: Harris free parameter in the corner detection equation, the value of which is [0,04,0.06]. DST: Target image borderType: borderTypeCopy the code

Test code:

def plot(img, n):

    gray = CV. CvtColor (img, CV.COLOR_BGR2GRAY) Gray = Np.float32 (gray) # The input image must be float32, the last parameter is in0.040.05Between DST = CV. CornerHarris (gray,2.3.0.04)
    dst = cv.dilate(dst, None)
    img[dst > 0.005*dst.max()] = [255.0.0]
    plt.subplot(1.3, n), plt.imshow(img, cmap='gray'),
    plt.title('dst'), plt.axis('off')

Copy the code

The effect is shown in the picture, that is, the corners of the picture are detected:

Shi-tomasi corner detection goodFeaturesToTrack()

GoodFeaturesToTrack () is a cornerHarris() function that works just as well as the cornerHarris() function. cv2.goodFeaturesToTrack ( image, maxcorners, qualityLevel, minDistance, [, corners [, mask [, blocksize [, UseHarrisDetector [, k]]]]]) normally, its input should be grayscale image; The second parameter N: is the N best corners in the image to be output; The third parameter: set the quality level of corner point, between 0 and 1; Represents the lowest mass of a corner, and any corner less than this mass is eliminated; The last parameter: set the shortest Euclidean distance between two corners; It's the sum of the squares of the pixel differences between the two corners;Copy the code

Test code: (the parameters used are the most common)

def plot01(img):# Corner detection gray= cv.cvtColor(img, cv.COLOR_BGR2GRAY)
    corners = cv.goodFeaturesToTrack(gray, 25.0.01.10)
    corners = np.int0(corners)
    for i in corners:
        x, y = i.ravel()
        cv.circle(img, (x, y), 3.255.- 1)
    cv.imshow("img", img)
Copy the code