#include <stdio.h>
#include "opencv2/core/core.hpp"
#include "opencv2/features2d/features2d.hpp"
#include "opencv2/highgui/highgui.hpp"
#include "opencv2/nonfree/nonfree.hpp"
#include <opencv2/calib3d/calib3d.hpp>
#include <iostream>
using namespace std;
using namespace cv;
static void help()
{
printf("\nThis program demonstrates using features2d detector, descriptor extractor and simple matcher\n"
"Using the SURF desriptor:\n"
"\n"
"Usage:\n matcher_simple <image1> <image2>\n");
}
int main(int argc, char** argv)
{
//if(argc != 3)
//{
// help();
// return -1;
//}
Mat img1 = imread("111.jpg", CV_LOAD_IMAGE_GRAYSCALE);
Mat img2 = imread("222.jpg", CV_LOAD_IMAGE_GRAYSCALE);
if(img1.empty() || img2.empty())
{
printf("Can't read one of the images\n");
return -1;
}
// detecting keypoints
SurfFeatureDetector detector(1000);
vector<KeyPoint> keypoints1, keypoints2;
detector.detect(img1, keypoints1);
detector.detect(img2, keypoints2);
// computing descriptors
SurfDescriptorExtractor extractor;
Mat descriptors1, descriptors2;
extractor.compute(img1, keypoints1, descriptors1);
extractor.compute(img2, keypoints2, descriptors2);
// matching descriptors
BFMatcher matcher(NORM_L2);
vector<DMatch> matches;
matcher.match(descriptors1, descriptors2, matches);
// drawing the results
namedWindow("matches", 1);
Mat img_matches0,img_matches1;
drawMatches(img1, keypoints1, img2, keypoints2, matches, img_matches0);
//for(int i=0;i<matches.size();i++)
//{
// //if(abs((keypoints1[matches[i].queryIdx].pt.y-keypoints2[matches[i].trainIdx].pt.y)>5)
// //{
// //}
// //cout<<keypoints1[matches[i].queryIdx].pt.x<<endl;
// Point2f pt1,pt2;
// pt1=keypoints1[matches[i].queryIdx].pt;
// pt2=keypoints2[matches[i].trainIdx].pt;
// if(abs(pt1.y-pt2.y)>5)
// {
// matches[i].distance=100;
// }
//}
// drawMatches(img1, keypoints1, img2, keypoints2, matches, img_matches1);
// 分配空间
int ptCount = (int)matches.size();
Mat p1(ptCount, 2, CV_32F);
Mat p2(ptCount, 2, CV_32F);
// 把Keypoint转换为Mat
Point2f pt;
for (int i=0; i<ptCount; i++)
{
pt = keypoints1[matches[i].queryIdx].pt;
p1.at<float>(i, 0) = pt.x;
p1.at<float>(i, 1) = pt.y;
pt = keypoints2[matches[i].trainIdx].pt;
p2.at<float>(i, 0) = pt.x;
p2.at<float>(i, 1) = pt.y;
}
// 用RANSAC方法计算F
// Mat m_Fundamental;
// 上面这个变量是基本矩阵
vector<uchar> m_RANSACStatus;
// 上面这个变量已经定义过,用于存储RANSAC后每个点的状态
//m_Fundamental = findFundamentalMat(p1, p2, m_RANSACStatus, FM_RANSAC);
Mat m_Fundamental=findFundamentalMat(p1, p2, m_RANSACStatus, FM_RANSAC);
cout<<m_Fundamental<<endl;
// 计算野点个数
int OutlinerCount = 0;
for (int i=0; i<ptCount; i++)
{
if (m_RANSACStatus[i] == 0) // 状态为0表示野点
{
OutlinerCount++;
}
}
// 计算内点
vector<Point2f> m_LeftInlier;
vector<Point2f> m_RightInlier;
vector<DMatch> m_InlierMatches;
// 上面三个变量用于保存内点和匹配关系
int InlinerCount = ptCount - OutlinerCount;
m_InlierMatches.resize(InlinerCount);
m_LeftInlier.resize(InlinerCount);
m_RightInlier.resize(InlinerCount);
InlinerCount = 0;
for (int i=0; i<ptCount; i++)
{
if (m_RANSACStatus[i] != 0)
{
m_LeftInlier[InlinerCount].x = p1.at<float>(i, 0);
m_LeftInlier[InlinerCount].y = p1.at<float>(i, 1);
m_RightInlier[InlinerCount].x = p2.at<float>(i, 0);
m_RightInlier[InlinerCount].y = p2.at<float>(i, 1);
m_InlierMatches[InlinerCount].queryIdx = InlinerCount;
m_InlierMatches[InlinerCount].trainIdx = InlinerCount;
InlinerCount++;
}
}
// 把内点转换为drawMatches可以使用的格式
vector<KeyPoint> key1(InlinerCount);
vector<KeyPoint> key2(InlinerCount);
KeyPoint::convert(m_LeftInlier, key1);
KeyPoint::convert(m_RightInlier, key2);
for(int i=0;i<10;i++)
cout<<key1[i].pt<<" "<<key2[i].pt<<" "<<key1[i].pt.x-key2[i].pt.x<<endl;
drawMatches(img1, key1, img2, key2, m_InlierMatches, img_matches1);
imshow("matches0", img_matches0);
imshow("matches1", img_matches1);
Mat test(img1.rows,img1.cols,CV_32FC1);
float a=0.0105591872,b=0.1024243227,c=10.106085341;
for(int i=0;i<test.rows;i++)
{
float* pt=test.ptr<float>(i);
for(int j=0;j<test.cols;j++)
{
float val=j*a+i*b+c;
pt[j]=val;
}
}
//cout<<test<<endl;
cv::normalize(test,test,0,1,CV_MINMAX);
cv::convertScaleAbs(test,test,255);
test.convertTo(test,CV_8UC1);
imshow("test",test);
waitKey(0);
return 0;
}
没有合适的资源?快使用搜索试试~ 我知道了~
资源推荐
资源详情
资源评论
收起资源包目录
jixianyuesu.rar (48个子文件)
jixianyuesu
ipch
jixianyuesu-28353e67
jixianyuesu-233a92aa.ipch 42.88MB
Debug
jixianyuesu.exe 120KB
jixianyuesu.ilk 716KB
jixianyuesu.pdb 1.17MB
jixianyuesu
3.png 8KB
11.jpg 58KB
jixianyuesu.cpp 5KB
jixianyuesu.vcxproj.user 143B
111.jpg 70KB
Debug
jixianyuesu.obj 464KB
CL.write.1.tlog 628B
vc100.idb 363KB
mt.read.1.tlog 264B
jixianyuesu.write.1.tlog 0B
link-cvtres.write.1.tlog 2B
jixianyuesu.Build.CppClean.log 3KB
link.7180.read.1.tlog 2B
rc.command.1.tlog 744B
link.command.1.tlog 4KB
vc100.pdb 588KB
CL.read.1.tlog 16KB
link.7180-cvtres.write.1.tlog 2B
mt.write.1.tlog 510B
link-cvtres.read.1.tlog 2B
jixianyuesu.exe.embed.manifest.res 472B
rc.read.1.tlog 482B
link.7180.write.1.tlog 2B
mt.command.1.tlog 496B
link.write.1.tlog 1KB
jixianyuesu.exe.embed.manifest 406B
cl.command.1.tlog 854B
link.7180-cvtres.read.1.tlog 2B
jixianyuesu.lastbuildstate 100B
rc.write.1.tlog 490B
jixianyuesu.log 6KB
jixianyuesu_manifest.rc 212B
jixianyuesu.exe.intermediate.manifest 381B
link.read.1.tlog 10KB
jixianyuesu.vcxproj 4KB
1.jpg 75KB
4.png 8KB
222.jpg 95KB
22.jpg 74KB
2.jpg 81KB
jixianyuesu.vcxproj.filters 949B
jixianyuesu.suo 11KB
jixianyuesu.sln 900B
jixianyuesu.sdf 11.21MB
共 48 条
- 1
资源评论
- 开心大爆炸2021-09-24一般,作用不大
- 小黑战记2020-04-28作用不大。慎重
- 半盏茶功夫2018-11-27参考一下,学习
刘大脸
- 粉丝: 1
- 资源: 2
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功