calculate_RT.cpp
Go to the documentation of this file.
00001 /*********************************************************************
00002 *  Software License Agreement (BSD License)
00003 *  Copyright (c) 2013, Intelligent Robotics Lab, DLUT.
00004 *  All rights reserved.
00005 *  Author:Zhao Cilang,Yan Fei,Zhuang Yan
00006 *  Redistribution and use in source and binary forms, with or without
00007 *  modification, are permitted provided that the following conditions
00008 *  are met:
00009 *
00010 *   * Redistributions of source code must retain the above copyright
00011 *     notice, this list of conditions and the following disclaimer.
00012 *   * Redistributions in binary form must reproduce the above
00013 *     copyright notice, this list of conditions and the following
00014 *     disclaimer in the documentation and/or other materials provided
00015 *     with the distribution.
00016 *   * Neither the name of the Intelligent Robotics Lab nor the names of its
00017 *     contributors may be used to endorse or promote products derived
00018 *     from this software without specific prior written permission.
00019 *
00020 *  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
00021 *  "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
00022 *  LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
00023 *  FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
00024 *  COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
00025 *  INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
00026 *  BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
00027 *  LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
00028 *  CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
00029 *  LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
00030 *  ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
00031 *  POSSIBILITY OF SUCH DAMAGE.
00032 *********************************************************************/
00033 #include <ros/ros.h>
00034 #include <opencv/cv.h>
00035 #include <opencv/highgui.h>
00036 #include <opencv/cxcore.h>
00037 
00038 #include <fstream>
00039 #include <vector>
00040 
00041 using namespace std;
00042 
00043 int main (int argc, char **argv)
00044 {
00045   int n_first_num = atoi (argv[1]);     //the number of first file
00046   int n_file_num = atoi (argv[2]);      //the number of files we read in
00047   ifstream inf_vision;
00048   ifstream inf_laser;
00049   std::vector < float >vision_pts_vec;
00050   std::vector < float >laser_pts_vec;
00051 
00052   //read in the vision data
00053   for (int i = n_first_num; i < n_file_num + n_first_num; i++)
00054   {
00055     char filename[100];
00056     sprintf (filename, "visionpts%d.txt", i);
00057     inf_vision.open (filename);
00058     if (inf_vision.fail ())
00059     {
00060       ROS_INFO ("can not open file");
00061       return -1;
00062     }
00063     float f;
00064     while (inf_vision >> f)
00065     {
00066       vision_pts_vec.push_back (f);
00067       inf_vision >> f;
00068       vision_pts_vec.push_back (f);
00069     }
00070     inf_vision.clear ();
00071     inf_vision.close ();
00072   }
00073 
00074   //read in the laser data
00075   for (int i = n_first_num; i < n_file_num + n_first_num; i++)
00076   {
00077     char filename[100];
00078     sprintf (filename, "laserpts%d.txt", i);
00079     inf_laser.open (filename);
00080     if (inf_laser.fail ())
00081     {
00082       ROS_INFO ("can not open file");
00083       return -1;
00084     }
00085     float f;
00086     while (inf_laser >> f)
00087     {
00088       laser_pts_vec.push_back (f);
00089       for (int j = 0; j < 2; j++)
00090       {
00091         inf_laser >> f;
00092         laser_pts_vec.push_back (f);
00093       }
00094     }
00095     inf_laser.clear ();
00096     inf_laser.close ();
00097   }
00098 
00099   int pts_num = laser_pts_vec.size () / 3;
00100   if (pts_num != (int) vision_pts_vec.size () / 2)
00101   {
00102     ROS_INFO ("Numbers not match ");
00103     
00104     return -1;
00105   }
00106   //define some CvMat
00107   CvMat *p_image_points = cvCreateMat (pts_num, 2, CV_32FC1);
00108   CvMat *p_object_points = cvCreateMat (pts_num, 3, CV_32FC1);
00109   CvMat *intrinsic_M = cvCreateMat (3, 3, CV_32FC1);
00110   CvMat *rotation_V = cvCreateMat (3, 1, CV_32FC1);
00111   CvMat *distortion_V = cvCreateMat (4, 1, CV_32FC1);
00112   CvMat *rotation_M = cvCreateMat (3, 3, CV_32FC1);
00113   CvMat *translate_V = cvCreateMat (3, 1, CV_32FC1);
00114 
00115   //read in the intrinsic parameters
00116   intrinsic_M = (CvMat *) cvLoad ("Intrinsics.xml");
00117   distortion_V = (CvMat *) cvLoad ("Distortion.xml");
00118   cvSetZero (distortion_V);
00119 
00120   float *ptrVision = p_image_points->data.fl;
00121   float *ptrLaser = p_object_points->data.fl;
00122   //save the vision corners as the order of laser corners
00123   float tempPoints[100][2];
00124   const int numperimg = 12;
00125   for (int imgcnt = 0; imgcnt < 48; imgcnt += numperimg)
00126   {
00127     for (int i = 0; i < numperimg; i += 4)
00128     {
00129       for (int j = 0; j < 4; j++)
00130       {
00131         tempPoints[imgcnt + i + j][0] = vision_pts_vec[(imgcnt + i / 4 + j * 3) * 2];
00132         tempPoints[imgcnt + i + j][1] = vision_pts_vec[(imgcnt + i / 4 + j * 3) * 2 + 1];
00133       }
00134     }
00135   }
00136 
00137   for (int imgP = 0; imgP < pts_num; imgP++)
00138   {
00139     ptrVision[2 * imgP] = tempPoints[imgP][0];
00140     ptrVision[2 * imgP + 1] = tempPoints[imgP][1];
00141   }
00142 
00143   for (int selP = 0; selP < pts_num; selP++)
00144   {
00145     ptrLaser[3 * selP] = laser_pts_vec[3 * selP];
00146     ptrLaser[3 * selP + 1] = laser_pts_vec[3 * selP + 1];
00147     ptrLaser[3 * selP + 2] = laser_pts_vec[3 * selP + 2];
00148   }
00149 
00150   //calculate the R,t use the OpenCV function
00151   cvFindExtrinsicCameraParams2 (p_object_points, p_image_points, intrinsic_M, distortion_V, rotation_V, translate_V);
00152   cvRodrigues2 (rotation_V, rotation_M, NULL);
00153   cvSave ("rotation.xml", rotation_M);  //save the parameters
00154   cvSave ("translation.xml", translate_V);
00155   cvReleaseMat (&p_image_points);       //release these matrices
00156   cvReleaseMat (&p_object_points);
00157   cvReleaseMat (&intrinsic_M);
00158   cvReleaseMat (&rotation_V);
00159   cvReleaseMat (&rotation_M);
00160   cvReleaseMat (&translate_V);
00161 
00162   return 0;
00163 }


camera_laser_calibration
Author(s): Zhao Cilang,Yan Fei,Zhuang Yan/zhuang@dlut.edu.cn
autogenerated on Sun Jan 5 2014 11:05:02