SoFunction
Updated on 2024-10-29

OpenCV Harris Corner Detection|Harris Corner Theory in Practice

goal

In this chapter, learn

  • The Idea Behind "Harris Corner Detection"
  • function:(),cv.2cornerSubPix()

doctrinal

This can be represented by the following diagram:

Thus, the result of Harris Corner Detection is a grayscale image with these scores. Suitable thresholds provide the corners of the image.

Harris Corner Detection in OpenCV

There is an implementation of Harris corner point detection in OpenCV that(). Its parameters are:

dst = (src, blockSize, ksize, k[, dst[, borderType]] )

  • src - Input image, grayscale and float32 type
  • blockSize - is the size of the neighborhood considered for corner detection
  • ksize - Aperture parameters of the Sobel derivative used
  • k- Harris detector free parameters in Eq.
import cv2
import numpy as np
from matplotlib import pyplot as plt
img = ('')
img_copy = ()
gray = (img, cv2.COLOR_BGR2GRAY)
dst = (gray, 2, 3, 0.04)
# result is dilated for marking the corners, not important
dst = (dst, None)
# Threshold for an optimal value, it may vary depending on the image.
img[dst >0.01*()]=[255,0,0]
# plot
(121)
(img_copy, cmap='gray')
([])
([])
(122)
(img, cmap='gray')
([])
([])
()

Here are the results:

As you can see, the individual corner points have been marked in red.

Corner of SubPixel precision

Sometimes it may be necessary to find the most accurate corners, and OpenCV comes with a function that does this(), which further refines the corner points detected with sub-pixel precision. Here is an example.

  • As before, the first thing you need to do is to find the Cape Harris point first
  • Then refine them by the centers of mass of those corners (there might be a bunch of pixels in a corner, take their centers of mass)
  • Harris corners are marked with red pixels, SubPixel corners are marked with green pixels

insofar as()function, the condition to stop iterating must be defined. WeIt can be stopped after a specific number of iterations or after a certain precision is reached. It is also necessary to define the size of the neighbors for which it will search for corner points.

corners = ( image, corners, winSize, zeroZone, criteria )

  • image: input image, single channel
  • corners: initial coordinates of the input and refined coordinates provided for the output
  • winSize: half the side length of the search window
  • zeroZone: half the size of the dead zone in the middle of the search area is summed in the following formula, sometimes used to avoid possible singularities in the autocorrelation matrix. A value of (-1,-1)(-1,-1)(-1,-1) means that there is no such size.
  • criteria: Conditions for terminating the corner refinement process
# sub pixel for more accurate corners
import cv2
import numpy as np
img = ('')
gray = (img, cv2.COLOR_BGR2GRAY)
# find Harris corners
dst = (gray,2, 3, 0.04)
dst = (dst, None)
ret, dst = (dst, 0.01*(), 255,0)
dst = np.uint8(dst)
# find centroids
ret, labels, stats, centroids = (dst)
# define the criteria to stop and refine the corners
criteria = (cv2.TERM_CRITERIA_EPS + cv2.TERM_CRITERIA_MAX_ITER, 100, 0.001)
corners = (gray, np.float32(centroids), (5, 5), (-1, -1), criteria)
# Now draw them
res = ((centroids,corners))
res = np.int0(res)
img[res[:,1],res[:,0]]=[0,0,255]
img[res[:,3],res[:,2]] = [0,255,0]
('subpixel', img)
(0)
()

Here are the results, you can see that SubPixel is a bit more accurate:

Additional resources

/4.1.2/dd/d1…

/4.1.2/dd/d1…

/4.1.2/dd/d1…

/4.1.2/d4/d8…

/4.1.2/dd/d1…

The above is OpenCV Harris Corner Detection | Harris Corner theory and practice of the details, more information about OpenCV Harris Corner Detection please pay attention to my other related articles!