OpenCV Python – Feature Detection
Feature detection is a fundamental concept in computer vision used to identify important points (keypoints) in an image such as corners, edges, and unique patterns.
These features are essential for tasks like object recognition, image stitching, and tracking.
1. What is Feature Detection?
Feature detection is the process of finding:
- Corners
- Edges
- Blobs
- Distinct visual points
These points are used to describe and match images.
2. Import OpenCV
import cv2
3. Read Image
img = cv2.imread("image.jpg")
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
4. Harris Corner Detection
Syntax:
cv2.cornerHarris(src, blockSize, ksize, k)
Example:
gray = np.float32(gray)
harris = cv2.cornerHarris(gray, 2, 3, 0.04)
img[harris > 0.01 * harris.max()] = [0, 0, 255]
cv2.imshow("Harris Corners", img)
cv2.waitKey(0)
cv2.destroyAllWindows()
5. Shi-Tomasi Corner Detection
corners = cv2.goodFeaturesToTrack(gray, 100, 0.01, 10)
for corner in corners:
x, y = corner.ravel()
cv2.circle(img, (x, y), 5, (0, 255, 0), -1)
cv2.imshow("Shi-Tomasi Corners", img)
cv2.waitKey(0)
cv2.destroyAllWindows()
6. ORB Feature Detection
ORB (Oriented FAST and Rotated BRIEF) is fast and free.
orb = cv2.ORB_create()
keypoints, descriptors = orb.detectAndCompute(gray, None)
img2 = cv2.drawKeypoints(img, keypoints, None, color=(255, 0, 0))
cv2.imshow("ORB Features", img2)
cv2.waitKey(0)
cv2.destroyAllWindows()
7. SIFT Feature Detection
SIFT detects scale-invariant features.
sift = cv2.SIFT_create()
keypoints, descriptors = sift.detectAndCompute(gray, None)
img3 = cv2.drawKeypoints(img, keypoints, None)
cv2.imshow("SIFT Features", img3)
cv2.waitKey(0)
cv2.destroyAllWindows()
8. Why Feature Detection is Important
Feature detection is used in:
- Image stitching (panorama)
- Object recognition
- 3D reconstruction
- Face recognition
- Augmented reality
9. Difference Between ORB and SIFT
| Feature | ORB | SIFT |
|---|---|---|
| Speed | Fast | Slower |
| Accuracy | Good | Very High |
| Patent | Free | Now Free |
| Use Case | Real-time apps | High accuracy systems |
10. Common Mistakes
❌ Using color image directly
✔ Solution:
- Convert image to grayscale
❌ Too many keypoints
✔ Solution:
- Limit number of features in goodFeaturesToTrack
11. Conclusion
Feature detection in OpenCV Python is a key step in many computer vision applications. It helps identify important points in images that can be used for matching, tracking, and recognition.
Once you master feature detection, you can move to feature matching and image stitching.


0 Comments