keyframe_associations.cpp
Go to the documentation of this file.
00001 #include "keyframe_associations.h"
00002 
00003 using namespace rgbdtools;
00004 
00005 int main(int argc, char** argv)
00006 {
00007   // **** handle input  **********************************************
00008   
00009   if (argc != 3)
00010   {
00011     printUsage(argv);
00012     return -1;
00013   }
00014   
00015   std::string keyframe_path = argv[1];
00016   std::string output_path   = argv[2];
00017   
00018   std::string bf_output_path   = output_path + "/bf/";
00019   std::string tree_output_path = output_path + "/tree/";
00020   
00021   // **** load keyframes and prepare features ***********************
00022   
00023   rgbdtools::KeyframeVector keyframes;
00024   loadKeyframes(keyframes, keyframe_path);
00025   
00026   rgbdtools::KeyframeGraphDetector graph_detector;
00027   graph_detector.setVerbose(true);
00028   graph_detector.prepareFeaturesForRANSAC(keyframes);
00029     
00030   // **** brute force ***********************************************
00031        
00032   bruteForceAssociations(graph_detector, keyframes, bf_output_path);
00033   
00034   // load BF matrix and show
00035   cv::Mat bf_assoc = cv::imread(bf_output_path + "/bf_assoc.png", -1);  
00036   cv::namedWindow("BF Associations", 0);
00037   cv::imshow("BF Associations", bf_assoc*255);
00038   cv::waitKey(1);
00039   
00040   // **** tree based ************************************************
00041    
00042   printf("--------------------------------------------------------------\n");
00043   printf("K\tN\tTOT\tFN(C)\tFN(A)\tR(C)\tR(A)\tDUR[s]\n");
00044   printf("--------------------------------------------------------------\n");
00045   
00046   cv::namedWindow("Tree Associations", 0);
00047   cv::namedWindow("Tree Candidates", 0);
00048     
00049   for (int n = 5; n <= 20; n+=5)
00050   for (int k = 2; k <= 10; k+=1)
00051   {
00052     treeAssociations(graph_detector, keyframes, tree_output_path, bf_assoc, k, n);
00053   }
00054   
00055   printf("--------------------------------------------------------------\n");
00056   
00057   return 0;
00058 }
00059 
00060 void bruteForceAssociations(
00061   rgbdtools::KeyframeGraphDetector& graph_detector,
00062   rgbdtools::KeyframeVector& keyframes,
00063   const std::string& bf_output_path)
00064 {
00065   graph_detector.setCandidateGenerationMethod(
00066     rgbdtools::KeyframeGraphDetector::CANDIDATE_GENERATION_BRUTE_FORCE);
00067   graph_detector.setPairwiseMatchingMethod(
00068     rgbdtools::KeyframeGraphDetector::PAIRWISE_MATCHING_BFSAC);
00069   graph_detector.setPairwiseMatcherIndex(
00070     rgbdtools::KeyframeGraphDetector::PAIRWISE_MATCHER_LINEAR);
00071 
00072   graph_detector.setVerbose(true);
00073   graph_detector.setOutputPath(bf_output_path + "/sac_images/");
00074   graph_detector.setSACSaveResults(true);
00075   graph_detector.setOutputPath("~/run");
00076   
00077   rgbdtools::KeyframeAssociationVector associations;
00078   
00079   const clock_t start = clock();
00080   graph_detector.buildAssociationMatrix(keyframes, associations);
00081   float dur_s = (clock() - start)/(float)CLOCKS_PER_SEC;
00082       
00083   printf("--------------------------------------------------------------\n");
00084   printf("BF:%.1f\n", dur_s);
00085   printf("--------------------------------------------------------------\n");
00086   
00087   // save BF matrices to file
00088   cv::imwrite(bf_output_path + "/bf_corr.png",  graph_detector.getCorrespondenceMatrix());
00089   cv::imwrite(bf_output_path + "/bf_assoc.png", graph_detector.getAssociationMatrix());
00090 }
00091 
00092 void treeAssociations(
00093   rgbdtools::KeyframeGraphDetector& graph_detector,
00094   rgbdtools::KeyframeVector& keyframes,
00095   const std::string& tree_output_path,
00096   const cv::Mat& bf_assoc,
00097   int k, int n)
00098 {
00099   // set parameters relating to tree matchin
00100   graph_detector.setCandidateGenerationMethod(
00101     rgbdtools::KeyframeGraphDetector::CANDIDATE_GENERATION_SURF_TREE);
00102   graph_detector.setPairwiseMatchingMethod(
00103     rgbdtools::KeyframeGraphDetector::PAIRWISE_MATCHING_RANSAC); 
00104   graph_detector.setPairwiseMatcherIndex(
00105     rgbdtools::KeyframeGraphDetector::PAIRWISE_MATCHER_KDTREE);
00106   graph_detector.setNCandidates(n);
00107   graph_detector.setKNearestNeighbors(k);
00108   
00109   // set output parameters
00110   graph_detector.setVerbose(false);
00111   graph_detector.setSACSaveResults(true);
00112   
00113   std::stringstream ss_tree_output_path_kn;
00114   ss_tree_output_path_kn << tree_output_path << n << "_" << k << "/";
00115   std::string tree_output_path_kn = ss_tree_output_path_kn.str();
00116     
00117   graph_detector.setOutputPath(tree_output_path_kn + "/sac_images/");
00118   
00119   // build the associations
00120   rgbdtools::KeyframeAssociationVector associations;
00121   const clock_t start = clock();
00122   graph_detector.buildAssociationMatrix(keyframes, associations);
00123   float dur_s = (clock() - start)/(float)CLOCKS_PER_SEC;
00124   
00125   // show & save matrices
00126   cv::Mat tree_cand  = graph_detector.getCandidateMatrix();
00127   cv::Mat tree_corr  = graph_detector.getCorrespondenceMatrix();
00128   cv::Mat tree_assoc = graph_detector.getAssociationMatrix();   
00129   
00130   // save BF matrices to file
00131   cv::imwrite(tree_output_path_kn + "/tree_cand.png",  tree_cand);
00132   cv::imwrite(tree_output_path_kn + "/tree_corr.png",  tree_corr);
00133   cv::imwrite(tree_output_path_kn + "/tree_assoc.png", tree_assoc);
00134   
00135   cv::imshow("Tree Associations", tree_assoc*255);
00136   cv::imshow("Tree Candidates",   tree_cand*255);
00137   cv::waitKey(1);
00138   
00139   // print out results
00140   int fp, fn, tot;
00141   compareAssociationMatrix(bf_assoc, tree_assoc, fp, fn, tot);
00142   double fnr = (double)fn / double(tot);
00143   
00144   int fp_c, fn_c, tot_c;
00145   compareAssociationMatrix(bf_assoc, tree_cand, fp_c, fn_c, tot_c);
00146   double fnr_c = (double)fn_c / double(tot_c);
00147 
00148   printf("%d\t%d\t%d\t%d\t%d\t%.3f\t%.3f\t%.1f\n", 
00149     k, n, tot, fn_c, fn, 1-fnr_c, 1-fnr, dur_s);
00150 }
00151  
00152 void printUsage(char** argv)
00153 {
00154   std::cerr << "error: usage is " << argv[0] 
00155             << " [keyframe_path] [output_path]"
00156             << std::endl;
00157 }
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends


lib_rgbdtools
Author(s): Ivan Dryanovski
autogenerated on Tue Aug 27 2013 10:33:54