Scikit Image - Shi-Tomasi Corner Detection



The Shi-Tomasi corner detector, also known as the Kanade-Tomasi corner detector, is indeed a widely used algorithm for corner detection in computer vision. It computes a corner score for each pixel in an image and selects the pixels with the highest scores as corners. The corner score is often referred to as autocorrelation and is computed based on the eigenvalues i.e., min(λ_1,λ_2).

This detector is often used for feature tracking and keypoint detection in computer vision applications.

The scikit-image library provides useful functions for applying the Shi-Tomasi corner detection algorithm and working with the detected corners using the corner_shi_tomasi(), and corner_peaks() functions, respectively.

Using the skimage.feature.corner_shi_tomasi() function

This corner detector relies on the auto-correlation matrix A, defined as −

$$\mathrm{A= \begin{matrix} [(imx^{\ast\ast} 2)(imx^{\ast}imy)] = [Axx \: Axy]\\ [(imx^{\ast}imy)(imy^{\ast\ast}2)] [Axy \: Ayy] \end{matrix}}$$

In this equation imx and imy represent the first derivatives of the image, which are typically smoothed (averaged) with a Gaussian filter.

The corner measure is then determined by computing the smaller eigenvalue of A using the formula −

$$\mathrm{((Axx + Ayy) - sqrt((Axx - Ayy)^{\ast \ast} 2 + 4^{\ast}Axy^{\ast \ast}2))/2}$$

Syntax

Here is the syntax of the function −

skimage.feature.corner_shi_tomasi(image, sigma=1)

Parameters

Here are the details of its parameters −

  • image (M, N) ndarray: The input image on which the Shi-Tomasi corner detection is performed.

  • sigma (float, optional): Standard deviation used for the Gaussian kernel, which is applied as a weighting function for the auto-correlation matrix. This parameter controls the smoothing of the first derivatives.

The function returns a Shi-Tomasi response image as a ndarray.

Example

Here is an example that demonstrates how to use the ShiTomasi corner detector method using the corner_shi_tomasi() function from the scikit-image library to compute the Shi-Tomasi corner measure response in a simple 12x12 array.

from skimage.feature import corner_shi_tomasi, corner_peaks
import numpy as np

# Create a 12x12 array with a square-shaped region
square = np.zeros((12, 12))
square[3:9, 3:9] = 1
square.astype(int)

# Display the input array
print("Input array:")
print(square)

# Compute Shi-Tomasi (Kanade-Tomasi) corner measures
corners = corner_shi_tomasi(square)

# Use corner_peaks to find corner coordinates with a minimum distance of 1 pixel
coords = corner_peaks(corners, min_distance=1)

print('Output the detected corners:', coords)

Output

Input array:
[[0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0.]
 [0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0.]
 [0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0.]
 [0. 0. 0. 1. 1. 1. 1. 1. 1. 0. 0. 0.]
 [0. 0. 0. 1. 1. 1. 1. 1. 1. 0. 0. 0.]
 [0. 0. 0. 1. 1. 1. 1. 1. 1. 0. 0. 0.]
 [0. 0. 0. 1. 1. 1. 1. 1. 1. 0. 0. 0.]
 [0. 0. 0. 1. 1. 1. 1. 1. 1. 0. 0. 0.]
 [0. 0. 0. 1. 1. 1. 1. 1. 1. 0. 0. 0.]
 [0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0.]
 [0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0.]
 [0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0.]]

Output the detected corners: 
[[3 3]
 [3 8]
 [8 3]
 [8 8]]

Example

The following example demonstrates the application of the Shi-Tomasi (Kanade-Tomasi) corner detection method to an image using the corner_shi_tomasi() function.

from skimage.feature import corner_shi_tomasi, corner_peaks
from skimage import io
import matplotlib.pyplot as plt

# Load an image
image = io.imread('Images/sample.png', as_gray=True)

# Compute Shi-Tomasi (Kanade-Tomasi) corner measures
corners = corner_shi_tomasi(image, sigma=2)

# Use corner_peaks to find corner coordinates with a minimum distance of 10 pixels
coords = corner_peaks(corners, min_distance=10)

# Create a plot to display the image and detected corners
plt.figure(figsize=(10, 5))
plt.imshow(image, cmap='gray')

# Mark the detected corners with red dots
plt.plot(coords[:, 1], coords[:, 0], 'ro', markersize=5)

plt.title('Shi-Tomasi Corner Detection') plt.axis('off')
plt.show()

Output

Shi-Tomasi Corner
Advertisements