I was playing around with cornerHarris function in OpenCV. I could not understand what does ksize
and k
mean in the function. The documentation mentions ksize
to be Aperture parameter of Sobel derivative used
and k
to be Harris detector free parameter in the equation
but I am not sure what does it really mean?
Could someone help me understand?
I tried to detect corners in a cube and it came it as :
with the simple code I used from the documentation:
import cv2
import numpy as np
filename = "cube.jpg"
img = cv2.imread("./images/{}".format(filename))
gray = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)
gray = np.float32(gray)
dst = cv2.cornerHarris(gray,12,3,0.04)
dst = cv2.dilate(dst,None)
# Threshold for an optimal value, it may vary depending on the image.
img[dst>0.01*dst.max()]=[0,0,255]
cv2.imshow('dst',img)
if cv2.waitKey(0) & 0xff == 27:
cv2.destroyAllWindows()
I tried tweaking K
but could not understand the role of it although I realized increasing it beyond a limit resulted in zero corner being detected.
Harris Corner detector is used to extract corners from grayscale images. The Harris detector works by first calculating the image gradient, then calculating the covariance of the gradient, which is an approximation of the local Hessian.
It has 4 main steps:
Edge detection (spatial derivative calculation) - The first step is to convert the grayscale image into an image of edges. There are many techniques to do this, but the cv2 uses a filter called Sobel's kernel, which gets cross-correlated with the original image. The ksize parameter determines the size of the Sobel kernel (3x3, 5x5, etc..). As the size increases, more pixels are part of each convolution process and the edges will get more blurry.
Structure tensor setup - Basically we construct a matrix M which represents the direction of the gradients (edges) at every point of the image. This matrix can then be used to determine which of the edge pixels are corners:
The k parameter lets you influence in this step, trading off precision and recall. So with a bigger k, you will get less false corners but you will also miss more real corners (high precision), with a smaller k you will get a lot more corners, so you will miss less true corners, but get a lot of false ones (high recall).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With