extractFast.cpp
Go to the documentation of this file.
00001 
00037 #include <iostream>
00038 #include <fstream>
00039 #include <opencv/cv.h>
00040 #include <opencv/highgui.h>
00041 #include <string>
00042 #include <vector>
00043 
00044 #include "Mask.h"
00045 
00046 #include "DUtils.h"
00047 #include "DUtilsCV.h"
00048 #include "DVision.h"
00049 
00050 typedef DVision::PixelPointFile::PixelPoint PixelPoint;
00051 
00052 using namespace std;
00053 
00054 void treatDirectory(const std::string &img_dir, const float fast_th);
00055 
00056 void findClosestPoints(vector<cv::KeyPoint> &keys, 
00057   const vector<PixelPoint> &pixelpoints, 
00058   vector<int> &indices2d);
00059   
00060 void saveGlobalIndices(const std::string &filename, 
00061   const vector<int> &indices2d, 
00062   const vector<PixelPoint> &pixelpoints);
00063 
00064 // ----------------------------------------------------------------------------
00065 
00066 int main(int argc, char *argv[])
00067 {
00068 
00069   if(argc < 2)
00070   {
00071     cout << "Usage: " << argv[0] << " <img dir> [FAST th = 0]"
00072       << endl;
00073     return 1;
00074   }
00075 
00076   string img_dir = argv[1];
00077   float fast_th = 0.f;
00078   if(argc >= 3) fast_th = atof(argv[2]);
00079   
00080   treatDirectory(img_dir, fast_th);
00081 
00082   return 0;
00083 
00084 }
00085 
00086 // ---------------------------------------------------------------------------- 
00087 
00088 void treatDirectory(const std::string &img_dir, const float fast_th)
00089 {
00090   const int MARGIN = 5;
00091   
00092   vector<string> img_files = 
00093     DUtils::FileFunctions::Dir(img_dir.c_str(), ".jpg", true);
00094   
00095   for(unsigned int i = 0; i < img_files.size(); ++i)
00096   {
00097     const string& img_file = img_files[i];
00098     
00099     cout << img_file << "..." << endl;
00100     
00101     string path, name, ext;
00102     DUtils::FileFunctions::FileParts(img_file, path, name, ext);
00103     
00104     const string pp_file = path + "/" + name + "_points.txt";
00105     const string mask_file = path + "/" + name + "_mask.png";
00106     
00107     vector<PixelPoint> pixelpoints;
00108     DVision::PixelPointFile::readFile(pp_file, pixelpoints);
00109     
00110     cv::Mat im = cv::imread(img_file, 0);
00111     
00112     vector<cv::KeyPoint> keys;
00113     cv::FAST(im, keys, fast_th);
00114     
00115     Mask mask(mask_file.c_str());
00116     mask.shrink(MARGIN);
00117     mask.maskKeyPoints(keys);
00118     
00119     // save all the keypoints for debugging
00120     {
00121       const string key_file = path + "/" + name + "_fast_all.key";
00122       cv::FileStorage fs(key_file, cv::FileStorage::WRITE);
00123       cv::write(fs, "fast", keys);
00124       fs.release();
00125     }
00126 
00127     // associate each fast keypoint with a 3d point
00128     vector<int> indices2d; // indices of the pixelpoint vector
00129     findClosestPoints(keys, pixelpoints, indices2d);
00130     
00131     // save surviving fast points
00132     const string key_file = path + "/" + name + "_fast.key";
00133     cv::FileStorage fs(key_file, cv::FileStorage::WRITE);
00134     cv::write(fs, "fast", keys);
00135     fs.release();
00136     
00137     // and index map
00138     const string map_file = path + "/" + name + "_2d3d.txt";
00139     // global indices of 3d features
00140     saveGlobalIndices(map_file, indices2d, pixelpoints);
00141   }
00142 
00143 }
00144 
00145 // ----------------------------------------------------------------------------
00146 
00147 void findClosestPoints(vector<cv::KeyPoint> &keys, 
00148   const vector<PixelPoint> &pixelpoints, 
00149   vector<int> &indices2d)
00150 {
00151   const double MAX_DIST = 2.; // px
00152   
00153   indices2d.clear();
00154   
00155   int deleted = 0;
00156   unsigned int i = 0;
00157   while(i < keys.size() - deleted)
00158   {
00159     const cv::KeyPoint &k = keys[i];
00160     
00161     double best_d = 1e9;
00162     int best_j = -1;
00163     
00164     for(unsigned int j = 0; j < pixelpoints.size(); ++j)
00165     {
00166       const PixelPoint &p = pixelpoints[j];
00167       
00168       double sqd = (k.pt.x - p.u)*(k.pt.x - p.u) + (k.pt.y - p.v)*(k.pt.y - p.v);
00169       
00170       if(sqd < best_d)
00171       {
00172         best_d = sqd;
00173         best_j = j;
00174       }
00175     }
00176     
00177     if(best_j > -1 && sqrt(best_d) <= MAX_DIST)
00178     {
00179       // keep this point and save the best corresponding index
00180       indices2d.push_back(best_j);
00181       ++i;
00182     }
00183     else
00184     {
00185       // delete this keypoint
00186       keys[i] = keys[keys.size()-1 - deleted];
00187       deleted++;
00188     }
00189   }
00190   
00191   if(deleted > 0) keys.resize(keys.size() - deleted);
00192   
00193 }
00194 
00195 // ----------------------------------------------------------------------------
00196 
00197 void saveGlobalIndices(const std::string &filename, 
00198   const vector<int> &indices2d, 
00199   const vector<PixelPoint> &pixelpoints)
00200 {
00201   fstream f(filename.c_str(), ios::out);
00202   
00203   f << indices2d.size() << endl;
00204   
00205   vector<int>::const_iterator iit;
00206   for(iit = indices2d.begin(); iit != indices2d.end(); ++iit)
00207   {
00208     int i3d = pixelpoints[*iit].idx;
00209     f << (iit - indices2d.begin()) << " " << i3d << endl;
00210   }
00211   
00212   f.close();
00213 }
00214 
00215 // ----------------------------------------------------------------------------
00216 


re_vision
Author(s): Dorian Galvez-Lopez
autogenerated on Sun Jan 5 2014 11:31:08