/examples/keypoints/example_sift_normal_keypoint_estimation.cpp

https://github.com/JunjieXie/pcl · C++ · 131 lines · 46 code · 13 blank · 72 comment · 3 complexity · 6e01b37cb859cf4857a99489a4bd6536 MD5 · raw file

  1. /*
  2. * Software License Agreement (BSD License)
  3. *
  4. * Point Cloud Library (PCL) - www.pointclouds.org
  5. * Copyright (c) 2009-2011, Willow Garage, Inc.
  6. *
  7. * All rights reserved.
  8. *
  9. * Redistribution and use in source and binary forms, with or without
  10. * modification, are permitted provided that the following conditions
  11. * are met:
  12. *
  13. * * Redistributions of source code must retain the above copyright
  14. * notice, this list of conditions and the following disclaimer.
  15. * * Redistributions in binary form must reproduce the above
  16. * copyright notice, this list of conditions and the following
  17. * disclaimer in the documentation and/or other materials provided
  18. * with the distribution.
  19. * * Neither the name of Willow Garage, Inc. nor the names of its
  20. * contributors may be used to endorse or promote products derived
  21. * from this software without specific prior written permission.
  22. *
  23. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  24. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  25. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
  26. * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
  27. * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
  28. * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
  29. * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  30. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  31. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  32. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
  33. * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  34. * POSSIBILITY OF SUCH DAMAGE.
  35. *
  36. * $Id$
  37. *
  38. *
  39. */
  40. // STL
  41. #include <iostream>
  42. // PCL
  43. #include <pcl/io/pcd_io.h>
  44. #include <pcl/point_types.h>
  45. #include <pcl/common/io.h>
  46. #include <pcl/keypoints/sift_keypoint.h>
  47. #include <pcl/features/normal_3d.h>
  48. // #include <pcl/visualization/pcl_visualizer.h>
  49. /* This example shows how to estimate the SIFT points based on the
  50. * Normal gradients i.e. curvature than using the Intensity gradient
  51. * as usually used for SIFT keypoint estimation.
  52. */
  53. int
  54. main(int, char** argv)
  55. {
  56. std::string filename = argv[1];
  57. std::cout << "Reading " << filename << std::endl;
  58. pcl::PointCloud<pcl::PointXYZ>::Ptr cloud_xyz (new pcl::PointCloud<pcl::PointXYZ>);
  59. if(pcl::io::loadPCDFile<pcl::PointXYZ> (filename, *cloud_xyz) == -1) // load the file
  60. {
  61. PCL_ERROR ("Couldn't read file");
  62. return -1;
  63. }
  64. std::cout << "points: " << cloud_xyz->points.size () <<std::endl;
  65. // Parameters for sift computation
  66. const float min_scale = 0.01f;
  67. const int n_octaves = 3;
  68. const int n_scales_per_octave = 4;
  69. const float min_contrast = 0.001f;
  70. // Estimate the normals of the cloud_xyz
  71. pcl::NormalEstimation<pcl::PointXYZ, pcl::PointNormal> ne;
  72. pcl::PointCloud<pcl::PointNormal>::Ptr cloud_normals (new pcl::PointCloud<pcl::PointNormal>);
  73. pcl::search::KdTree<pcl::PointXYZ>::Ptr tree_n(new pcl::search::KdTree<pcl::PointXYZ>());
  74. ne.setInputCloud(cloud_xyz);
  75. ne.setSearchMethod(tree_n);
  76. ne.setRadiusSearch(0.2);
  77. ne.compute(*cloud_normals);
  78. // Copy the xyz info from cloud_xyz and add it to cloud_normals as the xyz field in PointNormals estimation is zero
  79. for(size_t i = 0; i<cloud_normals->points.size(); ++i)
  80. {
  81. cloud_normals->points[i].x = cloud_xyz->points[i].x;
  82. cloud_normals->points[i].y = cloud_xyz->points[i].y;
  83. cloud_normals->points[i].z = cloud_xyz->points[i].z;
  84. }
  85. // Estimate the sift interest points using normals values from xyz as the Intensity variants
  86. pcl::SIFTKeypoint<pcl::PointNormal, pcl::PointWithScale> sift;
  87. pcl::PointCloud<pcl::PointWithScale> result;
  88. pcl::search::KdTree<pcl::PointNormal>::Ptr tree(new pcl::search::KdTree<pcl::PointNormal> ());
  89. sift.setSearchMethod(tree);
  90. sift.setScales(min_scale, n_octaves, n_scales_per_octave);
  91. sift.setMinimumContrast(min_contrast);
  92. sift.setInputCloud(cloud_normals);
  93. sift.compute(result);
  94. std::cout << "No of SIFT points in the result are " << result.points.size () << std::endl;
  95. /*
  96. // Copying the pointwithscale to pointxyz so as visualize the cloud
  97. pcl::PointCloud<pcl::PointXYZ>::Ptr cloud_temp (new pcl::PointCloud<pcl::PointXYZ>);
  98. copyPointCloud(result, *cloud_temp);
  99. std::cout << "SIFT points in the cloud_temp are " << cloud_temp->points.size () << std::endl;
  100. // Visualization of keypoints along with the original cloud
  101. pcl::visualization::PCLVisualizer viewer("PCL Viewer");
  102. pcl::visualization::PointCloudColorHandlerCustom<pcl::PointXYZ> keypoints_color_handler (cloud_temp, 0, 255, 0);
  103. pcl::visualization::PointCloudColorHandlerCustom<pcl::PointXYZ> cloud_color_handler (cloud_xyz, 255, 0, 0);
  104. viewer.setBackgroundColor( 0.0, 0.0, 0.0 );
  105. viewer.addPointCloud(cloud_xyz, cloud_color_handler, "cloud");
  106. viewer.addPointCloud(cloud_temp, keypoints_color_handler, "keypoints");
  107. viewer.setPointCloudRenderingProperties (pcl::visualization::PCL_VISUALIZER_POINT_SIZE, 7, "keypoints");
  108. while(!viewer.wasStopped ())
  109. {
  110. viewer.spinOnce ();
  111. }
  112. */
  113. return 0;
  114. }