How to do it...

The basic OpenCV function that is used to detect Harris corners is called cv::cornerHarris and is straightforward to use:

  1. You call it on an input image, and the result is an image of floats saved on cornerStrength that gives you the corner strength at each pixel location, with the following code:
// Detect Harris Corners 
cv::Mat cornerStrength; 
cv::cornerHarris(image,      // input image 
cornerStrength, // image of cornerness 
                3,              // neighborhood size 
                3,              // aperture size 
                0.01);          // Harris parameter
  1. A threshold is then applied to this output image in order to obtain a set of detected corners. This is accomplished with the following code:
// threshold the corner strengths cv::Mat harrisCorners; double threshold= 0.0001; cv::threshold(cornerStrength,harrisCorners, ...

Get OpenCV 4 Computer Vision Application Programming Cookbook - Fourth Edition now with O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.