Open In App

Python | Corner detection with Harris Corner Detection method using OpenCV

Last Updated : 05 Aug, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

Corners are points in an image where there is a significant change in intensity in more than one direction. These points typically occur where edges meet making them more distinctive than edges alone. Unlike edges which show intensity changes in just one direction, corners have a noticeable change in multiple directions making them valuable for tasks like feature matching and image alignment.

Corners are valuable because they are:

  1. Distinctive: They are easily identifiable and unique in an image.
  2. Stable: Corners remain consistent even when the image is rotated, scaled or slightly altered.
  3. Reliable for Matching: They are used for matching features between different images, important for tasks like image stitching and 3D reconstruction.

Harris Corner Detection Function in OpenCV

Harris Corner Detection is a key technique in computer vision for detecting corners in images. It works by analyzing how the intensity of the image changes in different directions, helping us identify areas with significant variations which are considered corners. The core of the Harris Corner Detection is handled by the OpenCV function cv2.cornerHarris().

This function finds the corner response for each pixel in an image, allowing us to identify the points where corners are located.

Function Syntax:

cv2.cornerHarris(src, dest, blockSize, kSize, freeParameter, borderType)

Parameters: 

  • src: Input image which should be in grayscale.
  • blockSize: Size of the neighborhood around each pixel to find the corner response. Larger values consider a bigger area which can detect larger corners but might slow down the process.
  • dest: Image to store the Harris detector responses. Size is same as source image 
  • ksize: Size of the Sobel kernel used for gradient calculation. It affects the sensitivity of corner detection to small intensity variations.
  • freeParameter: A constant used in the Harris corner detection formula, typically between 0.04 and 0.06. It controls the weight given to the corner response.
  • borderType: Specifies how border pixels are handled during corner detection.

Implementing Harris Corner Detection

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): Second argument (17) sets the neighborhood size for corner calculation, the third (21) defines the Sobel kernel size and the fourth (0.01) is the free parameter in the Harris formula.
  • 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.
Python
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:

output-harris
Output Image

Advantages of Harris Corner Detection:

  1. Accuracy: It detects corners with high precision making it reliable for feature extraction.
  2. Noise Resilience: The method works well even in images with noise, as it is based on intensity gradients.
  3. Rotation and Scale Invariance: Corners detected using this method remain consistent across rotated or scaled versions of the image.
  4. Foundation for Advanced Algorithms: It is used as the base for more advanced feature detection methods like SIFT and SURF.

Challenges of Harris Corner Detection:

  1. Sensitivity to Parameters: The algorithm’s performance depends on choosing the correct values for parameters like neighborhood size and Sobel kernel size.
  2. Computationally Expensive: It can be slow on large images or videos due to its computational complexity.
  3. Difficulty with Flat Surfaces: It struggles to detect corners in images with flat or uniform surfaces that lack significant intensity changes.
  4. Not Ideal for Real-Time Applications: Given its computational cost, it's less suitable for real-time systems where speed is critical.

Practice Tags :

Similar Reads