Detecting corners using Harris corner detector in Python OpenCV
Image processing is a vast and constantly evolving field with a variety of techniques to achieve different goals. One of the most important tasks in image processing is corner detection, which involves identifying points in an image where the brightness changes dramatically. These points, known as corners, are useful in various applications such as object detection, feature tracking, and image stitching. In this article, we will explore how to detect corners using the Harris corner detector in Python OpenCV.
Understanding the Harris Corner Detector
The Harris corner detector is a popular algorithm for detecting corners in an image. It looks for areas where small movements of a corner in any direction lead to significant changes in image intensity. The Harris algorithm uses the image derivatives to calculate the corner response function, which is then compared to a threshold to determine the presence of a corner.
Implementation in Python OpenCV
Here is the Python code for implementing the Harris corner detector in OpenCV:
import cv2
import numpy as np
img = cv2.imread('image.jpg')
gray = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)
gray = np.float32(gray)
dst = cv2.cornerHarris(gray,2,3,0.04)
dst = cv2.dilate(dst,None)
img[dst>0.01*dst.max()]=[0,0,255]
cv2.imshow('dst',img)
if cv2.waitKey(0) & 0xff == 27:
cv2.destroyAllWindows()
Let us break down each line of the code and understand its function:
- The first line imports the necessary libraries – OpenCV and NumPy.
- The second line reads the input image in BGR format.
- The third line converts the BGR image to grayscale.
- The fourth line converts the grayscale image to float32 format for numerical computations.
- The fifth line applies the Harris corner detector algorithm to the grayscale image with the aperture size of 2, Sobel kernel size of 3, and Harris detector free parameter (k) value of 0.04.
- The sixth line dilates the calculated corner points for better visibility.
- The seventh line overlays red dots on the original BGR image where the corner response is above 1% of the maximum value of the response function.
- The eighth line displays the final image with detected corners in a new window and waits for the user to press any key to close the window.
Example
For illustration purposes, we will apply this Harris corner detector algorithm to an ocean image.
import cv2
import numpy as np
img = cv2.imread('ocean.jpg')
grey = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)
corners = cv2.cornerHarris(grey,2,3,0.04)
# dilate corners to mark them easily
corners = cv2.dilate(corners,None)
# draw corners on the original image
img[corners>0.01*corners.max()]=[0,0,255]
cv2.imshow('Harris Corner',img)
cv2.waitKey(0)
cv2.destroyAllWindows()
Conclusion
In conclusion, the Harris corner detector algorithm is a powerful technique to detect corners in an image. In this article, we explored how to implement the Harris corner detector in Python OpenCV using a simple code example. By understanding this algorithm, you can further modify and optimize it as per your application needs. With this technique, you can easily find corners in any given image for better object detection and tracking.