如何在 OpenCV Python 中实现 ORB 特征检测器?

opencvpythonserver side programmingprogramming

ORB(Oriented FAST and Rotated BRIEF)是 FAST 关键点检测器和 BRIEF 描述符的融合,其中进行了许多更改以提高性能。要实现 ORB 特征检测器和描述符,您可以按照以下步骤操作

  • 导入所需的库 OpenCVNumPy。确保您已经安装了它们。

  • 使用 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 个特征关键点。


相关文章