Open In App

Python | Corner detection with Harris Corner Detection method using OpenCV

Improve
Improve
Improve
Like Article
Like
Save Article
Save
Share
Report issue
Report

Harris Corner detection algorithm was developed to identify the internal corners of an image. The corners of an image are basically identified as the regions in which there are variations in large intensity of the gradient in all possible dimensions and directions. Corners extracted can be a part of the image features, which can be matched with features of other images, and can be used to extract accurate information. Harris Corner Detection is a method to extract the corners from the input image and to extract features from the input image. 
About the function used: 
 

Syntax: cv2.cornerHarris(src, dest, blockSize, kSize, freeParameter, borderType)
Parameters: 
src – Input Image (Single-channel, 8-bit or floating-point) 
dest – Image to store the Harris detector responses. Size is same as source image 
blockSize – Neighborhood size ( for each pixel value blockSize * blockSize neighbourhood is considered ) 
ksize – Aperture parameter for the Sobel() operator 
freeParameter – Harris detector free parameter 
borderType – Pixel extrapolation method ( the extrapolation mode used returns the coordinate of the pixel corresponding to the specified extrapolated pixel )

Below is the Python implementation : 
 

Python3




# Python program to illustrate
# corner detection with
# Harris Corner Detection Method
  
# organizing imports
import cv2
import numpy as np
  
# path to input image specified and 
# image is loaded with imread command
image = cv2.imread('GeekforGeeks.jpg')
  
# convert the input image into
# grayscale color space
operatedImage = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
  
# modify the data type
# setting to 32-bit floating point
operatedImage = np.float32(operatedImage)
  
# apply the cv2.cornerHarris method
# to detect the corners with appropriate
# values as input parameters
dest = cv2.cornerHarris(operatedImage, 2, 5, 0.07)
  
# Results are marked through the dilated corners
dest = cv2.dilate(dest, None)
  
# Reverting back to the original image,
# with optimal threshold value
image[dest > 0.01 * dest.max()]=[0, 0, 255]
  
# the window showing output image with corners
cv2.imshow('Image with Borders', image)
  
# De-allocate any associated memory usage 
if cv2.waitKey(0) & 0xff == 27:
    cv2.destroyAllWindows()


Input: 
 

Output: 
 

 



Last Updated : 04 Jan, 2023
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads