opencv-之SIFT匹配点对坐标的查找

该博客介绍了如何利用OpenCV库进行SIFT特征检测、关键点绘制、特征描述符计算以及BFMatch暴力匹配。通过读取两张图片并调整尺寸,文章详细展示了如何提取和匹配SIFT特征点,最终展示匹配结果并输出关键点坐标。
摘要由CSDN通过智能技术生成

这一篇是接上篇的文章接着往下写的,这里着重介绍点的输出,其他的地方不做过多的赘述

#include "opencv2/opencv.hpp"
#include <opencv2/nonfree/nonfree.hpp>//SIFT
#include <opencv2/legacy/legacy.hpp>//BFMatch暴力匹配
#include <vector>
#include<iostream>
using namespace std;
using namespace cv;

void main()
{     
    Mat srcImg1 = imread("D:\\0.jpg");
    Mat srcImg2 = imread("D:\\1.jpg");
	resize(srcImg1,srcImg1,Size(srcImg1.cols*3,srcImg1.rows*3));
	resize(srcImg2,srcImg2,Size(srcImg2.cols/3,srcImg2.rows/3));
    //定义SIFT特征检测类对象
    SiftFeatureDetector siftDetector(1000);
    //定义KeyPoint变量
    vector<KeyPoint>keyPoints1;
    vector<KeyPoint>keyPoints2;
    //特征点检测
    siftDetector.detect(srcImg1, keyPoints1);
    siftDetector.detect(srcImg2, keyPoints2);
    //绘制特征点(关键点)
    Mat feature_pic1, feature_pic2;
    drawKeypoints(srcImg1, keyPoints1, feature_pic1, Scalar::all(-1));
    drawKeypoints(srcImg2, keyPoints2, feature_pic2, Scalar::all(-1));
    //显示原图
    imshow("src1", srcImg1);
    imshow("src2", srcImg2);
    //显示结果
    imshow("feature1", feature_pic1);
    imshow("feature2", feature_pic2);

    //计算特征点描述符 / 特征向量提取
    SiftDescriptorExtractor descriptor;
    Mat description1;
    descriptor.compute(srcImg1, keyPoints1, description1);
    Mat description2;
    descriptor.compute(srcImg2, keyPoints2, description2);
    cout<<description1.cols<<endl;
    cout<<description1.rows<<endl;

    //进行BFMatch暴力匹配
    BruteForceMatcher<L1<float>>matcher;    //实例化暴力匹配器
    vector<DMatch>matches;   //定义匹配结果变量
    matcher.match(description1, description2, matches);  //实现描述符之间的匹配

    //匹配结果筛选
    nth_element(matches.begin(), matches.begin()+0, matches.end());   //提取出前x个最佳匹配结果     
    matches.erase(matches.begin()+1, matches.end());    //剔除掉其余的匹配结果
	int index1,index2;
	for(size_t i=0;i<matches.size();i++)//这里是对筛选后坐标对进行输出
	{
		index1=matches.at(i).queryIdx;//这个是访问特征图片的属性。具体是啥我查看源码也没太搞明白
		index2=matches.at(i).trainIdx;//这个是访问待匹配图片的属性
		cout<<"这是个啥:"<<keyPoints2.at(index2).pt<<endl;//我这里是输出待匹配特征图片的坐标,keyPoints2这个是特征向量提取后的集合
		cout<<"这是个啥:"<<keyPoints2.at(index2).pt.x<<endl//这里是输出待匹配特征图片的x点
		cout<<"这是个啥:"<<keyPoints2.at(index2).pt.y<<endl//这里是输出待匹配特征图片的y点
	}
    Mat result;
    drawMatches(srcImg1, keyPoints1, srcImg2, keyPoints2, matches, result,  Scalar(0, 255, 0), Scalar::all(-1));//匹配特征点绿色,单一特征点颜色随机
    imshow("Match_Result", result);
    waitKey(0);
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值