Skip to content

Instantly share code, notes, and snippets.

@tshanmukh
Last active October 19, 2021 20:34
Show Gist options
  • Save tshanmukh/5e5ab92cfbd25b4788037d72fddeb5db to your computer and use it in GitHub Desktop.
Save tshanmukh/5e5ab92cfbd25b4788037d72fddeb5db to your computer and use it in GitHub Desktop.
Implements a function to detect the keypoints using the ORB algorithm in opencv
# importing copy as we have to copy the same image to different instances to visualize
import copy
plt.rcParams['figure.figsize'] = [14.0, 7.0]
# Set the parameters of the ORB algorithm by specifying the maximum number of keypoints to locate and
# the pyramid decimation ratio
orb = cv2.ORB_create(200, 2.0) # refer the opencv page for available arguments and usages
# computing the keypoints using the detectandcompute method, this accepts a grey scale image and None parameter
# is to let the ORB know we are not using any mask
keypoints, descriptor = orb.detectAndCompute(training_gray, None)
# Create copies of the training image to draw our keypoints on
kp_np = copy.copy(training_image)
keyp_with_size = copy.copy(training_image)
# using the drawkeypoints function to plot the keypoints on kp_np image without size or orientation details
cv2.drawKeypoints(training_image, keypoints, kp_np, color = (255, 20, 147))
# ploting the keypoint with size information on the keyp_with_size image
cv2.drawKeypoints(training_image, keypoints, keyp_with_size, flags = cv2.DRAW_MATCHES_FLAGS_DRAW_RICH_KEYPOINTS)
# Display the image with the keypoints without size or orientation
plt.subplot(121)
plt.title('Keypoints Without Size or Orientation')
plt.imshow(kp_np)
# Display the image with the keypoints with size and orientation
plt.subplot(122)
plt.title('Keypoints With Size and Orientation')
plt.imshow(keyp_with_size)
plt.show()
# Print the number of keypoints detected
print("\nNumber of keypoints Detected: ", len(keypoints))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment