openni_narf_keypoint_extraction.cpp
Go to the documentation of this file.
00001 /* \author Bastian Steder */
00002 
00003 /* ---[ */
00004 #include <iostream>
00005 using namespace std;
00006 #include <pcl/io/openni_grabber.h>
00007 #include <pcl/range_image/range_image_planar.h>
00008 #include <pcl/common/common_headers.h>
00009 #include <pcl/visualization/range_image_visualizer.h>
00010 #include <pcl/visualization/pcl_visualizer.h>
00011 #include <pcl/features/range_image_border_extractor.h>
00012 #include <pcl/keypoints/narf_keypoint.h>
00013 #include <pcl/console/parse.h>
00014 
00015 std::string device_id = "#1";
00016 
00017 float angular_resolution = 0.5;
00018 float support_size = 0.2f;
00019 bool set_unseen_to_max_range = true;
00020 int max_no_of_threads = 1;
00021 float min_interest_value = 0.5;
00022 
00023 boost::mutex depth_image_mutex,
00024              ir_image_mutex,
00025              image_mutex;
00026 pcl::PointCloud<pcl::PointXYZ>::ConstPtr point_cloud_ptr;
00027 boost::shared_ptr<openni_wrapper::DepthImage> depth_image_ptr;
00028 boost::shared_ptr<openni_wrapper::IRImage> ir_image_ptr;
00029 boost::shared_ptr<openni_wrapper::Image> image_ptr;
00030 
00031 bool received_new_depth_data = false,
00032      received_new_ir_image   = false,
00033      received_new_image   = false;
00034 struct EventHelper
00035 {
00036   void
00037   depth_image_cb (const boost::shared_ptr<openni_wrapper::DepthImage>& depth_image)
00038   {
00039     if (depth_image_mutex.try_lock ())
00040     {
00041       depth_image_ptr = depth_image;
00042       depth_image_mutex.unlock ();
00043       received_new_depth_data = true;
00044     }
00045   }
00046 };
00047 
00048 
00049 void
00050 printUsage (const char* progName)
00051 {
00052   cout << "\n\nUsage: "<<progName<<" [options] [scene.pcd] <model.pcl> [model_2.pcl] ... [model_n.pcl]\n\n"
00053        << "Options:\n"
00054        << "-------------------------------------------\n"
00055        << "-d <device_id>  set the device id (default \""<<device_id<<"\")\n"
00056        << "-r <float>      angular resolution in degrees (default "<<angular_resolution<<")\n"
00057        << "-s <float>      support size for the interest points (diameter of the used sphere in meters)"
00058        <<                 " (default "<<support_size<<")\n"
00059        << "-i <float>      minimum interest value (0-1) (default "<<min_interest_value<<")"
00060        << "-t <int>        maximum number of threads to use (default "<< max_no_of_threads<<")\n"
00061        << "-h              this help\n"
00062        << "\n\n";
00063 }
00064 
00065 int main (int argc, char** argv)
00066 {
00067   // --------------------------------------
00068   // -----Parse Command Line Arguments-----
00069   // --------------------------------------
00070   if (pcl::console::find_argument (argc, argv, "-h") >= 0)
00071   {
00072     printUsage (argv[0]);
00073     return 0;
00074   }
00075   if (pcl::console::parse (argc, argv, "-d", device_id) >= 0)
00076     cout << "Using device id \""<<device_id<<"\".\n";
00077   if (pcl::console::parse (argc, argv, "-r", angular_resolution) >= 0)
00078     cout << "Setting angular resolution to "<<angular_resolution<<"deg.\n";
00079   angular_resolution = pcl::deg2rad (angular_resolution);
00080   if (pcl::console::parse (argc, argv, "-s", support_size) >= 0)
00081     cout << "Setting support size to "<<support_size<<"m.\n";
00082   if (pcl::console::parse (argc, argv, "-i", min_interest_value) >= 0)
00083     cout << "Setting minimum interest value to "<<min_interest_value<<".\n";
00084   if (pcl::console::parse (argc, argv, "-t", max_no_of_threads) >= 0)
00085     cout << "Setting maximum number of threads to "<<max_no_of_threads<<".\n";
00086   
00087   pcl::visualization::RangeImageVisualizer range_image_widget ("Range Image");
00088   
00089   pcl::visualization::PCLVisualizer viewer ("3D Viewer");
00090   viewer.addCoordinateSystem (1.0f);
00091   viewer.setBackgroundColor (1, 1, 1);
00092   
00093   viewer.initCameraParameters ();
00094   viewer.camera_.pos[0] = 0.0;
00095   viewer.camera_.pos[1] = -0.3;
00096   viewer.camera_.pos[2] = -2.0;
00097   viewer.camera_.focal[0] = 0.0;
00098   viewer.camera_.focal[1] = 0.0+viewer.camera_.pos[1];
00099   viewer.camera_.focal[2] = 1.0;
00100   viewer.camera_.view[0] = 0.0;
00101   viewer.camera_.view[1] = -1.0;
00102   viewer.camera_.view[2] = 0.0;
00103   viewer.updateCamera ();
00104   
00105   openni_wrapper::OpenNIDriver& driver = openni_wrapper::OpenNIDriver::getInstance ();
00106   if (driver.getNumberDevices () > 0)
00107   {
00108     for (unsigned deviceIdx = 0; deviceIdx < driver.getNumberDevices (); ++deviceIdx)
00109     {
00110       cout << "Device: " << deviceIdx + 1 << ", vendor: " << driver.getVendorName (deviceIdx)
00111            << ", product: " << driver.getProductName (deviceIdx) << ", connected: "
00112            << (int) driver.getBus (deviceIdx) << " @ " << (int) driver.getAddress (deviceIdx)
00113            << ", serial number: \'" << driver.getSerialNumber (deviceIdx) << "\'\n";
00114     }
00115   }
00116   else
00117   {
00118     cout << "\nNo devices connected.\n\n";
00119     return 1;
00120   }
00121   
00122   pcl::Grabber* interface = new pcl::OpenNIGrabber (device_id);
00123   EventHelper event_helper;
00124   
00125   boost::function<void (const boost::shared_ptr<openni_wrapper::DepthImage>&) > f_depth_image =
00126     boost::bind (&EventHelper::depth_image_cb, &event_helper, _1);
00127   boost::signals2::connection c_depth_image = interface->registerCallback (f_depth_image);
00128   
00129   cout << "Starting grabber\n";
00130   interface->start ();
00131   cout << "Done\n";
00132   
00133   boost::shared_ptr<pcl::RangeImagePlanar> range_image_planar_ptr (new pcl::RangeImagePlanar);
00134   pcl::RangeImagePlanar& range_image_planar = *range_image_planar_ptr;
00135   
00136   pcl::RangeImageBorderExtractor range_image_border_extractor;
00137   pcl::NarfKeypoint narf_keypoint_detector;
00138   narf_keypoint_detector.setRangeImageBorderExtractor (&range_image_border_extractor);
00139   narf_keypoint_detector.getParameters ().support_size = support_size;
00140   narf_keypoint_detector.getParameters ().max_no_of_threads = max_no_of_threads;
00141   narf_keypoint_detector.getParameters ().min_interest_value = min_interest_value;
00142   //narf_keypoint_detector.getParameters ().calculate_sparse_interest_image = false;
00143   //narf_keypoint_detector.getParameloadters ().add_points_on_straight_edges = true;
00144   
00145   pcl::PointCloud<pcl::PointXYZ>::Ptr keypoints_cloud_ptr (new pcl::PointCloud<pcl::PointXYZ>);
00146   pcl::PointCloud<pcl::PointXYZ>& keypoints_cloud = *keypoints_cloud_ptr;
00147   
00148   while (!viewer.wasStopped ())
00149   {
00150     viewer.spinOnce ();
00151     range_image_widget.spinOnce ();  // process GUI events
00152     pcl_sleep (0.01);
00153     
00154     bool got_new_range_image = false;
00155     if (received_new_depth_data && depth_image_mutex.try_lock ())
00156     {
00157       received_new_depth_data = false;
00158       
00159       //unsigned long time_stamp = depth_image_ptr->getTimeStamp ();
00160       //int frame_id = depth_image_ptr->getFrameID ();
00161       const unsigned short* depth_map = depth_image_ptr->getDepthMetaData ().Data ();
00162       int width = depth_image_ptr->getWidth (), height = depth_image_ptr->getHeight ();
00163       float center_x = width/2, center_y = height/2;
00164       float focal_length_x = depth_image_ptr->getFocalLength (), focal_length_y = focal_length_x;
00165       float original_angular_resolution = asinf (0.5f*float (width)/float (focal_length_x)) / (0.5f*float (width));
00166       float desired_angular_resolution = angular_resolution;
00167       range_image_planar.setDepthImage (depth_map, width, height, center_x, center_y,
00168                                         focal_length_x, focal_length_y, desired_angular_resolution);
00169       depth_image_mutex.unlock ();
00170       got_new_range_image = !range_image_planar.points.empty ();
00171     }
00172     
00173     if (!got_new_range_image)
00174       continue;
00175     
00176     // --------------------------------
00177     // -----Extract NARF keypoints-----
00178     // --------------------------------
00179     if (set_unseen_to_max_range)
00180       range_image_planar.setUnseenToMaxRange ();
00181     narf_keypoint_detector.setRangeImage (&range_image_planar);
00182     pcl::PointCloud<int> keypoint_indices;
00183     double keypoint_extraction_start_time = pcl::getTime();
00184     narf_keypoint_detector.compute (keypoint_indices);
00185     double keypoint_extraction_time = pcl::getTime()-keypoint_extraction_start_time;
00186     std::cout << "Found "<<keypoint_indices.points.size ()<<" key points. "
00187               << "This took "<<1000.0*keypoint_extraction_time<<"ms.\n";
00188     
00189     // ----------------------------------------------
00190     // -----Show keypoints in range image widget-----
00191     // ----------------------------------------------
00192     range_image_widget.showRangeImage (range_image_planar, 0.5f, 10.0f);
00193     //for (size_t i=0; i<keypoint_indices.points.size (); ++i)
00194       //range_image_widget.markPoint (keypoint_indices.points[i]%range_image_planar.width,
00195                                     //keypoint_indices.points[i]/range_image_planar.width,
00196                                     //pcl::visualization::Vector3ub (0,255,0));
00197     
00198     // -------------------------------------
00199     // -----Show keypoints in 3D viewer-----
00200     // -------------------------------------
00201     pcl::visualization::PointCloudColorHandlerCustom<pcl::PointWithRange> color_handler_cloud
00202       (range_image_planar_ptr, 0, 0, 0);
00203     if (!viewer.updatePointCloud<pcl::PointWithRange> (range_image_planar_ptr, color_handler_cloud, "range image"))
00204       viewer.addPointCloud<pcl::PointWithRange> (range_image_planar_ptr, color_handler_cloud, "range image");
00205     
00206     keypoints_cloud.points.resize (keypoint_indices.points.size ());
00207     for (size_t i=0; i<keypoint_indices.points.size (); ++i)
00208       keypoints_cloud.points[i].getVector3fMap () =
00209         range_image_planar.points[keypoint_indices.points[i]].getVector3fMap ();
00210     pcl::visualization::PointCloudColorHandlerCustom<pcl::PointXYZ> color_handler_keypoints
00211       (keypoints_cloud_ptr, 0, 255, 0);
00212     if (!viewer.updatePointCloud (keypoints_cloud_ptr, color_handler_keypoints, "keypoints"))
00213       viewer.addPointCloud (keypoints_cloud_ptr, color_handler_keypoints, "keypoints");
00214     viewer.setPointCloudRenderingProperties (pcl::visualization::PCL_VISUALIZER_POINT_SIZE, 7, "keypoints");
00215   }
00216 
00217   interface->stop ();
00218 }


pcl
Author(s): Open Perception
autogenerated on Mon Oct 6 2014 03:16:03