Updated April 7, 2023
Introduction to OpenCV KeyPoint
The points or spatial locations in a given image which defines whatever stands out in the image is called keypoint. The keypoints found are special because even if we rotate the images or shrink the images or expand the images or translate the images or distort the images the keypoints remain the same in the modified image when compared to the original image. Hence keypoints are scale invariant and are circular and there are several algorithms to detect keypoints in a given image namely SIFT algorithm, SURF algorithm etc. and we make use of drawKeypoints() function in OpenCV to draw the key points on the given image.
Syntax to define drawKeypoints() function in OpenCV:
drawKeypoints(input_image, keypoints, output_image, color, flag)
Where,
- input_image is the image from which the key points are derived using SIFT or SURF algorithm.
- keypoints are the key points derived from the input image using SIFT or SURF algorithm.
- output_image is the image on which key points are drawn in circles.
- color represents the color of the key points drawn in circles.
- flag represents the drawing features.
Working of drawKeypoints() Function in OpenCV
The distinct features in a given image that makes the image stand out are called key points in a given image. Key points of a given image assists us in object detection of comparison of images. There are several algorithms to detect key points in a given image. In order to be able to draw the detected keypoints on a given image, we make use of a function called drawKeypoints() function in OpenCV. The drawKeypoints() function takes the input image, keypoints, color and flag as the input.
The possible values for flag are:
- cv.DRAW_MATCHES_FLAGS_DEFAULT
- cv.DRAW_MATCHES_FLAGS_DRAW_RICH_KEYPOINTS
- cv.DRAW_MATCHES_FLAGS_DRAW_OVER_OUTIMG
- cv.DRAW_MATCHES_FLAGS_NOT_DRAW_SINGLE_POINTS
The keypoints drawn using drawKeypoints() function are special because even if we rotate the images or shrink the images or expand the images or translate the images or distort the images the keypoints remain the same in the modified image when compared to the original image. The keypoints are scale invariant and are circular. The drawKeypoints() function returns an image with keypoints drawn on the image.
Examples of OpenCV KeyPoint
Given below are the examples of OpenCV KeyPoint:
Example #1
OpenCV program in python to demonstrate drawKeypoints() function to read the given image using imread() function. Implement SIFT algorithm to detect keypoints in the image and then use drawKeypoints() function to draw the key points on the image and display the output on the screen.
Code:
#importing the cv2 module
import cv2
#reading the image whose key points are to be detected using imread() function
imageread = cv2.imread('C:/Users/admin/Desktop/images/plane.jpg')
#converting the given image to grayscale image
imagegray = cv2.cvtColor(imageread, cv2.COLOR_BGR2GRAY)
#implementing SIFT algorithm to detect key points in the image
siftfeatures = cv2.xfeatures2d.SIFT_create()
keypoints = siftfeatures.detect(imagegray, None)
#drawing the key points on the input image using drawKeypoints() function
resultimage = cv2.drawKeypoints(imagegray, keypoints, 0, (0, 255, 0), flags=cv2.DRAW_MATCHES_FLAGS_DRAW_RICH_KEYPOINTS)
#displaying the image with keypoints as the output on the screen
cv2.imshow('image_with_keypoints', resultimage)
cv2.waitKey(0)
Output:
In the above program, we are importing the cv2 module. Then we are reading the image whose keypoints are to be detected using imread() function. Then we are implementing SIFT algorithm to detect the keypoints on the image. Then we are using drawKeypoints() function to draw the key points on the image and then display the image as the output on the screen.
Example #2
OpenCV program in python to demonstrate drawKeypoints() function to read the given image using imread() function. Implement SIFT algorithm to detect keypoints in the image and then use drawKeypoints() function to draw the key points on the image and display the output on the screen.
Code:
#importing the cv2 module
import cv2
#reading the image whose key points are to be detected using imread() function
imageread = cv2.imread('C:/Users/admin/Desktop/images/car.jpg')
#converting the given image to grayscale image
imagegray = cv2.cvtColor(imageread, cv2.COLOR_BGR2GRAY)
#implementing SIFT algorithm to detect key points in the image
siftfeatures = cv2.xfeatures2d.SIFT_create()
keypoints = siftfeatures.detect(imagegray, None)
#drawing the key points on the input image using drawKeypoints() function
resultimage = cv2.drawKeypoints(imagegray, keypoints, 0, (0, 255, 0), flags=cv2.DRAW_MATCHES_FLAGS_DRAW_RICH_KEYPOINTS)
#displaying the image with keypoints as the output on the screen
cv2.imshow('image_with_keypoints', resultimage)
cv2.waitKey(0)
Output:
In the above program, we are importing the cv2 module. Then we are reading the image whose keypoints are to be detected using imread() function. Then we are implementing SIFT algorithm to detect the keypoints on the image. Then we are using drawKeypoints() function to draw the key points on the image and then display the image as the output on the screen.
Example #3
OpenCV program in python to demonstrate drawKeypoints() function to read the given image using imread() function. Implement SIFT algorithm to detect keypoints in the image and then use drawKeypoints() function to draw the key points on the image and display the output on the screen.
Code:
#importing the cv2 module
import cv2
#reading the image whose key points are to be detected using imread() function
imageread = cv2.imread('C:/Users/admin/Desktop/images/educba.jpg')
#converting the given image to grayscale image
imagegray = cv2.cvtColor(imageread, cv2.COLOR_BGR2GRAY)
#implementing SIFT algorithm to detect key points in the image
siftfeatures = cv2.xfeatures2d.SIFT_create()
keypoints = siftfeatures.detect(imagegray, None)
#drawing the key points on the input image using drawKeypoints() function
resultimage = cv2.drawKeypoints(imagegray, keypoints, 0, (0, 255, 0), flags=cv2.DRAW_MATCHES_FLAGS_DRAW_RICH_KEYPOINTS)
#displaying the image with keypoints as the output on the screen
cv2.imshow('image_with_keypoints', resultimage)
cv2.waitKey(0)
Output:
In the above program, we are importing the cv2 module. Then we are reading the image whose keypoints are to be detected using imread() function. Then we are implementing SIFT algorithm to detect the keypoints on the image. Then we are using drawKeypoints() function to draw the key points on the image and then display the image as the output on the screen.
Recommended Articles
We hope that this EDUCBA information on “OpenCV KeyPoint ” was beneficial to you. You can view EDUCBA’s recommended articles for more information.