How does Shi Tomasi Corner Detector work in OpenCV

How does Shi Tomasi Corner Detector work in OpenCV

Recipe Objective: How does Shi-Tomasi Corner Detector work in OpenCV?

In this recipe, let us see how we can use the Shi Tomasi corner detector to detect corners inside an image.

Step 1: Import the libraries and read the image.

Let us first import the necessary libraries and read the image. The image that we are using here is the one shown below. Let us also convert the image to grayscale for later use.

Input Image

import numpy as np
import cv2
image = cv2.imread('chess.jpg') br` gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)

Step 2: Shi Tomasi corner detection

J.Shi and C.Tomasi, in the late 1940s, published a paper called 'Good Features to Track.' This paper was based on the Harris corner detection method. J.Shi and C.Tomasi made a minor modification in the Harris corner detector method where the score R is calculated. And because of that, this method is known to give better results than the Harris corner detector. Another advantage of this method is that we can specify the top n number of corners we want to detect in the image.

We can find out the corners using the Shi Tomasi corner detection method with the help of the cv2.goodFeaturesToTrack() method. This function was named after the name of the paper published. The following are the arguments of the cv2.goodFeaturesToTrack() function.

  • image: The input grayscale image
  • maxCorners: The maximum number of corners to be detected
  • qualityLevel: The minimal expected quality or threshold to be considered as a corner.
  • minDistance: The minimum possible Euclidean distance between two corners

Let us now see how to implement this in OpenCV.

corners = cv2.goodFeaturesToTrack(gray, 50, 0.01, 10)
corners = np.int64(corners)

Step 3: Draw circles around the corners

This step lets us draw circles around the corners detected using the cv2.circle() method, which takes the following arguments.

  • img: The input image
  • center: The coordinates of the center
  • radius: The radius of the circle
  • color: The color of the circle
  • thickness: (Optional) The thickness of the circle. If the thickness value is -1, then the circle is filled with the color.
for i in corners:
    x, y = i.ravel()
    cv2.circle(image, (x, y), 3, [255, 255, 0], -1)

Step 4: Display the Output

Let us display the output using cv2.imshow() function.

cv2.imshow('Corners Detected', image)
cv2.waitKey(0)
cv2.destroyAllWindows()

Output:

Harris corner


Download Materials


What Users are saying..

profile image

Abhinav Agarwal

Graduate Student at Northwestern University
linkedin profile url

I come from Northwestern University, which is ranked 9th in the US. Although the high-quality academics at school taught me all the basics I needed, obtaining practical experience was a challenge.... Read More

Relevant Projects

Build Multi Class Text Classification Models with RNN and LSTM
In this Deep Learning Project, you will use the customer complaints data about consumer financial products to build multi-class text classification models using RNN and LSTM.

Build a Text Generator Model using Amazon SageMaker
In this Deep Learning Project, you will train a Text Generator Model on Amazon Reviews Dataset using LSTM Algorithm in PyTorch and deploy it on Amazon SageMaker.

Learn to Build Generative Models Using PyTorch Autoencoders
In this deep learning project, you will learn how to build a Generative Model using Autoencoders in PyTorch

Learn Hyperparameter Tuning for Neural Networks with PyTorch
In this Deep Learning Project, you will learn how to optimally tune the hyperparameters (learning rate, epochs, dropout, early stopping) of a neural network model in PyTorch to improve model performance.

Hands-On Approach to Master PyTorch Tensors with Examples
In this deep learning project, you will learn how to perform various operations on the building block of PyTorch : Tensors.

Machine Learning Project to Forecast Rossmann Store Sales
In this machine learning project you will work on creating a robust prediction model of Rossmann's daily sales using store, promotion, and competitor data.

Build a Hybrid Recommender System in Python using LightFM
In this Recommender System project, you will build a hybrid recommender system in Python using LightFM .

Learn to Build an End-to-End Machine Learning Pipeline - Part 2
In this Machine Learning Project, you will learn how to build an end-to-end machine learning pipeline for predicting truck delays, incorporating Hopsworks' feature store and Weights and Biases for model experimentation.

ML Model Deployment on AWS for Customer Churn Prediction
MLOps Project-Deploy Machine Learning Model to Production Python on AWS for Customer Churn Prediction

Detectron2 Object Detection and Segmentation Example Python
Object Detection using Detectron2 - Build a Dectectron2 model to detect the zones and inhibitions in antibiogram images.

OSZAR »