run_sequence.cpp
Go to the documentation of this file.
00001 /*********************************************************************
00002 * Software License Agreement (BSD License)
00003 *
00004 *  Copyright (c) 2009, Willow Garage, Inc.
00005 *  All rights reserved.
00006 *
00007 *  Redistribution and use in source and binary forms, with or without
00008 *  modification, are permitted provided that the following conditions
00009 *  are met:
00010 *
00011 *   * Redistributions of source code must retain the above copyright
00012 *     notice, this list of conditions and the following disclaimer.
00013 *   * Redistributions in binary form must reproduce the above
00014 *     copyright notice, this list of conditions and the following
00015 *     disclaimer in the documentation and/or other materials provided
00016 *     with the distribution.
00017 *   * Neither the name of the Willow Garage nor the names of its
00018 *     contributors may be used to endorse or promote products derived
00019 *     from this software without specific prior written permission.
00020 *
00021 *  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
00022 *  "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
00023 *  LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
00024 *  FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
00025 *  COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
00026 *  INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
00027 *  BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
00028 *  LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
00029 *  CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
00030 *  LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
00031 *  ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
00032 *  POSSIBILITY OF SUCH DAMAGE.
00033 *********************************************************************/
00034 
00035 
00036 //
00037 // runs a sequence of stereo images into an SBA engine
00038 //
00039 
00040 #include <posest/pe3d.h>
00041 #include <opencv/highgui.h>
00042 #include <boost/shared_ptr.hpp>
00043 #include <cstdio>
00044 #include <fstream>
00045 #include <dirent.h>
00046 #include <fnmatch.h>
00047 
00048 using namespace frame_common;
00049 using namespace std;
00050 
00051 // elapsed time in milliseconds
00052 #include <sys/time.h>
00053 static double mstime()
00054 {
00055   timeval tv;
00056   gettimeofday(&tv,NULL);
00057   long long ts = tv.tv_sec;
00058   ts *= 1000000;
00059   ts += tv.tv_usec;
00060   return (double)ts*.001;
00061 }
00062 
00063 // Names of left and right files in directory (with wildcards)
00064 char *lreg, *rreg;
00065 
00066 // Filters for scandir
00067 int getleft(struct dirent const *entry)
00068 {
00069   if (!fnmatch(lreg,entry->d_name,0))
00070     return 1;
00071   return 0;
00072 }
00073 
00074 int getright(struct dirent const *entry)
00075 {
00076   if (!fnmatch(rreg,entry->d_name,0))
00077     return 1;
00078   return 0;
00079 }
00080 
00081 
00082 int main(int argc, char** argv)
00083 {
00084   if (argc < 5)
00085     {
00086       printf("Args are: <param file> <image dir> <left image file template> <right image file template> \n");
00087       exit(0);
00088     }
00089 
00090   // get camera parameters, in the form: fx fy cx cy tx
00091   fstream fstr;
00092   fstr.open(argv[1],fstream::in);
00093   if (!fstr.is_open())
00094     {
00095       printf("Can't open camera file %s\n",argv[1]);
00096       exit(0);
00097     }
00098   CamParams camp;
00099   fstr >> camp.fx;
00100   fstr >> camp.fy;
00101   fstr >> camp.cx;
00102   fstr >> camp.cy;
00103   fstr >> camp.tx;
00104 
00105   cout << "Cam params: " << camp.fx << " " << camp.fy << " " << camp.cx
00106        << " " << camp.cy << " " << camp.tx << endl;
00107 
00108 
00109   // get left/right image file names, sorted
00110   char *dir = argv[2];
00111   lreg = argv[3];
00112   rreg = argv[4];
00113   struct dirent **lims, **rims;
00114   int nlim, nrim;
00115   nlim = scandir(dir,&lims,getleft,alphasort);
00116   printf("Found %d left images\n", nlim);
00117   printf("%s\n",lims[0]->d_name);
00118 
00119   nrim = scandir(argv[2],&rims,getright,alphasort);
00120   printf("Found %d right images\n", nrim);
00121   printf("%s\n",rims[0]->d_name);
00122 
00123   if (nlim != nrim)
00124     {
00125       printf("Number of left/right images does not match: %d vs. %d\n", nlim, nrim);
00126       exit(0);
00127     }
00128 
00129   // set up structures
00130   cout << "Setting up frame processing..." << flush;
00131   FrameProc fp;                 // can be slow, setting up Calonder tree
00132   cout << "done" << endl;
00133   pe::PoseEstimator3d pe(500,true,2.0e-1,6.0,6.0);
00134 
00135   srand(mstime());              // for RANSAC
00136 
00137   // window
00138   const std::string window_name = "matches";
00139   cv::namedWindow(window_name, CV_WINDOW_AUTOSIZE);
00140 
00141   for (int i=0; i<nlim-1; i++)
00142     {
00143       // Load images
00144       char fn[2048];
00145       sprintf(fn,"%s/%s",dir,lims[i]->d_name);
00146       cv::Mat image1 = cv::imread(fn,0);
00147       sprintf(fn,"%s/%s",dir,lims[i+1]->d_name);
00148       cv::Mat image2 = cv::imread(fn,0);
00149       sprintf(fn,"%s/%s",dir,rims[i]->d_name);
00150       cv::Mat image1r = cv::imread(fn,0);
00151       sprintf(fn,"%s/%s",dir,rims[i+1]->d_name);
00152       cv::Mat image2r = cv::imread(fn,0);
00153 
00154 #if 0
00155       printf("Image size: %d x %d\n", image1.cols, image1.rows);
00156       printf("Image size: %d x %d\n", image1r.cols, image1r.rows);
00157       printf("Image size: %d x %d\n", image2.cols, image2.rows);
00158       printf("Image size: %d x %d\n", image2r.cols, image2r.rows);
00159 #endif
00160 
00161       if (image1.rows == 0 || image1r.rows == 0 || image2.rows == 0 || image2r.rows == 0)
00162         exit(0);
00163 
00164 
00165       //
00166       // do pose estimation
00167       //
00168 
00169       // set up frames
00170       Frame f0, f1;
00171       f0.setCamParams(camp);        // this sets the projection and reprojection matrices
00172       f1.setCamParams(camp);
00173 
00174       fp.setStereoFrame(f0,image1,image1r);
00175       double t0 = mstime();
00176       fp.setStereoFrame(f1,image2,image2r);
00177 
00178       int n0=0, n1=0;
00179       for (int i=0; i<(int)f0.goodPts.size(); i++)
00180         if (f0.goodPts[i]) n0++;
00181       for (int i=0; i<(int)f1.goodPts.size(); i++)
00182         if (f1.goodPts[i]) n1++;
00183       printf("%d/%d, %d/%d good stereo points\n", n0, (int)f0.goodPts.size(), 
00184              n1, (int)f1.goodPts.size());
00185 
00186       // do pose estimation
00187       double t3 = mstime();
00188       int inl = pe.estimate(f0,f1);
00189       double t4 = mstime();
00190       printf("Number of inliers: %d; RANSAC+polish time is %0.2f ms\n", inl, t4-t3);
00191       printf("Total time: %0.2f ms\n", t4-t0);
00192 
00193       cout << "Translation: " << pe.trans.transpose().head(3) << endl << endl;
00194       //      cout << pe.rot << endl;
00195 
00196 
00197       // Visualize
00199 #if 0
00200       cv::Mat display;
00201       //  f2d::drawMatchesFlow(image1, keypoints1, image2, keypoints2, matches, display);
00202       //  f2d::drawMatches(image1, keypoints1, image2, keypoints2, matches, display);
00203       f2d::drawMatchesFlow(image1, f0.kpts, image2, f1.kpts, pe.inliers, pe.matches, display);
00204       cv::imshow(window_name, display);
00205       cv::waitKey(10);
00206 #endif
00207     }
00208   return 0;
00209 }


posest
Author(s): Kurt Konolige
autogenerated on Thu Jan 2 2014 12:12:17