Python | Detect corner of an image using OpenCV
Corner detection is a fundamental technique in computer vision, powering applications like image stitching, panorama creation, object recognition and motion tracking. Corner as a point in an image where the pixel intensity changes sharply in two or more directions. These are:
- Repeatable: They appear reliably under different lighting or viewpoints.
- Descriptive: They help match and align images.
- Useful: Perfect for tracking moving objects or finding unique features.
Shi-Tomasi Corner Detection Method
OpenCVâs cv2.goodFeaturesToTrack() implements Shi-Tomasi algorithm, which selects the N strongest corners in a grayscale image.
Syntax
cv2.goodFeaturesToTrack(image, maxCorners, qualityLevel, minDistance, corners=None, mask=None, blockSize=3, useHarrisDetector=False, k=0.04)
Parameters:
- image: Input image in grayscale.
- maxCorners: Maximum number of corners to return.
- qualityLevel: Minimum accepted quality of corners (0â1; higher = stricter).
- minDistance: Minimum Euclidean distance between detected corners.
- corners (optional, default=None): Output array of detected corners (usually left as None â function returns it).
- mask (optional, default=None): Region of interest mask; corners outside the mask are ignored.
- blockSize (optional, default=3): Size of neighborhood considered for corner detection.
- useHarrisDetector (optional, default=False): If True, uses Harris detector instead of Shi-Tomasi.
- k (optional, default=0.04): Harris detector free parameter (useHarrisDetector=True only).
Step-by-Step Implementation
Step 1: Import Libraries
Start by importing required libraries NumPy for efficient array handling, OpenCV (cv2) for image processing operations and Matplotlib for visualizing results such as detected corners or processed images.
import numpy as np
import cv2
from matplotlib import pyplot as plt
Step 2: Load and Preprocess the Image
Use cv2.imread('corner1.png') to read the image file. Image can be downloaded from here.
img = cv2.imread('corner1.png')
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
Here, we are converting the image to grayscale corner detection works on intensity changes, so color isnât needed.
Step 3: Detect Corners
corners = cv2.goodFeaturesToTrack(
gray,
maxCorners=27,
qualityLevel=0.01,
minDistance=10,
blockSize=3,
useHarrisDetector=False,
k=0.04
)
We detect up to 27 corners with:
- qualityLevel=0.01: Less strict, more corners pass.
- minDistance=10: Corners at least 10 px apart.
- blockSize=3: Neighborhood size for detection.
- useHarrisDetector=False: Use Shi-Tomasi method.
- k=0.04: Harris free parameter (ignored here).
Step 4: Draw and Display Results
We convert corner coordinates to integers with np.intp(), flatten them using ravel() and draw small filled green circles at each corner using cv2.circle(). Finally, we convert BGR to RGB for Matplotlib, set the title, hide axes and display the image.
corners = np.intp(corners) # Convert to integer coords
for corner in corners:
x, y = corner.ravel()
cv2.circle(img, (x, y), radius=3, color=(0, 255, 0), thickness=-1)
plt.imshow(cv2.cvtColor(img, cv2.COLOR_BGR2RGB))
plt.title('Corners Detected')
plt.axis('off')
plt.show()
Output:
