【OpenCV】OpenCV3中的SURF特征点的寻找和匹配

2024-03-11 06:58

本文主要是介绍【OpenCV】OpenCV3中的SURF特征点的寻找和匹配,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!


不多说什么了,直接上代码吧:

#include <iostream>
#include <stdio.h>
#include "opencv2/core.hpp"
#include "opencv2/core/utility.hpp"
#include "opencv2/core/ocl.hpp"
#include "opencv2/imgcodecs.hpp"
#include "opencv2/highgui.hpp"
#include "opencv2/features2d.hpp"
#include "opencv2/calib3d.hpp"
#include "opencv2/imgproc.hpp"
#include"opencv2/flann.hpp"
#include"opencv2/xfeatures2d.hpp"
#include"opencv2/ml.hpp"using namespace cv;
using namespace std;
using namespace cv::xfeatures2d;
using namespace cv::ml;int main()
{Mat a = imread("box.png", IMREAD_GRAYSCALE);    //读取灰度图像Mat b = imread("box_in_scene.png", IMREAD_GRAYSCALE);Ptr<SURF> surf;      //创建方式和2中的不一样surf = SURF::create(800);BFMatcher matcher;Mat c, d;vector<KeyPoint>key1, key2;vector<DMatch> matches;surf->detectAndCompute(a, Mat(), key1, c);surf->detectAndCompute(b, Mat(), key2, d);matcher.match(c, d, matches);       //匹配sort(matches.begin(), matches.end());  //筛选匹配点vector< DMatch > good_matches;              int ptsPairs = std::min(50, (int)(matches.size() * 0.15));cout << ptsPairs << endl;for (int i = 0; i < ptsPairs; i++){good_matches.push_back(matches[i]);}Mat outimg;drawMatches(a, key1, b, key2, good_matches, outimg, Scalar::all(-1), Scalar::all(-1),vector<char>(), DrawMatchesFlags::NOT_DRAW_SINGLE_POINTS);  //绘制匹配点std::vector<Point2f> obj;std::vector<Point2f> scene;for (size_t i = 0; i < good_matches.size(); i++){obj.push_back(key1[good_matches[i].queryIdx].pt);scene.push_back(key2[good_matches[i].trainIdx].pt);}std::vector<Point2f> obj_corners(4);obj_corners[0] = Point(0, 0);obj_corners[1] = Point(a.cols, 0);obj_corners[2] = Point(a.cols, a.rows);obj_corners[3] = Point(0, a.rows);std::vector<Point2f> scene_corners(4);Mat H = findHomography(obj, scene, RANSAC);      //寻找匹配的图像perspectiveTransform(obj_corners, scene_corners, H);line(outimg,scene_corners[0] + Point2f((float)a.cols, 0), scene_corners[1] + Point2f((float)a.cols, 0),Scalar(0, 255, 0), 2, LINE_AA);       //绘制line(outimg,scene_corners[1] + Point2f((float)a.cols, 0), scene_corners[2] + Point2f((float)a.cols, 0),Scalar(0, 255, 0), 2, LINE_AA);line(outimg,scene_corners[2] + Point2f((float)a.cols, 0), scene_corners[3] + Point2f((float)a.cols, 0),Scalar(0, 255, 0), 2, LINE_AA);line(outimg,scene_corners[3] + Point2f((float)a.cols, 0), scene_corners[0] + Point2f((float)a.cols, 0),Scalar(0, 255, 0), 2, LINE_AA);imshow("aaaa",outimg);cvWaitKey(0);
}


运行图: 
这里写图片描述



