Python | Harris Corner Detection using OpenCV



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 neighborhood 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:

# Python program for illustrators and
# angle detection with
# Harris angle detection method

 
# organizing imports

import cv2

import numpy as np

 
# path to the specified input image and
# image is loaded using imread command

image = cv2.imread ( `GeekforGeeks.jpg` )

 
# convert input image to
Grayscale color space

operatedImage = cv2.cvtColor (image, cv2.COLOR_BGR2GRAY)

 
# change data type
# setting 32-bit floating point

operatedImage = np.float32 (operatedImage)

 
# apply cv2.cornerHarris method
# to define corners with matching
# values ​​as input

dest = cv2.cornerHarris (operatedImage, 2 , 5 , 0.07 )

 
# Results are marked with wide corners

dest = cv2.dilate (dest , None )

  
# Returning to the original image
# with optimal threshold

image [dest & gt;  0.01 * dest. max ()] = [ 0 , 0 , 255 ]

 
# window with output image with corners

cv2.imshow ( `Image with Borders` , image)

  
# Unselect any associated memory usage

if cv2.waitKey ( 0 ) & amp;  0xff = = 27 :

cv2.destroyAllWindows ()

Input data :

Exit: