Python | Corner detection with Harris Corner Detection method using OpenCV

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 :

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python programe 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()

chevron_right


Input:

Output:



My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.




Article Tags :

Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.