convolve.cpp
Go to the documentation of this file.
00001 /*
00002  * Software License Agreement (BSD License)
00003  *
00004  *  Point Cloud Library (PCL) - www.pointclouds.org
00005  *  Copyright (c) 2012, Willow Garage, Inc.
00006  *  All rights reserved.
00007  *
00008  *  Redistribution and use in source and binary forms, with or without
00009  *  modification, are permitted provided that the following conditions
00010  *  are met:
00011  *
00012  *   * Redistributions of source code must retain the above copyright
00013  *     notice, this list of conditions and the following disclaimer.
00014  *   * Redistributions in binary form must reproduce the above
00015  *     copyright notice, this list of conditions and the following
00016  *     disclaimer in the documentation and/or other materials provided
00017  *     with the distribution.
00018  *   * Neither the name of Willow Garage, Inc. nor the names of its
00019  *     contributors may be used to endorse or promote products derived
00020  *     from this software without specific prior written permission.
00021  *
00022  *  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
00023  *  "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
00024  *  LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
00025  *  FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
00026  *  COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
00027  *  INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
00028  *  BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
00029  *  LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
00030  *  CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
00031  *  LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
00032  *  ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
00033  *  POSSIBILITY OF SUCH DAMAGE.
00034  *
00035  * $Id$
00036  */
00037 
00038 #include <pcl/point_types.h>
00039 #include <pcl/io/pcd_io.h>
00040 #include <pcl/console/parse.h>
00041 #include <pcl/console/print.h>
00042 #include <pcl/filters/convolution.h>
00043 #include <pcl/visualization/pcl_visualizer.h>
00044 #include <pcl/common/time.h>
00045 
00046 void
00047 usage (char ** argv)
00048 {
00049   pcl::console::print_info ("usage: %s <filename> <-r|-c|-s> [-p <borders policy>] [-t <number of threads>] [-d <distance>]\n\n", argv[0]);
00050   pcl::console::print_info ("Where options are:\n");
00051   pcl::console::print_info ("\t\t\t-r convolve rows\n");
00052   pcl::console::print_info ("\t\t\t-c convolve columns\n");
00053   pcl::console::print_info ("\t\t\t-s convolve separate\n");
00054   pcl::console::print_info ("\t\t\t-p borders policy\n");
00055   pcl::console::print_info ("\t\t\t\t Z zero padding, default\n");
00056   pcl::console::print_info ("\t\t\t\t D duplicate borders\n");
00057   pcl::console::print_info ("\t\t\t\t M mirror borders\n");
00058   pcl::console::print_info ("\t\t\t-t optional, number of threads, default 1\n");
00059   pcl::console::print_info ("\t\t\t-d optional, distance threshold, default 0.001\n");
00060 }
00061 
00062 int
00063 main (int argc, char ** argv)
00064 {
00065   int viewport_source, viewport_convolved = 0;
00066   int direction = -1;
00067   int nb_threads = 0;
00068   char border_policy = 'Z';
00069   double threshold = 0.001;
00070   pcl::filters::Convolution<pcl::PointXYZRGB, pcl::PointXYZRGB> convolution;
00071   Eigen::ArrayXf gaussian_kernel(5);
00072   gaussian_kernel << 1.f/16, 1.f/4, 3.f/8, 1.f/4, 1.f/16;
00073   pcl::console::print_info ("convolution kernel:");
00074   for (int i = 0; i < gaussian_kernel.size (); ++i)
00075     pcl::console::print_info (" %f", gaussian_kernel[i]);
00076   pcl::console::print_info ("\n");
00077 
00078   if (argc < 3)
00079   {
00080     usage (argv);
00081     return 1;
00082   }
00083 
00084   // check if user is requesting help
00085   std::string arg (argv[1]);
00086 
00087   if (arg == "--help" || arg == "-h")
00088   {
00089     usage (argv);
00090     return 1;
00091   }
00092 
00093   // user don't need help find convolving direction
00094   // convolve row
00095   if (pcl::console::find_switch (argc, argv, "-r"))
00096     direction = 0;
00097   else
00098   {
00099     // convolve column
00100     if (pcl::console::find_switch (argc, argv, "-c"))
00101       direction = 1;
00102     else
00103       // convolve both
00104       if (pcl::console::find_switch (argc, argv, "-s"))
00105         direction = 2;
00106       else
00107       {
00108         // wrong direction given print usage
00109         usage (argv);
00110         return 1;
00111       }
00112   }
00113 
00114   // number of threads if any
00115   if (pcl::console::parse_argument (argc, argv, "-t", nb_threads) != -1 )
00116   {
00117     if (nb_threads <= 0)
00118       nb_threads = 1;
00119   }
00120   convolution.setNumberOfThreads (nb_threads);
00121 
00122   // borders policy if any
00123   if (pcl::console::parse_argument (argc, argv, "-p", border_policy) != -1 )
00124   {
00125     switch (border_policy)
00126     {
00127       case 'Z' : convolution.setBordersPolicy (pcl::filters::Convolution<pcl::PointXYZRGB, pcl::PointXYZRGB>::BORDERS_POLICY_IGNORE);
00128         break;
00129       case 'M' : convolution.setBordersPolicy (pcl::filters::Convolution<pcl::PointXYZRGB, pcl::PointXYZRGB>::BORDERS_POLICY_MIRROR);
00130         break;
00131       case 'D' : convolution.setBordersPolicy (pcl::filters::Convolution<pcl::PointXYZRGB, pcl::PointXYZRGB>::BORDERS_POLICY_DUPLICATE);
00132         break;
00133       default :
00134       {
00135         usage (argv);
00136         return (1);
00137       }
00138     }
00139   }
00140   else
00141     convolution.setBordersPolicy (pcl::filters::Convolution<pcl::PointXYZRGB, pcl::PointXYZRGB>::BORDERS_POLICY_IGNORE);
00142 
00143   // distance threshold if any
00144   if (pcl::console::parse_argument (argc, argv, "-d", threshold) == -1 )
00145   {
00146     threshold = 0.01;
00147   }
00148   convolution.setDistanceThreshold (static_cast<float> (threshold));
00149 
00150   // all set
00151   // we have file name and convolving direction
00152   pcl::PointCloud<pcl::PointXYZRGB>::Ptr cloud (new pcl::PointCloud<pcl::PointXYZRGB> ());
00153   if (pcl::io::loadPCDFile (argv[1], *cloud) == -1)
00154   {
00155     pcl::console::print_error ("Couldn't read file %s \n", argv[1]);
00156     return (-1);
00157   }
00158   cloud->is_dense = false;
00159   convolution.setInputCloud (cloud);
00160   convolution.setKernel (gaussian_kernel);
00161   pcl::PointCloud<pcl::PointXYZRGB>::Ptr convolved (new pcl::PointCloud<pcl::PointXYZRGB> ());
00162   double t0;
00163   pcl::console::print_info ("convolving %s along \n", argv[1]);
00164   std::ostringstream convolved_label;
00165   convolved_label << "convolved along ";
00166   switch (direction)
00167   {
00168     case 0:
00169     {
00170       convolved_label << "rows... ";
00171       t0 = pcl::getTime ();
00172       convolution.convolveRows (*convolved);
00173       break;
00174     }
00175     case 1:
00176     {
00177       convolved_label << "columns... ";
00178       t0 = pcl::getTime ();
00179       convolution.convolveCols (*convolved);
00180       break;
00181     }
00182     case 2:
00183     {
00184       convolved_label << "rows and columns... ";
00185       t0 = pcl::getTime ();
00186       convolution.convolve (*convolved);
00187       break;
00188     }
00189   }
00190   convolved_label << pcl::getTime () - t0 << "s";
00191   // Display
00192   boost::shared_ptr<pcl::visualization::PCLVisualizer> viewer (new pcl::visualization::PCLVisualizer ("Convolution"));
00193   // viewport stuff
00194   viewer->createViewPort (0, 0, 0.5, 1, viewport_source);
00195   viewer->createViewPort (0.5, 0, 1, 1, viewport_convolved);
00196   viewer->setBackgroundColor (0, 0, 0);
00197 
00198   // Source
00199   pcl::visualization::PointCloudColorHandlerRGBField<pcl::PointXYZRGB> color_handler_source (cloud);
00200   viewer->addPointCloud<pcl::PointXYZRGB> (cloud, color_handler_source, "source", viewport_source);
00201   viewer->addText ("source", 10, 10, "source_label", viewport_source);
00202 
00203   // Convolved
00204   pcl::visualization::PointCloudColorHandlerRGBField<pcl::PointXYZRGB> color_handler_convolved (convolved);
00205   viewer->addPointCloud<pcl::PointXYZRGB> (convolved, color_handler_convolved, "convolved", viewport_convolved);
00206   viewer->addText (convolved_label.str (), 10, 10, "convolved_label", viewport_convolved);
00207   viewer->spin ();
00208   pcl::PCDWriter writer;
00209   writer.write<pcl::PointXYZRGB> ("convolved.pcd", *convolved, false);
00210 }


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