0
0
Computer Visionml~5 mins

Corner detection (Harris) in Computer Vision

Choose your learning style9 modes available
Introduction
Corner detection helps find points in images where edges meet, which are useful for recognizing shapes and tracking objects.
To detect important points in a photo for object recognition.
When tracking moving objects in video frames.
To help stitch multiple images together in panorama creation.
For robot navigation by recognizing landmarks.
To analyze textures or patterns in images.
Syntax
Computer Vision
cv2.cornerHarris(src_gray, blockSize, ksize, k)

# Parameters:
# src_gray: Grayscale input image (float32 type)
# blockSize: Size of neighborhood considered for corner detection
# ksize: Aperture parameter for Sobel operator
# k: Harris detector free parameter (usually 0.04 to 0.06)
Input image must be grayscale and of type float32.
The function returns a response image where higher values indicate stronger corners.
Examples
Detect corners using a 2x2 neighborhood, Sobel operator size 3, and k=0.04.
Computer Vision
dst = cv2.cornerHarris(gray_img, 2, 3, 0.04)
Use a larger neighborhood and Sobel size with a slightly higher k value.
Computer Vision
dst = cv2.cornerHarris(gray_img, 3, 5, 0.06)
Sample Model
This program loads a chessboard image, converts it to grayscale, and applies the Harris corner detector. It marks detected corners in red and saves the result. It also prints the maximum corner response value and how many corners were detected above the threshold.
Computer Vision
import cv2
import numpy as np

# Load image and convert to grayscale
img = cv2.imread('chessboard.png')
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
gray = np.float32(gray)

# Detect corners using Harris
block_size = 2
ksize = 3
k = 0.04
corner_response = cv2.cornerHarris(gray, block_size, ksize, k)

# Dilate corner points to mark them
corner_response = cv2.dilate(corner_response, None)

# Threshold to mark strong corners in red
img[corner_response > 0.01 * corner_response.max()] = [0, 0, 255]

# Save result image
cv2.imwrite('chessboard_corners.png', img)

# Print some stats
print(f"Max corner response: {corner_response.max():.2f}")
print(f"Number of corners detected: {(corner_response > 0.01 * corner_response.max()).sum()}")
OutputSuccess
Important Notes
Choosing the right threshold is important to detect meaningful corners without noise.
Harris corner detection is rotation invariant but not scale invariant.
For color images, convert to grayscale before applying the detector.
Summary
Harris corner detection finds points where edges meet in images.
It uses gradients in a small neighborhood to compute a corner response.
Strong corners have high response values and can be marked or used for further tasks.