stereo_visual_odometry_alg.cpp
Go to the documentation of this file.
00001 #include "stereo_visual_odometry_alg.h"
00002 
00003 using namespace std;
00004 
00005 StereoVisualOdometryAlgorithm::StereoVisualOdometryAlgorithm(void)
00006 {
00007 }
00008 
00009 StereoVisualOdometryAlgorithm::~StereoVisualOdometryAlgorithm(void)
00010 {
00011 }
00012 
00013 void StereoVisualOdometryAlgorithm::config_update(Config& new_cfg, uint32_t level)
00014 {
00015   this->lock();
00016 
00017   // save the current configuration
00018   this->config_=new_cfg;
00019   
00020   this->unlock();
00021 }
00022 
00023 // StereoVisualOdometryAlgorithm Public API
00024 /*
00025 void StereoVisualOdometryAlgorithm::show_image_alg_test(cv::Mat img){
00026   ROS_WARN("mostra imatge");
00027   imshow("img", img);
00028 }
00029 */
00030 
00031 void StereoVisualOdometryAlgorithm::compute_H_Hcov(cv::Mat& image1, cv::Mat& image2, cv::Mat& image3, cv::Mat& image4, cv::Mat& H, cv::Mat& Hcov, cv::Mat& point)
00032 {
00033   CStereo_Vo vo;
00034   
00035   int minhessian=400;
00036   
00037   vector<cv::KeyPoint> kp_image1;
00038   vector<cv::KeyPoint> kp_image2;
00039   vector<cv::KeyPoint> kp_image3;
00040   vector<cv::KeyPoint> kp_image4;
00041   
00042   cv::Mat des_image1;
00043   cv::Mat des_image2;
00044   cv::Mat des_image3;
00045   cv::Mat des_image4;
00046   
00047   vector<cv::DMatch> matches_1_2;
00048   vector<cv::DMatch> matches_3_4;
00049   
00050   vector<cv::Point2f> kp_1;
00051   vector<cv::Point2f> kp_2;
00052   vector<cv::Point2f> kp_3;
00053   vector<cv::Point2f> kp_4;
00054   
00055   cv::Mat l_kp_1;
00056   cv::Mat r_kp_2;
00057   cv::Mat l_kp_3;
00058   cv::Mat r_kp_4;
00059   
00060   cv::Mat reconstructed_1_2;
00061   cv::Mat reconstructed_3_4;
00062   
00063   cv::Mat final_kp1;
00064   cv::Mat final_kp2;
00065   cv::Mat final_kp3;
00066   cv::Mat final_kp4;
00067   
00068   cv::Mat kp1;
00069   cv::Mat kp2;
00070   cv::Mat kp3;
00071   cv::Mat kp4;
00072   
00073   cv::Mat final_points_1_2;
00074   cv::Mat final_points_3_4;
00075   
00076   int dthld=5000;
00077   
00078   cv::Mat inliers_1_2;
00079   cv::Mat inliers_3_4;
00080   
00081   cv::Mat S12;
00082   cv::Mat S34;
00083   
00084   cv::Mat mean_1_2;
00085   cv::Mat mean_3_4;
00086   
00087   cv::Mat R;
00088   cv::Mat T;
00089   cv::Mat RT;
00090   
00091   int inliers;
00092   
00093   cv::Mat Srt;
00094   cv::Mat Jx;
00095   cv::Mat Ju;
00096   cv::Mat Jxt;
00097   cv::Mat Jut;
00098   
00099   cv::Mat left_projection_matrix;
00100   cv::Mat right_projection_matrix;
00101   cv::Mat initial_T;
00102   double fl1, fl2, u0l1, u0l2, fr1, fr2, u0r1, u0r2;
00103   
00104   //getting matrix projections
00105   
00106   vo.matrix_projections(fl1, fl2, u0l1, u0l2, fr1, fr2, u0r1, u0r2, initial_T, left_projection_matrix, right_projection_matrix);
00107   
00108   //detecting keypoints of two image pair
00109   
00110   vo.Keypoints_detection(image1, kp_image1, image2, kp_image2, image3, kp_image3, image4, kp_image4, minhessian);
00111   
00112   //computing descriptors of two image pair
00113     
00114    vo.descriptors_computing (image1, kp_image1, des_image1, image2, kp_image2, des_image2,
00115                              image3, kp_image3, des_image3, image4, kp_image4, des_image4);
00116    
00117   //matching descriptors
00118   
00119   vo.matching(image1, kp_image1, des_image1, image2, kp_image2, des_image2, image3, kp_image3, des_image3,
00120               image4, kp_image4, des_image4, matches_1_2, matches_3_4);
00121   
00122   for(unsigned int i = 0; i < matches_1_2.size(); i++ )
00123   {
00124     kp_1.push_back(kp_image1[matches_1_2[i].queryIdx].pt);
00125     kp_2.push_back(kp_image2[matches_1_2[i].trainIdx].pt);
00126     kp_3.push_back(kp_image3[matches_3_4[i].queryIdx].pt);
00127     kp_4.push_back(kp_image4[matches_3_4[i].trainIdx].pt);
00128   }
00129   
00130   //getting the triangulate points
00131   
00132   vo.triangulate_points(kp_1, kp_2, l_kp_1, r_kp_2, left_projection_matrix, right_projection_matrix, reconstructed_1_2);
00133   vo.triangulate_points(kp_3, kp_4, l_kp_3, r_kp_4, left_projection_matrix, right_projection_matrix, reconstructed_3_4);
00134   
00135   vo.dthld(l_kp_1, r_kp_2, l_kp_3, r_kp_4, final_kp1, final_kp2, final_kp3, final_kp4, reconstructed_1_2, reconstructed_3_4, final_points_1_2, final_points_3_4, dthld);
00136   
00137   //RANSAC
00138     
00139   vo.ransac(final_kp1, final_kp2, final_kp3, final_kp4, kp1, kp2, kp3, kp4, final_points_1_2, final_points_3_4, inliers_1_2, inliers_3_4);
00140     
00141   //getting the stereo covariance
00142     
00143   vo.stereocovar(fl1, fl2, u0l1, u0l2, fr1, fr2, u0r1, u0r2, initial_T, kp1, kp2, S12);
00144   vo.stereocovar(fl1, fl2, u0l1, u0l2, fr1, fr2, u0r1, u0r2, initial_T, kp3, kp4, S34);
00145 
00146   mean_1_2=cv::Mat::zeros(3,1,CV_64F);
00147   vo.mean_points(inliers_1_2, mean_1_2);
00148   
00149   mean_3_4=cv::Mat::zeros(3,1,CV_64F);
00150   vo.mean_points(inliers_3_4, mean_3_4);
00151     
00152   //Computing the transformation between two states
00153   
00154   vo.compute_RT(inliers_1_2, inliers_3_4, mean_1_2, mean_3_4, R, T, RT);
00155   
00156   inliers=kp1.cols;
00157   
00158   //getting the covariance of the transformation
00159     
00160   vo.covarRT(inliers, R, T, S12, S34, mean_1_2, mean_3_4, inliers_1_2, inliers_3_4, Srt);
00161     
00162   vo.JxJu(R, T, H, Jx, Ju);
00163     
00164   cv::transpose(Jx, Jxt);
00165   cv::transpose(Ju, Jut);
00166     
00167   //getting the error propagation for each iteration
00168     
00169   Hcov=Jx*Hcov*Jxt+Ju*Srt*Jut;
00170     
00171   //updating the trasformation for each iteration
00172     
00173   H=H*RT;
00174   point=RT*point;
00175   
00176   cout<<"current position: "<<point<<endl;
00177   
00178   //results
00179   
00180   cout<<"Transformation: "<< RT << endl; 
00181   cout<<"Updated transformation: "<< H << endl;
00182   cout<<"Updated error propagation: "<<Hcov<<endl;
00183   
00184 }
00185 
00186 void StereoVisualOdometryAlgorithm::extract_ypr(cv::Mat& H,double& y,double& p,double& r)
00187 {
00188   CStereo_Vo vo;
00189   cv::Mat R;
00190   
00191   R=cv::Mat::zeros(3,3,CV_64F);
00192   R.at<double>(0,0)=H.at<double>(0,0);
00193   R.at<double>(0,1)=H.at<double>(0,1);
00194   R.at<double>(0,2)=H.at<double>(0,2);
00195   R.at<double>(1,0)=H.at<double>(1,0);
00196   R.at<double>(1,1)=H.at<double>(1,1);
00197   R.at<double>(1,2)=H.at<double>(1,2);
00198   R.at<double>(2,0)=H.at<double>(2,0);
00199   R.at<double>(2,1)=H.at<double>(2,1);
00200   R.at<double>(2,2)=H.at<double>(2,2);
00201   
00202   r=atan2(R.at<double>(1,0), R.at<double>(0,0));
00203   p=atan2(-R.at<double>(2,0), R.at<double>(0,0)*cos(r)+R.at<double>(1,0)*sin(r));
00204   y=atan2(R.at<double>(2,1), R.at<double>(2,2));
00205   
00206   //cout <<"yaw: "<<y<<"    pitch: "<<p<<"      roll: "<<r<<endl;
00207   
00208 }


iri_stereo_visual_odometry
Author(s): gmoreno
autogenerated on Fri Dec 6 2013 23:59:48