Reputation: 65
I am using a SIFT keypoint extractor/descriptor to extract and plot keypoints on an image as shown in the code below:
import cv2 as cv
img = cv.imread("my_img.jpg")
sift = cv.SIFT_create()
(keypoints, descriptors) = sift.detectAndCompute(img,None)
img_kp=cv.drawKeypoints(img,kp,cv.DRAW_MATCHES_FLAGS_DEFAULT,color=(120,157,187))
cv.imwrite("img.jpg", img_kp)
the code above draw keypoints as orange circles, how can I change the representation to an orange "+" sign for each keypoint ?
Thank you very much for your help
Upvotes: 2
Views: 1973
Reputation: 32094
A simple solution may be: Iterating all keypoints and draw a "+" sign using cv2.drawMarker.
Here is a code sample:
import numpy as np
import cv2
def draw_cross_keypoints(img, keypoints, color):
""" Draw keypoints as crosses, and return the new image with the crosses. """
img_kp = img.copy() # Create a copy of img
# Iterate over all keypoints and draw a cross on evey point.
for kp in keypoints:
x, y = kp.pt # Each keypoint as an x, y tuple https://stackoverflow.com/questions/35884409/how-to-extract-x-y-coordinates-from-opencv-cv2-keypoint-object
x = int(round(x)) # Round an cast to int
y = int(round(y))
# Draw a cross with (x, y) center
cv2.drawMarker(img_kp, (x, y), color, markerType=cv2.MARKER_CROSS, markerSize=5, thickness=1, line_type=cv2.LINE_8)
return img_kp # Return the image with the drawn crosses.
img = cv2.imread("my_img.jpg")
sift = cv2.SIFT_create()
(keypoints, descriptors) = sift.detectAndCompute(img, None)
#img_kp = cv2.drawKeypoints(img, keypoints, cv2.DRAW_MATCHES_FLAGS_DEFAULT, color=(120,157,187))
img_kp = draw_cross_keypoints(img, keypoints, color=(120,157,187)) # Draw keypoints as "+" signs
cv2.imwrite("img.jpg", img_kp)
Crosses and circles - for testing:
As you can see, the "+" sings are at the centers of the circles.
Upvotes: 3