两张图片sift,surf,orb,lbp对比

#include<opencv2\opencv.hpp>
#include<iostream>
#include<opencv2/highgui/highgui.hpp>  




#include <iostream>
#include <fstream>
#include <cmath>
#include "opencv2/core/core.hpp"  
#include "opencv2/features2d/features2d.hpp"  
#include "opencv2/highgui/highgui.hpp"  
#include "opencv2/legacy/legacy.hpp"  
#include "opencv2/calib3d/calib3d.hpp" 
#include "opencv2/nonfree/nonfree.hpp"  
#include <opencv2/imgproc/imgproc.hpp> 
#include <opencv2/features2d/features2d.hpp>
#include <io.h>  
#include <stdio.h>    
#include <vector>  
#include <Windows.h>  
#include <fstream>    
#include <iterator>  
#include <string>


#include <opencv2/opencv.hpp>
using namespace std;
using namespace cv;








float surfMatch(Mat img1, Mat img2)
{


SURF surf;
vector<KeyPoint> keyPoints_1, keyPoints_2;
Mat descriptors_1, descriptors_2;
surf(img1, Mat(), keyPoints_1);
surf(img2, Mat(), keyPoints_2);
SurfDescriptorExtractor extrator;
extrator.compute(img1, keyPoints_1, descriptors_1);
extrator.compute(img2, keyPoints_2, descriptors_2);
BruteForceMatcher<L2<float> >  matcher;
//cout << "图像1特征描述矩阵大小:" << descriptors_1.size()<< ",特征向量个数:" << descriptors_1.rows << ",维数:" << descriptors_1.cols << endl;
//cout << "图像2特征描述矩阵大小:" << descriptors_2.size()<< ",特征向量个数:" << descriptors_2.rows << ",维数:" << descriptors_2.cols << endl;




vector<DMatch> matches, goodMatches;
matcher.match(descriptors_1, descriptors_2, matches);
//cout << "Match个数:" << matches.size() << endl;


double max_dst = 0;
double min_dst = 100;
for (int i = 0; i < matches.size(); ++i)
{
double dst = matches[i].distance;
if (dst < min_dst) min_dst = dst;
if (dst > max_dst) max_dst = dst;
}


//cout << "最大距离:" << max_dst << endl;
//cout << "最小距离:" << min_dst << endl;


//筛选出较好的匹配点??
for (int i = 0; i < matches.size(); ++i)
{
if (matches[i].distance<0.31*max_dst)
{
goodMatches.push_back(matches[i]);
}
}
//cout << "goodMatch个数:" << goodMatches.size() << endl;
float res = (float)goodMatches.size() / (float)(descriptors_1.rows);


return res;
}




float ORBMatch(Mat img1, Mat img2)
{
//Mat img1 = imread("box.png");
//Mat img2 = imread("box_in_scene.png");
ORB orb;
vector<KeyPoint> keyPoints_1, keyPoints_2;
Mat descriptors_1, descriptors_2;
orb(img1, Mat(), keyPoints_1, descriptors_1);
orb(img2, Mat(), keyPoints_2, descriptors_2);
BruteForceMatcher<HammingLUT> matcher;


//cout << "图像1特征描述矩阵大小:" << descriptors_1.size()<< ",特征向量个数:" << descriptors_1.rows << ",维数:" << descriptors_1.cols << endl;
//cout << "图像2特征描述矩阵大小:" << descriptors_2.size()<< ",特征向量个数:" << descriptors_2.rows << ",维数:" << descriptors_2.cols << endl;




vector<DMatch> matches, goodMatches;//匹配结果??
matcher.match(descriptors_1, descriptors_2, matches);


//计算匹配结果中距离的最大和最小值??
//距离是指两个特征向量间的欧式距离,表明两个特征的差异,值越小表明两个特征点越接近??
double max_dst = 0;
double min_dst = 100;
for (int i = 0; i < matches.size(); ++i)
{
double dst = matches[i].distance;
if (dst < min_dst) min_dst = dst;
if (dst > max_dst) max_dst = dst;
}


//cout << "最大距离:" << max_dst << endl;
//cout << "最小距离:" << min_dst << endl;


//筛选出较好的匹配点??
for (int i = 0; i < matches.size(); ++i)
{
if (matches[i].distance<0.31*max_dst)
{
goodMatches.push_back(matches[i]);
}
}
//cout << "goodMatch个数:" << goodMatches.size() << endl;
float res = (float)goodMatches.size() / (float)(descriptors_1.rows);
return res;
}




