00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
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
00085 std::string arg (argv[1]);
00086
00087 if (arg == "--help" || arg == "-h")
00088 {
00089 usage (argv);
00090 return 1;
00091 }
00092
00093
00094
00095 if (pcl::console::find_switch (argc, argv, "-r"))
00096 direction = 0;
00097 else
00098 {
00099
00100 if (pcl::console::find_switch (argc, argv, "-c"))
00101 direction = 1;
00102 else
00103
00104 if (pcl::console::find_switch (argc, argv, "-s"))
00105 direction = 2;
00106 else
00107 {
00108
00109 usage (argv);
00110 return 1;
00111 }
00112 }
00113
00114
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
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
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
00151
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
00192 boost::shared_ptr<pcl::visualization::PCLVisualizer> viewer (new pcl::visualization::PCLVisualizer ("Convolution"));
00193
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
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
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 }