OpenCV Tutorial
Image Processing
Feature Detection and Description
Drawing Functions
Video Processing
Applications and Projects
Detecting corners in an image is an essential step in many computer vision applications, including image registration, object detection, and feature extraction. OpenCV provides various methods to detect corners, with the Harris Corner Detection and Shi-Tomasi (Good Features to Track) methods being the most popular. In this tutorial, we'll explore the Harris Corner Detection method:
pip install opencv-python
import numpy as np import cv2
image = cv2.imread('path_to_image.jpg') gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
gray = np.float32(gray) dst = cv2.cornerHarris(gray, 2, 3, 0.04)
Here:
2
) is the block size. It decides the size of the neighborhood considered for corner detection.3
) is the aperture parameter for the Sobel operator.0.04
) is the free parameter in the Harris detection equation.This step is optional, but it helps in marking the corners.
dst = cv2.dilate(dst, None)
This step will mark the corners in the original image.
image[dst > 0.01 * dst.max()] = [0, 0, 255] # Marking the corners in red color
cv2.imshow('Harris Corners', image) cv2.waitKey(0) cv2.destroyAllWindows()
The parameters in the cv2.cornerHarris()
function can be adjusted based on the type of image and the desired precision. For instance, the threshold value (0.01
in the tutorial) can be increased or decreased to detect fewer or more corners, respectively.
If you're interested in detecting corners with the Shi-Tomasi method instead, you can use the cv2.goodFeaturesToTrack()
function. It often provides better results, especially in cases with more prominent and clearer corners.
Detecting corners in an image is a foundational task in computer vision. The Harris Corner Detection method provided by OpenCV is a robust method for this purpose. Proper parameter tuning is essential to achieve the best results tailored to the specific image or application.
Corner Detection in OpenCV:
import cv2 import numpy as np # Read the image img = cv2.imread('image.jpg', 0) # Apply corner detection algorithm (e.g., Harris) corners = cv2.cornerHarris(img, blockSize, ksize, k) # Display the result cv2.imshow('Corners', corners) cv2.waitKey(0) cv2.destroyAllWindows()
Harris Corner Detection in OpenCV:
import cv2 import numpy as np # Read the image img = cv2.imread('image.jpg', 0) # Apply Harris corner detection corners = cv2.cornerHarris(img, blockSize, ksize, k) # Threshold and mark the corners img[corners > threshold * corners.max()] = [0, 0, 255] # Display the result cv2.imshow('Harris Corners', img) cv2.waitKey(0) cv2.destroyAllWindows()
Shi-Tomasi Corner Detection in OpenCV:
import cv2 import numpy as np # Read the image img = cv2.imread('image.jpg', 0) # Detect Shi-Tomasi corners corners = cv2.goodFeaturesToTrack(img, maxCorners, qualityLevel, minDistance) # Draw corners on the image for corner in corners: x, y = corner.ravel() cv2.circle(img, (x, y), 3, 255, -1) # Display the result cv2.imshow('Shi-Tomasi Corners', img) cv2.waitKey(0) cv2.destroyAllWindows()
Corner Detection and Feature Extraction in OpenCV:
import cv2 import numpy as np # Read the image img = cv2.imread('image.jpg', 0) # Detect corners using Shi-Tomasi corners = cv2.goodFeaturesToTrack(img, maxCorners, qualityLevel, minDistance) # Compute features (e.g., using SIFT or ORB) orb = cv2.ORB_create() keypoints, descriptors = orb.detectAndCompute(img, None) # Draw keypoints on the image img_keypoints = cv2.drawKeypoints(img, keypoints, None) # Display the result cv2.imshow('Corners and Features', img_keypoints) cv2.waitKey(0) cv2.destroyAllWindows()
Real-time Corner Detection using OpenCV:
import cv2 # Initialize camera cap = cv2.VideoCapture(0) while True: # Capture frame-by-frame ret, frame = cap.read() # Convert to grayscale gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY) # Apply corner detection (e.g., Shi-Tomasi) corners = cv2.goodFeaturesToTrack(gray, maxCorners, qualityLevel, minDistance) # Draw corners on the frame for corner in corners: x, y = corner.ravel() cv2.circle(frame, (x, y), 3, 255, -1) # Display the result cv2.imshow('Real-time Corner Detection', frame) # Break the loop on 'q' key press if cv2.waitKey(1) & 0xFF == ord('q'): break # Release the camera and close windows cap.release() cv2.destroyAllWindows()
FAST Corner Detection in OpenCV:
import cv2 # Read the image img = cv2.imread('image.jpg', 0) # Create FAST object fast = cv2.FastFeatureDetector_create() # Detect and draw FAST corners kp = fast.detect(img, None) img_fast = cv2.drawKeypoints(img, kp, None, color=(255, 0, 0)) # Display the result cv2.imshow('FAST Corners', img_fast) cv2.waitKey(0) cv2.destroyAllWindows()