OpenCV Python 如何检测和绘制FAST特征点

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特征检测器在输入图像中检测和绘制特征点的示例。

输入图像

我们将在下面的示例中使用以下图像作为输入文件。

OpenCV Python 如何检测和绘制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

并且我们得到以下窗口,其中显示了在图像上绘制的关键点 −

OpenCV Python 如何检测和绘制FAST特征点

示例

在这个程序中,我们使用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

然后我们得到以下窗口,显示了带有绘制的关键点的图像−

OpenCV Python 如何检测和绘制FAST特征点

我们注意到,当 nonmaxSuppressionFalse 时,与 nonmaxSuppressionTrue 时相比,检测到的总关键点数量更多。

Camera课程

Python教程

Java教程

Web教程

数据库教程

图形图像教程

办公软件教程

Linux教程

计算机教程

大数据教程

开发工具教程