//-------------读取模板------------cv::Mat img_object = imread("/storage/emulated/0/ApplePearFace/imgTemp.jpg");//-------------图像处理---------cv::Mat img_scene(yimage);/*// 检测surf特征点int minHessian = 400;OrbDescriptorExtractor detector(minHessian);std::vector<KeyPoint> keypoints_1, keypoints_2;detector.detect(img_1, keypoints_1);detector.detect(img_2, keypoints_2);//-- Step 2: Calculate descriptors (feature vectors)OrbDescriptorExtractor extractor;Mat descriptors_1, descriptors_2;extractor.compute(img_1, keypoints_1, descriptors_1);extractor.compute(img_2, keypoints_2, descriptors_2);//-- Step 3: Matching descriptor vectors with a brute force matcherBFMatcher matcher(NORM_L2);std::vector< DMatch > matches;matcher.match(descriptors_1, descriptors_2, matches);//-- Draw matchesMat img_matches;drawMatches(img_1, keypoints_1, img_2, keypoints_2, matches, img_matches);*/// 读取数据//cv::Mat img_object = cv::imread("doll01.jpg");//cv::Mat img_scene = cv::imread("doll012.jpg");if (!img_object.data || !img_scene.data) {cout << "Error reading images." << endl;return 0;}// 构建特征检测器和描述子提取器cv::Ptr<cv::FeatureDetector> detector = cv::FeatureDetector::create("ORB");cv::Ptr<cv::DescriptorExtractor> descriptor = cv::DescriptorExtractor::create("ORB");// 检测特征点vector<cv::KeyPoint> kp_object, kp_scene;detector->detect(img_object, kp_object);detector->detect(img_scene, kp_scene);// 计算描述子cv::Mat desp_object, desp_scene;descriptor->compute(img_object, kp_object, desp_object);descriptor->compute(img_scene, kp_scene, desp_scene);/*if (desp_object.type() != CV_32F) {desp_object.convertTo(desp_object, CV_32F);}if (desp_scene.type() != CV_32F) {desp_scene.convertTo(desp_scene, CV_32F);}*/// 匹配描述子vector<cv::DMatch> matches;cv::FlannBasedMatcher matcher(new cv::flann::LshIndexParams(20, 10, 2));matcher.match(desp_object, desp_scene, matches);//cout << "Find total " << matches.size() << " matches." << endl;// 筛选匹配//double min_dist = 100000;//for (int i = 0; i < matches.size(); i++) {//	float a = matches[i].distance;//	if (a < min_dist) {//		min_dist = matches[i].distance;//	}//}//vector<cv::DMatch> good_matches;//for (int i = 0; i < matches.size(); i++) {////	if (matches[i].distance < 3 * min_dist) {//		good_matches.push_back(matches[i]);//	}//}// 显示匹配//cout << "Good matches=" << matches.size() << endl;cv::Mat img_matches;cv::drawMatches(img_object, kp_object, img_scene, kp_scene, matches, img_matches);// 定位目标cv::vector<cv::Point2f> obj_points;cv::vector<cv::Point2f> scene;for (int i = 0; i < matches.size(); i++) {obj_points.push_back(kp_object[matches[i].queryIdx].pt);scene.push_back(kp_scene[matches[i].trainIdx].pt);}cv::Mat H = cv::findHomography(obj_points, scene, CV_RANSAC);cv::vector<cv::Point2f> obj_corners(4);cv::vector<cv::Point2f> scene_corners(4);obj_corners[0] = cv::Point(0, 0);obj_corners[1] = cv::Point(img_object.cols, 0);obj_corners[2] = cv::Point(img_object.cols, img_object.rows);obj_corners[3] = cv::Point(0, img_object.rows);cv::perspectiveTransform(obj_corners, scene_corners, H);cv::line(img_matches, scene_corners[0] + cv::Point2f(img_object.cols, 0), scene_corners[1] + cv::Point2f(img_object.cols, 0), cv::Scalar(0, 255, 0), 4);cv::line(img_matches, scene_corners[1] + cv::Point2f(img_object.cols, 0), scene_corners[2] + cv::Point2f(img_object.cols, 0), cv::Scalar(0, 255, 0), 4);cv::line(img_matches, scene_corners[2] + cv::Point2f(img_object.cols, 0), scene_corners[3] + cv::Point2f(img_object.cols, 0), cv::Scalar(0, 255, 0), 4);cv::line(img_matches, scene_corners[3] + cv::Point2f(img_object.cols, 0), scene_corners[0] + cv::Point2f(img_object.cols, 0), cv::Scalar(0, 255, 0), 4);cv::Mat dstSize;cv::resize(img_matches, dstSize, Size(2 * h, w));


