add_gaussian_noise.cpp
Go to the documentation of this file.
00001 /*
00002  * Software License Agreement (BSD License)
00003  *
00004  *  Copyright (c) 2011, Willow Garage, Inc.
00005  *  All rights reserved.
00006  *
00007  *  Redistribution and use in source and binary forms, with or without
00008  *  modification, are permitted provided that the following conditions
00009  *  are met:
00010  *
00011  *   * Redistributions of source code must retain the above copyright
00012  *     notice, this list of conditions and the following disclaimer.
00013  *   * Redistributions in binary form must reproduce the above
00014  *     copyright notice, this list of conditions and the following
00015  *     disclaimer in the documentation and/or other materials provided
00016  *     with the distribution.
00017  *   * Neither the name of the copyright holder(s) nor the names of its
00018  *     contributors may be used to endorse or promote products derived
00019  *     from this software without specific prior written permission.
00020  *
00021  *  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
00022  *  "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
00023  *  LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
00024  *  FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
00025  *  COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
00026  *  INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
00027  *  BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
00028  *  LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
00029  *  CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
00030  *  LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
00031  *  ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
00032  *  POSSIBILITY OF SUCH DAMAGE.
00033  *
00034  * $Id$
00035  *
00036  */
00037 
00038 #include <pcl/PCLPointCloud2.h>
00039 #include <pcl/point_types.h>
00040 #include <pcl/io/pcd_io.h>
00041 #include <pcl/console/print.h>
00042 #include <pcl/console/parse.h>
00043 #include <pcl/console/time.h>
00044 #include "boost.h"
00045 
00046 using namespace pcl;
00047 using namespace pcl::io;
00048 using namespace pcl::console;
00049 
00050 double default_standard_deviation = 0.01;
00051 
00052 void
00053 printHelp (int, char **argv)
00054 {
00055   print_error ("Syntax is: %s input.pcd output.pcd <options>\n", argv[0]);
00056   print_info ("  where options are:\n");
00057   print_info ("                     -sd X = the standard deviation for the normal distribution (default: ");
00058   print_value ("%f", default_standard_deviation); print_info (")\n");
00059 }
00060 
00061 bool
00062 loadCloud (const std::string &filename, pcl::PCLPointCloud2 &cloud)
00063 {
00064   TicToc tt;
00065   print_highlight ("Loading "); print_value ("%s ", filename.c_str ());
00066 
00067   tt.tic ();
00068   if (loadPCDFile (filename, cloud) < 0)
00069     return (false);
00070   print_info ("[done, "); print_value ("%g", tt.toc ()); print_info (" ms: "); print_value ("%d", cloud.width * cloud.height); print_info (" points]\n");
00071   print_info ("Available dimensions: "); print_value ("%s\n", pcl::getFieldsList (cloud).c_str ());
00072 
00073   return (true);
00074 }
00075 
00076 void
00077 compute (const pcl::PCLPointCloud2::ConstPtr &input, pcl::PCLPointCloud2 &output,
00078          double standard_deviation)
00079 {
00080   TicToc tt;
00081   tt.tic ();
00082 
00083   print_highlight ("Adding Gaussian noise with mean 0.0 and standard deviation %f\n", standard_deviation);
00084 
00085   PointCloud<PointXYZ>::Ptr xyz_cloud (new pcl::PointCloud<PointXYZ> ());
00086   fromPCLPointCloud2 (*input, *xyz_cloud);
00087 
00088   PointCloud<PointXYZ>::Ptr xyz_cloud_filtered (new PointCloud<PointXYZ> ());
00089   xyz_cloud_filtered->points.resize (xyz_cloud->points.size ());
00090   xyz_cloud_filtered->header = xyz_cloud->header;
00091   xyz_cloud_filtered->width = xyz_cloud->width;
00092   xyz_cloud_filtered->height = xyz_cloud->height;
00093 
00094 
00095   boost::mt19937 rng; rng.seed (static_cast<unsigned int> (time (0)));
00096   boost::normal_distribution<> nd (0, standard_deviation);
00097   boost::variate_generator<boost::mt19937&, boost::normal_distribution<> > var_nor (rng, nd);
00098 
00099   for (size_t point_i = 0; point_i < xyz_cloud->points.size (); ++point_i)
00100   {
00101     xyz_cloud_filtered->points[point_i].x = xyz_cloud->points[point_i].x + static_cast<float> (var_nor ());
00102     xyz_cloud_filtered->points[point_i].y = xyz_cloud->points[point_i].y + static_cast<float> (var_nor ());
00103     xyz_cloud_filtered->points[point_i].z = xyz_cloud->points[point_i].z + static_cast<float> (var_nor ());
00104   }
00105 
00106   pcl::PCLPointCloud2 input_xyz_filtered;
00107   toPCLPointCloud2 (*xyz_cloud_filtered, input_xyz_filtered);
00108   concatenateFields (*input, input_xyz_filtered, output);
00109 
00110   print_info ("[done, "); print_value ("%g", tt.toc ()); print_info (" ms: "); print_value ("%d", output.width * output.height); print_info (" points]\n");
00111 }
00112 
00113 void
00114 saveCloud (const std::string &filename, const pcl::PCLPointCloud2 &output)
00115 {
00116   TicToc tt;
00117   tt.tic ();
00118 
00119   print_highlight ("Saving "); print_value ("%s ", filename.c_str ());
00120 
00121   pcl::io::savePCDFile (filename, output);
00122 
00123   print_info ("[done, "); print_value ("%g", tt.toc ()); print_info (" ms: "); print_value ("%d", output.width * output.height); print_info (" points]\n");
00124 }
00125 
00126 /* ---[ */
00127 int
00128 main (int argc, char** argv)
00129 {
00130   print_info ("Add Gaussian noise to a point cloud. For more information, use: %s -h\n", argv[0]);
00131 
00132   if (argc < 3)
00133   {
00134     printHelp (argc, argv);
00135     return (-1);
00136   }
00137 
00138   // Parse the command line arguments for .pcd files
00139   std::vector<int> p_file_indices;
00140   p_file_indices = parse_file_extension_argument (argc, argv, ".pcd");
00141   if (p_file_indices.size () != 2)
00142   {
00143     print_error ("Need one input PCD file and one output PCD file to continue.\n");
00144     return (-1);
00145   }
00146 
00147   // Command line parsing
00148   double standard_deviation = default_standard_deviation;
00149   parse_argument (argc, argv, "-sd", standard_deviation);
00150 
00151   // Load the first file
00152   pcl::PCLPointCloud2::Ptr cloud (new pcl::PCLPointCloud2);
00153   if (!loadCloud (argv[p_file_indices[0]], *cloud))
00154     return (-1);
00155 
00156   // Add the noise
00157   pcl::PCLPointCloud2 output;
00158   compute (cloud, output, standard_deviation);
00159 
00160   // Save into the second file
00161   saveCloud (argv[p_file_indices[1]], output);
00162 }
00163 


pcl
Author(s): Open Perception
autogenerated on Wed Aug 26 2015 15:22:32