float siftMatch(Mat img1, Mat img2)
{


SIFT sift;
vector<KeyPoint> keyPoints_1, keyPoints_2;
Mat descriptors_1, descriptors_2;
sift(img1, Mat(), keyPoints_1, descriptors_1);
sift(img2, Mat(), keyPoints_2, descriptors_2);
BruteForceMatcher<L2<float> >  matcher;
//cout << "图像1特征描述矩阵大小:" << descriptors_1.size()<< ",特征向量个数:" << descriptors_1.rows << ",维数:" << descriptors_1.cols << endl;
//cout << "图像2特征描述矩阵大小:" << descriptors_2.size()<< ",特征向量个数:" << descriptors_2.rows << ",维数:" << descriptors_2.cols << endl;




vector<DMatch> matches, goodMatches;
matcher.match(descriptors_1, descriptors_2, matches);
//cout << "Match个数:" << matches.size() << endl;


double max_dst = 0;
double min_dst = 100;
for (int i = 0; i < matches.size(); ++i)
{
double dst = matches[i].distance;
if (dst < min_dst) min_dst = dst;
if (dst > max_dst) max_dst = dst;
}


//cout << "最大距离:" << max_dst << endl;
//cout << "最小距离:" << min_dst << endl;


for (int i = 0; i < matches.size(); ++i)
{
if (matches[i].distance<0.31*max_dst)
{
goodMatches.push_back(matches[i]);
}
}
//cout << "goodMatch个数:" << goodMatches.size() << endl;
float res = (float)goodMatches.size() / (float)(descriptors_1.rows);


return res;
}




vector<Mat> read_images_in_folder(cv::String pattern)
{
vector<cv::String> fn;
glob(pattern, fn, false);


vector<Mat> images;
size_t count = fn.size(); //number of png files in images folder
for (size_t i = 0; i < count; i++)
{
images.push_back(imread(fn[i]));
//imshow("img", imread(fn[i]));
//waitKey(1000);
}
return images;
}




Mat LBP(Mat img)
{
Mat result;
result.create(img.rows - 2, img.cols - 2, img.type());


result.setTo(0);


for (int i = 1; i<img.rows - 1; i++)
{
for (int j = 1; j<img.cols - 1; j++)
{
uchar center = img.at<uchar>(i, j);
uchar code = 0;
code |= (img.at<uchar>(i - 1, j - 1) >= center) << 7;
code |= (img.at<uchar>(i - 1, j) >= center) << 6;
code |= (img.at<uchar>(i - 1, j + 1) >= center) << 5;
code |= (img.at<uchar>(i, j + 1) >= center) << 4;
code |= (img.at<uchar>(i + 1, j + 1) >= center) << 3;
code |= (img.at<uchar>(i + 1, j) >= center) << 2;
code |= (img.at<uchar>(i + 1, j - 1) >= center) << 1;
code |= (img.at<uchar>(i, j - 1) >= center) << 0;
result.at<uchar>(i - 1, j - 1) = code;
}
}
return result;
}




float cosinesimilar(Mat descriptors1_mat, Mat descriptors2_mat)
{


Mat temp = descriptors1_mat.clone();
float sum_up = temp.dot(descriptors2_mat);
float sum_down1 = sqrt(descriptors1_mat.dot(descriptors1_mat));
float sum_down2 = sqrt(descriptors2_mat.dot(descriptors2_mat));
float cosine = sum_up / (sum_down1*sum_down2);
descriptors1_mat.release();
descriptors2_mat.release();
temp.release();
return cosine;
}




int main()
{
cv::String pattern = "C:/Users/prismc/Desktop/1/*.jpg";
cv::String pattern2 = "C:/Users/prismc/Desktop/2/*.jpg";


vector<Mat> images = read_images_in_folder(pattern);
vector<Mat> images2 = read_images_in_folder(pattern2);


for (int i = 0; i < images.size(); i++)
{
Mat img1 = images[i];
float scoresift = 0;
float scoreORB = 0;
float scoresurf = 0;
for (int j = 0; j < images2.size(); j++)
{
Mat img2 = images2[i];
scoresift = siftMatch(img1, img2);
scoreORB = ORBMatch(img1, img2);
scoresurf = surfMatch(img1, img2);
cout << "scoresift" << scoresift <<"     ";
cout << "scoreORB" << scoreORB << "     ";
cout << "scoresurf"  << scoresurf<< "     ";
}
cout << endl;
}


for (int i = 0; i < images.size(); i++)
{
Mat img1 = LBP(images[i]);
Mat img1re;
resize(img1, img1re, Size(200, 300), 0, 0, CV_INTER_LINEAR);
float scorecos = 0;
for (int j = 0; j < images2.size(); j++)
{
Mat img2 = LBP(images2[i]);
Mat img2re;
resize(img2, img2re, Size(200, 300), 0, 0, CV_INTER_LINEAR);
scorecos = cosinesimilar(img1re, img2re);


cout << "scorecos" << "     " << scorecos;


}
}




//Mat src1 = imread("C:/Users/prismc/Desktop/1/1.jpg", 0);
//Mat src1re, src2re;
//resize(src1, src1re, Size(200, 300), 0, 0, CV_INTER_LINEAR);


//Mat src2 = imread("C:/Users/prismc/Desktop/2/1.jpg", 0);
//resize(src2, src2re, Size(200, 300), 0, 0, CV_INTER_LINEAR);
//Mat img1 = LBP(src1re);
//Mat img2 = LBP(src2re);


//imshow("原始图片", src1re);
//imshow("原始图片", src2re);
//imshow("原始LBP", img1);
//imshow("原始LBP", img2);
//waitKey(0);
//
//float scorecos1 = cosinesimilar(src1re, src2re);
//float scorecos2 = cosinesimilar(img1, img2);
//cout << "scorecos1" << "     " << scorecos1;
//cout << "scorecos2" << "     " << scorecos2;


return 0;
}









  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值