00001 
00002 
00003 
00004 
00005 
00006 
00007 
00008 
00009 
00010 
00011 
00012 
00013 
00014 
00015 
00016 
00017 
00018 
00019 
00020 
00021 
00022 
00023 
00024 
00025 
00026 
00027 
00028 
00029 
00030 
00031 
00032 
00033 
00034 
00035 
00036 
00037 
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 
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 
00064 char *lreg, *rreg;
00065 
00066 
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   
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   
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   
00130   cout << "Setting up frame processing..." << flush;
00131   FrameProc fp;                 
00132   cout << "done" << endl;
00133   pe::PoseEstimator3d pe(500,true,2.0e-1,6.0,6.0);
00134 
00135   srand(mstime());              
00136 
00137   
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       
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       
00167       
00168 
00169       
00170       Frame f0, f1;
00171       f0.setCamParams(camp);        
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       
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       
00195 
00196 
00197       
00199 #if 0
00200       cv::Mat display;
00201       
00202       
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 }