Open In App

Python OpenCV – drawKeypoints() Function

Improve
Improve
Like Article
Like
Save
Share
Report

In this article let’s discuss the drawKeypoints() function of OpenCV. The distinguishing qualities of an image that make it stand out are referred to as key points in an image. The key points of a particular image let us recognize objects and compare images. Detecting critical spots in a picture may be done using a variety of techniques and algorithms. We utilize the drawKeypoints() method in OpenCV to be able to draw the identified key points on a given picture. The input picture, keypoints, color, and flag are sent to the drawKeypoints() method. key points are the most important aspects of the detection. Even after the image is modified the key points remain the same. As of now, we can only use the SIRF_create() function as the surf function is patented. 

Syntax of drawKeypoints() function:

drawKeypoints(input_image, key_points, output_image, colour, flag)

parameters:

  • input_image : The image  which  is turned into grayscale and then the key points are extracted using the  SURF  or SIFT algorithms is called input image.
  • key_points : The key points obtained from the input picture after using the algorithms are referred to as keypoints.
  • output_image :  image on which the keypoints are drawn.
  • colour : the colour of the keypoints.
  •  flag : drawing features are represented by the flag.

Example 1:

This example begins with importing the OpenCV and matplotlib packages. we read an image, turn it into grayscale and then apply the SIRF_create() algorithm which helps us detect key points in the images. drawKeypoints() function takes in a number of parameters and draws the key points on the image. the flag can be changed. In the below example we used cv2.DRAW_MATCHES_FLAGS_NOT_DRAW_SINGLE_POINTS as a flag. The output image is plotted. instead of using cv2.SIRF_create() , one can also use cv2.xfeatures2d.SIFT_create(), in few versions of OpenCV it might not work. The same goes with cv2.xfeatures2d.SURF_create() algorithm.

Note: colour is (255,0,0) for red, (0,0,255) for blue and (0,255,0) for green.

Image Used:

Python3




# importing packages
import cv2
import matplotlib.pyplot as plt
  
# reading image using the imread() function
imageread = cv2.imread('img1.jpeg')
  
# input image is converted to gray scale image
imagegray = cv2.cvtColor(imageread, cv2.COLOR_BGR2GRAY)
  
# using the SIRF algorithm to detect key
# points in the image
features = cv2.SIFT_create()
  
keypoints = features.detect(imagegray, None)
  
# drawKeypoints function is used to draw keypoints
output_image = cv2.drawKeypoints(imagegray, keypoints, 0, (0, 0, 255),
                                 flags=cv2.DRAW_MATCHES_FLAGS_NOT_DRAW_SINGLE_POINTS)
  
# displaying the image with keypoints as the
# output on the screen
  
plt.imshow(output_image)
  
# plotting image
plt.show()


Output:

Example 2:

This example is similar to the previous one except that we changed the colour to red (255,0,0) and the flag to cv2.DRAW_MATCHES_FLAGS_DRAW_RICH_KEYPOINTS.

Python3




# importing packages
import cv2
import matplotlib.pyplot as plt
  
# reading image using the imread() function
imageread = cv2.imread('img1.jpeg')
  
# input image is converted to gray scale image
imagegray = cv2.cvtColor(imageread, cv2.COLOR_BGR2GRAY)
  
# using the SIRF algorithm to detect key 
# points in the image
features = cv2.SIFT_create()
  
keypoints = features.detect(imagegray, None)
  
# drawKeypoints function is used to draw keypoints
output_image = cv2.drawKeypoints(imagegray, keypoints, 0, (255, 0, 0),
                                 flags=cv2.DRAW_MATCHES_FLAGS_DRAW_RICH_KEYPOINTS)
  
# displaying the image with keypoints as the
# output on the screen
plt.imshow(output_image)
  
# plotting image
plt.show()


Output:

Example 3: 

This example is similar to the previous one except that we changed the colour to green (0,255,0) and the flag to cv2.DRAW_MATCHES_FLAGS_DEFAULT.

Python3




import cv2
import matplotlib.pyplot as plt
  
# reading image using the imread() function
imageread = cv2.imread('img1.jpeg')
  
# input image is converted to gray scale image
imagegray = cv2.cvtColor(imageread, cv2.COLOR_BGR2GRAY)
  
# using the SIRF algorithm to detect key
# points in the image
features = cv2.SIFT_create()
  
keypoints = features.detect(imagegray, None)
  
# drawKeypoints function is used to draw keypoints
output_image = cv2.drawKeypoints(imagegray, keypoints, 0, (0, 255, 0),
                                 flags=cv2.DRAW_MATCHES_FLAGS_DEFAULT)
  
# displaying the image with keypoints as
# the output on the screen
plt.imshow(output_image)
  
# plotting image
plt.show()


Output:



Last Updated : 03 Jan, 2023
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads