Implementation
The sample image we’ve used can be found here.
We’ll first convert it to grayscale.
from skimage.io import imread
from skimage.color import rgb2gray
img = imread('resources/box.jpg')
imggray = rgb2gray(img)

You can also try this code with Online Python Compiler
Run Code

Computing the spatial derivatives.
from scipy import signal as sig
import numpy as np
def gradient_x(imggray):
##Sobel operator kernels.
kernel_x = np.array([[-1, 0, 1],[-2, 0, 2],[-1, 0, 1]])
return sig.convolve2d(imggray, kernel_x, mode='same')
def gradient_y(imggray):
kernel_y = np.array([[1, 2, 1], [0, 0, 0], [-1, -2, -1]])
return sig.convolve2d(imggray, kernel_y, mode='same')
I_x = gradient_x(imggray)
I_y = gradient_y(imggray)

You can also try this code with Online Python Compiler
Run Code
Structure tensor setup
Ixx = ndi.gaussian_filter(I_x**2, sigma=1)
Ixy = ndi.gaussian_filter(I_y*I_x, sigma=1)
Iyy = ndi.gaussian_filter(I_y**2, sigma=1)

You can also try this code with Online Python Compiler
Run Code
Computing Harris response
k = 0.05
# determinant
detA = Ixx * Iyy - Ixy ** 2
# trace
traceA = Ixx + Iyy
harris_response = detA - k * traceA ** 2

You can also try this code with Online Python Compiler
Run Code
Finding edges and corners using value of R.
For Edge : R < 0
For Corner : R > 0
For Flat: R = 0
img_copy_for_corners = np.copy(img)
img_copy_for_edges = np.copy(img)
for rowindex, response in enumerate(harris_response):
for colindex, r in enumerate(response):
if r > 0:
# this is a corner
img_copy_for_corners[rowindex, colindex] = [255,0,0]
elif r < 0:
# this is an edge
img_copy_for_edges[rowindex, colindex] = [0,255,0]

You can also try this code with Online Python Compiler
Run Code

Grouping the corners into one point
You can see there are multiple corners points found in the image, we need to find the local maxima of the corner points. there is a method in skimage which does a neat job in finding the peaks.
#Harris corner detection using skimage library
from skimage.feature import corner_harris, corner_peaks
coords = corner_peaks(skimage_harris_response)
fig, ax = plt.subplots()
ax.imshow(img, interpolation='nearest', cmap=plt.cm.gray)
ax.plot(coords[:, 1], coords[:, 0], '.r', markersize=3)

You can also try this code with Online Python Compiler
Run Code

Advantages and Disadvantages
The biggest advantage of Harris Corner Detector is that its invariant to factors like rotation, translation, and changes in illumination which makes it the first choice for image data retrieval and stereomatching. It is the most repetitive and most informative among other known corner detection algorithms.
The one big drawback of harris corner detection is that we need to set different threshold values for every image in order to detect the most prominent interest points. If the threshold value used is too low, then the algorithm may end up detecting large amount of points with noisy image data.
Applications
Harris corner detection is used commonly used in day-to-day tasks.
- It is used for image alignment, stitching and registration.
- It can be used for creating 2D mosaics.
- It is used in 3D modelling.
-
It is used in object detection and motion detection.
Also read, Sampling and Quantization
FAQs
-
Mention some of the commonly used corner detection algorithms.
Other than Harris Corner detection, other corner detection techniques are - Susan Corner detection, Moravec Corner detection, Förstner corner detection, and Robust Fuzzy Rule Corner Detection.
-
How do R values determine a feature?
For R < 0, It is an edge.
For R > 0, it is a corner.
For R = 0, it is a flat.
-
How do eigen values vary with different features?
For a flat region, both the eigen values are small.
For an edge, one of the eigen values is significantly large and the other one is small.
For a corner, both the eigen values are large.
Key Takeaways
Harris corner detection is a commonly used computer vision algorithm for corner extraction and inferring features in an image. This blog explains the algorithm in detail along with its implementation and its limitations. Readers are advised to go through the blog a couple of times to have a better grasp over the details. If you want to deep dive into machine learning and deep learning, check out our industry-oriented Machine learning course curated by Stanford University alumni and industry experts.