Corner Detection
-
Corner detection is an important operation in computer vision for identifying and localizing corners in an image.
-
This feature is widely used for object detection, tracking, and image registration.
-
OpenCV provides several algorithms for corner detection, like - Harris corner detection, Shi-Tomasi corner detection, and FAST (Features from Accelerated Segment Test) corner detection.
In this tutorial, we’ll learn about corner detection in OpenCV. We’ll use the Harris corner detection algorithm to detect corners.
What is the Harris corner detection algorithm in OpenCV?
-
Harris corner detection algorithm is a computer vision algorithm that is used to identify the corners in an image.
-
It was developed by Chris Harris and Mike Stephens in 1988.
-
It works by finding the regions in an image where the intensity changes significantly in all directions.
-
The algorithm operates by computing a "corner response" function at each pixel in the image.
-
After the corner response function has been computed for each pixel in the image, a threshold is applied to determine which pixels are corners.
-
Pixels with a corner response function value above the threshold are considered corners.
Input:
Code:
import cv2
import numpy as np
img = cv2.imread('drawing-2.jpg')
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
gray = np.float32(gray)
corners = cv2.cornerHarris(gray, 3, 0, 0.05)
corners = cv2.dilate(corners, None)
img[corners > 0.01 * corners.max()]=[0, 0, 255]
cv2.imshow('Image with Corners', img)
cv2.waitKey(0)
cv2.destroyAllWindows()
-
First we imported required libraries.
-
Then, we used cv2.imread() to read the image and cv2.cvtcolor() to convert the image into grayscale.
-
Then, we converted the data type to 32-bit floating point.
-
Then, we applied the cv2.cornerHarris method with some parameters to detect the corners.
-
Then, we used cv2.dilate to dilate the detected corners to make them more visible.
-
Then, we set up a threshold value for the corner response, where only the corners with a response higher than a certain threshold are selected.
-
At last, we used cv2.imshow function to display the final result.
Result: