写一个改进后的orb算法c++代码,要求:1 在orb算法中加入shi-tomasi算法
时间: 2023-06-02 20:01:26 浏览: 164
以下是一个改进后的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`函数对检测到的角点进行了亚像素级别的精度提升。
阅读全文