“SurfFeatureDetector”: 未声明的标识符/不能实例化抽象类
《OpenCV3编程入门》配套示例程序89:SURF特征点检测示例中,出现的问题及解决方法:
问题一:
直接按照原文代码写,报错“SurfFeatureDetector”: 未声明的标识符
1 2 3 4 5 6 7 8 | .......<br> //【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 );<br> ....... |
解决方法:
头文件加上:(SURF算法在opencv3中是nonfree的)
1 2 3 4 5 6 | //OpenCV #include <opencv/cv.hpp> #include<xfeatures2d/nonfree.hpp> #include "opencv2/core/core.hpp" #include "opencv2/features2d/features2d.hpp" #include "opencv2/highgui/highgui.hpp" |
最重要的是加上:
1 | using namespace xfeatures2d; |
(试了一下,头文件加上各种.hpp后还是报错未声明标识符,加上这句话就OK了)
问题二:
进行以上操作后,又会出现新的报错"SurfFeatureDetector”: 不能实例化抽象类",需要将代码改为:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 | //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下的用法如下:
1 | SurfFeatureDetector detector( minHessian );detector.detect(image,keypoints); |
改为:
1 2 | Ptr<SURF>detector = SURF::create(); detector->detect(image,keypoints); |
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· 没有Manus邀请码?试试免邀请码的MGX或者开源的OpenManus吧
· 【自荐】一款简洁、开源的在线白板工具 Drawnix
· 园子的第一款AI主题卫衣上架——"HELLO! HOW CAN I ASSIST YOU TODAY
· Docker 太简单,K8s 太复杂?w7panel 让容器管理更轻松!