In the previous blog, we discussed what are features and how corners are considered as a good feature as compared to edges and flat surfaces. In this blog, let’s discuss one of the famous and most commonly used corner detection methods known as Harris Corner Detection. This wasone of the early attempts to find the corners by**Chris Harris & Mike Stephens**in their paper**A Combined Corner and Edge Detector**in 1988. Now it is called the Harris Corner Detector. So, let’s first understand the basic idea behind this algorithm, and then we will dive into mathematics. Let’s get started.

As discussed in the previous blog, corners are regions in the image with large variations in intensity in all directions. For instance, take a look at the below image. If you shift the window by a small amount, then corners will produce a significant change in all directions while edges will output no change if we move the window along the edge direction. And the flat region will output no change in all directions on window movement.

So, the authors took this simple idea of finding the difference in intensity for a displacement of(u,v)in all directions into a mathematical form. This is expressed as

Here,

- the window function is either a rectangular window or a Gaussian window which gives weights to pixels underneath.
- E(u,v) is the difference in intensities between the original and the moved window.

As can be clearly seen, for nearly constant patches the error function will be close to 0 while for distinctive patches this will be larger. Hence, our aim is to find patches where this error function is large. In other words, we need to maximize this error function for corner detection. That means we have to maximize the second term.We can do this by applying Taylor Expansionand using some mathematical stepsas shown below

So, the final equation becomes

Then comes the main part. As we have already discussed that corners are the regions in the image with large variations in intensity in all directions. Or we can say it in terms of the above matrix M as “A corner is characterized by a large variation ofMin all directions of the vector [u,v]”. So, if you remember that eigenvalues tell us about the variance thus by simply analyzing the eigenvalues of the matrix M we can infer the results.

But the authors note that the exact computation of the eigenvalues is computationally expensive, since it requires the computation of asquare root, and instead suggests the following score functionwhich determines if a window contains a corner or not. This is shown below

Therefore, the algorithmdoes not have to actually compute theeigenvalue decompositionof the matrix M and instead it is sufficient to evaluate thedeterminantandtraceofmatrix Mto find the corners.

Now, depending upon the magnitude of the eigenvalues and the score (R), we can decide whether a region is a corner, an edge, or flat.

- When|R|is small, which happens whenλ1andλ2are small, the region is flat.
- WhenR<0, which happens whenλ1>>λ2or vice versa, the region is edge.
- If λ1>>λ2, then vertical edge
- otherwise horizontal edge

- WhenRis large, which happens whenλ1andλ2are large andλ1∼λ2, the region is a corner

This can also be represented by the below image

So, this algorithm will give us a score corresponding to each pixel. Then we need to do thresholding in order to find the corners.

Because we consider only the eigenvalues of the matrix (M), we are considering quantities that are invariant also to rotation, which is important because objects that we are tracking might rotate as well as move. So, this makes this algorithm rotation invariant.

So, this concludes the Harris Corner Detector. I hope you understood this. Now, let’s see how to do this using OpenCV-Python.

## OpenCV

OpenCV provides a builtin function cv2.cornerHarris() that runs the Harris corner detector on the image. Below is the syntax for this.

1 2 3 4 5 6 | cv2.cornerHarris(src, blockSize, ksize, k) # src - Input single-channel 8-bit or floating-point image. # blockSize - Neighborhood size used when computing the matrix M. # ksize - Aperture parameter for the Sobel operator. # k - Harris detector free parameter in the score equation. |

For each pixel(x,y)it calculates a2×2gradient covariance matrixM(x,y)over ablockSize×blockSizeneighborhood. Then using this matrix M, this calculates the score for each pixel. Below is the code for this

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 | import cv2 # Read the image and convert to greyscale img = cv2.imread('D:/downloads/contracing.png') gray = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY) # Find the corners using the Harris Corner Detector dst = cv2.cornerHarris(gray,2,3,0.04) #result is dilated for enhancing the corners, not important dst = cv2.dilate(dst,None) # Threshold for an optimal value, it may vary depending on the image. thresh = 0.01*dst.max() img[dst>thresh]=[0,255,0] # Display the image cv2.imshow('dst',img) cv2.waitKey(0) |

Below is the result of this.

So, this is how you can implement the Harris Corner Detector using OpenCV-Python. I hope you understood this. In the next blog, we will discuss the Shi-Tomasi algorithm that improves this Harris Corner Detector even further. Hope you enjoy reading.

If you have any doubts/suggestions please feel free to ask and I will do my best to help or improve myself. Goodbye until next time.

0 Shares