// opencv2413_test.cpp : 定义控制台应用程序的入口点。
//
#include "stdafx.h"
#include <opencv2\opencv.hpp>
#include <iostream>
#include <string>
#include<opencv2\core\core.hpp>
#include<opencv2\features2d\features2d.hpp>
#include<opencv2\nonfree\nonfree.hpp>
using namespace cv;
using namespace std;
int main()
{
system("color 2F");
Mat srcImage1 = imread("1.jpg",-1);
Mat srcImage2 = imread("2.jpg",-1);
imshow("src1", srcImage1);
imshow("src2", srcImage2);
//定义SURF中的hessian阈值特征点检测算子
int minHessian = 400;
//定义一个SurfFeatureDetector特征检测类对象
SurfFeatureDetector detector(minHessian);
//定义模板类,vector是可以存放任意类型的动态数组,能够增加和压缩数据
vector<KeyPoint> keypoints_1, keypoints_2;
//detect函数检测出surf特征关键点,保存在vector容器中
detector.detect(srcImage1, keypoints_1);
detector.detect(srcImage2, keypoints_2);
//绘制特征关键点
Mat img_keypoints_1;
Mat img_keypoints_2;
drawKeypoints(srcImage1, keypoints_1, img_keypoints_1,
Scalar::all(-1), DrawMatchesFlags::DRAW_RICH_KEYPOINTS);
drawKeypoints(srcImage2, keypoints_2, img_keypoints_2,
Scalar::all(-1), DrawMatchesFlags::DRAW_RICH_KEYPOINTS);
imshow("特征点检测图1", img_keypoints_1);
imshow("特征点检测图2", img_keypoints_2);
waitKey();
return 0;
}
特征提取匹配
// opencv2413_test.cpp : 定义控制台应用程序的入口点。
//
#include "stdafx.h"
#include <opencv2\opencv.hpp>
#include <iostream>
#include <string>
#include<opencv2\core\core.hpp>
#include<opencv2\features2d\features2d.hpp>
#include<opencv2\nonfree\nonfree.hpp>
#include<opencv2\legacy\legacy.hpp>
using namespace cv;
using namespace std;
int main()
{
system("color 2F");
Mat srcImage1 = imread("1.jpg",-1);
Mat srcImage2 = imread("2.jpg",-1);
imshow("src1", srcImage1);
imshow("src2", srcImage2);
//定义SURF中的hessian阈值特征点检测算子
int minHessian = 400;
//定义一个SurfFeatureDetector特征检测类对象
SurfFeatureDetector detector(minHessian);
//定义模板类,vector是可以存放任意类型的动态数组,能够增加和压缩数据
vector<KeyPoint> keypoints_1, keypoints_2;
//detect函数检测出surf特征关键点,保存在vector容器中
detector.detect(srcImage1, keypoints_1);
detector.detect(srcImage2, keypoints_2);
//绘制特征关键点
Mat img_keypoints_1;
Mat img_keypoints_2;
drawKeypoints(srcImage1, keypoints_1, img_keypoints_1,
Scalar::all(-1), DrawMatchesFlags::DRAW_RICH_KEYPOINTS);
drawKeypoints(srcImage2, keypoints_2, img_keypoints_2,
Scalar::all(-1), DrawMatchesFlags::DRAW_RICH_KEYPOINTS);
imshow("特征点检测图1", img_keypoints_1);
imshow("特征点检测图2", img_keypoints_2);
/*
进行SURF的特征提取
*/
//计算描述符(特征向量)
SurfDescriptorExtractor extractor;
Mat descriptors1, descriptors2;
extractor.compute(srcImage1, keypoints_1, descriptors1);
extractor.compute(srcImage2, keypoints_2, descriptors2);
//实例化一个匹配器
BruteForceMatcher<L2<float>> matcher;
vector<DMatch> matches;
matcher.match(descriptors1, descriptors2, matches);
//绘制从两个图像中匹配出的关键点
Mat imgMatches;
drawMatches(srcImage1, keypoints_1, srcImage2, keypoints_2, matches, imgMatches);
imshow("匹配图", imgMatches);
waitKey();
return 0;
}
使用Flann进行关键点的描述和匹配
// opencv2413_test.cpp : 定义控制台应用程序的入口点。
//
#include "stdafx.h"
#include <opencv2\opencv.hpp>
#include <iostream>
#include <string>
#include<opencv2\core\core.hpp>
#include<opencv2\features2d\features2d.hpp>
#include<opencv2\nonfree\nonfree.hpp>
#include<opencv2\legacy\legacy.hpp>
using namespace cv;
using namespace std;
int main()
{
system("color 2F");
Mat srcImage1 = imread("1.jpg",-1);
Mat srcImage2 = imread("2.jpg",-1);
imshow("src1", srcImage1);
imshow("src2", srcImage2);
//定义SURF中的hessian阈值特征点检测算子
int minHessian = 400;
//定义一个SurfFeatureDetector特征检测类对象
SurfFeatureDetector detector(minHessian);
//定义模板类,vector是可以存放任意类型的动态数组,能够增加和压缩数据
vector<KeyPoint> keypoints_1, keypoints_2;
//detect函数检测出surf特征关键点,保存在vector容器中
detector.detect(srcImage1, keypoints_1);
detector.detect(srcImage2, keypoints_2);
//绘制特征关键点
Mat img_keypoints_1;
Mat img_keypoints_2;
drawKeypoints(srcImage1, keypoints_1, img_keypoints_1,
Scalar::all(-1), DrawMatchesFlags::DRAW_RICH_KEYPOINTS);
drawKeypoints(srcImage2, keypoints_2, img_keypoints_2,
Scalar::all(-1), DrawMatchesFlags::DRAW_RICH_KEYPOINTS);
imshow("特征点检测图1", img_keypoints_1);
imshow("特征点检测图2", img_keypoints_2);
/*
进行SURF的特征提取
*/
//计算描述符(特征向量)
SurfDescriptorExtractor extractor;
Mat descriptors1, descriptors2;
extractor.compute(srcImage1, keypoints_1, descriptors1);
extractor.compute(srcImage2, keypoints_2, descriptors2);
/*
//实例化一个匹配器
BruteForceMatcher<L2<float>> matcher;
vector<DMatch> matches;
matcher.match(descriptors1, descriptors2, matches);
//绘制从两个图像中匹配出的关键点
Mat imgMatches;
drawMatches(srcImage1, keypoints_1, srcImage2, keypoints_2, matches, imgMatches);
imshow("SURF--匹配图", imgMatches);
*/
/*
使用Flann进行特征点匹配
*/
FlannBasedMatcher matcher;
vector<DMatch> matches2;
matcher.match(descriptors1, descriptors2, matches2);
double max_dist = 0;
double min_dist = 100;
//快速计算关键点之间的最大和最小距离
for (int i = 0; i < descriptors1.rows; i++) {
double dist = matches2[i].distance;
if (dist < min_dist)min_dist = dist;
if (dist > max_dist)max_dist = dist;
}
//输出距离信息
cout << "最大距离(Max Dist):" << "\n" << max_dist;
cout << "最小距离(Min Dist):" << "\n" << min_dist;
//存下所有符合条件的匹配结果,距离小于2*min_dist
vector<DMatch> good_matches;
for (int i = 0; i < descriptors1.rows; i++) {
if (matches2[i].distance < 2 * min_dist) {
good_matches.push_back(matches2[i]);
}
}
//绘制符合条件的匹配点
Mat img_matches;
drawMatches(srcImage1, keypoints_1, srcImage2, keypoints_2, good_matches,
img_matches, Scalar::all(-1), Scalar::all(-1), vector<char>(), DrawMatchesFlags::
NOT_DRAW_SINGLE_POINTS);
//输出相关匹配点信息
for (int i = 0; i < good_matches.size(); i++) {
printf(">符合条件的匹配点[%d] 特征点1:%d -- 特征点2:%d\n", i,
good_matches[i].queryIdx, good_matches[i].trainIdx);
}
imshow("Flann--匹配效果图", img_matches);
waitKey();
return 0;
}
基于摄像头捕获图片进行特征匹配
// opencv2413_test.cpp : 定义控制台应用程序的入口点。
//
#include "stdafx.h"
#include <opencv2\opencv.hpp>
#include <iostream>
#include <string>
#include<opencv2\core\core.hpp>
#include<opencv2\features2d\features2d.hpp>
#include<opencv2\nonfree\nonfree.hpp>
#include<opencv2\legacy\legacy.hpp>
using namespace cv;
using namespace std;
int main()
{
system("color 2F");
//【0】改变console字体颜色
//【1】载入图像、显示并转化为灰度图
Mat trainImage = imread("3.jpg"), trainImage_gray;
imshow("原始图", trainImage);
cvtColor(trainImage, trainImage_gray, CV_BGR2GRAY);
//【2】检测Surf关键点、提取训练图像描述符
vector<KeyPoint> train_keyPoint;
Mat trainDescriptor;
SurfFeatureDetector featureDetector(80);
featureDetector.detect(trainImage_gray, train_keyPoint);
SurfDescriptorExtractor featureExtractor;
featureExtractor.compute(trainImage_gray, train_keyPoint, trainDescriptor);
//【3】创建基于FLANN的描述符匹配对象
FlannBasedMatcher matcher;
vector<Mat> train_desc_collection(1, trainDescriptor);
matcher.add(train_desc_collection);
matcher.train();
//【4】创建视频对象、定义帧率
VideoCapture cap(0);
unsigned int frameCount = 0;//帧数
//【5】不断循环,直到q键被按下
while (char(waitKey(1)) != 'q')
{
//<1>参数设置
int64 time0 = getTickCount();
Mat testImage, testImage_gray;
cap >> testImage;//采集视频到testImage中
if (testImage.empty())
continue;
//<2>转化图像到灰度
cvtColor(testImage, testImage_gray, CV_BGR2GRAY);
//<3>检测S关键点、提取测试图像描述符
vector<KeyPoint> test_keyPoint;
Mat testDescriptor;
featureDetector.detect(testImage_gray, test_keyPoint);
featureExtractor.compute(testImage_gray, test_keyPoint, testDescriptor);
//<4>匹配训练和测试描述符
vector<vector<DMatch> > matches;
matcher.knnMatch(testDescriptor, matches, 2);
// <5>根据劳氏算法(Lowe's algorithm),得到优秀的匹配点
vector<DMatch> goodMatches;
for (unsigned int i = 0; i < matches.size(); i++)
{
if (matches[i][0].distance < 0.6 * matches[i][1].distance)
goodMatches.push_back(matches[i][0]);
}
//<6>绘制匹配点并显示窗口
Mat dstImage;
drawMatches(testImage, test_keyPoint, trainImage, train_keyPoint, goodMatches, dstImage);
imshow("匹配窗口", dstImage);
//<7>输出帧率信息
cout << "当前帧率为:" << getTickFrequency() / (getTickCount() - time0) << endl;
}
waitKey();
return 0;
}