range_image_border_extraction.cpp
Go to the documentation of this file.
00001 /* \author Bastian Steder */
00002 
00003 #include <iostream>
00004 
00005 #include <boost/thread/thread.hpp>
00006 #include <pcl/range_image/range_image.h>
00007 #include <pcl/io/pcd_io.h>
00008 #include <pcl/visualization/range_image_visualizer.h>
00009 #include <pcl/visualization/pcl_visualizer.h>
00010 #include <pcl/features/range_image_border_extractor.h>
00011 #include <pcl/console/parse.h>
00012 
00013 typedef pcl::PointXYZ PointType;
00014 
00015 // --------------------
00016 // -----Parameters-----
00017 // --------------------
00018 float angular_resolution = 0.5f;
00019 pcl::RangeImage::CoordinateFrame coordinate_frame = pcl::RangeImage::CAMERA_FRAME;
00020 bool setUnseenToMaxRange = false;
00021 
00022 // --------------
00023 // -----Help-----
00024 // --------------
00025 void 
00026 printUsage (const char* progName)
00027 {
00028   std::cout << "\n\nUsage: "<<progName<<" [options] <scene.pcd>\n\n"
00029             << "Options:\n"
00030             << "-------------------------------------------\n"
00031             << "-r <float>   angular resolution in degrees (default "<<angular_resolution<<")\n"
00032             << "-c <int>     coordinate frame (default "<< (int)coordinate_frame<<")\n"
00033             << "-m           Treat all unseen points to max range\n"
00034             << "-h           this help\n"
00035             << "\n\n";
00036 }
00037 
00038 // --------------
00039 // -----Main-----
00040 // --------------
00041 int 
00042 main (int argc, char** argv)
00043 {
00044   // --------------------------------------
00045   // -----Parse Command Line Arguments-----
00046   // --------------------------------------
00047   if (pcl::console::find_argument (argc, argv, "-h") >= 0)
00048   {
00049     printUsage (argv[0]);
00050     return 0;
00051   }
00052   if (pcl::console::find_argument (argc, argv, "-m") >= 0)
00053   {
00054     setUnseenToMaxRange = true;
00055     cout << "Setting unseen values in range image to maximum range readings.\n";
00056   }
00057   int tmp_coordinate_frame;
00058   if (pcl::console::parse (argc, argv, "-c", tmp_coordinate_frame) >= 0)
00059   {
00060     coordinate_frame = pcl::RangeImage::CoordinateFrame (tmp_coordinate_frame);
00061     cout << "Using coordinate frame "<< (int)coordinate_frame<<".\n";
00062   }
00063   if (pcl::console::parse (argc, argv, "-r", angular_resolution) >= 0)
00064     cout << "Setting angular resolution to "<<angular_resolution<<"deg.\n";
00065   angular_resolution = pcl::deg2rad (angular_resolution);
00066   
00067   // ------------------------------------------------------------------
00068   // -----Read pcd file or create example point cloud if not given-----
00069   // ------------------------------------------------------------------
00070   pcl::PointCloud<PointType>::Ptr point_cloud_ptr (new pcl::PointCloud<PointType>);
00071   pcl::PointCloud<PointType>& point_cloud = *point_cloud_ptr;
00072   pcl::PointCloud<pcl::PointWithViewpoint> far_ranges;
00073   Eigen::Affine3f scene_sensor_pose (Eigen::Affine3f::Identity ());
00074   std::vector<int> pcd_filename_indices = pcl::console::parse_file_extension_argument (argc, argv, "pcd");
00075   if (!pcd_filename_indices.empty ())
00076   {
00077     std::string filename = argv[pcd_filename_indices[0]];
00078     if (pcl::io::loadPCDFile (filename, point_cloud) == -1)
00079     {
00080       cout << "Was not able to open file \""<<filename<<"\".\n";
00081       printUsage (argv[0]);
00082       return 0;
00083     }
00084     scene_sensor_pose = Eigen::Affine3f (Eigen::Translation3f (point_cloud.sensor_origin_[0],
00085                                                                point_cloud.sensor_origin_[1],
00086                                                                point_cloud.sensor_origin_[2])) *
00087                         Eigen::Affine3f (point_cloud.sensor_orientation_);
00088   
00089     std::string far_ranges_filename = pcl::getFilenameWithoutExtension (filename)+"_far_ranges.pcd";
00090     if (pcl::io::loadPCDFile(far_ranges_filename.c_str(), far_ranges) == -1)
00091       std::cout << "Far ranges file \""<<far_ranges_filename<<"\" does not exists.\n";
00092   }
00093   else
00094   {
00095     cout << "\nNo *.pcd file given => Genarating example point cloud.\n\n";
00096     for (float x=-0.5f; x<=0.5f; x+=0.01f)
00097     {
00098       for (float y=-0.5f; y<=0.5f; y+=0.01f)
00099       {
00100         PointType point;  point.x = x;  point.y = y;  point.z = 2.0f - y;
00101         point_cloud.points.push_back (point);
00102       }
00103     }
00104     point_cloud.width = (int) point_cloud.points.size ();  point_cloud.height = 1;
00105   }
00106   
00107   // -----------------------------------------------
00108   // -----Create RangeImage from the PointCloud-----
00109   // -----------------------------------------------
00110   float noise_level = 0.0;
00111   float min_range = 0.0f;
00112   int border_size = 1;
00113   boost::shared_ptr<pcl::RangeImage> range_image_ptr (new pcl::RangeImage);
00114   pcl::RangeImage& range_image = *range_image_ptr;   
00115   range_image.createFromPointCloud (point_cloud, angular_resolution, pcl::deg2rad (360.0f), pcl::deg2rad (180.0f),
00116                                    scene_sensor_pose, coordinate_frame, noise_level, min_range, border_size);
00117   range_image.integrateFarRanges (far_ranges);
00118   if (setUnseenToMaxRange)
00119     range_image.setUnseenToMaxRange ();
00120 
00121   // --------------------------------------------
00122   // -----Open 3D viewer and add point cloud-----
00123   // --------------------------------------------
00124   pcl::visualization::PCLVisualizer viewer ("3D Viewer");
00125   viewer.setBackgroundColor (1, 1, 1);
00126   viewer.addCoordinateSystem (1.0f);
00127   pcl::visualization::PointCloudColorHandlerCustom<PointType> point_cloud_color_handler (point_cloud_ptr, 0, 0, 0);
00128   viewer.addPointCloud (point_cloud_ptr, point_cloud_color_handler, "original point cloud");
00129   //PointCloudColorHandlerCustom<pcl::PointWithRange> range_image_color_handler (range_image_ptr, 150, 150, 150);
00130   //viewer.addPointCloud (range_image_ptr, range_image_color_handler, "range image");
00131   //viewer.setPointCloudRenderingProperties (PCL_VISUALIZER_POINT_SIZE, 2, "range image");
00132   
00133   // -------------------------
00134   // -----Extract borders-----
00135   // -------------------------
00136   pcl::RangeImageBorderExtractor border_extractor (&range_image);
00137   pcl::PointCloud<pcl::BorderDescription> border_descriptions;
00138   border_extractor.compute (border_descriptions);
00139   
00140   // ----------------------------------
00141   // -----Show points in 3D viewer-----
00142   // ----------------------------------
00143   pcl::PointCloud<pcl::PointWithRange>::Ptr border_points_ptr(new pcl::PointCloud<pcl::PointWithRange>),
00144                                             veil_points_ptr(new pcl::PointCloud<pcl::PointWithRange>),
00145                                             shadow_points_ptr(new pcl::PointCloud<pcl::PointWithRange>);
00146   pcl::PointCloud<pcl::PointWithRange>& border_points = *border_points_ptr,
00147                                       & veil_points = * veil_points_ptr,
00148                                       & shadow_points = *shadow_points_ptr;
00149   for (int y=0; y< (int)range_image.height; ++y)
00150   {
00151     for (int x=0; x< (int)range_image.width; ++x)
00152     {
00153       if (border_descriptions.points[y*range_image.width + x].traits[pcl::BORDER_TRAIT__OBSTACLE_BORDER])
00154         border_points.points.push_back (range_image.points[y*range_image.width + x]);
00155       if (border_descriptions.points[y*range_image.width + x].traits[pcl::BORDER_TRAIT__VEIL_POINT])
00156         veil_points.points.push_back (range_image.points[y*range_image.width + x]);
00157       if (border_descriptions.points[y*range_image.width + x].traits[pcl::BORDER_TRAIT__SHADOW_BORDER])
00158         shadow_points.points.push_back (range_image.points[y*range_image.width + x]);
00159     }
00160   }
00161   pcl::visualization::PointCloudColorHandlerCustom<pcl::PointWithRange> border_points_color_handler (border_points_ptr, 0, 255, 0);
00162   viewer.addPointCloud<pcl::PointWithRange> (border_points_ptr, border_points_color_handler, "border points");
00163   viewer.setPointCloudRenderingProperties (pcl::visualization::PCL_VISUALIZER_POINT_SIZE, 7, "border points");
00164   pcl::visualization::PointCloudColorHandlerCustom<pcl::PointWithRange> veil_points_color_handler (veil_points_ptr, 255, 0, 0);
00165   viewer.addPointCloud<pcl::PointWithRange> (veil_points_ptr, veil_points_color_handler, "veil points");
00166   viewer.setPointCloudRenderingProperties (pcl::visualization::PCL_VISUALIZER_POINT_SIZE, 7, "veil points");
00167   pcl::visualization::PointCloudColorHandlerCustom<pcl::PointWithRange> shadow_points_color_handler (shadow_points_ptr, 0, 255, 255);
00168   viewer.addPointCloud<pcl::PointWithRange> (shadow_points_ptr, shadow_points_color_handler, "shadow points");
00169   viewer.setPointCloudRenderingProperties (pcl::visualization::PCL_VISUALIZER_POINT_SIZE, 7, "shadow points");
00170   
00171   //-------------------------------------
00172   // -----Show points on range image-----
00173   // ------------------------------------
00174   pcl::visualization::RangeImageVisualizer* range_image_borders_widget = NULL;
00175   range_image_borders_widget =
00176     pcl::visualization::RangeImageVisualizer::getRangeImageBordersWidget (range_image, -std::numeric_limits<float>::infinity (), std::numeric_limits<float>::infinity (), false,
00177                                                                           border_descriptions, "Range image with borders");
00178   // -------------------------------------
00179   
00180   
00181   //--------------------
00182   // -----Main loop-----
00183   //--------------------
00184   while (!viewer.wasStopped ())
00185   {
00186     range_image_borders_widget->spinOnce ();
00187     viewer.spinOnce ();
00188     pcl_sleep(0.01);
00189   }
00190 }


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