#include "opencv2/highgui/highgui.hpp"
#include "opencv2/imgproc/imgproc.hpp"
#include<opencv2/core/core.hpp>
#include<opencv2/nonfree/nonfree.hpp>
#include<opencv2/features2d/features2d.hpp>
#include<opencv2/legacy/legacy.hpp>
#include <iostream>
#include <stdio.h>
#include <stdlib.h>
using namespace cv;
using namespace std;
int main( int argc, char** argv )
{
Mat srcImage=imread("testimage.jpg");
imshow("原始图",srcImage);
Mat grayImage;
cvtColor(srcImage,grayImage,CV_BGR2GRAY); //转换为灰度图
ORB detector;
ORB descriptor;
vector<KeyPoint>keypoints;
Mat descriptions;
detector.detect(grayImage,keypoints);
descriptor.compute(grayImage,keypoints,descriptions);
//提取出特征向量
VideoCapture cap(0);
unsigned int frameCount=0;
while(1)
{
double time0=static_cast<double>(getTickCount());//记录起始时间
Mat captureImage,captureImage_gray;//定义两个mat变量
cap>>captureImage;
if(captureImage.empty())
continue;
cvtColor(captureImage,captureImage_gray,CV_BGR2GRAY);
vector<KeyPoint>captureKeypoints;
Mat captureDscription;
detector.detect(captureImage_gray,captureKeypoints); //检测出关键点
descriptor.compute(captureImage_gray,captureKeypoints,captureDscription);//计算出特征向量
BruteForceMatcher<HammingLUT> matcher; //汉明距离匹配器
vector<DMatch> matches;
matcher.match(descriptions, captureDscription, matches); //进行匹配
double max_dist = 0; double min_dist = 100;
for( int i = 0; i < descriptions.rows; i++ )
{
double dist = matches[i].distance;
if( dist < min_dist ) min_dist = dist;
if( dist > max_dist ) max_dist = dist;
}
printf("-- Max dist : %f \n", max_dist );
printf("-- Min dist : %f \n", min_dist );
std::vector< DMatch > good_matches;
for( int i = 0; i < descriptions.rows; i++ ) //计算出最佳距离的点
{
if( matches[i].distance < 0.6*max_dist )
{
good_matches.push_back( matches[i]);
}
}
Mat img_matches;
drawMatches(srcImage, keypoints, captureImage, captureKeypoints, //绘制图像
good_matches, img_matches, Scalar::all(-1), Scalar::all(-1),
vector<char>(), DrawMatchesFlags::NOT_DRAW_SINGLE_POINTS);
imshow( "Match", img_matches);
cout<<getTickFrequency()/(getTickCount()-time0)<<endl; //统计帧率
waitKey(10);
}
waitKey(0);
return(0);
}
#include "opencv2/imgproc/imgproc.hpp"
#include<opencv2/core/core.hpp>
#include<opencv2/nonfree/nonfree.hpp>
#include<opencv2/features2d/features2d.hpp>
#include<opencv2/legacy/legacy.hpp>
#include <iostream>
#include <stdio.h>
#include <stdlib.h>
using namespace cv;
using namespace std;
int main( int argc, char** argv )
{
Mat srcImage=imread("testimage.jpg");
imshow("原始图",srcImage);
Mat grayImage;
cvtColor(srcImage,grayImage,CV_BGR2GRAY); //转换为灰度图
ORB detector;
ORB descriptor;
vector<KeyPoint>keypoints;
Mat descriptions;
detector.detect(grayImage,keypoints);
descriptor.compute(grayImage,keypoints,descriptions);
//提取出特征向量
VideoCapture cap(0);
unsigned int frameCount=0;
while(1)
{
double time0=static_cast<double>(getTickCount());//记录起始时间
Mat captureImage,captureImage_gray;//定义两个mat变量
cap>>captureImage;
if(captureImage.empty())
continue;
cvtColor(captureImage,captureImage_gray,CV_BGR2GRAY);
vector<KeyPoint>captureKeypoints;
Mat captureDscription;
detector.detect(captureImage_gray,captureKeypoints); //检测出关键点
descriptor.compute(captureImage_gray,captureKeypoints,captureDscription);//计算出特征向量
BruteForceMatcher<HammingLUT> matcher; //汉明距离匹配器
vector<DMatch> matches;
matcher.match(descriptions, captureDscription, matches); //进行匹配
double max_dist = 0; double min_dist = 100;
for( int i = 0; i < descriptions.rows; i++ )
{
double dist = matches[i].distance;
if( dist < min_dist ) min_dist = dist;
if( dist > max_dist ) max_dist = dist;
}
printf("-- Max dist : %f \n", max_dist );
printf("-- Min dist : %f \n", min_dist );
std::vector< DMatch > good_matches;
for( int i = 0; i < descriptions.rows; i++ ) //计算出最佳距离的点
{
if( matches[i].distance < 0.6*max_dist )
{
good_matches.push_back( matches[i]);
}
}
Mat img_matches;
drawMatches(srcImage, keypoints, captureImage, captureKeypoints, //绘制图像
good_matches, img_matches, Scalar::all(-1), Scalar::all(-1),
vector<char>(), DrawMatchesFlags::NOT_DRAW_SINGLE_POINTS);
imshow( "Match", img_matches);
cout<<getTickFrequency()/(getTickCount()-time0)<<endl; //统计帧率
waitKey(10);
}
waitKey(0);
return(0);
}