molecule_builder.cpp
Go to the documentation of this file.
00001 /*
00002  * molecule_builder.cpp
00003  *
00004  *  Created on: Oct 20, 2010
00005  *      Author: erublee
00006  */
00007 #include <opencv2/opencv.hpp>
00008 
00009 #include <iostream>
00010 #include <list>
00011 
00012 #include "pano_core/ImageMolecule.h"
00013 #include "pano_core/MoleculeProcessor.h"
00014 #include "pano_core/ImageAtom.h"
00015 #include "pano_core/feature_utils.h"
00016 #include "pano_core/ModelFitter.h"
00017 
00018 using namespace std;
00019 using namespace cv;
00020 using namespace pano;
00021 
00022 int main(int ac, char ** av)
00023 {
00024 
00025   if (ac != 3)
00026   {
00027     cout << "usage: " << av[0] << " camera.yml <video device number>" << endl;
00028     return 1;
00029   }
00030 
00031   Camera camera;
00032   camera.setCameraIntrinsics(av[1]);
00033 
00034   FileStorage fs("camera.serial.yml", FileStorage::WRITE);
00035   fs << "camera";
00036   camera.serialize(fs);
00037 
00038   BriefDescriptorExtractor brief(32);
00039 
00040   GriddedDynamicDetectorAdaptor detector(200, 3,2, 2, cv::FastAdjuster());
00041 
00042   VideoCapture capture;
00043   capture.open(atoi(av[2]));
00044 
00045   if (!capture.isOpened())
00046   {
00047     cout << "capture device failed to open!" << endl;
00048     return 1;
00049   }
00050 
00051   cout << "following keys do stuff:" << endl;
00052   cout << "t : grabs a reference frame to match against" << endl;
00053   cout << "l : makes the reference frame new every frame" << endl;
00054   cout << "q : quit" << endl;
00055 
00056   Mat frame;
00057 
00058   vector<DMatch> matches;
00059 
00060   BruteForceMatcher<Hamming> matcher;
00061 
00062   bool ref_live = true;
00063 
00064   SVDRSolverParams params;
00065   params.error_thresh = 4;
00066   params.inliers_thresh = 12;
00067   params.maxiters = 10;
00068   params.nNeeded = 3;
00069 
00070   Ptr<ModelFitter> fitter(new SVDFitter(params));
00071 
00072   Ptr<ImageAtom> prior(new ImageAtom()), atom(new ImageAtom());
00073 
00074   atom->camera() = camera;
00075   prior->camera() = camera;
00076   ref_live = true;
00077   ImageMolecule molecule;
00078   int fail_count =0;
00079   for (;;)
00080   {
00081 
00082     capture >> frame;
00083     if (frame.empty())
00084       continue;
00085 
00086     atom->images().load(frame);
00087     atom->detect(detector);
00088     atom->extract(brief, matcher);
00089 
00090     Mat f_atom, f_prior;
00091 
00092     if (!prior->features().descriptors().empty() && !atom->features().kpts().empty())
00093     {
00094       AtomPair atom_pair = MoleculeProcessor::matchwithFitter(prior, atom, fitter);
00095       if (atom_pair.result().success())
00096       {
00097 
00098 
00099         atom->extrinsics().mat(Extrinsics::R) = atom_pair.RofThis(atom);
00100 
00101         atom->extrinsics().flag(Extrinsics::ESTIMATED) = true;
00102         atom->extrinsics().val(Extrinsics::CONFIDENCE) = atom_pair.result().err();
00103 
00104         drawMatchesRelative(atom_pair.atom1()->features(), atom_pair.atom2()->features(), atom_pair.matches(), frame,
00105                             atom_pair.result().inlier_mask());
00106         fail_count = 0;
00107       }else{
00108         if(fail_count++ > 3){
00109           atom->extrinsics().flag(Extrinsics::ESTIMATED) = false;
00110           atom->extrinsics().mat(Extrinsics::R) = Mat::eye(3,3,CV_32FC1);
00111         }
00112       }
00113 
00114     }
00115 
00116     imshow("frame", frame);
00117 
00118     if (ref_live)
00119     {
00120       swap(prior, atom);
00121       atom->extrinsics() = prior->extrinsics();
00122     }
00123 
00124     char key = waitKey(2);
00125 
00126     switch (key)
00127     {
00128       case 'l':
00129         ref_live = true;
00130         break;
00131       case 't':
00132         ref_live = false;
00133         swap(prior, atom);
00134         atom->extrinsics() = prior->extrinsics();
00135         break;
00136       case 'q':
00137         return 0;
00138         break;
00139     }
00140 
00141   }
00142   return 0;
00143 }


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