cv2.cornerHarris is OpenCV's function which implements, as follows from the name, the Harris corners detector. It takes six arguments: the first four arguments are mandatory and last two arguments have default values. The arguments are as follows:
- Single-channel 8-bit or floating-point image, on which corners are to be detected
- Size of the neighborhood window: it should be set to a small value larger than 1
- Size of the window to compute derivatives: it should be set to an odd number
- Sensitivity coefficient for the corners detector: it's usually set to 0.04
- An object where you can store the results
- The borders extrapolation method
The borders extrapolation method determines the manner of image extending. It can be set to a bunch of values (cv2.BORDER_CONSTANT, cv2.BORDER_REPLICATE, and so on), and by default, cv2.BORDER_REFLECT_101 is used. The result of the cv2.cornerHarris call is a map of the Harris measure. Points with higher values are more likely to be good corners. As a result of launching the code related to the Harris corner detector, you will get an image similar to the following (the left part of the image is corners visualization, and the right part is the Harris measure map):
Another method we've applied in this recipe is the Features from Accelerated Segment Test (FAST) detector. It also finds corners on an image, but in another way. It considers a circle around each point and computes some statistics on that circle. Let's find out how to use FAST.
First, we need to create a detector using cv2.FastFeatureDetector_create. This function accepts an integer threshold, a flag to enable non-maximum suppression, and a mode that determines both the size of the neighbor area and the number of points threshold. All of these parameters can be modified later using corresponding methods of the cv2.FastFeatureDetector class (setNonmaxSuppression, in the previous code).
To use the detector after the initialization, we need to call the cv2.FastFeatureDetector.detect function. It takes a single-channel image and returns a list of cv2.KeyPoint objects. This list can be converted to a numpy array by cv2.KeyPoint.convert. Each element in the resulting array is a point of the corner.
Execution of the code related to the FAST detector brings up the following images (the left image for non-maximum suppression enabled, the right image for non-maximum suppression being disabled):