如何在 OpenCV Python 中檢測和繪製 FAST 特徵點?


FAST(加速分段測試特徵)是一種高速角點檢測演算法。我們使用FAST演算法來檢測影像中的特徵。我們首先使用cv2.FastFeatureDetector_create()建立一個FAST物件。然後使用fast.detect()檢測特徵點,其中fast是建立的FAST物件。要繪製特徵點,我們使用cv2.drawKeypoints()

步驟

要使用 FAST 特徵檢測器在輸入影像中檢測和繪製特徵點,您可以按照以下步驟操作

  • 匯入所需的庫OpenCVNumPy。確保您已安裝它們。

  • 使用cv2.imread()方法讀取輸入影像。指定影像的完整路徑。使用cv2.cvtColor()方法將輸入影像轉換為灰度影像。

  • 使用預設值初始化FAST物件,例如fast=cv2.FastFeatureDetector_create()。您可以選擇使用fast.setNonmaxSuppression(0)將非最大抑制設定為False。

  • 檢測灰度影像中的特徵點。使用fast.detect(gray, None)。它返回關鍵點kp。

  • 在影像cv2.drawKeypoints()函式上繪製檢測到的關鍵點kp

  • 顯示帶有繪製關鍵點的影像。

讓我們看看使用 FAST 特徵檢測器在輸入影像中檢測和繪製特徵點的示例。

輸入影像

我們將在下面的示例中使用以下影像作為輸入檔案。


示例

在此程式中,我們使用 FAST 演算法檢測和繪製特徵點。預設的nonmaxSuppression設定為True

# import required libraries import cv2 # read input image img = cv2.imread('architecture.jpg') # convert the image to grayscale gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) # Initiate FAST object with default values fast = cv2.FastFeatureDetector_create() # find the keypoints on image (grayscale) kp = fast.detect(gray,None) # draw keypoints in image img2 = cv2.drawKeypoints(img, kp, None) # Print all default params print("Threshold: ", fast.getThreshold()) print("nonmaxSuppression: ", fast.getNonmaxSuppression()) print("neighborhood: ", fast.getType()) print("Total Keypoints with nonmaxSuppression: ", len(kp)) # display the image with keypoints drawn on it cv2.imshow("Keypoints with nonmaxSuppression", img2) cv2.waitKey(0) cv2.destroyAllWindows()

輸出

執行後,它將產生以下輸出

Threshold: 10
nonmaxSuppression: True
neighborhood: 2
Total Keypoints with nonmaxSuppression: 5791

我們得到以下視窗,顯示帶有繪製關鍵點的影像:


示例

在此程式中,我們使用 FAST 演算法檢測和繪製特徵點。我們將nonmaxSuppression設定為False

# import required libraries import cv2 # read input image img = cv2.imread('architecture.jpg') # convert the image to grayscale gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) # Initiate FAST object with default values fast = cv2.FastFeatureDetector_create() # Disable nonmaxSuppression fast.setNonmaxSuppression(0) # find the keypoints on image (grayscale) kp = fast.detect(gray,None) # Print all default params print("Threshold: ", fast.getThreshold()) print("nonmaxSuppression: ", fast.getNonmaxSuppression()) print("neighborhood: ", fast.getType()) print("Total Keypoints without nonmaxSuppression: ", len(kp)) img2 = img.copy() img2 = cv2.drawKeypoints(img2, kp, None) # display the image with keypoints drawn on it cv2.imshow('Keypoints without nonmaxSuppression',img2) cv2.waitKey(0) cv2.destroyAllWindows()

輸出

執行後,它將產生以下輸出

Threshold: 10
nonmaxSuppression: False
neighborhood: 2
Total Keypoints without nonmaxSuppression: 27101

我們得到以下視窗,顯示帶有繪製關鍵點的影像:


我們注意到,當nonmaxSuppressionFalse時,檢測到的關鍵點總數比nonmaxSuppressionTrue時更多。

更新於: 2022年12月5日

3K+ 瀏覽量

開啟你的 職業生涯

透過完成課程獲得認證

開始學習
廣告