Implementing Shi-Tomasi Corner Detector in OpenCV Python

The Shi-Tomasi Corner Detector is an enhanced algorithm of the Harris Corner Detector. To implement the Shi-Tomasi corner detector, OpenCV provides us with the function, cv2.goodFeaturesToTrack(). It detects N strongest corners in the image.


To detect corners in an image using Shi-Tomasi corner detector, you could follow the steps given below −

  • Import required libraries OpenCV and NumPy. Make sure you have already installed them.

  • Read the input image using cv2.imread() method. Specify the full path of the image. Convert the input image to grayscale image using cv2.cvtColor() metod.

  • Apply cv2.goodFeaturesToTrack() function on the grayscale image. Pass suitable number of corners, quality level and Euclidean distance between two corners to the method as parameters. This function returns the corners as floating points in the image. Convert these floating point corners to the integers.

  • Draw corner points on the input image as filled circles with a small radius.

  • Display the image with detected corners.

Let's see the examples to detect corners in an image using Shi-Tomasi Corner Detector.

Input Image

We will use this image as an input file in the example below.


In this example, we detect corners in an input image using the Shi-Tomasi corner detector.

# import required libraries import numpy as np import cv2 # read the input image img = cv2.imread('building.jpg') # convert the image to grayscale gray = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY) # apply Shi-Tomasi corner detector on gray image # to detect 50 best corners corners = cv2.goodFeaturesToTrack(gray,50,0.01,10) # convert floating points to integers corners = np.int0(corners) # loop over all points and draw corner points as circles for i in corners: x,y = i.ravel(),(x,y),3,(0,0,255),-1) # Display image with corner points drawn on it cv2.imshow("Corners", img) cv2.waitKey(0) cv2.destroyAllWindows()


When you run the above Python program, it will produce the following output window −

The above output image shows the corners detected using the Shi-Tomasi corner detector. The corner points are shown in red color.