引入极线约束的surf特征匹配

[cpp]  view plain  copy
  1. #include <stdio.h>  
  2. #include "opencv2/core/core.hpp"  
  3. #include "opencv2/features2d/features2d.hpp"  
  4. #include "opencv2/highgui/highgui.hpp"  
  5. #include "opencv2/nonfree/nonfree.hpp"  
  6. #include <opencv2/calib3d/calib3d.hpp>  
  7. #include <iostream>  
  8. using namespace std;  
  9.   
  10. using namespace cv;  
  11.   
  12. static void help()  
  13. {  
  14.     printf("\nThis program demonstrates using features2d detector, descriptor extractor and simple matcher\n"  
  15.             "Using the SURF desriptor:\n"  
  16.             "\n"  
  17.             "Usage:\n matcher_simple <image1> <image2>\n");  
  18. }  
  19.   
  20. int main(int argc, char** argv)  
  21. {  
  22.     //if(argc != 3)  
  23.     //{  
  24.     //    help();  
  25.     //    return -1;  
  26.     //}  
  27.   
  28.     Mat img1 = imread("D:\\faceData\\stereopairs\\cones\\imL.jpg", CV_LOAD_IMAGE_GRAYSCALE);  
  29.     Mat img2 = imread("D:\\faceData\\stereopairs\\cones\\imR.jpg", CV_LOAD_IMAGE_GRAYSCALE);  
  30.     if(img1.empty() || img2.empty())  
  31.     {  
  32.         printf("Can't read one of the images\n");  
  33.         return -1;  
  34.     }  
  35.   
  36.     // detecting keypoints  
  37.     SurfFeatureDetector detector(400);  
  38.     vector<KeyPoint> keypoints1, keypoints2;  
  39.     detector.detect(img1, keypoints1);  
  40.     detector.detect(img2, keypoints2);  
  41.   
  42.     // computing descriptors  
  43.     SurfDescriptorExtractor extractor;  
  44.     Mat descriptors1, descriptors2;  
  45.     extractor.compute(img1, keypoints1, descriptors1);  
  46.     extractor.compute(img2, keypoints2, descriptors2);  
  47.   
  48.     // matching descriptors  
  49.     BFMatcher matcher(NORM_L2);  
  50.     vector<DMatch> matches;  
  51.     matcher.match(descriptors1, descriptors2, matches);  
  52.   
  53.     // drawing the results  
  54.     namedWindow("matches", 1);  
  55.     Mat img_matches0,img_matches1;  
  56.     drawMatches(img1, keypoints1, img2, keypoints2, matches, img_matches0);  
  57.     //for(int i=0;i<matches.size();i++)  
  58.     //{       
  59.     //  //if(abs((keypoints1[matches[i].queryIdx].pt.y-keypoints2[matches[i].trainIdx].pt.y)>5)  
  60.     //  //{  
  61.   
  62.     //  //}  
  63.     //  //cout<<keypoints1[matches[i].queryIdx].pt.x<<endl;  
  64.     //  Point2f pt1,pt2;  
  65.     //  pt1=keypoints1[matches[i].queryIdx].pt;  
  66.     //  pt2=keypoints2[matches[i].trainIdx].pt;  
  67.     //  if(abs(pt1.y-pt2.y)>5)  
  68.     //  {  
  69.     //      matches[i].distance=100;  
  70.     //  }  
  71.   
  72.   
  73.     //}  
  74.  //   drawMatches(img1, keypoints1, img2, keypoints2, matches, img_matches1);  
  75.     // 分配空间  
  76.     int ptCount = (int)matches.size();  
  77.     Mat p1(ptCount, 2, CV_32F);  
  78.     Mat p2(ptCount, 2, CV_32F);  
  79.   
  80.     // 把Keypoint转换为Mat  
  81.     Point2f pt;  
  82.     for (int i=0; i<ptCount; i++)  
  83.     {  
  84.         pt = keypoints1[matches[i].queryIdx].pt;  
  85.         p1.at<float>(i, 0) = pt.x;  
  86.         p1.at<float>(i, 1) = pt.y;  
  87.   
  88.         pt = keypoints2[matches[i].trainIdx].pt;  
  89.         p2.at<float>(i, 0) = pt.x;  
  90.         p2.at<float>(i, 1) = pt.y;  
  91.     }  
  92.     // 用RANSAC方法计算F  
  93.     // Mat m_Fundamental;  
  94.     // 上面这个变量是基本矩阵  
  95.      vector<uchar> m_RANSACStatus;  
  96.     // 上面这个变量已经定义过,用于存储RANSAC后每个点的状态  
  97.     //m_Fundamental = findFundamentalMat(p1, p2, m_RANSACStatus, FM_RANSAC);  
  98.      Mat m_Fundamental=findFundamentalMat(p1, p2, m_RANSACStatus, FM_RANSAC);  
  99.      cout<<m_Fundamental<<endl;  
  100.     // 计算野点个数  
  101.     int OutlinerCount = 0;  
  102.     for (int i=0; i<ptCount; i++)  
  103.     {  
  104.         if (m_RANSACStatus[i] == 0) // 状态为0表示野点  
  105.         {  
  106.             OutlinerCount++;  
  107.         }  
  108.     }  
  109.   
  110.     // 计算内点  
  111.     vector<Point2f> m_LeftInlier;  
  112.     vector<Point2f> m_RightInlier;  
  113.     vector<DMatch> m_InlierMatches;  
  114.     // 上面三个变量用于保存内点和匹配关系  
  115.     int InlinerCount = ptCount - OutlinerCount;  
  116.     m_InlierMatches.resize(InlinerCount);  
  117.     m_LeftInlier.resize(InlinerCount);  
  118.     m_RightInlier.resize(InlinerCount);  
  119.     InlinerCount = 0;  
  120.     for (int i=0; i<ptCount; i++)  
  121.     {  
  122.         if (m_RANSACStatus[i] != 0)  
  123.         {  
  124.             m_LeftInlier[InlinerCount].x = p1.at<float>(i, 0);  
  125.             m_LeftInlier[InlinerCount].y = p1.at<float>(i, 1);  
  126.             m_RightInlier[InlinerCount].x = p2.at<float>(i, 0);  
  127.             m_RightInlier[InlinerCount].y = p2.at<float>(i, 1);  
  128.             m_InlierMatches[InlinerCount].queryIdx = InlinerCount;  
  129.             m_InlierMatches[InlinerCount].trainIdx = InlinerCount;  
  130.             InlinerCount++;  
  131.         }  
  132.     }  
  133.   
  134.     // 把内点转换为drawMatches可以使用的格式  
  135.     vector<KeyPoint> key1(InlinerCount);  
  136.     vector<KeyPoint> key2(InlinerCount);  
  137.     KeyPoint::convert(m_LeftInlier, key1);  
  138.     KeyPoint::convert(m_RightInlier, key2);  
  139.     for(int i=0;i<10;i++)  
  140.     cout<<key1[i].pt<<" "<<key2[i].pt<<" "<<key1[i].pt.x-key2[i].pt.x<<endl;  
  141.   
  142.     drawMatches(img1, key1, img2, key2, m_InlierMatches, img_matches1);  
  143.     imshow("matches0", img_matches0);  
  144.      imshow("matches1", img_matches1);  
  145.      Mat test(img1.rows,img1.cols,CV_32FC1);  
  146.      float a=0.0105591872,b=0.1024243227,c=10.106085341;  
  147.      for(int i=0;i<test.rows;i++)  
  148.      {  
  149.          float* pt=test.ptr<float>(i);          
  150.          for(int j=0;j<test.cols;j++)  
  151.          {  
  152.              float val=j*a+i*b+c;  
  153.              pt[j]=val;  
  154.          }  
  155.      }  
  156.      //cout<<test<<endl;  
  157.      cv::normalize(test,test,0,1,CV_MINMAX);  
  158.      cv::convertScaleAbs(test,test,255);      
  159.      test.convertTo(test,CV_8UC1);  
  160.      imshow("test",test);  
  161.     waitKey(0);  
  162.   
  163.     return 0;  
  164. }  
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值