点击上方蓝字关注我们
微信公众号:OpenCV学堂
关注获取更多计算机视觉与深度学习知识
假设存在连续N个点满足
其中n=p+q称为N阶距。得到中心坐标为:
计算得到角度为:
#include
#include
#include
#define RATIO 0.4
using namespace cv;
using namespace std;
int main(int argc, char** argv) {
Mat box = imread("D:/vcprojects/images/box.png");
Mat scene = imread("D:/vcprojects/images/box_in_scene.png");
if (scene.empty()) {
printf("could not load image...\n");
return -1;
}
imshow("input image", scene);
vector<KeyPoint> keypoints_obj, keypoints_sence;
Mat descriptors_box, descriptors_sence;
Ptr<ORB> detector = ORB::create();
detector->detectAndCompute(scene, Mat(), keypoints_sence, descriptors_sence);
detector->detectAndCompute(box, Mat(), keypoints_obj, descriptors_box);
vector<DMatch> matches;
// 初始化flann匹配
// Ptr
matcher = FlannBasedMatcher::create(); // default is bad, using local sensitive hash(LSH)
Ptr<DescriptorMatcher> matcher = makePtr<FlannBasedMatcher>(makePtr<flann::LshIndexParams>(12, 20, 2));
matcher->match(descriptors_box, descriptors_sence, matches);
// 发现匹配
vector<DMatch> goodMatches;
printf("total match points : %d\n", matches.size());
float maxdist = 0;
for (unsigned int i = 0; i < matches.size(); ++i) {
printf("dist : %.2f \n", matches[i].distance);
maxdist = max(maxdist, matches[i].distance);
}
for (unsigned int i = 0; i < matches.size(); ++i) {
if(matches[i].distance < maxdist*RATIO)
goodMatches.push_back(matches[i]);
}
Mat dst;
drawMatches(box, keypoints_obj, scene, keypoints_sence, goodMatches, dst);
imshow("output", dst);
waitKey(0);
return 0;
}
Ptr<DescriptorMatcher> matcher = makePtr<FlannBasedMatcher>
(makePtr<flann::LshIndexParams>(12, 20, 2));
运行结果:
为学患无疑,疑则进也
推荐阅读
OpenCV4.8+YOLOv8对象检测C++推理演示
ZXING+OpenCV打造开源条码检测应用
攻略 | 学习深度学习只需要三个月的好方法
三行代码实现 TensorRT8.6 C++ 深度学习模型部署
实战 | YOLOv8+OpenCV 实现DM码定位检测与解析
对象检测边界框损失 – 从IOU到ProbIOU
初学者必看 | 学习深度学习的五个误区