Corners are points in an image where the intensity changes significantly in multiple directions. They typically occur at the intersection of edges or at locations with strong variations in brightness, making them important feature points in image analysis and computer vision tasks.
- Formed at intersections of edges or regions with abrupt intensity changes
- Represent key structural points used for detecting and tracking image features
Harris Corner Detection Function in OpenCV
Harris Corner Detection is a computer vision technique used to detect corners by analyzing intensity changes in multiple directions. It identifies points with strong variations in pixel values, which are useful for feature-based analysis. In OpenCV, it is implemented using cv2.cornerHarris(), which computes a corner response for each pixel.
- Measures intensity changes in multiple directions to locate corners
- Generates a response map where higher values indicate stronger corner points
Function Syntax:
cv2.cornerHarris(src, blockSize, ksize, k)
Parameters:
- src: Input grayscale image
- blockSize: Neighborhood size used for corner detection
- ksize: Aperture size for Sobel derivative calculation
- k: Harris free parameter (typically 0.04–0.06)
- borderType: Defines how image borders are handled during processing
Implementing
Let’s see how to implement Harris Corner Detection and highlight the corners detected in an image. Here we will be using OpenCV, Numpy and Matplotlib libraries for the implementation.
You can download the image used from here.
- dest = cv2.cornerHarris(operatedImage, 17, 21, 0.01): Applies Harris corner detection, where 17 defines the neighborhood size, 21 sets the Sobel kernel size, and 0.01 is the Harris free parameter.
- dest = cv2.dilate(dest, None): This enlarges the detected corners for better visibility using cv2.dilate().
- image[dest > 0.01 * dest.max()] = [0, 0, 255]: Corners with responses greater than 1% of the max are marked red on the original image.
import cv2
import numpy as np
import matplotlib.pyplot as plt
image_path = "/content/sample_image.jpg"
image = cv2.imread(image_path)
operatedImage = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
operatedImage = np.float32(operatedImage)
dest = cv2.cornerHarris(operatedImage, 17, 21, 0.01)
dest = cv2.dilate(dest, None)
image[dest > 0.01 * dest.max()] = [0, 0, 255]
image_rgb = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
plt.imshow(image_rgb)
plt.title('Harris Corner Detection')
plt.axis('off')
plt.show()
Output:

Advantages
- Detects corner points with high precision, making it effective for feature extraction.
- Works reliably even in noisy images due to gradient-based computation.
- Maintains consistency under rotation and scaling of images.
- Serves as a basis for feature detectors like SIFT and SURF.
Challenges
- Performance depends on proper selection of parameters like block size and kernel size.
- Can be slow for large images due to heavy processing.
- Fails to detect corners where intensity variation is minimal.
- Not ideal for time-critical applications due to processing overhead.