这篇关于【OpenCV】OpenCV3中的SURF特征点的寻找和匹配的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/797007

相关文章

Nginx location匹配模式与规则详解

《Nginxlocation匹配模式与规则详解》:本文主要介绍Nginxlocation匹配模式与规则,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录一、环境二、匹配模式1. 精准模式2. 前缀模式(不继续匹配正则)3. 前缀模式(继续匹配正则)4. 正则模式(大

Java 正则表达式URL 匹配与源码全解析

《Java正则表达式URL匹配与源码全解析》在Web应用开发中,我们经常需要对URL进行格式验证,今天我们结合Java的Pattern和Matcher类,深入理解正则表达式在实际应用中... 目录1.正则表达式分解:2. 添加域名匹配 (2)3. 添加路径和查询参数匹配 (3) 4. 最终优化版本5.设计思

在PyCharm中安装PyTorch、torchvision和OpenCV详解

《在PyCharm中安装PyTorch、torchvision和OpenCV详解》:本文主要介绍在PyCharm中安装PyTorch、torchvision和OpenCV方式,具有很好的参考价值,... 目录PyCharm安装PyTorch、torchvision和OpenCV安装python安装PyTor

openCV中KNN算法的实现

《openCV中KNN算法的实现》KNN算法是一种简单且常用的分类算法,本文主要介绍了openCV中KNN算法的实现,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的... 目录KNN算法流程使用OpenCV实现KNNOpenCV 是一个开源的跨平台计算机视觉库,它提供了各

Python中使用正则表达式精准匹配IP地址的案例

《Python中使用正则表达式精准匹配IP地址的案例》Python的正则表达式(re模块)是完成这个任务的利器,但你知道怎么写才能准确匹配各种合法的IP地址吗,今天我们就来详细探讨这个问题,感兴趣的朋... 目录为什么需要IP正则表达式?IP地址的基本结构基础正则表达式写法精确匹配0-255的数字验证IP地

OpenCV图像形态学的实现

《OpenCV图像形态学的实现》本文主要介绍了OpenCV图像形态学的实现,包括腐蚀、膨胀、开运算、闭运算、梯度运算、顶帽运算和黑帽运算,文中通过示例代码介绍的非常详细,需要的朋友们下面随着小编来一起... 目录一、图像形态学简介二、腐蚀(Erosion)1. 原理2. OpenCV 实现三、膨胀China编程(

浅谈配置MMCV环境,解决报错,版本不匹配问题

《浅谈配置MMCV环境,解决报错,版本不匹配问题》:本文主要介绍浅谈配置MMCV环境,解决报错,版本不匹配问题,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录配置MMCV环境,解决报错,版本不匹配错误示例正确示例总结配置MMCV环境,解决报错,版本不匹配在col

详解nginx 中location和 proxy_pass的匹配规则

《详解nginx中location和proxy_pass的匹配规则》location是Nginx中用来匹配客户端请求URI的指令,决定如何处理特定路径的请求,它定义了请求的路由规则,后续的配置(如... 目录location 的作用语法示例:location /www.chinasem.cntestproxy

opencv图像处理之指纹验证的实现

《opencv图像处理之指纹验证的实现》本文主要介绍了opencv图像处理之指纹验证的实现,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学... 目录一、简介二、具体案例实现1. 图像显示函数2. 指纹验证函数3. 主函数4、运行结果三、总结一、

python+opencv处理颜色之将目标颜色转换实例代码

《python+opencv处理颜色之将目标颜色转换实例代码》OpenCV是一个的跨平台计算机视觉库,可以运行在Linux、Windows和MacOS操作系统上,:本文主要介绍python+ope... 目录下面是代码+ 效果 + 解释转HSV: 关于颜色总是要转HSV的掩膜再标注总结 目标:将红色的部分滤