utest.cpp
Go to the documentation of this file.
00001 /*
00002  * Software License Agreement (Apache License)
00003  *
00004  * Copyright (c) 2014, Southwest Research Institute
00005  *
00006  * Licensed under the Apache License, Version 2.0 (the "License");
00007  * you may not use this file except in compliance with the License.
00008  * You may obtain a copy of the License at
00009  *
00010  * http://www.apache.org/licenses/LICENSE-2.0
00011  *
00012  * Unless required by applicable law or agreed to in writing, software
00013  * distributed under the License is distributed on an "AS IS" BASIS,
00014  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00015  * See the License for the specific language governing permissions and
00016  * limitations under the License.
00017  */
00018 
00019 #include <industrial_extrinsic_cal/calibration_job_definition.h>
00020 
00021 #include <gtest/gtest.h>
00022 #include <yaml-cpp/yaml.h>
00023 #include <fstream>
00024 
00025 using namespace industrial_extrinsic_cal;
00026 using industrial_extrinsic_cal::CalibrationJob;
00027 
00028 void print_AAtoH(double x, double y, double z, double tx, double ty, double tz);
00029 
00030 std::string camera_file_name("/home/cgomez/ros/hydro/catkin_ws/src/industrial_calibration/industrial_extrinsic_cal/yaml/test1_camera_def.yaml");
00031 std::string target_file_name("/home/cgomez/ros/hydro/catkin_ws/src/industrial_calibration/industrial_extrinsic_cal/yaml/test1_target_def.yaml");
00032 std::string caljob_file_name("/home/cgomez/ros/hydro/catkin_ws/src/industrial_calibration/industrial_extrinsic_cal/yaml/test1_caljob_def.yaml");
00033 
00034 //CalibrationJob Cal_job(camera_file_name, target_file_name, caljob_file_name);
00035 CalibrationJob Cal_job(camera_file_name, target_file_name, caljob_file_name);
00036 
00037 //need: Camera intrinstics, camera extrinsics, observations (x,y),
00038 //camera name, target name, target pose, and point position
00039 std::vector<int> point_ids;
00040 std::vector<float> obs_x;
00041 std::vector<float> obs_y;
00042 
00043 P_BLOCK intrinsics;
00044 P_BLOCK extrinsics;
00045 P_BLOCK target_pose;
00046 P_BLOCK pnt_pos;
00047 
00048 
00049 TEST(DISABLED_IndustrialExtrinsicCalSuite, load_check)
00050 {
00051   //need: Camera intrinstics, camera extrinsics, observations (x,y),
00052   //camera name, target name, scene id, pnt id, target pose, and point position
00053 
00054   EXPECT_TRUE(Cal_job.load());//should import camera instrinsics
00055 
00056   //read in observation file which contains Points with pnt_id, x and y
00057   std::ifstream obs_input_file("/home/cgomez/ros/hydro/catkin_ws/src/industrial_calibration/industrial_extrinsic_cal/observations.txt");
00058   YAML::Parser obs_parser(obs_input_file);
00059 
00060   YAML::Node obs_doc;
00061   obs_parser.GetNextDocument(obs_doc);
00062 
00063   // read in all observations
00064   if (const YAML::Node *observations = obs_doc.FindValue("Points"))
00065   {
00066     ROS_DEBUG_STREAM("Found "<<observations->size() <<" observations ");
00067     point_ids.resize(observations->size());
00068     obs_x.resize(observations->size());
00069     obs_y.resize(observations->size());
00070     for (unsigned int i = 0; i < observations->size(); i++)
00071     {
00072       (*observations)[i]["Point_id"] >> point_ids.at(i);
00073 
00074       const YAML::Node *points_node = (*observations)[i].FindValue("Pnts");
00075       std::vector<float> temp_pnt;
00076       (*points_node) >> temp_pnt;
00077       obs_x.at(i) = temp_pnt[0];
00078       obs_y.at(i) = temp_pnt[1];
00079     }
00080   }
00081 
00082 }
00083 
00084 TEST(DISABLED_IndustrialExtrinsicCalSuite, param_check)
00085 {
00086   //need: Camera intrinstics, camera extrinsics, observations (x,y),
00087   //camera name, target name, scene id, pnt id, target pose, and point position
00088   std::vector<ObservationScene> obs_scene_list_ = Cal_job.getSceneList();
00089 
00090   EXPECT_STRCASEEQ("Asus1", obs_scene_list_.at(0).cameras_in_scene_.at(0)->camera_name_.c_str());
00091 
00092   EXPECT_STRCASEEQ("Checkerboard",
00093                    obs_scene_list_.at(0).observation_command_list_.at(0).target->target_name.c_str());
00094 
00095   CeresBlocks c_blocks_ = Cal_job.getCeresBlocks();
00096   ASSERT_GT(c_blocks_.static_cameras_.size(), 0);
00097 
00098   intrinsics = c_blocks_.getStaticCameraParameterBlockIntrinsics("Asus1");
00099   extrinsics = c_blocks_.getStaticCameraParameterBlockExtrinsics("Asus1");
00100 
00101   target_pose = c_blocks_.getStaticTargetPoseParameterBlock("Checkerboard");
00102   pnt_pos = c_blocks_.getStaticTargetPointParameterBlock("Checkerboard", 0);
00103 
00104   //Cal_job.current_scene_=1;
00105 
00106   EXPECT_EQ(525, intrinsics[0]);//focal_length_x from camera yaml file
00107   EXPECT_EQ(525, intrinsics[1]);//focal_length_y from camera yaml file
00108   EXPECT_EQ(320, intrinsics[2]);//center_x from camera yaml file
00109   EXPECT_EQ(240, intrinsics[3]);//center_y from camera yaml file
00110   EXPECT_EQ(0.01, intrinsics[4]);//distortion_k1 from camera yaml file
00111   EXPECT_EQ(0.02, intrinsics[5]);//distortion_k2 from camera yaml file
00112   EXPECT_EQ(0.03, intrinsics[6]);//distortion_k3 from camera yaml file
00113   EXPECT_EQ(0.01, intrinsics[7]);//distortion_p1 from camera yaml file
00114   EXPECT_EQ(0.01, intrinsics[8]);//distortion_p2 from camera yaml file
00115 /*  ROS_INFO_STREAM("instrinsics 0: "<<intrinsics[0]);
00116   ROS_INFO_STREAM("instrinsics 1: "<<intrinsics[1]);
00117   ROS_INFO_STREAM("instrinsics 2: "<<intrinsics[2]);
00118   ROS_INFO_STREAM("instrinsics 3: "<<intrinsics[3]);
00119   ROS_INFO_STREAM("instrinsics 4: "<<intrinsics[4]);
00120   ROS_INFO_STREAM("instrinsics 5: "<<intrinsics[5]);
00121   ROS_INFO_STREAM("instrinsics 6: "<<intrinsics[6]);
00122   ROS_INFO_STREAM("instrinsics 7: "<<intrinsics[7]);
00123   ROS_INFO_STREAM("instrinsics 8: "<<intrinsics[8]);
00124   ROS_INFO_STREAM("extrinsics 0: "<<extrinsics[0]);
00125   ROS_INFO_STREAM("extrinsics 1: "<<extrinsics[1]);
00126   ROS_INFO_STREAM("extrinsics 2: "<<extrinsics[2]);
00127   ROS_INFO_STREAM("extrinsics 3: "<<extrinsics[3]);
00128   ROS_INFO_STREAM("extrinsics 4: "<<extrinsics[4]);
00129   ROS_INFO_STREAM("extrinsics 5: "<<extrinsics[5]);*/
00130   EXPECT_EQ(1.0, extrinsics[0]);//angle_axis_ax in camera yaml file
00131   EXPECT_EQ(1.1, extrinsics[1]);//angle_axis_ay in camera yaml file
00132   EXPECT_EQ(1.2, extrinsics[2]);//angle_axis_az in camera yaml file
00133   EXPECT_EQ(1.3, extrinsics[3]);//position_x in camera yaml file
00134   EXPECT_EQ(1.4, extrinsics[4]);//position_y in camera yaml file
00135   EXPECT_EQ(1.5, extrinsics[5]);//position_z in camera yaml file
00136 /*  ROS_INFO_STREAM("target_pose 0: "<<target_pose[0]);
00137   ROS_INFO_STREAM("target_pose 1: "<<target_pose[1]);
00138   ROS_INFO_STREAM("target_pose 2: "<<target_pose[2]);
00139   ROS_INFO_STREAM("target_pose 3: "<<target_pose[3]);
00140   ROS_INFO_STREAM("target_pose 4: "<<target_pose[4]);
00141   ROS_INFO_STREAM("target_pose 5: "<<target_pose[5]);*/
00142   EXPECT_EQ(2.2, target_pose[0]);//position_x from target yaml
00143   EXPECT_EQ(2.3, target_pose[1]);//position_y from target yaml
00144   EXPECT_EQ(2.4, target_pose[2]);//position_z from target yaml
00145   EXPECT_EQ(1.1, target_pose[3]);//angle_axis_ax from target yaml
00146   EXPECT_EQ(2.0, target_pose[4]);//angle_axis_ay from target yaml
00147   EXPECT_EQ(2.1, target_pose[5]);//angle_axis_ay from target yaml
00148 
00149   //check first and last points (from target yaml)
00150   EXPECT_EQ(0.0, pnt_pos[0]);
00151   EXPECT_EQ(0.0, pnt_pos[1]);
00152   EXPECT_EQ(0.0, pnt_pos[2]);
00153   pnt_pos = c_blocks_.getStaticTargetPointParameterBlock("Checkerboard", 120);
00154   EXPECT_FLOAT_EQ(0.40, pnt_pos[0]);
00155   EXPECT_FLOAT_EQ(0.40, pnt_pos[1]);
00156   EXPECT_EQ(0.0, pnt_pos[2]);
00157 
00158   ASSERT_GT(point_ids.size(), 0);
00159   //check first and last points (from observation.txt)
00160   EXPECT_EQ(0,point_ids.at(0));
00161   EXPECT_EQ(120,point_ids.at(120));
00162   EXPECT_FLOAT_EQ(64.8908386230469,obs_x.at(0));
00163   EXPECT_FLOAT_EQ(58.9467353820801,obs_y.at(0));
00164   EXPECT_FLOAT_EQ(282.849487304688,obs_x.at(120));
00165   EXPECT_FLOAT_EQ(280.590637207031,obs_y.at(120));
00166 }
00167 
00168 TEST(DISABLED_IndustrialExtrinsicCalSuite, ceres_check)
00169 {
00170 
00171   CeresBlocks c_blocks_ = Cal_job.getCeresBlocks();
00172   ceres::Problem problem;//=Cal_job.getProblem();
00173   for (int i=0; i<point_ids.size(); i++)
00174   {
00175 
00176     pnt_pos = c_blocks_.getStaticTargetPointParameterBlock("Checkerboard", i);
00177   // create the cost function
00178   ceres::CostFunction* cost_function = TargetCameraReprjErrorNoDistortion::Create(obs_x.at(i), obs_y.at(i),
00179                                                                            intrinsics[0],
00180                                                                            intrinsics[1],
00181                                                                            intrinsics[2],
00182                                                                            intrinsics[3],
00183                                                                            pnt_pos[0],
00184                                                                            pnt_pos[1],
00185                                                                            pnt_pos[2]);
00186   problem.AddResidualBlock(cost_function, NULL , extrinsics, target_pose);
00187   }
00188   ceres::Solver::Options options;
00189   options.linear_solver_type = ceres::DENSE_SCHUR;
00190   options.minimizer_progress_to_stdout = true;
00191   options.max_num_iterations = 1000;
00192 
00193   ceres::Solver::Summary summary;
00194   ceres::Solve(options, &problem, &summary);
00195   std::cout << summary.BriefReport() << "\n";
00196   //std::cout << summary.FullReport() << "\n";
00197 
00198 
00199   printf("World to Camera\n");
00200   printf("Optimized Camera\n");
00201   print_AAtoH(extrinsics[0], extrinsics[1], extrinsics[2],
00202                extrinsics[3], extrinsics[4], extrinsics[5]);
00203 
00204   /*
00205   //printf("%s\n", words.c_str());
00206   printf("Camera to World Transform:\n");
00207   //print_AATasHI(C.aa[0], C.aa[1], C.aa[2], C.pos[0], C.pos[1], C.pos[2]);
00208   double R[9];
00209   double aa[3];
00210   aa[0] = extrinsics[0];
00211   aa[1] = extrinsics[1];
00212   aa[2] = extrinsics[2];
00213   ceres::AngleAxisToRotationMatrix(aa, R);
00214   double ix = -(extrinsics[3] * R[0] + extrinsics[4] * R[1] + extrinsics[5] * R[2]);
00215   double iy = -(extrinsics[3] * R[3] + extrinsics[4] * R[4] + extrinsics[5] * R[5]);
00216   double iz = -(extrinsics[3] * R[6] + extrinsics[4] * R[7] + extrinsics[5] * R[8]);
00217   printf("%6.3lf %6.3lf %6.3lf %6.3lf\n", R[0], R[1], R[2], ix);
00218   printf("%6.3lf %6.3lf %6.3lf %6.3lf\n", R[3], R[4], R[5], iy);
00219   printf("%6.3lf %6.3lf %6.3lf %6.3lf\n", R[6], R[7], R[8], iz);
00220   printf("%6.3lf %6.3lf %6.3lf %6.3lf\n", 0.0, 0.0, 0.0, 1.0);*/
00221 }
00222 // angle axis to homogeneous transform
00223 void print_AAtoH(double x, double y, double z, double tx, double ty, double tz)
00224 {
00225   double R[9];
00226   double aa[3];
00227   aa[0] = x;
00228   aa[1] = y;
00229   aa[2] = z;
00230   ceres::AngleAxisToRotationMatrix(aa, R);
00231   printf("%6.3lf %6.3lf %6.3lf %6.3lf\n", R[0], R[3], R[6], tx);
00232   printf("%6.3lf %6.3lf %6.3lf %6.3lf\n", R[1], R[4], R[7], ty);
00233   printf("%6.3lf %6.3lf %6.3lf %6.3lf\n", R[2], R[5], R[8], tz);
00234   printf("%6.3lf %6.3lf %6.3lf %6.3lf\n", 0.0, 0.0, 0.0, 1.0);
00235 }
00236 
00237 
00238 // Run all the tests that were declared with TEST()
00239 int main(int argc, char **argv)
00240 {
00241   ros::init(argc, argv, "test");
00242   testing::InitGoogleTest(&argc, argv);
00243   return RUN_ALL_TESTS();
00244 }


industrial_extrinsic_cal
Author(s): Chris Lewis
autogenerated on Wed Aug 26 2015 12:00:27