《OpenCV3編程入門》配套示例程序89:SURF特征點檢測示例中,出現的問題及解決方法:
問題一:
直接按照原文代碼寫,報錯“SurfFeatureDetector”: 未聲明的標識符
.......
//【2】定義需要用到的變量和類 int minHessian = 400;//定義SURF中的hessian閾值特征點檢測算子 SurfFeatureDetector detector( minHessian );//定義一個SurfFeatureDetector(SURF) 特征檢測類對象 std::vector<KeyPoint> keypoints_1, keypoints_2;//vector模板類是能夠存放任意類型的動態數組,能夠增加和壓縮數據 //【3】調用detect函數檢測出SURF特征關鍵點,保存在vector容器中 detector.detect( srcImage1, keypoints_1 ); detector.detect( srcImage2, keypoints_2 );
.......
解決方法:
頭文件加上:(SURF算法在opencv3中是nonfree的)
//OpenCV #include <opencv/cv.hpp> #include<xfeatures2d/nonfree.hpp> #include "opencv2/core/core.hpp" #include "opencv2/features2d/features2d.hpp" #include "opencv2/highgui/highgui.hpp"
最重要的是加上:
using namespace xfeatures2d;
(試了一下,頭文件加上各種.hpp后還是報錯未聲明標識符,加上這句話就OK了)
問題二:
進行以上操作后,又會出現新的報錯"SurfFeatureDetector”: 不能實例化抽象類",需要將代碼改為:
//SURF特征點檢測 cv::Mat BasicAlgorithm::on_SURF(cv::Mat mat) { //【1】載入源圖片並顯示 Mat srcImage1 = mat; Mat srcImage2= imread("C:\\Users\\asus\\Desktop\\2.jpg"); //【2】定義需要用到的變量和類 int minHessian = 400; //定義SUFR中的hessian閾值特征點檢測算子 //SurfFeatureDetector detector( minHessian );//定義一個SurfFeatureDetector(SURF) 特征檢測類對象(opencv2中或許可用,opencv3這么寫就會報錯),改為下邊這句 Ptr<SURF>detector = SURF::create(minHessian); vector<KeyPoint> keypoints_1, keypoints_2;//vector模板類是能夠存放任意類型的動態數組,能夠增加和壓縮數據 //【3】調用detect函數檢測出SURF特征關鍵點,保存在vector容器中 detector->detect(srcImage1, keypoints_1); detector->detect(srcImage2, keypoints_2); //【4】繪制特征關鍵點. Mat img_keypoints_1; Mat img_keypoints_2; drawKeypoints( srcImage1, keypoints_1, img_keypoints_1, Scalar::all(-1), DrawMatchesFlags::DEFAULT ); drawKeypoints( srcImage2, keypoints_2, img_keypoints_2, Scalar::all(-1), DrawMatchesFlags::DEFAULT ); //【5】顯示效果圖 imshow("特征點檢測效果圖1", img_keypoints_1 ); imshow("特征點檢測效果圖2", img_keypoints_2 ); return img_keypoints_1; }
以上,SURF特征點檢測的完整代碼。
總結:
SURF在3.1.0下的用法如下:
SurfFeatureDetector detector( minHessian );
detector.detect(image,keypoints);
改為:
Ptr<SURF>detector = SURF::create(); detector->detect(image,keypoints);