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?