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 an Image Segmentation Model using Amazon SageMaker
In this Machine Learning Project, you will learn to implement the UNet Architecture and build an Image Segmentation Model using Amazon SageMaker

Build ARCH and GARCH Models in Time Series using Python
In this Project we will build an ARCH and a GARCH model using Python

Build a Autoregressive and Moving Average Time Series Model
In this time series project, you will learn to build Autoregressive and Moving Average Time Series Models to forecast future readings, optimize performance, and harness the power of predictive analytics for sensor data.

Classification Projects on Machine Learning for Beginners - 2
Learn to implement various ensemble techniques to predict license status for a given business.

Stock Price Prediction Project using LSTM and RNN
Learn how to predict stock prices using RNN and LSTM models. Understand deep learning concepts and apply them to real-world financial data for accurate forecasting.

Build Classification Algorithms for Digital Transformation[Banking]
Implement a machine learning approach using various classification techniques in Python to examine the digitalisation process of bank customers.

Natural language processing Chatbot application using NLTK for text classification
In this NLP AI application, we build the core conversational engine for a chatbot. We use the popular NLTK text classification library to achieve this.

Learn Object Tracking (SOT, MOT) using OpenCV and Python
Get Started with Object Tracking using OpenCV and Python - Learn to implement Multiple Instance Learning Tracker (MIL) algorithm, Generic Object Tracking Using Regression Networks Tracker (GOTURN) algorithm, Kernelized Correlation Filters Tracker (KCF) algorithm, Tracking, Learning, Detection Tracker (TLD) algorithm for single and multiple object tracking from various video clips.

Medical Image Segmentation Deep Learning Project
In this deep learning project, you will learn to implement Unet++ models for medical image segmentation to detect and classify colorectal polyps.

Credit Card Default Prediction using Machine learning techniques
In this data science project, you will predict borrowers chance of defaulting on credit loans by building a credit score prediction model.