C++不允许使用抽象类类型“cv::ORB”的对象 的解决办法

方法借鉴:https://blog.csdn.net/weixin_43860261/article/details/88672429

平台:VS2017,opencv4.0.1进行编程
报错问题:在定义ORB对象的时候报错,不允许使用抽象类类型“cv::ORB”的对象

为什么会报错?
角点检测的几个方法(SURF,SIFT,ORB)都被转移opencv_contrib中了。所以版本之间有差异。

这是转移前的写法:

ORB orb;
orb.detect(img_1, keypoints_1);
orb.detect(img_2, keypoints_2);

这种是转移后的写法:

Ptr<ORB> orb = ORB::create();
orb->detect(img_1, keypoints_1);
orb->detect(img_2, keypoints_2);

希望大家能避开这个坑。

以下是一个改进后的ORB算法代码,它包括了Shi-Tomasi算法: ```c #include <opencv2/opencv.hpp> #include <vector> #include <iostream> using namespace cv; using namespace std; void cornerSubPix_wrapper(const Mat& gray, vector<Point2f>& corners, Size winSize, Size zeroZone, TermCriteria criteria) { cornerSubPix(gray, corners, winSize, zeroZone, criteria); } vector<KeyPoint> myORB(const Mat& img, int nfeatures = 500, float scaleFactor = 1.2f, int nlevels = 8, int edgeThreshold = 31, int firstLevel = 0, int WTA_K = 2, int scoreType = ORB::HARRIS_SCORE, int patchSize = 31, int fastThreshold = 20, double minDistance = 10.0, bool useShiTomasi = true) { // 灰度图 Mat gray; cvtColor(img, gray, COLOR_BGR2GRAY); // 角点检测 vector<Point2f> corners; if (useShiTomasi) { // Shi-Tomasi角点检测 goodFeaturesToTrack(gray, corners, nfeatures, 0.01, minDistance); // 亚像素级别的精度 cornerSubPix_wrapper(gray, corners, Size(3, 3), Size(-1, -1), TermCriteria(TermCriteria::MAX_ITER + TermCriteria::EPS, 30, 0.1)); } else { // Harris角点检测 cornerHarris(gray, gray, 2, 3, 0.04); // 非极大值抑制 Mat dst_norm, dst_norm_scaled; normalize(gray, dst_norm, 0, 255, NORM_MINMAX, CV_32FC1, Mat()); convertScaleAbs(dst_norm, dst_norm_scaled); for (int i = 0; i < dst_norm.rows; i++) { for (int j = 0; j < dst_norm.cols; j++) { if (dst_norm.at<float>(i, j) > edgeThreshold) { corners.push_back(Point2f(j, i)); } } } } // ORB描述子 vector<KeyPoint> keypoints; for (const auto& pt : corners) { keypoints.emplace_back(pt, patchSize); } Ptr<ORB> orb = ORB::create(nfeatures, scaleFactor, nlevels, edgeThreshold, firstLevel, WTA_K, scoreType, patchSize); Mat descriptors; orb->compute(img, keypoints, descriptors); // 绘制角点 Mat img_with_corners; drawKeypoints(img, keypoints, img_with_corners); imshow("ORB with Shi-Tomasi", img_with_corners); waitKey(0); return keypoints; } int main() { Mat img = imread("test.png"); vector<KeyPoint> keypoints = myORB(img); return 0; } ``` 这个改进的ORB算法代码中,我们在角点检测部分加入了Shi-Tomasi算法。如果`useShiTomasi`参数为`true`,则使用Shi-Tomasi算法进行角点检测;如果为`false`,则使用Harris角点检测。在使用Shi-Tomasi算法时,我们还使用了`cornerSubPix`函数对检测到的角点进行了亚像素级别的精度提升。
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值