Go to the documentation of this file.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 #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]);
00046 int n_file_num = atoi (argv[2]);
00047 ifstream inf_vision;
00048 ifstream inf_laser;
00049 std::vector < float >vision_pts_vec;
00050 std::vector < float >laser_pts_vec;
00051
00052
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
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
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
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
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
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);
00154 cvSave ("translation.xml", translate_V);
00155 cvReleaseMat (&p_image_points);
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 }