desc_match.cpp
Go to the documentation of this file.
00001 #include <highgui.h>
00002 #include "opencv2/core/core.hpp"
00003 #include "opencv2/calib3d/calib3d.hpp"
00004 #include "opencv2/imgproc/imgproc.hpp"
00005 #include "features_2d/features_2d.h"
00006 #include <iostream>
00007 
00008 using namespace cv;
00009 using namespace std;
00010 namespace f2d = features_2d;
00011 
00012 #define DRAW_RICH_KEYPOINTS_MODE     0
00013 #define DRAW_OUTLIERS_MODE           0
00014 
00015 const string winName = "correspondences";
00016 
00017 //Display function - DO NOT MODIFY
00018 void drawMatchesOnly(const cv::Mat& image1, const std::vector<cv::KeyPoint>& keypoints1,
00019                  const cv::Mat& image2, const std::vector<cv::KeyPoint>& keypoints2,
00020                  const std::vector<f2d::Match>& matches, cv::Mat& display)
00021 {
00022   // Set up composite image
00023   display = cv::Mat::zeros(std::max(image1.rows, image2.rows), image1.cols + image2.cols, CV_8UC3);
00024   cv::Mat sub_display1 = display( cv::Rect(0, 0, image1.cols, image1.rows) );
00025   cv::Mat sub_display2 = display( cv::Rect(image1.cols, 0, image2.cols, image2.rows) );
00026   
00027   cvtColor(image1, sub_display1, CV_GRAY2BGR);
00028   cvtColor(image2, sub_display2, CV_GRAY2BGR);
00029 
00030   // Draw lines between matches
00031   int shift_bits = 4;
00032   int multiplier = 1 << shift_bits;
00033   for (std::vector<f2d::Match>::const_iterator i = matches.begin(), ie = matches.end(); i != ie; ++i) {
00034     const cv::KeyPoint& keypt1 = keypoints1[i->index1];
00035     const cv::KeyPoint& keypt2 = keypoints2[i->index2];
00036     cv::Point center1(keypt1.pt.x * multiplier, keypt1.pt.y * multiplier);
00037     cv::Point center2((keypt2.pt.x + image1.cols) * multiplier, keypt2.pt.y * multiplier);
00038     cv::Scalar color(std::rand() % 256, std::rand() % 256, std::rand() % 256);
00039     cv::line(display, center1, center2, color, 1, CV_AA, shift_bits);
00040   }
00041 }
00042 
00043 int main(int argc, char** argv)
00044 {
00045     if( argc != 4 )
00046     {
00047         cout << "Format:" << endl;
00048         cout << argv[0] << " [image1] [image2] [ransacReprojThreshold]" << endl;
00049 
00050         return -1;
00051     }
00052     double ransacReprojThreshold = atof(argv[3]);
00053 
00054     cout << "< Creating detector, descriptor extractor and descriptor matcher ..." << endl;
00055     //detector, descriptor and matcher objects
00056     f2d::SurfFeatureDetector detector;
00057     f2d::SurfDescriptorExtractor descriptorExtractor;
00058     f2d::BruteForceMatcher<f2d::L2<float> > descriptorMatcher;
00059 
00060     /*****************************************************
00061      *TODO 1: read in img1 and img2 from the command line
00062      */
00063 
00064 
00065 
00066 
00067     
00068     /*****************************************************
00069      *TODO 2: a)Extract keypoints and b)compute SURF descriptor for both images
00070      * For a) use "detector" object and detect function: https://code.ros.org/svn/wg-ros-pkg/branches/trunk_cturtle/stacks/visual_feature_detectors/features_2d/include/features_2d/detector.h
00071      * For b) use descriptorExtractor object and compute function: https://code.ros.org/svn/wg-ros-pkg/branches/trunk_cturtle/stacks/visual_feature_detectors/features_2d/include/features_2d/descriptor.h
00072      */
00073 
00074 
00075 
00076 
00077     
00078     /*****************************************************
00079      *TODO 3: Find matches in both images
00080      * Use descriptorMatcher object and matchWindowless function: https://code.ros.org/svn/wg-ros-pkg/branches/trunk_cturtle/stacks/visual_feature_detectors/features_2d/include/features_2d/matcher.h
00081      */
00082     vector<f2d::Match> matches;
00083 
00084 
00085 
00086 
00087 
00088     //push matching keypoints coordinates in points1, points2 structures
00089     vector<Point2f> points1, points2;
00090     for(size_t i = 0; i < matches.size(); i++)
00091     {
00092         points1.push_back(keypoints1[matches[i].index1].pt);
00093         points2.push_back(keypoints2[matches[i].index2].pt);
00094     }
00095     /*****************************************************
00096      *TODO 4: Find homography between the set of both matches
00097      * Use findHomography function from openCV
00098      */
00099 
00100     //estimate projection matrix
00101     Mat points1Projected;
00102     /*****************************************************
00103      *TODO 5: What does perspectiveTransform function do?
00104      */
00105     perspectiveTransform(Mat(points1), points1Projected, H);
00106     
00107 
00108     
00109     /*****************************************************
00110      *TODO : Filter out bad matches using ransacReprojThreshold
00111      * to do so iterate over points1 and check if the normalized distance 
00112      * between original and projected points is less than ransacReprojThreshold
00113      */
00114     vector<f2d::Match> matchesFiltered;
00115     for(size_t i = 0; i < points1.size(); i++)
00116     {
00117       /*todo*/
00118       if(/*todo*/)
00119         {
00120                 matchesFiltered.push_back(matches[i]);
00121         }
00122     }
00123 
00124 
00125     //draw matches
00126     namedWindow(winName, 1);
00127     Mat drawImg;
00128     drawMatchesOnly(img1, keypoints1, img2, keypoints2, matchesFiltered, drawImg);
00129     imshow(winName, drawImg);
00130     waitKey(0);
00131 
00132     return 0;
00133 }
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Properties Friends Defines


as11
Author(s): lucian goron
autogenerated on Sun Oct 6 2013 12:08:02