sac_model_registration.cpp
Go to the documentation of this file.
00001 #include <pcl/common/transforms.h>
00002 #include <pcl/console/parse.h>
00003 #include <pcl/console/time.h>
00004 #include <pcl/point_types.h>
00005 #include <pcl/io/pcd_io.h>
00006 #include <pcl/sample_consensus/method_types.h>
00007 #include <pcl/sample_consensus/ransac.h>
00008 #include <pcl/sample_consensus/sac_model_registration.h>
00009 
00010 using namespace pcl;
00011 
00013 void
00014 compute (const PointCloud<PointXYZ>::Ptr &input, 
00015          const PointCloud<PointXYZ>::Ptr &target,
00016          Eigen::Matrix4f &transformation,
00017          const double thresh)
00018 {
00019   SampleConsensusModelRegistration<PointXYZ>::Ptr model (new SampleConsensusModelRegistration<PointXYZ> (input));
00020   model->setInputTarget (target);
00021 
00022   RandomSampleConsensus<PointXYZ> sac (model, thresh);
00023   sac.setMaxIterations (100000);
00024 
00025   if (!sac.computeModel (2))
00026   {
00027     PCL_ERROR ("Could not compute a valid transformation!\n");
00028     return;
00029   }
00030   Eigen::VectorXf coeff;
00031   sac.getModelCoefficients (coeff);
00032   transformation.row (0) = coeff.segment<4>(0);
00033   transformation.row (1) = coeff.segment<4>(4);
00034   transformation.row (2) = coeff.segment<4>(8);
00035   transformation.row (3) = coeff.segment<4>(12);
00036 }
00037 
00039 int
00040 main (int argc, char** argv)
00041 {
00042   PointCloud<PointXYZ>::Ptr source (new PointCloud<PointXYZ>);
00043   PointCloud<PointXYZ>::Ptr target (new PointCloud<PointXYZ>);
00044 
00045   std::vector<int> p_file_indices = console::parse_file_extension_argument (argc, argv, ".pcd");
00046   if (p_file_indices.size () < 2)
00047   {
00048     PCL_ERROR ("Needs a source.PCD, and an output source_transformed.PCD file! For example: %s bun0.pcd bun0-tr.pcd\n", argv[0]);
00049     PCL_INFO ("Additionally, please specify a threshold (0.002 used by default) via: -thresh X\n");
00050     return (-1);
00051   }
00052 
00053   double thresh = 0.002;
00054   console::parse_argument (argc, argv, "-thresh", thresh);
00055 
00056   io::loadPCDFile (argv[p_file_indices[0]], *source);
00057 
00058   // Transform the dataset by translations on x (+1), y (+2), and z (+3) and add noise
00059   *target = *source;
00060   for (size_t i = 0; i < source->points.size (); ++i)
00061   {
00062     target->points[i].x += ((double)rand () / RAND_MAX) * 0.01 + 1.0;
00063     target->points[i].y += ((double)rand () / RAND_MAX) * 0.01 + 2.0;
00064     target->points[i].z += ((double)rand () / RAND_MAX) * 0.01 + 3.0;
00065   }
00066   io::savePCDFileBinary ("target.pcd", *target);
00067 
00068   // Compute
00069   Eigen::Matrix4f transform;
00070   console::TicToc tt;
00071   tt.tic ();
00072   compute (source, target, transform, thresh);
00073   tt.toc_print ();
00074   std::cerr << transform << std::endl;
00075 
00076   PointCloud<PointXYZ> output;
00077   transformPointCloud (*source, output, transform);
00078   io::savePCDFileBinary (argv[p_file_indices[1]], output);
00079 
00080   return (0);
00081 }


pcl
Author(s): Open Perception
autogenerated on Mon Oct 6 2014 03:17:44