global_cloud_align.cpp
Go to the documentation of this file.
00001 #include "global_cloud_align.h"
00002 
00003 using namespace rgbdtools;
00004 
00005 int main(int argc, char** argv)
00006 {
00007   if (argc != 3)
00008   {
00009     printUsage(argv);
00010     return -1;
00011   }
00012   
00013   std::string filename_in  = argv[1];
00014   std::string filename_out = argv[2];
00015   
00016   // read in
00017   printf("Reading cloud\n");
00018   PointCloudT::Ptr cloud;
00019   cloud.reset(new rgbdtools::PointCloudT());
00020   pcl::PCDReader reader;
00021   reader.read(filename_in, *cloud);
00022   
00023   alignGlobalCloud(cloud);
00024   
00025   return 0;
00026 }
00027 
00028 void printUsage(char** argv)
00029 {
00030   std::cerr << "error: usage is " << argv[0] 
00031             << " [filename_in] [filename_out]"
00032             << std::endl;
00033 }
00034 
00035 void alignGlobalCloud(const PointCloudT::Ptr& cloud)
00036 {
00037   double vgf_res = 0.01;
00038 
00039   // filter cloud
00040   printf("Filtering cloud\n");
00041   PointCloudT::Ptr cloud_f;
00042   cloud_f.reset(new PointCloudT());
00043   pcl::VoxelGrid<PointT> vgf;
00044   vgf.setInputCloud(cloud);
00045   vgf.setLeafSize(vgf_res, vgf_res, vgf_res);
00046   vgf.filter(*cloud_f);
00047 
00048   /*
00049   // rotate 45 deg
00050   tf::Transform t;
00051   t.setOrigin(tf::Vector3(0,0,0));
00052   tf::Quaternion q;
00053   q.setRPY(0, 0, M_PI/6.0);
00054   t.setRotation(q);
00055   pcl::transformPointCloud(*cloud_f, *cloud_f, eigenFromTf(t)); 
00056   */
00057 
00058   // show map
00059   printf("Showing map\n");
00060   cv::Mat map_r;
00061   create2DProjectionImage(*cloud_f, map_r);
00062   cv::imshow("map_r", map_r);
00063   cv::waitKey(0);
00064 
00065   // Create the normal estimation class, and pass the input dataset to it
00066   pcl::NormalEstimation<PointT, pcl::PointXYZRGBNormal> ne;
00067   ne.setInputCloud(cloud_f);
00068 
00069   // Create an empty kdtree representation, and pass it to the normal estimation object.
00070   // Its content will be filled inside the object, based on the given input dataset (as no other search surface is given).
00071   printf("Creating kd-tree\n");
00072   pcl::search::KdTree<PointT>::Ptr tree;
00073   tree.reset(new pcl::search::KdTree<PointT>());
00074   ne.setSearchMethod(tree);
00075 
00076   // Output datasets
00077   pcl::PointCloud<pcl::PointXYZRGBNormal>::Ptr cloud_normals;
00078   cloud_normals.reset(new pcl::PointCloud<pcl::PointXYZRGBNormal>);
00079 
00080   // Use all neighbors in a sphere of radius x cm
00081   ne.setRadiusSearch(0.05);
00082   
00083   // Compute the features
00084   printf("Estimating normals\n");
00085   ne.compute (*cloud_normals);
00086 
00087   /*
00088   for (unsigned int i = 0; i < cloud_f->points.size(); ++i)
00089   {
00090     const PointT& p_in = cloud_f->points[i]; 
00091     pcl::PointXYZRGBNormal& p_out = cloud_normals->points[i]; 
00092 
00093     p_out.x   = p_in.x;
00094     p_out.y   = p_in.y;
00095     p_out.z   = p_in.z;
00096     p_out.rgb = p_in.rgb;
00097   }
00098 
00099   // write out  
00100   printf("Writing out\n");
00101   pcl::PCDWriter writer;
00102   writer.write ("/home/idryanov/cloud_00_n.pcd", *cloud_normals);
00103 */
00104   
00105   // create expected histogram
00106   double hist_resolution = 0.25;
00107   cv::Mat hist_exp;
00108   buildExpectedPhiHistorgtam(hist_exp, hist_resolution, 5.0); 
00109   cv::Mat hist_exp_img;
00110   createImageFromHistogram(hist_exp, hist_exp_img);
00111   cv::imshow("hist_exp_img", hist_exp_img);
00112 
00113   // create histogram
00114   printf("creating histogram\n");
00115   cv::Mat hist;
00116   buildPhiHistogram(*cloud_normals, hist, hist_resolution);
00117 
00118   // show histogram
00119   printf("showing histogram\n");
00120   cv::Mat hist_img;
00121   createImageFromHistogram(hist, hist_img);
00122   cv::imshow("Histogram Phi", hist_img);
00123   cv::waitKey(0);
00124 
00125   // find alignement
00126   double best_angle;
00127   alignHistogram(hist, hist_exp, hist_resolution, best_angle);
00128   printf("best_angle: %f\n", best_angle);
00129 
00130   // show best aligned histogram
00131   cv::Mat hist_best;
00132   shiftHistogram(hist, hist_best, best_angle/hist_resolution);
00133   cv::Mat hist_best_img;
00134   createImageFromHistogram(hist_best, hist_best_img);
00135   cv::imshow("hist_best_img", hist_best_img);
00136   cv::waitKey(0);
00137 
00138   // derotate
00139   AffineTransform best_tf;
00140   XYZRPYToEigenAffine(0, 0, 0, 0, 0, best_angle * M_PI/180.0, best_tf);
00141 
00142   /*
00143   // derotate
00144   tf::Transform t1;
00145   t1.setOrigin(tf::Vector3(0,0,0));
00146   tf::Quaternion q1;
00147   q1.setRPY(0, 0, best_angle * M_PI/180.0);
00148   t1.setRotation(q1);
00149   */
00150   
00151   pcl::transformPointCloud(*cloud_f, *cloud_f, best_tf); 
00152 
00153   // show map
00154   cv::Mat map_f;
00155   create2DProjectionImage(*cloud_f, map_f);
00156   cv::imshow("map_f", map_f);
00157   cv::waitKey(0);
00158 
00159   printf("Done\n");
00160 }
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends


lib_rgbdtools
Author(s): Ivan Dryanovski
autogenerated on Tue Aug 27 2013 10:33:54