- 操作系统:ubuntu22.04
- OpenCV版本:OpenCV4.9
- IDE:Visual Studio Code
- 编程语言:C++11
算法描述
cv::cuda::FastFeatureDetector 是 OpenCV 的 CUDA 加速模块中的一部分,用于在图像中快速检测特征点。FAST(Features from Accelerated Segment Test)算法是一种高效的角点检测算法,能够在保持较高精度的同时显著提高速度。
cv::cuda::FastFeatureDetector 提供了 GPU 加速的 FAST 角点检测功能。它继承自 cv::Algorithm 类,并且实现了与 CPU 版本的 cv::FastFeatureDetector 相似的接口,但利用了 CUDA 来加速计算过程。
主要成员函数
构造函数
cv::cuda::FastFeatureDetector::FastFeatureDetector
(
int threshold=10,
bool nonmaxSuppression=true,
int type=cv::FastFeatureDetector::TYPE_9_16
)
- threshold: 阈值,用来判断一个像素是否为角点。
- nonmaxSuppression: 是否启用非极大值抑制来过滤掉一些不是最强响应的角点。
- type: 指定使用的FAST类型,可以是 TYPE_9_16, TYPE_7_12, 或者 TYPE_5_8,分别对应不同的测试模式。
检测函数
void detect
(
cv::InputArray image,
cv::Ptr<cv::cuda::GpuMat>& keypoints,
cv::Stream& stream = cv::cuda::Stream::Null()
)
- image: 输入图像,通常是一个灰度图(CV_8UC1),也可以是彩色图(CV_8UC3),但会被转换为灰度图处理。
- keypoints: 输出的关键点集合。
- stream: 可选参数,指定CUDA流以实现异步操作。
示例代码
#include <opencv2/cudafeatures2d.hpp>
#include <opencv2/cudaimgproc.hpp>
#include <opencv2/opencv.hpp>
int main()
{
// 加载图像
cv::Mat img = cv::imread( "/media/dingxin/data/study/OpenCV/sources/images/Lenna.png", cv::IMREAD_GRAYSCALE );
if ( img.empty() )
{
std::cerr << "无法加载图像" << std::endl;
return -1;
}
// 将图像上传到 GPU
cv::cuda::GpuMat d_img( img );
// 创建 FastFeatureDetector
cv::Ptr< cv::cuda::FastFeatureDetector > detector = cv::cuda::FastFeatureDetector::create( 30 ); // 设置阈值为30
// 检测特征点
std::vector< cv::KeyPoint > keypoints;
detector->detect( d_img, keypoints );
// 绘制特征点
cv::Mat img_keypoints;
cv::drawKeypoints( img, keypoints, img_keypoints );
cv::imshow( "FAST Feature Detector", img_keypoints );
cv::waitKey( 0 );
return 0;
}