Advertisement
Guest User

openCV ORB

a guest
May 10th, 2016
425
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 10.25 KB | None | 0 0
  1. /**
  2.  * @file SURF_FlannMatcher
  3.  * @brief SURF detector + descriptor + FLANN Matcher
  4.  * @author A. Huaman
  5.  */
  6.  
  7. #include <stdio.h>
  8. #include <iostream>
  9. #include "opencv2/core/core.hpp"
  10. #include "opencv2/highgui/highgui.hpp"
  11. #include "opencv2/calib3d/calib3d.hpp"
  12. //#include "opencv2/xfeatures2d.hpp"
  13. //#include "opencv2/features2d/features2d.hpp"
  14. #include "opencv2/imgproc/imgproc.hpp"
  15. #include <time.h>
  16. //#include "ORB_source.cpp"
  17. //#include "filtering.cpp"
  18.  
  19. using namespace cv;
  20.  
  21. /**
  22.  * @function main
  23.  * @brief Main function
  24.  */
  25. int main( int argc, char** argv )
  26. {
  27.    
  28.     //freopen("d:\\only_ranking.txt","w",stdout);
  29.     //The following variables are for execution time calculation using Clock
  30.     clock_t start, end_time , det_time , descr_time , match_time , ransac_time ;
  31.     double cpu_time_used;
  32.  
  33.     start = clock();
  34.  
  35.     printf("The Program is starting\n");
  36.  
  37.     //Mat img_1 = imread( "C://uav/img_5.bmp", CV_LOAD_IMAGE_COLOR );
  38.     //Mat img_2 = imread( "C://uav/img_10.bmp", CV_LOAD_IMAGE_COLOR );
  39.   // Mat img_1 = imread( "C://view_pnt_graffiti/img1.ppm", CV_LOAD_IMAGE_COLOR );
  40.    //Mat img_2 = imread( "C://view_pnt_graffiti/img5.ppm", CV_LOAD_IMAGE_COLOR );
  41.  
  42.     //Mat img_1 = imread( "C://zoom_rotn_boat/img1.pgm", CV_LOAD_IMAGE_COLOR );
  43.     //Mat img_2 = imread( "C://zoom_rotn_boat/img5.pgm", CV_LOAD_IMAGE_COLOR );
  44.  
  45.     // Taking the pair of 1 and 7 as the performance drastically degrades for 1 and 8 image pair
  46.     Mat img_1 = imread( "/home/dell/Documents/Postgraduate_Application/NUS/Dropbox/NUS_Sem_1/EE5003/eclipse/ORB/Source/photo1.png", CV_LOAD_IMAGE_COLOR );
  47.     Mat img_2 = imread( "/home/dell/Documents/Postgraduate_Application/NUS/Dropbox/NUS_Sem_1/EE5003/eclipse/ORB/Source/photo2.png", CV_LOAD_IMAGE_COLOR );
  48.  
  49.     if( !img_1.data || !img_2.data )
  50.         {
  51.             std::cout<< " --(!) Error reading images " << std::endl; return -1;
  52.     }
  53.    
  54.     //-- Step 1: Detect the keypoints using SURF Detector
  55.     //int minHessian = 400;
  56.     //int count = 0;
  57.    
  58.     //OrbFeatureDetector detector(400,1.8f,1,0,0,2,0,31);
  59.    
  60.    
  61.     //OrbFeatureDetector detector(400,1.2f,8,31,0,2,0,31);
  62.  
  63.     //OrbFeatureDetector detector(1000,1.5f,8,0,0,2,0,31);
  64.  
  65.     cv::Ptr<cv::ORB> detector = cv::ORB::create();
  66.    
  67.     std::vector<KeyPoint> keypoints_1, keypoints_2;
  68.  
  69.     //detector -> detect( img_1, keypoints_1 );
  70.     //detector -> detect( img_2, keypoints_2 );
  71.  
  72.     Mat descriptors_1, descriptors_2;
  73.  
  74.     detector -> detectAndCompute( img_1, Mat(), keypoints_1, descriptors_1 );
  75.     detector -> detectAndCompute( img_2, Mat(), keypoints_2, descriptors_2 );
  76.  
  77.     det_time = clock();
  78.     int No_of_keypoints_in_Image1 = keypoints_1.size();
  79.            int No_of_keypoints_in_Image2 = keypoints_2.size();
  80.            //printf("No_of_keypoints_in_Image1=%d\nNo_of_keypoints_in_Image2=%d\n", No_of_keypoints_in_Image1, No_of_keypoints_in_Image2);
  81.            //for( int q = 0 ; q< keypoints_1.size(); q++)
  82.            //printf(" x = %f, y = %f \n", keypoints_1[q].pt.x ,keypoints_1[q].pt.y);
  83.  
  84.     //-- Step 2: Calculate descriptors (feature vectors)
  85.     //OrbDescriptorExtractor extractor;
  86.     cv::DescriptorExtractor extractor;
  87.    
  88.     printf("Size_of_Descriptor_1 = %d\n", descriptors_1.size());
  89.     printf("Size_of_Descriptor_2 = %d \n", descriptors_2.size());
  90.     descr_time = clock();
  91.  
  92.     //-- Step 3: Matching descriptor vectors using FLANN matcher
  93.     //FlannBasedMatcher matcher;
  94.     BFMatcher matcher(NORM_HAMMING);
  95.    
  96.     std::vector< DMatch > matches;
  97.     matcher.match( descriptors_1, descriptors_2, matches );
  98.  
  99.     double max_dist = 0; double min_dist = 100;
  100.  
  101.     //-- Quick calculation of max and min distances between keypoints
  102.     for( int i = 0; i < descriptors_1.rows; i++ )  
  103.     {
  104.         double dist = matches[i].distance;
  105.         if( dist < min_dist )
  106.             {
  107.                 min_dist = dist;
  108.                 //printf("value of matches[i]=%d\n",matches[i]);
  109.             //  printf("min_dist=%f\n",min_dist);
  110.         }
  111.         if( dist > max_dist )
  112.             {
  113.                 max_dist = dist;
  114.                 //printf("value of matches[i]=%d\n",matches[i]);
  115.         }
  116.     }
  117.  
  118. //    printf("-- Max dist : %f \n", max_dist );
  119.   //  printf("-- Min dist : %f \n", min_dist );
  120.  
  121.     //-- Draw only "good" matches (i.e. whose distance is less than 2*min_dist,
  122.     //-- or a small arbitary value ( 0.02 ) in the event that min_dist is very
  123.     //-- small)
  124.     //-- PS.- radiusMatch can also be used here.
  125.     std::vector< DMatch > good_matches, good_matches1;
  126.  
  127.     for( int i = 0; i < descriptors_1.rows; i++ )
  128.     {
  129.        if( matches[i].distance <= (2*min_dist))//max(2*min_dist, 0.02) )
  130.         {
  131.             good_matches.push_back( matches[i]);
  132.         }
  133.     }
  134.     // the code for the new matcher is adder here
  135.    
  136.     //look if the match is inside a defined area of the image
  137.  // First part of code taken from the the internet for matching
  138.     /* double tresholdDist = 0.25 * sqrt(double(img_1.size().height*img_1.size().height + img_1.size().width*img_1.size().width));
  139.  
  140.  
  141.   good_matches.reserve(matches.size());  
  142. for (size_t i = 0; i < matches.size(); ++i)
  143. {
  144.     for (int j = 0; j < matches[i].size(); j++)
  145.     {
  146.     //calculate local distance for each possible match
  147.     Point2f from = keypoints_1[matches[i][j].queryIdx].pt;
  148.     Point2f to = keypoints_2[matches[i][j].trainIdx].pt;        
  149.     double dist = sqrt((from.x - to.x) * (from.x - to.x) + (from.y - to.y) * (from.y - to.y));
  150.     //save as best match if local distance is in specified area
  151.     if ((dist < tresholdDist) && (abs(from.y-to.y)<5))
  152.     {
  153.         good_matches.push_back(matches[i][j]);
  154.         j = matches[i].size();
  155.     }
  156. }
  157. }*/
  158.     // second part of matching taken from the internet
  159.     ransac_time = clock();
  160.     void ransacTest(const std::vector<cv::DMatch> good_matches,const std::vector<cv::KeyPoint>&keypoints_1,
  161.         const std::vector<cv::KeyPoint>& keypoints_2,std::vector<cv::DMatch>& good_matches1,double distance,double confidence,double minInlierRatio);
  162. {
  163.     //good_matches.clear();
  164.     // Convert keypoints into Point2f
  165.     std::vector<cv::Point2f> points1, points2;
  166.     for (std::vector<cv::DMatch>::const_iterator it= good_matches.begin();it!= good_matches.end(); ++it)
  167.     {
  168.         // Get the position of left keypoints
  169.         float x= keypoints_1[it->queryIdx].pt.x;
  170.         float y= keypoints_1[it->queryIdx].pt.y;
  171.         points1.push_back(cv::Point2f(x,y));
  172.         // Get the position of right keypoints
  173.         x= keypoints_2[it->trainIdx].pt.x;
  174.         y= keypoints_2[it->trainIdx].pt.y;
  175.         points2.push_back(cv::Point2f(x,y));
  176.     }
  177.     // Compute F matrix using RANSAC
  178.     std::vector<uchar> inliers(points1.size(),0);
  179.     cv::Mat fundemental= cv::findFundamentalMat(cv::Mat(points1),cv::Mat(points2),inliers,8,3,.99); // confidence probability
  180.     // extract the surviving (inliers) matches
  181.     std::vector<uchar>::const_iterator
  182.     itIn= inliers.begin();
  183.     std::vector<cv::DMatch>::const_iterator
  184.     itM= good_matches.begin();
  185.     // for all matches
  186.     for ( ;itIn!= inliers.end(); ++itIn, ++itM)
  187.     {
  188.         if (*itIn)
  189.         { // it is a valid match
  190.             good_matches1.push_back(*itM);
  191.         }
  192.     }
  193. }
  194.     //-- Draw only "good" matches
  195.     Mat img_matches;
  196.     drawMatches( img_1, keypoints_1, img_2, keypoints_2,
  197.                  good_matches1, img_matches, Scalar::all(-1), Scalar::all(-1),
  198.     std::vector<char>(), DrawMatchesFlags::NOT_DRAW_SINGLE_POINTS );
  199.    
  200.    
  201.     match_time = clock();
  202.    //-- Localize the object from img_1 in img_2
  203.   std::vector<Point2f> obj;
  204.   std::vector<Point2f> scene;
  205.  
  206.   for( size_t i = 0; i < good_matches.size(); i++ )
  207.   {
  208.     //-- Get the keypoints from the good matches
  209.     obj.push_back( keypoints_1[ good_matches[i].queryIdx ].pt );
  210.     scene.push_back( keypoints_2[ good_matches[i].trainIdx ].pt );
  211.   }
  212.  
  213.    
  214.   Mat H = findHomography(obj, scene ,CV_RANSAC);
  215.  
  216.   //-- Get the corners from the image_1 ( the object to be "detected" )
  217.   std::vector<Point2f> obj_corners(4);
  218.   obj_corners[0] = cvPoint(0,0);
  219.   obj_corners[1] = cvPoint( img_1.cols, 0 );
  220.   obj_corners[2] = cvPoint( img_1.cols, img_1.rows );
  221.   obj_corners[3] = cvPoint( 0, img_1.rows );
  222.   std::vector<Point2f> scene_corners(4);
  223.  
  224.   perspectiveTransform( obj_corners, scene_corners, H);
  225.  
  226.  
  227.   //-- Draw lines between the corners (the mapped object in the scene - image_2 )
  228.   Point2f offset( (float)img_1.cols, 0);
  229.   line( img_matches, scene_corners[0] + offset, scene_corners[1] + offset, Scalar(0, 255, 0), 4 );
  230.   line( img_matches, scene_corners[1] + offset, scene_corners[2] + offset, Scalar( 0, 255, 0), 4 );
  231.   line( img_matches, scene_corners[2] + offset, scene_corners[3] + offset, Scalar( 0, 255, 0), 4 );
  232.   line( img_matches, scene_corners[3] + offset, scene_corners[0] + offset, Scalar( 0, 255, 0), 4 );
  233.  
  234.  
  235.     //-- Show detected matches
  236.    
  237.  
  238.   imshow( "Good Matches", img_matches );
  239.  
  240.  
  241.     /*for( int i = 0; i < (int)good_matches.size(); i++ )
  242.     {
  243.         printf( "-- Good Match [%d] Keypoint 1: %d  -- Keypoint 2: %d  \n", i, good_matches[i].queryIdx, good_matches[i].trainIdx );
  244.     }*/
  245.     //printf("\n");
  246.     //The following code is for calculating the total execution time
  247.     end_time = clock();
  248.     cpu_time_used = ((double)(end_time - start)) / ((double)CLOCKS_PER_SEC);
  249.     double time_for_detection = det_time - start;
  250.     double time_for_description = descr_time - det_time;
  251.     double time_for_matching = match_time - descr_time;
  252.     double time_for_RANSAC = match_time - ransac_time;
  253.     //printf("\n\n########################################################################\nDetails on Execution time\n########################################################################\n");
  254.     printf("Time for detection = %.10f\n\nTime for description  = %.10f\n\nTime for matching = %.10f\n\nTime for RANSAC = %.10f\n\n",
  255.             time_for_detection,time_for_description,time_for_matching,time_for_RANSAC);
  256.  
  257.     printf("Total matches found=%d\n\n",matches.size());
  258.     printf("No of good matches=%d\n\n",good_matches1.size());
  259.    
  260.     /*printf("start = %.10f\n\nend_time   = %.10fs\n\ndet_time = %.10f\n\ndescr_time   = %.10f\n\nmatch_time = %.10f\n\n",
  261.            ((double) start), ((double) end_time),((double) det_time), ((double) descr_time), ((double) match_time));*/
  262.     printf("Total_time = %.10f\n\n", ((double) (end_time - start)));
  263.     printf("cpu_time_used  = %.10fs\n\n", cpu_time_used);
  264.     printf("CLOCKS_PER_SEC = %i\n\n", CLOCKS_PER_SEC);
  265.    
  266.     // Keep window there until user presses 'q' to quit.
  267.     char c = ' ';
  268.     while ((c = waitKey(0)) != 'q');  
  269.    
  270.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement