Search-Kdtree-PCL-Cpp (70%)

์ฝ”๋“œ๋Š” [์ด๊ณณ]์—์„œ ๋‹ค์šด๋กœ๋“œ ๊ฐ€๋Šฅํ•ฉ๋‹ˆ๋‹ค. ์ƒ˜ํ”ŒํŒŒ์ผ์€ [cloud_cluster_0.pcd]์„ ์‚ฌ์šฉํ•˜์˜€์Šต๋‹ˆ๋‹ค.

#include <pcl/point_cloud.h>
#include <pcl/kdtree/kdtree_flann.h>
#include <pcl/io/pcd_io.h>
#include <iostream>
#include <vector>
#include <ctime>

//How to use a KdTree to search
//http://pointclouds.org/documentation/tutorials/kdtree_search.php#kdtree-search
//Commnets : Hunjung, Lim (hunjung.lim@hotmail.com)

int
main (int argc, char** argv)
{

  // *.PCD ํŒŒ์ผ ์ฝ๊ธฐ (https://raw.githubusercontent.com/adioshun/gitBook_Tutorial_PCL/master/Intermediate/sample/cloud_cluster_0.pcd)
  pcl::PointCloud<pcl::PointXYZRGB>::Ptr cloud (new pcl::PointCloud<pcl::PointXYZRGB>);    
  pcl::io::loadPCDFile<pcl::PointXYZRGB>("cloud_cluster_0.pcd", *cloud);

  // ์‹œ๊ฐ์  ํ™•์ธ์„ ์œ„ํ•ด ์ƒ‰์ƒ ํ†ต์ผ (255,255,255)
  for (size_t i = 0; i < cloud->points.size(); ++i){
  cloud->points[i].r = 255;
  cloud->points[i].g = 255;
  cloud->points[i].b = 255;
  }

  //KdTree ์˜ค๋ธŒ์ ํŠธ ์ƒ์„ฑ 
  pcl::KdTreeFLANN<pcl::PointXYZRGB> kdtree;
  kdtree.setInputCloud (cloud);    //์ž…๋ ฅ 

     //๊ธฐ์ค€์ (searchPoint) ์„ค์ • ๋ฐฉ๋ฒ• #1(x,y,z ์ขŒํ‘œ ์ง€์ •)
     //pcl::PointXYZRGB searchPoint;
     //searchPoint.x = 0.026256f;
     //searchPoint.y = -1.464739f;
     //searchPoint.z = 0.929567f;
  //๊ธฐ์ค€์ (searchPoint) ์„ค์ • ๋ฐฉ๋ฒ• #2(3000๋ฒˆ์งธ ํฌ์ธํŠธ)
  pcl::PointXYZRGB searchPoint = cloud->points[3000]; 

  //๊ธฐ์ค€์  ์ขŒํ‘œ ์ถœ๋ ฅ 
  std::cout << "searchPoint :" << searchPoint.x << " " << searchPoint.y << " " << searchPoint.z  << std::endl;


  //๊ธฐ์ค€์ ์—์„œ ๊ฐ€๊นŒ์šด ์ˆœ์„œ์ค‘ K๋ฒˆ์งธ๊นŒ์ง€์˜ ํฌ์ธํŠธ ํƒ์ƒ‰ (K nearest neighbor search)
  int K = 10;   // ํƒ์ƒ‰ํ•  ํฌ์ธํŠธ ์ˆ˜ ์„ค์ • 
  std::vector<int> pointIdxNKNSearch(K);
  std::vector<float> pointNKNSquaredDistance(K);

  if ( kdtree.nearestKSearch (searchPoint, K, pointIdxNKNSearch, pointNKNSquaredDistance) > 0 )
  {
    //์‹œ๊ฐ์  ํ™•์ธ์„ ์œ„ํ•˜์—ฌ ์ƒ‰์ƒ ๋ณ€๊ฒฝ (0,255,0)
    for (size_t i = 0; i < pointIdxNKNSearch.size (); ++i)
    {
      cloud->points[pointIdxNKNSearch[i]].r = 0;
      cloud->points[pointIdxNKNSearch[i]].g = 255;
      cloud->points[pointIdxNKNSearch[i]].b = 0;
    }
  }

  // ํƒ์ƒ‰๋œ ์ ์˜ ์ˆ˜ ์ถœ๋ ฅ 
  std::cout << "K = 10 ๏ผš" << pointIdxNKNSearch.size() << std::endl;


  // ๊ธฐ์ค€์ ์—์„œ ์ง€์ •๋œ ๋ฐ˜๊ฒฝ๋‚ด ํฌ์ธํŠธ ํƒ์ƒ‰ (Neighbor search within radius)
  float radius = 0.02; //ํƒ์ƒ‰ํ•  ๋ฐ˜๊ฒฝ ์„ค์ •(Set the search radius)
  std::vector<int> pointIdxRadiusSearch;
  std::vector<float> pointRadiusSquaredDistance;

  if ( kdtree.radiusSearch (searchPoint, radius, pointIdxRadiusSearch, pointRadiusSquaredDistance) > 0 )
  {
    //์‹œ๊ฐ์  ํ™•์ธ์„ ์œ„ํ•˜์—ฌ ์ƒ‰์ƒ ๋ณ€๊ฒฝ (0,0,255)
    for (size_t i = 0; i < pointIdxRadiusSearch.size (); ++i)
      for (size_t i = 0; i < pointIdxRadiusSearch.size(); ++i)
        {
        cloud->points[pointIdxRadiusSearch[i]].r = 0;
        cloud->points[pointIdxRadiusSearch[i]].g = 0;
        cloud->points[pointIdxRadiusSearch[i]].b = 255;
        }
  }

  // ํƒ์ƒ‰๋œ ์ ์˜ ์ˆ˜ ์ถœ๋ ฅ 
  std::cout << "Radius 0.02 nearest neighbors: " << pointIdxRadiusSearch.size() << std::endl;

  // ์ƒ์„ฑ๋œ ํฌ์ธํŠธํด๋ผ์šฐ๋“œ ์ €์žฅ 
  pcl::io::savePCDFile<pcl::PointXYZRGB>("Kdtree_AllinOne.pcd", *cloud);

  return 0;
}

๊ฒฐ๊ณผ

searchPoint :0.0346006 -1.46636 0.975463
K = 10 ๏ผš10
Radius 0.02 nearest neighbors: 141

์ฐธ๊ณ ์œ„์น˜

๊ฒฐ๊ณผ

Last updated

Was this helpful?