这里有一个值得说明的问题是:OpenCV2.4版本后好像把SurfFeatureDetector这个类的定义移到了头文件nonfree/features2d.hpp
中,所以头文件中要加入该文件,并且要把opencv_nonfree24xd.lib加入属性表的链接器熟悉的输入中,其中x换成你当前opencv的版本号。
最终的显示效果如下:
四、SURF特征的描述
在图像配准中,特征点的描述往往不是位置这么简单,而是使用了一个N维向量来描述一个特征点,这些描述子之间可以通过定义距离公式来比较相近程度。
SurfDescriptorExtractor 是一个提取SURF特征点以及其描述的类。
下面是一个宽景图像的拼接配准的例子:
#include <opencv2/core/core.hpp> #include <opencv2/highgui/highgui.hpp> #include <opencv2/nonfree/features2d.hpp> #include <opencv2/legacy/legacy.hpp> using namespace cv; int main() { Mat image1=imread("../b1.png"); Mat image2=imread("../b2.png"); // 检测surf特征点 vector<KeyPoint> keypoints1,keypoints2; SurfFeatureDetector detector(400); detector.detect(image1, keypoints1); detector.detect(image2, keypoints2); // 描述surf特征点 SurfDescriptorExtractor surfDesc; Mat descriptros1,descriptros2; surfDesc.compute(image1,keypoints1,descriptros1); surfDesc.compute(image2,keypoints2,descriptros2); // 计算匹配点数 BruteForceMatcher<L2<float>>matcher; vector<DMatch> matches; matcher.match(descriptros1,descriptros2,matches); std::nth_element(matches.begin(),matches.begin()+24,matches.end()); matches.erase(matches.begin()+25,matches.end()); // 画出匹配图 Mat imageMatches; drawMatches(image1,keypoints1,image2,keypoints2,matches, imageMatches,Scalar(255,0,0)); namedWindow("image2"); imshow("image2",image2); waitKey(); return 0; }
程序中我们选择了25个配准点,得到最后的匹配如下: