Harris Corner Detection (2024)

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 byChris Harris & Mike Stephensin their paperA Combined Corner and Edge Detectorin 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.

Harris Corner Detection (1)

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

Harris Corner Detection (2)

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

Harris Corner Detection (3)

So, the final equation becomes

Harris Corner Detection (4)

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

Harris Corner Detection (5)

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

Harris Corner Detection (6)

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.

Harris Corner Detection (7)

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

Harris Corner Detection (2024)

References

Top Articles
Bigcutie Eve
The Night Agent Star Luciane Buchanan Shares Bathing Suit Video From Hawaii
Gaseous Form Pathfinder
Laura Loomer, far-right provocateur who spread 9/11 conspiracy theory, influencing Trump as he searches for a message | CNN Politics
Butte Jail Roster Butte Mt
Osrs Tokkul Calculator
Understanding Pickleball Court Dimensions: Essential Guide
Osu Worday
Florida death row inmates promised more humane treatment after lawsuit settlement
Cbs Fantasy Trade Values
Selinas Gold Full Movie Netflix
Anchor Martha MacCallum Talks Her 20-Year Journey With FOX News and How She Stays Grounded (EXCLUSIVE)
Hose Woe Crossword Clue
Sabermetrics Input Crossword Clue
Faotp Meaning In Text
Short Swords Resource Pack (1.21.1, 1.20.1) - Texture Pack
Shore Lodge Webcam
Diablo 3 Legendary Reforge
Pair sentenced for May 2023 murder of Roger Driesel
Jacksonville Nc Skipthegames
8005607994
Craigslist St. Paul
2022 Jeep Grand Cherokee Lug Nut Torque
02080797947
3Kh0 1V1 Lol
Small Party Hall Near Me
Remembering the names of those who died on 9/11
Bakkesmod Preset
Aspect of the Dragons
Snowy Hydro Truck Jobs in All Sydney NSW - Sep 2024 | SEEK
Jcpenney Salon Salinas
manhattan cars & trucks - by owner - craigslist
Lagniappemobile
Ny Lottery Second Chance App
Every Act That's Auditioned for AGT Season 18 So Far
Green Press Gazette Obits
9294027542
Baroque Violin Shop Cincinnati Oh
U Arizona Phonebook
The Penitent One Unmasked
Ava Kayla And Scarlet - Mean Bitches Humiliate A Beta
Myrtle Beach Pelicans Stadium Seating Chart
Ups Carrier Locations Near Me
Synergy Grand Rapids Public Schools
Savor Some Southern Tradition With Crispy Deep-Fried Chitterlings
Akc Eo Tryouts 2022
Nordstrom Rack Glendale Photos
Vrlbi Rentals
Redbox Walmart Near Me
Nine Star Hegemon Body Art
Yvi Eulb Meaning In Latin
Latest Posts
Article information

Author: Ouida Strosin DO

Last Updated:

Views: 5337

Rating: 4.6 / 5 (56 voted)

Reviews: 87% of readers found this page helpful

Author information

Name: Ouida Strosin DO

Birthday: 1995-04-27

Address: Suite 927 930 Kilback Radial, Candidaville, TN 87795

Phone: +8561498978366

Job: Legacy Manufacturing Specialist

Hobby: Singing, Mountain biking, Water sports, Water sports, Taxidermy, Polo, Pet

Introduction: My name is Ouida Strosin DO, I am a precious, combative, spotless, modern, spotless, beautiful, precious person who loves writing and wants to share my knowledge and understanding with you.