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.setCameraPosition (0.0, -0.3, -2.0,
00095                             0.0, -0.3, 1.0,
00096                             0.0, -1.0, 0.0);
00097   
00098   openni_wrapper::OpenNIDriver& driver = openni_wrapper::OpenNIDriver::getInstance ();
00099   if (driver.getNumberDevices () > 0)
00100   {
00101     for (unsigned deviceIdx = 0; deviceIdx < driver.getNumberDevices (); ++deviceIdx)
00102     {
00103       cout << "Device: " << deviceIdx + 1 << ", vendor: " << driver.getVendorName (deviceIdx)
00104            << ", product: " << driver.getProductName (deviceIdx) << ", connected: "
00105            << (int) driver.getBus (deviceIdx) << " @ " << (int) driver.getAddress (deviceIdx)
00106            << ", serial number: \'" << driver.getSerialNumber (deviceIdx) << "\'\n";
00107     }
00108   }
00109   else
00110   {
00111     cout << "\nNo devices connected.\n\n";
00112     return 1;
00113   }
00114   
00115   pcl::Grabber* interface = new pcl::OpenNIGrabber (device_id);
00116   EventHelper event_helper;
00117   
00118   boost::function<void (const boost::shared_ptr<openni_wrapper::DepthImage>&) > f_depth_image =
00119     boost::bind (&EventHelper::depth_image_cb, &event_helper, _1);
00120   boost::signals2::connection c_depth_image = interface->registerCallback (f_depth_image);
00121   
00122   cout << "Starting grabber\n";
00123   interface->start ();
00124   cout << "Done\n";
00125   
00126   boost::shared_ptr<pcl::RangeImagePlanar> range_image_planar_ptr (new pcl::RangeImagePlanar);
00127   pcl::RangeImagePlanar& range_image_planar = *range_image_planar_ptr;
00128   
00129   pcl::RangeImageBorderExtractor range_image_border_extractor;
00130   pcl::NarfKeypoint narf_keypoint_detector;
00131   narf_keypoint_detector.setRangeImageBorderExtractor (&range_image_border_extractor);
00132   narf_keypoint_detector.getParameters ().support_size = support_size;
00133   narf_keypoint_detector.getParameters ().max_no_of_threads = max_no_of_threads;
00134   narf_keypoint_detector.getParameters ().min_interest_value = min_interest_value;
00135   //narf_keypoint_detector.getParameters ().calculate_sparse_interest_image = false;
00136   //narf_keypoint_detector.getParameloadters ().add_points_on_straight_edges = true;
00137   
00138   pcl::PointCloud<pcl::PointXYZ>::Ptr keypoints_cloud_ptr (new pcl::PointCloud<pcl::PointXYZ>);
00139   pcl::PointCloud<pcl::PointXYZ>& keypoints_cloud = *keypoints_cloud_ptr;
00140   
00141   while (!viewer.wasStopped ())
00142   {
00143     viewer.spinOnce ();
00144     range_image_widget.spinOnce ();  // process GUI events
00145     pcl_sleep (0.01);
00146     
00147     bool got_new_range_image = false;
00148     if (received_new_depth_data && depth_image_mutex.try_lock ())
00149     {
00150       received_new_depth_data = false;
00151       
00152       //unsigned long time_stamp = depth_image_ptr->getTimeStamp ();
00153       //int frame_id = depth_image_ptr->getFrameID ();
00154       const unsigned short* depth_map = depth_image_ptr->getDepthMetaData ().Data ();
00155       int width = depth_image_ptr->getWidth (), height = depth_image_ptr->getHeight ();
00156       float center_x = width/2, center_y = height/2;
00157       float focal_length_x = depth_image_ptr->getFocalLength (), focal_length_y = focal_length_x;
00158       float original_angular_resolution = asinf (0.5f*float (width)/float (focal_length_x)) / (0.5f*float (width));
00159       float desired_angular_resolution = angular_resolution;
00160       range_image_planar.setDepthImage (depth_map, width, height, center_x, center_y,
00161                                         focal_length_x, focal_length_y, desired_angular_resolution);
00162       depth_image_mutex.unlock ();
00163       got_new_range_image = !range_image_planar.points.empty ();
00164     }
00165     
00166     if (!got_new_range_image)
00167       continue;
00168     
00169     // --------------------------------
00170     // -----Extract NARF keypoints-----
00171     // --------------------------------
00172     if (set_unseen_to_max_range)
00173       range_image_planar.setUnseenToMaxRange ();
00174     narf_keypoint_detector.setRangeImage (&range_image_planar);
00175     pcl::PointCloud<int> keypoint_indices;
00176     double keypoint_extraction_start_time = pcl::getTime();
00177     narf_keypoint_detector.compute (keypoint_indices);
00178     double keypoint_extraction_time = pcl::getTime()-keypoint_extraction_start_time;
00179     std::cout << "Found "<<keypoint_indices.points.size ()<<" key points. "
00180               << "This took "<<1000.0*keypoint_extraction_time<<"ms.\n";
00181     
00182     // ----------------------------------------------
00183     // -----Show keypoints in range image widget-----
00184     // ----------------------------------------------
00185     range_image_widget.showRangeImage (range_image_planar, 0.5f, 10.0f);
00186     //for (size_t i=0; i<keypoint_indices.points.size (); ++i)
00187       //range_image_widget.markPoint (keypoint_indices.points[i]%range_image_planar.width,
00188                                     //keypoint_indices.points[i]/range_image_planar.width,
00189                                     //pcl::visualization::Vector3ub (0,255,0));
00190     
00191     // -------------------------------------
00192     // -----Show keypoints in 3D viewer-----
00193     // -------------------------------------
00194     pcl::visualization::PointCloudColorHandlerCustom<pcl::PointWithRange> color_handler_cloud
00195       (range_image_planar_ptr, 0, 0, 0);
00196     if (!viewer.updatePointCloud<pcl::PointWithRange> (range_image_planar_ptr, color_handler_cloud, "range image"))
00197       viewer.addPointCloud<pcl::PointWithRange> (range_image_planar_ptr, color_handler_cloud, "range image");
00198     
00199     keypoints_cloud.points.resize (keypoint_indices.points.size ());
00200     for (size_t i=0; i<keypoint_indices.points.size (); ++i)
00201       keypoints_cloud.points[i].getVector3fMap () =
00202         range_image_planar.points[keypoint_indices.points[i]].getVector3fMap ();
00203     pcl::visualization::PointCloudColorHandlerCustom<pcl::PointXYZ> color_handler_keypoints
00204       (keypoints_cloud_ptr, 0, 255, 0);
00205     if (!viewer.updatePointCloud (keypoints_cloud_ptr, color_handler_keypoints, "keypoints"))
00206       viewer.addPointCloud (keypoints_cloud_ptr, color_handler_keypoints, "keypoints");
00207     viewer.setPointCloudRenderingProperties (pcl::visualization::PCL_VISUALIZER_POINT_SIZE, 7, "keypoints");
00208   }
00209 
00210   interface->stop ();
00211 }


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