match_linemod_template.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/point_cloud.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 
00045 #include <pcl/recognition/linemod.h>
00046 #include <pcl/recognition/color_gradient_modality.h>
00047 #include <pcl/recognition/surface_normal_modality.h>
00048 
00049 //#include <png++/png.hpp>
00050 
00051 using namespace pcl;
00052 using namespace pcl::io;
00053 using namespace pcl::console;
00054 
00055 typedef pcl::PointCloud<pcl::PointXYZRGBA> PointCloudXYZRGBA;
00056 
00057 void
00058 printHelp (int, char **argv)
00059 {
00060   print_error ("Syntax is: %s input_cloud.pcd input_template.lmt\n", argv[0]);
00061 }
00062 
00063 void printElapsedTimeAndNumberOfPoints (double t, int w, int h=1)
00064 {
00065   print_info ("[done, "); print_value ("%g", t); print_info (" ms : "); 
00066   print_value ("%d", w*h); print_info (" points]\n");
00067 }
00068 
00069 bool
00070 loadCloud (const std::string & filename, PointCloudXYZRGBA & cloud)
00071 {
00072   TicToc tt;
00073   print_highlight ("Loading "); print_value ("%s ", filename.c_str ());
00074 
00075   tt.tic ();
00076   if (loadPCDFile (filename, cloud) < 0)
00077     return (false);
00078 
00079   printElapsedTimeAndNumberOfPoints (tt.toc (), cloud.width, cloud.height);
00080 
00081   print_info ("Available dimensions: "); print_value ("%s\n", pcl::getFieldsList (cloud).c_str ());
00082 
00083   return (true);
00084 }
00085 
00086 std::vector<pcl::LINEMODDetection>
00087 matchTemplates (const PointCloudXYZRGBA::ConstPtr & input, const pcl::LINEMOD & linemod)
00088 {  
00089   pcl::ColorGradientModality<pcl::PointXYZRGBA> color_grad_mod;
00090   color_grad_mod.setInputCloud (input);
00091   color_grad_mod.processInputData ();
00092 
00093   pcl::SurfaceNormalModality<pcl::PointXYZRGBA> surface_norm_mod;
00094   surface_norm_mod.setInputCloud (input);
00095   surface_norm_mod.processInputData ();
00096 
00097   std::vector<pcl::QuantizableModality*> modalities (2);
00098   modalities[0] = &color_grad_mod;
00099   modalities[1] = &surface_norm_mod;
00100 
00101   std::vector<pcl::LINEMODDetection> detections;
00102   linemod.matchTemplates (modalities, detections);
00103 
00104   return (detections);
00105 }
00106 
00107 void
00108 compute (const PointCloudXYZRGBA::ConstPtr & input, const char * templates_filename)
00109 {
00110   pcl::LINEMOD linemod;
00111 
00112   // Load the templates from disk
00113   linemod.loadTemplates (templates_filename);
00114 
00115   // Match the templates to the provided image
00116   std::vector<pcl::LINEMODDetection> detections = matchTemplates (input, linemod);
00117 
00118   // Output the position and score of the best match for each template
00119   for (size_t i = 0; i < detections.size (); ++i)
00120   {
00121     const LINEMODDetection & d = detections[i];
00122     printf ("%zu: %d %d %d %f\n", i, d.x, d.y, d.template_id, d.score);
00123   }
00124 
00125   /*// Visualization code for testing purposes (requires libpng++)
00126   int i = 0;
00127   png::image<png::rgb_pixel> image (640, 480);
00128   for (size_t y = 0; y < image.get_height (); ++y)
00129   {
00130     for (size_t x = 0; x < image.get_width (); ++x)
00131     {
00132       const pcl::PointXYZRGBA & p = input->points[i++];
00133       image[y][x] = png::rgb_pixel(p.r, p.g, p.b);
00134     }
00135   }
00136   // Draw a green box around the object
00137   for (size_t i = 0; i < detections.size (); ++i)
00138   {
00139     const LINEMODDetection & d = detections[i];
00140     if (d.score < 0.6)
00141       continue;
00142 
00143     const pcl::SparseQuantizedMultiModTemplate & tmplt = linemod.getTemplate (d.template_id);
00144     int w = tmplt.region.width;
00145     int h = tmplt.region.height;
00146     for (int x = d.x; x < d.x+w; ++x)
00147     {
00148       image[d.y][x] = png::rgb_pixel(0, 255, 0);
00149       image[d.y+h-1][x] = png::rgb_pixel(0, 255, 0);
00150     }
00151     for (int y = d.y; y < d.y+h; ++y)
00152     {
00153       image[y][d.x] = png::rgb_pixel(0, 255, 0);
00154       image[y][d.x+w-1] = png::rgb_pixel(0, 255, 0);
00155     }
00156   }
00157   image.write("output.png");
00158   */
00159 }
00160 
00161 /* ---[ */
00162 int
00163 main (int argc, char** argv)
00164 {
00165   print_info ("Match a LINE-MOD template to an organized point cloud. For more information, use: %s -h\n", argv[0]);
00166 
00167   if (argc < 2)
00168   {
00169     printHelp (argc, argv);
00170     return (-1);
00171   }
00172 
00173   // Load the input point cloud from the provided PCD file
00174   PointCloudXYZRGBA::Ptr cloud (new PointCloudXYZRGBA);
00175   if (!loadCloud (argv[1], *cloud)) 
00176     return (-1);
00177 
00178   // Load the specified templates and match them to the provided input cloud
00179   compute (cloud, argv[2]);
00180 
00181 }
00182 


pcl
Author(s): Open Perception
autogenerated on Wed Aug 26 2015 15:25:28