Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
OpenCV Python – How to detect and draw keypoints in an image using SIFT?
SIFT (Scale-Invariant Feature Transform) is a scale invariant feature descriptor that detects keypoints in images and computes their descriptors. SIFT keypoints are robust to changes in scale, rotation, and illumination, making them ideal for object recognition and image matching tasks.
How SIFT Works
SIFT detects keypoints by finding locations in an image that are distinctive and stable across different scales. The algorithm creates a SIFT object with cv2.SIFT_create(), detects keypoints using sift.detect(), and draws them using cv2.drawKeypoints().
Steps to Detect and Draw Keypoints
To detect and draw keypoints in an input image using SIFT algorithm, follow these steps ?
Import the required libraries OpenCV and NumPy. Make sure you have already installed them.
Read the input image using
cv2.imread()method. Convert the input image to grayscale usingcv2.cvtColor()method.Initiate SIFT object with default values using
sift = cv2.SIFT_create().Detect the keypoints in the grayscale image using
sift.detect(). It returns keypointskp.Draw the detected keypoints using
cv2.drawKeypoints()function. To draw rich keypoints, passflags=cv2.DRAW_MATCHES_FLAGS_DRAW_RICH_KEYPOINTSas a parameter.Display the image with drawn keypoints on it.
Input Image
We will use the following image as the input file in the examples below.

Example 1: Basic Keypoint Detection
In this program, we detect and draw keypoints in the input image using the SIFT algorithm with basic drawing ?
# import required libraries
import cv2
import numpy as np
# create a sample image for demonstration
img = np.ones((300, 400, 3), dtype=np.uint8) * 255
cv2.rectangle(img, (50, 50), (150, 150), (0, 0, 0), 2)
cv2.circle(img, (300, 100), 50, (0, 0, 0), 2)
cv2.line(img, (200, 200), (350, 250), (0, 0, 0), 2)
# convert the image to grayscale
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
# Initiate SIFT object with default values
sift = cv2.SIFT_create()
# find the keypoints on image (grayscale)
kp = sift.detect(gray, None)
# draw keypoints in image
img_keypoints = cv2.drawKeypoints(gray, kp, None, flags=0)
print(f"Number of keypoints detected: {len(kp)}")
print("Keypoints drawn with basic flags")
Number of keypoints detected: 12 Keypoints drawn with basic flags
Notice that the keypoints are drawn with different colors. You can pass a specific color (e.g., (0,0,255) for red) as a parameter to the drawKeypoints() function to draw keypoints with a single color.
Example 2: Rich Keypoint Drawing
In this example, we draw keypoints with rich information including size and orientation by passing flags=cv2.DRAW_MATCHES_FLAGS_DRAW_RICH_KEYPOINTS ?
# import required libraries
import cv2
import numpy as np
# create a sample image for demonstration
img = np.ones((300, 400, 3), dtype=np.uint8) * 255
cv2.rectangle(img, (50, 50), (150, 150), (0, 0, 0), 2)
cv2.circle(img, (300, 100), 50, (0, 0, 0), 2)
cv2.line(img, (200, 200), (350, 250), (0, 0, 0), 2)
# convert the image to grayscale
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
# Initiate SIFT object with default values
sift = cv2.SIFT_create()
# find the keypoints on image (grayscale)
kp = sift.detect(gray, None)
# draw keypoints with rich information
img_rich_keypoints = cv2.drawKeypoints(gray, kp, None, flags=cv2.DRAW_MATCHES_FLAGS_DRAW_RICH_KEYPOINTS)
print(f"Number of keypoints detected: {len(kp)}")
print("Rich keypoints show size and orientation")
# Print information about first few keypoints
for i, keypoint in enumerate(kp[:3]):
print(f"Keypoint {i+1}: Position=({keypoint.pt[0]:.1f}, {keypoint.pt[1]:.1f}), Size={keypoint.size:.1f}")
Number of keypoints detected: 12 Rich keypoints show size and orientation Keypoint 1: Position=(99.5, 50.0), Size=3.2 Keypoint 2: Position=(150.0, 99.5), Size=3.2 Keypoint 3: Position=(99.5, 150.0), Size=3.2
Keypoint Properties
Each SIFT keypoint contains several important properties ?
Position (pt) ? The (x, y) coordinates of the keypoint
Size ? The diameter of the meaningful keypoint neighborhood
Angle ? The orientation of the keypoint
Response ? The strength of the keypoint
Drawing Options
| Flag | Description | Visual Result |
|---|---|---|
flags=0 |
Basic points | Small colored circles |
DRAW_MATCHES_FLAGS_DRAW_RICH_KEYPOINTS |
Rich information | Circles with size and orientation |
Conclusion
SIFT keypoint detection is powerful for finding distinctive features in images. Use basic drawing for simple visualization and rich keypoints to see scale and orientation information. SIFT is particularly useful for object recognition and image matching applications.
