imagelist_matcher.cpp
Go to the documentation of this file.
00001 #include "pano_core/ImageAtom.h"
00002 #include "pano_core/feature_utils.h"
00003 #include "pano_core/MoleculeProcessor.h"
00004 #include "pano_core/CaptureEngine.h"
00005 #include "pano_core/ModelFitter.h"
00006 #include "pano_core/Blender.h"
00007 #include "pano_core/panoutils.h"
00008 #include <sstream>
00009 #include <opencv2/core/core.hpp>
00010 #include <iostream>
00011 #include <list>
00012 
00013 using namespace std;
00014 using namespace cv;
00015 using namespace pano;
00016 
00017 static bool readStringList(const string& filename, vector<string>& l)
00018 {
00019   l.resize(0);
00020   FileStorage fs(filename, FileStorage::READ);
00021   if (!fs.isOpened())
00022     return false;
00023   FileNode n = fs.getFirstTopLevelNode();
00024   if (n.type() != FileNode::SEQ)
00025     return false;
00026   FileNodeIterator it = n.begin(), it_end = n.end();
00027   for (; it != it_end; ++it)
00028     l.push_back((string)*it);
00029   return true;
00030 }
00031 
00032 void maskMatchesByTrainImgIdx(const vector<DMatch>& matches, int trainImgIdx, vector<char>& mask)
00033 {
00034   mask.resize(matches.size());
00035   fill(mask.begin(), mask.end(), 0);
00036   for (size_t i = 0; i < matches.size(); i++)
00037   {
00038     if (matches[i].imgIdx == trainImgIdx)
00039       mask[i] = 1;
00040   }
00041 }
00042 
00043 void condenseByTrainImgIdx(const vector<DMatch>& matches, int trainImgIdx, vector<DMatch>& tmatches)
00044 {
00045   tmatches.clear();
00046   for (size_t i = 0; i < matches.size(); i++)
00047   {
00048     if (matches[i].imgIdx == trainImgIdx)
00049       tmatches.push_back(matches[i]);
00050   }
00051 }
00052 int main(int ac, char** av)
00053 {
00054   if (ac != 4)
00055   {
00056     cout << "usage: " << av[0] << " camera.yml directory images.yml" << endl;
00057     return 1;
00058   }
00059   Camera camera;
00060   camera.setCameraIntrinsics(av[1]);
00061 
00062   string directory = av[2];
00063   string image_list_yaml = av[3];
00064 
00065   vector<std::string> image_list;
00066   if (!readStringList(image_list_yaml, image_list))
00067   {
00068     cerr << "could not read image list" << endl;
00069     return 1;
00070   }
00071 
00072   Ptr<FeatureDetector> detector(new DynamicAdaptedFeatureDetector( new FastAdjuster(),100, 110, 10));
00073 
00074   SVDRSolverParams params;
00075   params.error_thresh = 3;
00076   params.inliers_thresh = 12;
00077   params.maxiters = 50;
00078   params.nNeeded = 2;
00079 
00080   Ptr<SVDFitter> svdfitter(new SVDFitter(params));
00081 
00082   Ptr<ModelFitter> fitter(reinterpret_cast<const Ptr<ModelFitter>&> (svdfitter));
00083 
00084   //the glob stores the pano graph
00085   //think of it as the map
00086   MoleculeGlob glob;
00087 
00088   BlurDetector blur_detector;
00089 
00090   vector<ImageAtom> atoms;
00091   vector<Mat> descriptors;
00092   Images images;
00093   for (size_t i = 0; i < image_list.size(); i++)
00094   {
00095     images.load(image_list[i], directory);
00096     atoms.push_back(ImageAtom(camera, images));
00097     ImageAtom& atom = atoms.back();
00098     atom.setUid(i);
00099     atom.extrinsics() = Extrinsics(Mat::eye(3, 3, CV_32F), 1);
00100     atom.extrinsics().val(Extrinsics::ESTIMATED) = false;
00101     atom.detect(*detector);
00102     atom.extract(BriefDescriptorExtractor(), BruteForceMatcher<Hamming> ());
00103 
00104     atom.images().clear();
00105     descriptors.push_back(atom.features().descriptors());
00106     cout << "detected and extracted " << i + 1 << "/" << image_list.size() << endl;
00107   }
00108 
00109   BruteForceMatcher<Hamming> matcher;
00110   matcher.add(vector<Mat> (1, descriptors[0]));
00111   vector<vector<DMatch> > matches_all;
00112   matches_all.reserve(descriptors.size());
00113 
00114   vector<AtomPair> pairs;
00115 
00116   for (size_t i = 1; i < descriptors.size(); i++)
00117   {
00118     matches_all.push_back(vector<DMatch> ());
00119     matcher.match(descriptors[i], matches_all.back());
00120     cout << "matched descriptors: " << i + 1 << "/" << descriptors.size() << endl;
00121 
00122     for (size_t j = 0; j < i; j++)
00123     {
00124       vector<DMatch> matches;
00125       condenseByTrainImgIdx(matches_all.back(), j, matches);
00126       if (matches.size() >= params.inliers_thresh)
00127       {
00128         AtomPair atom_pair(atoms[j].ptrToSelf(), atoms[i].ptrToSelf(), matches);
00129         pairs.push_back(atom_pair);
00130       }
00131     }
00132     matcher.add(vector<Mat> (1, descriptors[i]));
00133   }
00134 
00135   cout << "fitting pairs, have " << pairs.size() << " to fit" << endl;
00136   FitPair pair_fitter(fitter, -1, new list<AtomPair> ());
00137   std::for_each(pairs.begin(), pairs.end(), pair_fitter);
00138   cout << "done fitting... found :" << pair_fitter.good_pairs->size() << " good pairs" << endl;
00139 
00140   glob.addPrefittedPairs(*pair_fitter.good_pairs);
00141   glob.batchFindAndSetTrinsics();
00142   FileStorage fs("glob.yaml.gz", FileStorage::WRITE);
00143   fs << "glob";
00144   glob.serialize(fs);
00145   cout << "found - " << glob.getMolecules().size() << " molecules" << endl;
00146   return 0;
00147 }


pano_core
Author(s): Ethan Rublee
autogenerated on Mon Oct 6 2014 08:04:38