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

Ray han

Tech Leader | Stanford / Yale University
linkedin profile url

I think that they are fantastic. I attended Yale and Stanford and have worked at Honeywell,Oracle, and Arthur Andersen(Accenture) in the US. I have taken Big Data and Hadoop,NoSQL, Spark, Hadoop... Read More

Relevant Projects

Build CNN Image Classification Models for Real Time Prediction
Image Classification Project to build a CNN model in Python that can classify images into social security cards, driving licenses, and other key identity information.

NLP Project for Beginners on Text Processing and Classification
This Project Explains the Basic Text Preprocessing and How to Build a Classification Model in Python

Build a Graph Based Recommendation System in Python-Part 2
In this Graph Based Recommender System Project, you will build a recommender system project for eCommerce platforms and learn to use FAISS for efficient similarity search.

Deploying Machine Learning Models with Flask for Beginners
In this MLOps on GCP project you will learn to deploy a sales forecasting ML Model using Flask.

Build a Face Recognition System in Python using FaceNet
In this deep learning project, you will build your own face recognition system in Python using OpenCV and FaceNet by extracting features from an image of a person's face.

AWS MLOps Project for ARCH and GARCH Time Series Models
Build and deploy ARCH and GARCH time series forecasting models in Python on AWS .

MLOps Project to Deploy Resume Parser Model on Paperspace
In this MLOps project, you will learn how to deploy a Resume Parser Streamlit Application on Paperspace Private Cloud.

Time Series Project to Build a Multiple Linear Regression Model
Learn to build a Multiple linear regression model in Python on Time Series Data

AWS Project to Build and Deploy LSTM Model with Sagemaker
In this AWS Sagemaker Project, you will learn to build a LSTM model on Sagemaker for sales forecasting while analyzing the impact of weather conditions on Sales.

Build a Graph Based Recommendation System in Python -Part 1
Python Recommender Systems Project - Learn to build a graph based recommendation system in eCommerce to recommend products.