narf_feature_extraction.cpp
Go to the documentation of this file.
00001 /*
00002  * Software License Agreement (BSD License)
00003  *
00004  *  Copyright (c) 2010, 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 Willow Garage, Inc. 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  */
00035 
00036 /* \author Bastian Steder */
00037 
00038 /* ---[ */
00039 
00040 
00041 #include <iostream>
00042 using namespace std;
00043 
00044 #include "pcl/range_image/range_image.h"
00045 #include "pcl/io/pcd_io.h"
00046 #include "pcl/visualization/range_image_visualizer.h"
00047 #include "pcl/visualization/pcl_visualizer.h"
00048 #include "pcl/features/range_image_border_extractor.h"
00049 #include "pcl/keypoints/narf_keypoint.h"
00050 #include "pcl/features/narf_descriptor.h"
00051 
00052 using namespace pcl;
00053 typedef PointXYZ PointType;
00054 
00055 // --------------------
00056 // -----Parameters-----
00057 // --------------------
00058 float angular_resolution = 0.5f;
00059 float support_size = 0.2f;
00060 RangeImage::CoordinateFrame coordinate_frame = RangeImage::CAMERA_FRAME;
00061 bool setUnseenToMaxRange = false, rotation_invariant=true;
00062 
00063 // --------------
00064 // -----Help-----
00065 // --------------
00066 void printUsage(const char* progName)
00067 {
00068   cout << "\n\nUsage: "<<progName<<" [options] <scene.pcd>\n\n"
00069        << "Options:\n"
00070        << "-------------------------------------------\n"
00071        << "-r <float>   angular resolution in degrees (default "<<angular_resolution<<")\n"
00072        << "-c <int>     coordinate frame (default "<<(int)coordinate_frame<<")\n"
00073        << "-m           Treat all unseen points to max range\n"
00074        << "-s <float>   support size for the interest points (diameter of the used sphere in meters) (default "<<support_size<<")\n"
00075        << "-o <0/1>     switch rotational invariant version of the feature on/off (default "<<(int)rotation_invariant<<")\n"
00076        << "-h           this help\n"
00077        << "\n\n";
00078 }
00079 
00080 // --------------
00081 // -----Main-----
00082 // --------------
00083 int main (int argc, char** argv)
00084 {
00085   // --------------------------------------
00086   // -----Parse Command Line Arguments-----
00087   // --------------------------------------
00088   for (char c; (c = getopt(argc, argv, "r:c:ms:o:h")) != -1; ) {
00089     switch (c) {
00090       case 'r':
00091       {
00092         angular_resolution = strtod(optarg, NULL);
00093         cout << "Setting angular resolution to "<<angular_resolution<<".\n";
00094         break;
00095       }
00096       case 'c':
00097       {
00098         coordinate_frame = (RangeImage::CoordinateFrame)strtol(optarg, NULL, 0);
00099         cout << "Using coordinate frame "<<(int)coordinate_frame<<".\n";
00100         break;
00101       }
00102       case 'm':
00103       {
00104         setUnseenToMaxRange = true;
00105         break;
00106       }
00107       case 's':
00108       {
00109         support_size = strtod(optarg, NULL);
00110         cout << "Setting support size to "<<support_size<<".\n";
00111         break;
00112       }
00113       case 'o':
00114       {
00115         rotation_invariant = strtod(optarg, NULL);
00116         cout << "Switching rotation invariant feature version "<<(rotation_invariant ? "on" : "off")<<".\n";
00117         break;
00118       }
00119       case 'h':
00120         printUsage(argv[0]);
00121         exit(0);
00122     }
00123   }
00124   angular_resolution = deg2rad(angular_resolution);
00125   
00126   // ------------------------------------------------------------------
00127   // -----Read pcd file or create example point cloud if not given-----
00128   // ------------------------------------------------------------------
00129   // Read/Generate point cloud
00130   pcl::PointCloud<PointType> point_cloud;
00131   PointCloud<PointWithViewpoint> far_ranges;
00132   Eigen::Affine3f scene_sensor_pose(Eigen::Affine3f::Identity());
00133   if (optind < argc)
00134   {
00135     sensor_msgs::PointCloud2 point_cloud_data;
00136     if (pcl::io::loadPCDFile(argv[optind], point_cloud_data) == -1)
00137     {
00138       cerr << "Was not able to open file \""<<argv[optind]<<"\".\n";
00139       printUsage(argv[0]);
00140       exit(0);
00141     }
00142     fromROSMsg(point_cloud_data, point_cloud);
00143     RangeImage::extractFarRanges(point_cloud_data, far_ranges);
00144     if (pcl::getFieldIndex(point_cloud_data, "vp_x")>=0)
00145     {
00146       cout << "Scene point cloud has viewpoint information.\n";
00147       PointCloud<PointWithViewpoint> tmp_pc;  fromROSMsg(point_cloud_data, tmp_pc);
00148       Eigen::Vector3f average_viewpoint = RangeImage::getAverageViewPoint(tmp_pc);
00149       scene_sensor_pose = Eigen::Translation3f(average_viewpoint) * scene_sensor_pose;
00150     }
00151   }
00152   else
00153   {
00154     cout << "\nNo *.pcd file given => Genarating example point cloud.\n\n";
00155     for (float x=-0.5f; x<=0.5f; x+=0.01f)
00156     {
00157       for (float y=-0.5f; y<=0.5f; y+=0.01f)
00158       {
00159         PointType point;  point.x = x;  point.y = y;  point.z = 2.0f - y;
00160         point_cloud.points.push_back(point);
00161       }
00162     }
00163     point_cloud.width = point_cloud.points.size();  point_cloud.height = 1;
00164   }
00165 
00166   
00167   // -----------------------------------------------
00168   // -----Create RangeImage from the PointCloud-----
00169   // -----------------------------------------------
00170   float noise_level = 0.0;
00171   float min_range = 0.0f;
00172   int border_size = 1;
00173   RangeImage range_image;
00174   range_image.createFromPointCloud(point_cloud, angular_resolution, deg2rad(360.0f), deg2rad(180.0f),
00175                                    scene_sensor_pose, coordinate_frame, noise_level, min_range, border_size);
00176   range_image.integrateFarRanges(far_ranges);
00177   if (setUnseenToMaxRange)
00178     range_image.setUnseenToMaxRange();
00179 
00180   // --------------------------------------------
00181   // -----Open 3D viewer and add point cloud-----
00182   // --------------------------------------------
00183   visualization::PCLVisualizer viewer("3D Viewer");
00184   viewer.addCoordinateSystem(1.0f);
00185   viewer.addPointCloud(point_cloud.makeShared(), "original point cloud");
00186   //viewer.addPointCloud(range_image, "range image");
00187   
00188   // --------------------------
00189   // -----Show range image-----
00190   // --------------------------
00191   visualization::RangeImageVisualizer range_image_widget("Range image");
00192   range_image_widget.setRangeImage(range_image);
00193   
00194   // --------------------------------
00195   // -----Extract NARF keypoints-----
00196   // --------------------------------
00197   RangeImageBorderExtractor range_image_border_extractor;
00198   NarfKeypoint narf_keypoint_detector;
00199   narf_keypoint_detector.setRangeImageBorderExtractor(&range_image_border_extractor);
00200   narf_keypoint_detector.setRangeImage(&range_image);
00201   narf_keypoint_detector.getParameters().support_size = support_size;
00202   
00203   PointCloud<int> keypoint_indices;
00204   narf_keypoint_detector.compute(keypoint_indices);
00205   std::cout << "Found "<<keypoint_indices.points.size()<<" key points.\n";
00206 
00207   // ----------------------------------------------
00208   // -----Show keypoints in range image widget-----
00209   // ----------------------------------------------
00210   for (size_t i=0; i<keypoint_indices.points.size(); ++i)
00211     range_image_widget.markPoint(keypoint_indices.points[i]%range_image.width, keypoint_indices.points[i]/range_image.width);
00212   
00213   // -------------------------------------
00214   // -----Show keypoints in 3D viewer-----
00215   // -------------------------------------
00216   PointCloud<PointXYZ> keypoints;
00217   keypoints.points.resize(keypoint_indices.points.size());
00218   for (size_t i=0; i<keypoint_indices.points.size(); ++i)
00219     keypoints.points[i].getVector3fMap() = range_image.points[keypoint_indices.points[i]].getVector3fMap();
00220   viewer.addPointCloud(keypoints.makeShared(), "keypoints");
00221   viewer.setPointCloudRenderingProperties(visualization::PCL_VISUALIZER_POINT_SIZE, 7, "keypoints");
00222   
00223   // ------------------------------------------------------
00224   // -----Extract NARF descriptors for interest points-----
00225   // ------------------------------------------------------
00226   vector<int> keypoint_indices2;
00227   keypoint_indices2.resize(keypoint_indices.points.size());
00228   for (unsigned int i=0; i<keypoint_indices.size(); ++i) // This step is necessary to get the right vector type
00229     keypoint_indices2[i]=keypoint_indices.points[i];
00230   NarfDescriptor narf_descriptor(&range_image, &keypoint_indices2);
00231   narf_descriptor.getParameters().support_size = support_size;
00232   narf_descriptor.getParameters().rotation_invariant = rotation_invariant;
00233   PointCloud<Narf36> narf_descriptors;
00234   narf_descriptor.compute(narf_descriptors);
00235   cout << "Extracted "<<narf_descriptors.size()<<" descriptors for "<<keypoint_indices.points.size()<< " keypoints.\n";
00236   
00237   //--------------------
00238   // -----Main loop-----
00239   //--------------------
00240   while(!viewer.wasStopped() || range_image_widget.isShown())
00241   {
00242     visualization::ImageWidgetWX::spinOnce();  // process GUI events
00243     viewer.spinOnce(100);
00244     usleep(100000);
00245   }
00246 }
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Properties Friends Defines


narf_recognition
Author(s): Juergen Hess
autogenerated on Wed Dec 26 2012 16:37:09