如何在OpenCV Python中實現ORB特徵檢測器?
ORB (Oriented FAST and Rotated BRIEF) 是FAST關鍵點檢測器和BRIEF描述符的融合,其中進行了許多更改以提高效能。要實現ORB特徵檢測器和描述符,您可以按照以下步驟操作:
匯入所需的庫OpenCV和NumPy。確保您已安裝它們。
使用cv2.imread()方法讀取輸入影像。指定影像的完整路徑。使用cv2.cvtColor()方法將輸入影像轉換為灰度影像。
使用orb=cv2.ORB_create()以預設值啟動ORB物件。
在灰度影像中檢測並計算特徵關鍵點'kp'和描述符'des'。使用orb.detectAndCompute()。它返回關鍵點'kp'和描述符'des'。
使用cv2.drawKeypoints()函式在影像上繪製檢測到的特徵關鍵點kp。要繪製豐富的特徵關鍵點,您可以將flags=cv2.DRAW_MATCHES_FLAGS_DRAW_RICH_KEYPOINTS作為引數傳遞。
顯示帶有繪製的特徵關鍵點的影像。
讓我們看一些示例,以使用ORB特徵檢測器在輸入影像中檢測和繪製關鍵點。
輸入影像
我們將在下面的示例中使用以下影像作為輸入檔案。

示例
在這個Python程式中,我們使用ORB特徵檢測器在輸入影像中檢測和計算關鍵點和描述符。我們還在影像上繪製關鍵點並顯示它。
# import required libraries import cv2 # read input image img = cv2.imread('house.jpg') # convert the image to grayscale gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) # Initiate ORB object with default values orb = cv2.ORB_create(nfeatures=2000) # detect and compute the keypoints on image (grayscale) kp = orb.detect(gray, None) kp, des = orb.compute(gray, kp) # draw keypoints in image img1 = cv2.drawKeypoints(gray, kp, None, (0,0,255), flags=0) # display the image with keypoints drawn on it cv2.imshow("ORB Keypoints", img1) cv2.waitKey(0) cv2.destroyAllWindows()
輸出
當我們執行上述程式時,它將生成以下輸出視窗:

關鍵點以紅色顯示。
示例
在這個Python程式中,我們也使用ORB特徵檢測器在輸入影像中檢測和計算關鍵點和描述符。我們還在影像上繪製關鍵點並顯示它。
我們使用標誌cv2.DRAW_MATCHES_FLAGS_DRAW_RICH_KEYPOINTS來繪製關鍵點。
# import required libraries import cv2 # read input image img = cv2.imread('house.jpg') # convert the image to grayscale gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) # Initiate ORB object with default values orb = cv2.ORB_create(nfeatures=50) # detect and compute the keypoints on image (grayscale) kp, des = orb.detectAndCompute(gray, None) # draw keypoints in image img1 = cv2.drawKeypoints(img, kp, None,(0,0,255), flags=cv2.DRAW_MATCHES_FLAGS_DRAW_RICH_KEYPOINTS) # display the image with keypoints drawn on it cv2.imshow("ORB Keypoints", img1) cv2.waitKey(0) cv2.destroyAllWindows()
輸出
當我們執行上述程式時,它將生成以下輸出視窗:

在上圖中,關鍵點及其大小和方向一起繪製。我們找到並繪製了50個特徵關鍵點。
資料結構
網路
關係資料庫管理系統(RDBMS)
作業系統
Java
iOS
HTML
CSS
Android
Python
C語言程式設計
C++
C#
MongoDB
MySQL
Javascript
PHP