run_posest.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 #include <posest/pe3d.h>
00037 #include <opencv/highgui.h>
00038 #include <boost/shared_ptr.hpp>
00039 #include <cstdio>
00040 #include <fstream>
00041 
00042 
00043 using namespace frame_common;
00044 using namespace std;
00045 
00046 // elapsed time in milliseconds
00047 #include <sys/time.h>
00048 static double mstime()
00049 {
00050   timeval tv;
00051   gettimeofday(&tv,NULL);
00052   long long ts = tv.tv_sec;
00053   ts *= 1000000;
00054   ts += tv.tv_usec;
00055   return (double)ts*.001;
00056 }
00057 
00058 
00059 int main(int argc, char** argv)
00060 {
00061   if (argc < 6)
00062     {
00063       printf("Args are: <params> <left0> <left1> <right0> <right1>\n");
00064       exit(0);
00065     }
00066 
00067   // get camera parameters, in the form: fx fy cx cy tx
00068   fstream fstr;
00069   fstr.open(argv[1],fstream::in);
00070   if (!fstr.is_open())
00071     {
00072       printf("Can't open camera file %s\n",argv[1]);
00073       exit(0);
00074     }
00075   CamParams camp;
00076   fstr >> camp.fx;
00077   fstr >> camp.fy;
00078   fstr >> camp.cx;
00079   fstr >> camp.cy;
00080   fstr >> camp.tx;
00081 
00082   cout << "Cam params: " << camp.fx << " " << camp.fy << " " << camp.cx
00083        << " " << camp.cy << " " << camp.tx << endl;
00084 
00085   // Load images
00086   cv::Mat image1 = cv::imread(argv[2], 0);
00087   cv::Mat image2 = cv::imread(argv[3], 0);
00088   cv::Mat image1r = cv::imread(argv[4], 0);
00089   cv::Mat image2r = cv::imread(argv[5], 0);
00090   printf("Image size: %d x %d\n", image1.cols, image1.rows);
00091   printf("Image size: %d x %d\n", image1r.cols, image1r.rows);
00092   printf("Image size: %d x %d\n", image2.cols, image2.rows);
00093   printf("Image size: %d x %d\n", image2r.cols, image2r.rows);
00094 
00095   if (image1.rows == 0 || image1r.rows == 0 || image2.rows == 0 || image2r.rows == 0)
00096     exit(0);
00097 
00098   cout << "Setting up frame processing..." << flush;
00099   FrameProc fp(30);
00100   cout << "done" << endl;
00101   // set up frames
00102   Frame f0, f1;
00103   f0.setCamParams(camp);        // this sets the projection and reprojection matrices
00104   f1.setCamParams(camp);
00105 
00106   double t0 = mstime();
00107   fp.setStereoFrame(f0,image1,image1r);
00108   fp.setStereoFrame(f1,image2,image2r);
00109 
00110   printf("%d keypoints from %s\n", (int)f0.kpts.size(), argv[2]);
00111   printf("%d keypoints from %s\n", (int)f1.kpts.size(), argv[3]);
00112 
00113   //
00114   // do pose estimation
00115   //
00116 
00117   srand(mstime());              // for RANSAC
00118 
00119   int n0=0, n1=0;
00120   for (int i=0; i<(int)f0.goodPts.size(); i++)
00121     if (f0.goodPts[i]) n0++;
00122   for (int i=0; i<(int)f1.goodPts.size(); i++)
00123     if (f1.goodPts[i]) n1++;
00124   printf("%d/%d, %d/%d good stereo points\n", n0, (int)f0.goodPts.size(), 
00125          n1, (int)f1.goodPts.size());
00126 
00127   for (int i=0; i<100; i++)
00128     {
00129       // do pose estimation
00130       pe::PoseEstimator3d pe(500,true,10.0,4.0,4.0);
00131       double t0 = mstime();
00132       int inl = pe.estimate(f0,f1);
00133       double t1 = mstime();
00134       printf("\n");
00135       printf("%d matches\n", (int)pe.matches.size());
00136       printf("Number of inliers: %d; time is %0.2f ms\n", inl, t1-t0);
00137 
00138       cout << pe.trans.transpose() << endl << endl;
00139       cout << pe.rot << endl << "==========================" << endl;
00140 
00141 
00142       // Visualize
00143       cv::Mat display;
00144       drawMatches(image1, f0.kpts, image2, f1.kpts, pe.inliers, display);
00145       //  f2d::drawMatchesFlow(image1, f0.kpts, image2, f1.kpts, pe.inliers, pe.matches, display);
00146       const std::string window_name = "matches";
00147       cv::namedWindow(window_name,0);
00148       cv::imshow(window_name, display);
00149       cv::waitKey(2000);
00150     }
00151   return 0;
00152 }


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