當前位置: 首頁>>代碼示例 >>用法及示例精選 >>正文


Python OpenCV drawKeypoints()用法及代碼示例


在本文中,我們討論 OpenCV 的 drawKeypoints() 函數。使圖像脫穎而出的獨特品質被稱為圖像中的關鍵點。特定圖像的關鍵點讓我們能夠識別物體並比較圖像。可以使用多種技術和算法來檢測圖片中的關鍵點。我們利用OpenCV中的drawKeypoints()方法能夠在給定圖片上繪製識別出的關鍵點。輸入圖片、關鍵點、顏色和標誌被發送到drawKeypoints()方法。關鍵點是檢測中最重要的方麵。即使圖像被修改後,關鍵點仍然保持不變。目前,我們隻能使用SIRF_create()函數,因為surf函數已獲得專利。

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.

示例 1:

此示例首先導入 OpenCV 和 matplotlib 包。我們讀取圖像,將其轉換為灰度,然後應用SIRF_create()算法來幫助我們檢測圖像中的關鍵點。 drawKeypoints()函數接收多個參數並在圖像上繪製關鍵點。標誌可以改變。在下麵的示例中,我們使用 cv2.DRAW_MATCHES_FLAGS_NOT_DRAW_SINGLE_POINTS 作為標誌。繪製輸出圖像。除了使用 cv2.SIRF_create() 之外,還可以使用 cv2.xfeatures2d.SIFT_create() ,在 OpenCV 的幾個版本中它可能不起作用。 cv2.xfeatures2d.SURF_create() 算法也是如此。

注意:紅色為 (255,0,0),藍色為 (0,0,255),綠色為 (0,255,0)。

使用的圖像:

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() 

輸出:

示例 2:

此示例與上一個示例類似,隻是我們將顏色更改為紅色 (255,0,0),並將標誌更改為 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() 

輸出:

示例3:

此示例與上一個示例類似,隻是我們將顏色更改為綠色 (0,255,0),並將標誌更改為 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() 

輸出:



相關用法


注:本文由純淨天空篩選整理自isitapol2002大神的英文原創作品 Python OpenCV – drawKeypoints() Function。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。