pythonopencvfeature-extractionfeature-detectioncorner-detection

How to decrease number of points detected by Harris Corner Detector?


I am using Harris Corner Detector for Feature Detection. The code I am using is given below. The result is more than 10000+ keypoints being detected. How do I decrease the number of keypoints detected to around 1000 and make sure that it's precise?


import numpy as np
import cv2 as cv
img = cv.imread('img.jpg')

half = cv.resize(img, (1200, 800), fx=0.1, fy=0.1)

gray = cv.cvtColor(half, cv.COLOR_BGR2GRAY)

gray = np.float32(gray)
dst = cv.cornerHarris(gray, 2, 3, 0.20)

dst = cv.dilate(dst, None)

half[dst > 0.01 * dst.max()] = [0, 0, 255]

num_corners = np.sum(dst > 0.01 * dst.max())

print(num_corners)

cv.imshow('dst', half)

cv.waitKey(0)
cv.destroyAllWindows()

Solution

  • Play with the value "0.01" in the following lines of your code. Increase it to decrease the number of corners detected.

    half[dst > 0.01 * dst.max()] = [0, 0, 255]
    
    num_corners = np.sum(dst > 0.01 * dst.max())
    

    Information about Harris Corner Detection is available here.