Harris Corner Detection using Python OpenCV
In this post, you will learn about the Harris Corner Detection using the Python OpenCV.
The Harris Corner Detector algorithm is commonly used in computer vision to extract corners and infer features of an image. It was first introduced by Chris Harris and Mike Stephens in 1988. The corner is the point where two edges are joined. This corner is termed as interest points which are invariant to translation, rotation, and illumination. There are many corner detection algorithms that capture the corners of an image. But the Harris Corner Detector algorithm is the simplest, efficient, and most reliable for use in corner detection. It is fast enough to work on computers.
Python cv2.cornerHarris()
The OpenCV library provides a function cv2.cornerHarris() for this purpose. The syntax is-
cv2.cornerHarris(image, blockSize, ksize, k, borderType)
image- the input image, a single-channel 8-bit or floating-point image,
blockSize- the size of the neighbourhood considered for corner detection,
ksize - Aperture parameter of the Sobel derivative used,
k- harris detector free parameter in the equation,
borderType- pixel extrapolation method.
Harris Corner Detection Code Explanation
First, we will load the input image using the imread() function of OpenCV.
image = cv2.imread('nature.jpg')
Next, we will convert the imported image in grayscale. The cv2.cvtColor() method is used to convert an image from one color-space to another. It specifies the type of conversion, i.e., cv2.COLOR_BGR2GRAY in the second parameter.
img_gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
We will modify the input image data type to 32 bits float. It is a single precision floating point of 8 bits exponent, 23 bits mantissa.
img_gray = np.float32(img_gray)
Next, apply the Harris Corner Detector method to detect the corners with appropriate input parameters -
hcd_img = cv2.cornerHarris(img_gray, 3, 5, 0.08)
We will use the cv2.dilate() method to mark the corners in the returned image. It adds pixels to the corners of objects in an image.
hcd_img = cv2.dilate(hcd_img, None)
At last, we will revert back the original image with optimal threshold value and specifies the corner color.
image[hcd_img > 0.01 * hcd_img.max()]=[0, 0, 255]
Complete Code: Harris corner detection OpenCV
We hope the above code explanation will help you to understand the code flow. Here, we have merged the above code chunks.
import cv2
import numpy as np
# loading image
image = cv2.imread('house.jpg')
# convert the input image into grayscale
img_gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
# convert the data type
img_gray = np.float32(img_gray)
# implementing cv2.cornerHarris method
hcd_img = cv2.cornerHarris(img_gray, 5, 5, 0.08)
# marking dilated corners
hcd_img = cv2.dilate(hcd_img, None)
# reverting back to the original image
image[hcd_img > 0.01 * hcd_img.max()]=[0, 97, 38]
# show the image
cv2.imshow('Image with corners', image)
cv2.waitKey(0);
cv2.destroyAllWindows();
cv2.waitKey(1)
The leftmost is the original image and the rightmost image is the output of the above code.
Similarly, when we provide a cube image in input, we get the following output-
Related Articles
Python OpenCV Specific Color Detection from Capture VideoEye Detection Program in Python OpenCV
Arithmetic Operations on Images using Python OpenCV
OpenCV Logical Operators- Bitwise AND, OR, NOR, XOR
Face Recognition OpenCV Source Code
Canny Edge Detector OpenCV Python
Python NumPy: Overview and Examples
Image processing using Python Pillow
Python OpenCV Histogram Equalization
Python OpenCV Histogram of Color Image
Python OpenCV Histogram of Grayscale Image
Python OpenCV Image Filtering
Python OpenCV ColorMap
Python OpenCV Gaussian Blur Filtering
Python OpenCV Overview and Examples
Draw different shapes on image using Python OpenCV
How to convert MySQL query result to JSON in Python
How to display data from MongoDB in HTML table using Python