您的位置:首页 > 运维架构

opencv--特征匹配

2014-12-09 10:54 148 查看
#include "stdafx.h"
#include <stdio.h>
#include <iostream>
#include <opencv2/opencv.hpp>
#include <opencv2\features2d\features2d.hpp>
#include <opencv2\nonfree\nonfree.hpp>
#include <opencv2\legacy\legacy.hpp>
#include <opencv2/core/core.hpp>//因为在属性中已经配置了opencv等目录,所以把其当成了本地目录一样
#include <opencv2/features2d/features2d.hpp>
#include <opencv2/highgui/highgui.hpp>

using namespace cv;
using namespace std;

void readme();

int main(int argc,char* argv[])
{
Mat img_1=imread("C:\\Users\\Bite07\\Desktop\\1.3.jpg",CV_LOAD_IMAGE_GRAYSCALE);//宏定义时CV_LOAD_IMAGE_GRAYSCALE=0,也就是读取灰度图像
Mat img_2=imread("C:\\Users\\Bite07\\Desktop\\4.jpg",CV_LOAD_IMAGE_GRAYSCALE);//一定要记得这里路径的斜线方向,这与Matlab里面是相反的

if(!img_1.data || !img_2.data)//如果数据为空
{
cout<<"opencv error"<<endl;
return -1;
}
cout<<"open right"<<endl;

//第一步,用SIFT算子检测关键点
SiftFeatureDetector detector;//构造函数采用内部默认的
std::vector<KeyPoint> keypoints_1,keypoints_2;//构造2个专门由点组成的点向量用来存储特征点

detector.detect(img_1,keypoints_1);//将img_1图像中检测到的特征点存储起来放在keypoints_1中
detector.detect(img_2,keypoints_2);//同理

//在图像中画出特征点
Mat img_keypoints_1,img_keypoints_2;

drawKeypoints(img_1,keypoints_1,img_keypoints_1,Scalar::all(-1),DrawMatchesFlags::DEFAULT);//在内存中画出特征点
drawKeypoints(img_2,keypoints_2,img_keypoints_2,Scalar::all(-1),DrawMatchesFlags::DEFAULT);

imshow("sift_keypoints_1",img_keypoints_1);//显示特征点
imshow("sift_keypoints_2",img_keypoints_2);

//计算特征向量
SiftDescriptorExtractor extractor;//定义描述子对象

Mat descriptors_1,descriptors_2;//存放特征向量的矩阵

extractor.compute(img_1,keypoints_1,descriptors_1);//计算特征向量
extractor.compute(img_2,keypoints_2,descriptors_2);

//用burte force进行匹配特征向量
BruteForceMatcher<L2<float>>matcher;//定义一个burte force matcher对象
vector<DMatch>matches;
matcher.match(descriptors_1,descriptors_2,matches);

//绘制匹配线段
Mat img_matches;
drawMatches(img_1,keypoints_1,img_2,keypoints_2,matches,img_matches);//将匹配出来的结果放入内存img_matches中

//显示匹配线段
imshow("sift_Matches",img_matches);//显示的标题为Matches
waitKey(0);
return 0;
}


参考至:/article/4670182.html
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: