πŸ’»
Tutorial
  • INTRO
  • Part 0 (κ°œμš”)
    • README
    • 3D μ˜μƒμ²˜λ¦¬
    • [별첨] PCL & PCDλž€ (100%)
    • chapter02 : PCL μ„€μΉ˜ (100%)
    • chapter03 : ROS μ‹€μŠ΅ μ€€λΉ„(100%)
  • Part 1 (μ΄ˆκΈ‰)
    • README
    • PCL 기반 λ‘œλ΄‡ λΉ„μ Ό
    • [별첨] 파일 생성 및 μž…μΆœλ ₯ (70%)
      • PCL-Cpp (70%)
      • PCL-Python (70%)
      • Open3D-Python (70%)
      • ROS μ‹€μŠ΅ (90%)
    • Filter
    • [별첨] μƒ˜ν”Œλ§ (70%)
      • λ‹€μš΄μƒ˜ν”Œλ§-PCL-Cpp (70%)
      • λ‹€μš΄μƒ˜ν”Œλ§-PCL-Python (50%)
      • μ—…μƒ˜ν”Œλ§-PCL-Cpp (70%)
      • ROS μ‹€μŠ΅ (90%)
    • [별첨] 관심 μ˜μ—­ μ„€μ • (70%)
      • PCL-Cpp (70%)
      • PCL-Python (70%)
      • ROS μ‹€μŠ΅ (90%)
    • [별첨] λ…Έμ΄μ¦ˆ 제거 (70%)
      • PCL-Cpp (70%)
      • PCL-Python (50%)
      • ROS μ‹€μŠ΅ (90%)
  • Part 2 (쀑급)
    • README
    • Kd-Tree/Octree Search
    • Chapter03 : Sample Consensus
    • [별첨] λ°”λ‹₯제거 (RANSAC) (70%)
      • PCL-Cpp (70%)
      • PCL-Python (70%)
      • ROS μ‹€μŠ΅ (90%)
    • κ΅°μ§‘ν™” (70%)
      • Euclidean-PCL-Cpp (70%)
      • Euclidean-PCL-Python (0%)
      • Conditional-Euclidean-PCL-Cpp (50%)
      • DBSCAN-PCL-Python (0%)
      • Region-Growing-PCL-Cpp (50%)
      • Region-Growing-RGB-PCL-Cpp (50%)
      • Min-Cut-PCL-Cpp (50%)
      • Model-Outlier-Removal-PCL-Cpp (50%)
      • Progressive-Morphological-Filter-PCL-Cpp (50%)
    • 포인트 탐색과 배경제거 (60%)
      • Search-Octree-PCL-Cpp (70%)
      • Search-Octree-PCL-Python (70%)
      • Search-Kdtree-PCL-Cpp (70%)
      • Search-Kdtree-PCL-Python (70%)
      • Compression-PCL-Cpp (70%)
      • DetectChanges-PCL-Cpp (50%)
      • DetectChanges-PCL-Python (50%)
    • νŠΉμ§• μ°ΎκΈ° (50%)
      • PFH-PCL-Cpp
      • FPFH-PCL-Cpp
      • Normal-PCL-Cpp (70%)
      • Normal-PCL-Python (80%)
      • Tmp
    • λΆ„λ₯˜/인식 (30%)
      • 인식-GeometricConsistencyGrouping
      • SVM-RGBD-PCL-Python (70%)
      • SVM-LIDAR-PCL-Python (0%)
      • SVM-ROS (0%)
    • μ •ν•© (70%)
      • ICP-PCL-Cpp (70%)
      • ICP-ROS μ‹€μŠ΅ (10%)
    • μž¬κ΅¬μ„± (30%)
      • Smoothig-PCL-Cpp (70%)
      • Smoothig-PCL-Python (70%)
      • Triangulation-PCL-Cpp (70%)
  • Part 3 (κ³ κΈ‰)
    • README
    • λ”₯λŸ¬λ‹ 기반 ν•™μŠ΅ 데이터 생성 (0%)
      • PointGAN (90%)
      • AutoEncoder (0%)
    • λ”₯λŸ¬λ‹ 기반 μƒ˜ν”Œλ§ 기법 (0%)
      • DenseLidarNet (50%)
      • Point Cloud Upsampling Network
      • Pseudo-LiDAR
    • λ”₯λŸ¬λ‹ 기반 μžμœ¨μ£Όν–‰ 탐지 기술 (0%)
    • λ”₯λŸ¬λ‹ 기반 μžμœ¨μ£Όν–‰ λΆ„λ₯˜ 기술 (0%)
      • Multi3D
      • PointNet
      • VoxelNet (50%)
      • YOLO3D
      • SqueezeSeg
      • butNet
  • Snippets
    • PCL-Snippets
    • PCL-Python-Helper (10%)
    • Lidar Data Augmentation
  • Appendix
    • μ‹œκ°ν™”Code
    • μ‹œκ°ν™”νˆ΄
    • Annotation툴
    • Point Cloud Libraries (0%)
    • 데이터셋
    • Cling_PCL
    • μ°Έκ³  자료
    • μž‘μ„± κ³„νš_Tips
    • μš©μ–΄μ§‘
Powered by GitBook
On this page
  • PCL-CPP 기반 Octree 탐색
  • 각 κΈ°λŠ₯별 μ½”λ“œ 3개
  • 1. Neighbors within voxel search
  • 2. K nearest neighbor search
  • 3. Neighbors within radius search

Was this helpful?

  1. Part 2 (쀑급)
  2. 포인트 탐색과 배경제거 (60%)

Search-Octree-PCL-Cpp (70%)

Previous포인트 탐색과 배경제거 (60%)NextSearch-Octree-PCL-Python (70%)

Last updated 5 years ago

Was this helpful?

PCL-CPP 기반 Octree 탐색

μ½”λ“œλŠ” μ—μ„œ λ‹€μš΄λ‘œλ“œ κ°€λŠ₯ν•©λ‹ˆλ‹€. μƒ˜ν”ŒνŒŒμΌμ€ 을 μ‚¬μš©ν•˜μ˜€μŠ΅λ‹ˆλ‹€.

#include <pcl/io/pcd_io.h> 
#include <pcl/octree/octree_search.h>  
#include <pcl/visualization/cloud_viewer.h>  
#include <pcl/point_types.h>  

#include <iostream>
#include <vector>

//Spatial Partitioning and Search Operations with Octrees
//http://pointclouds.org/documentation/tutorials/octree.php#octree-search
//Commnets : Hunjung, Lim (hunjung.lim@hotmail.com)

int main()
{

    pcl::PointCloud<pcl::PointXYZRGB>::Ptr cloud(new pcl::PointCloud<pcl::PointXYZRGB>);

     // *.PCD 파일 읽기 (https://raw.githubusercontent.com/adioshun/gitBook_Tutorial_PCL/master/Intermediate/sample/cloud_cluster_0.pcd)
    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;
    }

    //Octree 였브젝트 생성 
    float resolution = 0.03f; //볡셀 크기 μ„€μ •(Set octree voxel resolution)
    pcl::octree::OctreePointCloudSearch<pcl::PointXYZRGB> octree(resolution); 
    octree.setInputCloud(cloud);       // μž…λ ₯ 
    octree.addPointsFromInputCloud();  //Octree 생성 (Build Octree)

    //기쀀점(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;

     //기쀀점과 λ™μΌν•œ 볡셀내 쑴재 ν•˜λŠ” ν•˜λŠ” 포인트 탐색(Voxel Neighbor Search)
    std::vector<int> pointIdxVec;  //κ²°κ³Όλ¬Ό 포인트의 Index μ €μž₯(Save the result vector of the voxel neighbor search) 

    if (octree.voxelSearch(searchPoint, pointIdxVec))
    {
        //μ‹œκ°μ  확인을 μœ„ν•˜μ—¬ 색상 λ³€κ²½ (255,0,0)
        for (size_t i = 0; i < pointIdxVec.size(); ++i){
            cloud->points[pointIdxVec[i]].r = 255;
            cloud->points[pointIdxVec[i]].g = 0;
            cloud->points[pointIdxVec[i]].b = 0;
        }        
    }

    // κΈ°μ€€μ μ—μ„œ κ°€κΉŒμš΄ μˆœμ„œμ€‘ Kλ²ˆμ§ΈκΉŒμ§€μ˜ 포인트 탐색 (K nearest neighbor search)
    int K = 50;   // 탐색할 포인트 수 μ„€μ • 
    std::vector<int> pointIdxNKNSearch; //Save the index result of the K nearest neighbor
    std::vector<float> pointNKNSquaredDistance;  //Save the index result of the K nearest neighbor

    if (octree.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 = 50 nearest neighbors:" << pointIdxNKNSearch.size() << endl;

    //κΈ°μ€€μ μ—μ„œ μ§€μ •λœ λ°˜κ²½λ‚΄ 포인트 탐색 (Neighbor search within radius)
    float radius = 0.02; //탐색할 반경 μ„€μ •(Set the search radius)
     std::vector<int> pointIdxRadiusSearch;  //Save the index of each neighbor
    std::vector<float> pointRadiusSquaredDistance;  //Save the square of the Euclidean distance between each neighbor and the search point

    if (octree.radiusSearch(searchPoint, radius, pointIdxRadiusSearch, pointRadiusSquaredDistance) > 0)
    {    
          //μ‹œκ°μ  확인을 μœ„ν•˜μ—¬ 색상 λ³€κ²½ (0,0,255)
        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() << endl;

     // μƒμ„±λœ ν¬μΈνŠΈν΄λΌμš°λ“œ μ €μž₯ 
    pcl::io::savePCDFile<pcl::PointXYZRGB>("Octree_AllinOne.pcd", *cloud);
}

κ²°κ³Ό

searchPoint :0.0346006 -1.46636 0.975463
K = 50 nearest neighbors:50
Radius 0.02 nearest neighbors: 141

μ°Έκ³ μœ„μΉ˜

κ²°κ³Ό

각 κΈ°λŠ₯별 μ½”λ“œ 3개

1. Neighbors within voxel search

#include <pcl/point_cloud.h>
#include <pcl/io/pcd_io.h>
#include <pcl/octree/octree_search.h>

#include <iostream>
#include <vector>
#include <ctime>

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

  pcl::PointCloud<pcl::PointXYZRGB>::Ptr cloud (new pcl::PointCloud<pcl::PointXYZRGB>);
  pcl::io::loadPCDFile<pcl::PointXYZRGB>("cloud_cluster_0.pcd", *cloud);

  float resolution = 128.0f;
  pcl::octree::OctreePointCloudSearch<pcl::PointXYZRGB> octree (resolution);
  octree.setInputCloud (cloud);
  octree.addPointsFromInputCloud ();

  pcl::PointXYZRGB searchPoint;
  searchPoint.x = 0.026256f;
  searchPoint.y = -1.464739f;
  searchPoint.z = 0.929567f;

  // Neighbors within voxel search
  std::vector<int> pointIdxVec;

  if (octree.voxelSearch (searchPoint, pointIdxVec))
  {
    std::cout << "Neighbors within voxel search at (" << searchPoint.x 
     << " " << searchPoint.y 
     << " " << searchPoint.z << ")" 
     << std::endl;

    for (size_t i = 0; i < pointIdxVec.size (); ++i)
   std::cout << "    " << cloud->points[pointIdxVec[i]].x 
       << " " << cloud->points[pointIdxVec[i]].y 
       << " " << cloud->points[pointIdxVec[i]].z << std::endl;
  }


}

κ²°κ³Ό

...
-0.00606756 -1.46653 0.797328
-0.00904433 -1.46755 0.796737
-0.0120327 -1.46887 0.795969
...

2. K nearest neighbor search

#include <pcl/point_cloud.h>
#include <pcl/io/pcd_io.h>
#include <pcl/octree/octree_search.h>

#include <iostream>
#include <vector>
#include <ctime>

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

  pcl::PointCloud<pcl::PointXYZRGB>::Ptr cloud (new pcl::PointCloud<pcl::PointXYZRGB>);
  pcl::io::loadPCDFile<pcl::PointXYZRGB>("cloud_cluster_0.pcd", *cloud);

  float resolution = 128.0f;
  pcl::octree::OctreePointCloudSearch<pcl::PointXYZRGB> octree (resolution);
  octree.setInputCloud (cloud);
  octree.addPointsFromInputCloud ();

  pcl::PointXYZRGB searchPoint;
  searchPoint.x = 0.026256f;
  searchPoint.y = -1.464739f;
  searchPoint.z = 0.929567f;

  // K nearest neighbor search

  int K = 10;

  std::vector<int> pointIdxNKNSearch;
  std::vector<float> pointNKNSquaredDistance;

  std::cout << "K nearest neighbor search at (" << searchPoint.x 
            << " " << searchPoint.y 
            << " " << searchPoint.z
            << ") with K=" << K << std::endl;

  if (octree.nearestKSearch (searchPoint, K, pointIdxNKNSearch, pointNKNSquaredDistance) > 0)
  {
    for (size_t i = 0; i < pointIdxNKNSearch.size (); ++i)
      std::cout << "    "  <<   cloud->points[ pointIdxNKNSearch[i] ].x 
                << " " << cloud->points[ pointIdxNKNSearch[i] ].y 
                << " " << cloud->points[ pointIdxNKNSearch[i] ].z 
                << " (squared distance: " << pointNKNSquaredDistance[i] << ")" << std::endl;
  }


}

κ²°κ³Ό

K nearest neighbor search at (0.026256 -1.46474 0.929567) with K=10
0.0262559 -1.46474 0.929567 (squared distance: 3.69042e-13)
0.0234182 -1.46435 0.929759 (squared distance: 8.24415e-06)
0.0290953 -1.46517 0.929357 (squared distance: 8.28962e-06)
0.0262519 -1.46476 0.932708 (squared distance: 9.86657e-06)
0.0262599 -1.46472 0.926419 (squared distance: 9.90814e-06)
0.0290885 -1.46518 0.932502 (squared distance: 1.68363e-05)
0.0234196 -1.46433 0.926612 (squared distance: 1.69452e-05)
0.0234169 -1.46437 0.932899 (squared distance: 1.93018e-05)
0.029102 -1.46515 0.926206 (squared distance: 1.95655e-05)
0.0205821 -1.46402 0.929919 (squared distance: 3.28378e-05)

3. Neighbors within radius search

#include <pcl/point_cloud.h>
#include <pcl/io/pcd_io.h>
#include <pcl/octree/octree_search.h>

#include <iostream>
#include <vector>
#include <ctime>

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

  pcl::PointCloud<pcl::PointXYZRGB>::Ptr cloud (new pcl::PointCloud<pcl::PointXYZRGB>);
  pcl::io::loadPCDFile<pcl::PointXYZRGB>("cloud_cluster_0.pcd", *cloud);

  float resolution = 128.0f;
  pcl::octree::OctreePointCloudSearch<pcl::PointXYZRGB> octree (resolution);
  octree.setInputCloud (cloud);
  octree.addPointsFromInputCloud ();

  pcl::PointXYZRGB searchPoint;
  searchPoint.x = 0.026256f;
  searchPoint.y = -1.464739f;
  searchPoint.z = 0.929567f;

  // Neighbors within radius search

  std::vector<int> pointIdxRadiusSearch;
  std::vector<float> pointRadiusSquaredDistance;

  float radius = 256.0f * rand () / (RAND_MAX + 1.0f);

  std::cout << "Neighbors within radius search at (" << searchPoint.x 
      << " " << searchPoint.y 
      << " " << searchPoint.z
      << ") with radius=" << radius << std::endl;


  if (octree.radiusSearch (searchPoint, radius, pointIdxRadiusSearch, pointRadiusSquaredDistance) > 0)
  {
    for (size_t i = 0; i < pointIdxRadiusSearch.size (); ++i)
      std::cout << "    "  <<   cloud->points[ pointIdxRadiusSearch[i] ].x 
                << " " << cloud->points[ pointIdxRadiusSearch[i] ].y 
                << " " << cloud->points[ pointIdxRadiusSearch[i] ].z 
                << " (squared distance: " << pointRadiusSquaredDistance[i] << ")" << std::endl;
  }


}

κ²°κ³Ό

...
-0.00606756 -1.46653 0.797328
-0.00904433 -1.46755 0.796737
-0.0120327 -1.46887 0.795969
...

μ½”λ“œλŠ” μ—μ„œ λ‹€μš΄λ‘œλ“œ κ°€λŠ₯ν•©λ‹ˆλ‹€. μƒ˜ν”ŒνŒŒμΌμ€ 을 μ‚¬μš©ν•˜μ˜€μŠ΅λ‹ˆλ‹€.

μ½”λ“œλŠ” μ—μ„œ λ‹€μš΄λ‘œλ“œ κ°€λŠ₯ν•©λ‹ˆλ‹€. μƒ˜ν”ŒνŒŒμΌμ€ 을 μ‚¬μš©ν•˜μ˜€μŠ΅λ‹ˆλ‹€.

μ½”λ“œλŠ” μ—μ„œ λ‹€μš΄λ‘œλ“œ κ°€λŠ₯ν•©λ‹ˆλ‹€. μƒ˜ν”ŒνŒŒμΌμ€ 을 μ‚¬μš©ν•˜μ˜€μŠ΅λ‹ˆλ‹€.

[이곳]
[cloud_cluster_0.pcd]
[이곳]
[cloud_cluster_0.pcd]
[이곳]
[cloud_cluster_0.pcd]
[이곳]
[cloud_cluster_0.pcd]