SIFT算法需要安装额外的扩展模块,安装步骤和注意事项见我博客:https://blog.csdn.net/qq_27396861/article/details/88095820
cv::xfeatures2d::SIFT::create( int nfeatures = 0, int nOctaveLayers = 3,
double constrastThreshold = 0.04, double edgeThreshold = 10,
double sigma = 1.5 )
(1)nfeatures,保留的最佳特性的数量。特征按其得分进行排序(以SIFT算法作为局部对比度进行测量);
(2)nOctavelLayers,高斯金字塔最小层级数,由图像自动计算出;
(3)constrastThreshold,对比度阈值用于过滤区域中的弱特征。阈值越大,检测器产生的特征越少。;
(4)edgeThreshold ,用于过滤掉类似边缘特征的阈值。 请注意,其含义与contrastThreshold不同,即edgeThreshold越大,滤出的特征越少;
(5)sigma,高斯输入层级, 如果图像分辨率较低,则可能需要减少数值。
示例:
#include <opencv2/opencv.hpp>
#include <opencv2/xfeatures2d.hpp>
#include <opencv2/xfeatures2d/nonfree.hpp>
#include <iostream>
using namespace cv;
using namespace cv::xfeatures2d;
using namespace std;
int main()
{
Mat srcImage = imread("curry_dlt.jpg");
int numFeatures = 20;
Ptr<SIFT> dectector = SIFT::create(numFeatures);
vector<KeyPoint> keypoints;
dectector->detect(srcImage, keypoints, Mat());
printf("Total keypoints: %d\n", keypoints.size());
Mat keypoint_img;
drawKeypoints(srcImage, keypoints, keypoint_img, Scalar::all(-1), DrawMatchesFlags::DEFAULT);
imshow("keypoint_img", keypoint_img);
waitKey(0);
return 0;
}
效果图: