How to implement the Shi-Tomasi corner detector in OpenCV Python

OpenCV is a Python library used to perform computer vision tasks. It provides us with built-in functions to detect objects, faces, corners, etc, in an image. In this Answer, we will explore how to detect corners in an image. A corner is a point where two lines join, and the color intensity changes in all directions. We will use the Shi-Tomasi algorithm developed by J.Shi and C.Tomasi to detect the corners.

Basic implementation

The Shi-Tomasi algorithm uses a window that moves through the image to detect any change in color intensities in the window. Three cases occur while detecting corners:

  1. The window is over a place with no corner or edge: There is no color intensity change in any direction. The diagram below shows the window over a flat area

Window positioned on a flat surface
Window positioned on a flat surface
  1. The window is over an edge: Although the color intensity changes above and below the edge, we do not consider it. We keep an eye on the intensity change along the edge direction. The diagram below shows the window present over the edge:

Window positioned on an edge
Window positioned on an edge
  1. The window is over a corner: The color intensity changes along the edge direction, indicating that there might be a corner. The diagram below shows the window over the corner:

Window positioned on a corner
Window positioned on a corner

Formula

OpenCV provides us with the Shi-Tomasi corner detection function with the name goodFeaturesToTrack. It has four parameters:

  • Parameter 1: Takes the greyscale image.

  • Parameter 2: Takes the maximum number of corners to detect.

  • Parameter 3: Takes the quality of the detected corner (preferred value is 0.1).

  • Parameter 4: Takes the maximum distance between the corners (preferred value is 10).

The underlying score function that tells if the corner is detected on not is:

In the function, λ1 and λ2 are the eigenvaluesEigenvalues are scalar values that define how a matrix scales certain vectors when multiplied by it..

Code example

In the example below, we load a wall image and apply the Shi-Tomasi corner detector. Click the "Run" button below to view the corners detected in the image.

import cv2
import numpy as np

wall_img = cv2.imread('wall.jpg')
cv2.imshow("Corner Detector", wall_img)
  
gray_img = cv2.cvtColor(wall_img, cv2.COLOR_BGR2GRAY)
  
img_corners = cv2.goodFeaturesToTrack(gray_img, 100, 0.01, 10)
  
img_corners = np.int64(img_corners)
  
for i in img_corners:
    x, y = i.ravel()
    cv2.circle(wall_img, (x, y), 5, (0, 0, 0), -1)
  
cv2.imshow("Corner Detector", wall_img)

if cv2.waitKey(0) & 0xff == 27: 
    cv2.destroyAllWindows()
Code to detect corners in an image using Shi-Tomasi corner detection algorithm

Code explanation

  • Lines 1–2: We import the cv2 and numpy libraries.

  • Line 4: We read the wall.jpg image and store it in the wall_img variable.

  • Line 6: We convert the image into a greyscale image using the cvtColor provided by cv2.

  • Line 8: We use the goodFeaturesToTrack function and pass the greyscale image to it that detects the corners in the image.

  • Line 10: We convert the detected corner's coordinates to integer values using the np.int64 function provided by numpy.

  • Line 12–14: We iterate through the corner coordinates and create black circles on the image where the corners are detected.

  • Line 16: We display the image with corners detected on the screen using the imshow function.

Applications

Corner detection finds its applications in various fields:

  • Robotics: It is used in robotics for navigation.

  • Object detection: It is used for detecting and recognizing objects.

  • Tracking: It is used in tracking and detecting people in surveillance.

Free Resources

Copyright ©2025 Educative, Inc. All rights reserved