区域增长
- 区域增长(region growing)是指将成组的像素或区域发展成更大区域的过程。从种子点的集合开始,从这些点的区域增长是通过将与每个种子点有相似属性像强度、灰度级、纹理颜色等的相邻像素合并到此区域。
区域增长算法
实现步骤如下:
- 随机或者对图像进行扫描,找到第一个还没有赋予属性的像素, 设该像素为(x0, y0);
- 以(x0, y0)为中心, 考虑(x0, y0)的4邻域或者8邻域像素(x,y)与种子像素的灰度值之差的绝对值小于某个阈值T,如果满足条件, 将(x, y)与(x0, y0)合并(在同一区域内), 同时将(x, y)压入堆栈;
- 从堆栈中取出一个像素, 把它当作(x0, y0)返回到步骤2;
- 当堆栈为空时!返回到步骤1;
- 重复步骤1 - 4直到图像中的每个点都有归属时。生长结束。
算法的使用流程:
1、输入点云;
2、构建k-dtree;
3、估计法线;
4、区域增长。
具体函数使用
// Region growing clustering object.
pcl::RegionGrowing<pcl::PointXYZ, pcl::Normal> clustering;
clustering.setMinClusterSize(100);
clustering.setMaxClusterSizsetMinClusterSizee(10000);
clustering.setSearchMethod(kdtree);
clustering.setNumberOfNeighbours(30);
clustering.setInputCloud(cloud);
clustering.setInputNormals(normals);
// Set the angle in radians that will be the smoothness threshold
// (the maximum allowable deviation of the normals).
clustering.setSmoothnessThreshold(7.0 / 180.0 * M_PI); // 7 degrees.
// Set the curvature threshold. The disparity between curvatures will be
// tested after the normal deviation check has passed.
clustering.setCurvatureThreshold(1.0);
std::vector <pcl::PointIndices> clusters;
clustering.extract(clusters);
具体实例解析
// growth.cpp : 定义控制台应用程序的入口点。
//
#include "stdafx.h"
#include <iostream>
#include <vector>
#include <pcl/point_types.h>
#include <pcl/io/pcd_io.h>
#include <pcl/search/search.h>
#include <pcl/search/kdtree.h>
#include <pcl/features/normal_3d.h>
#include <pcl/visualization/cloud_viewer.h>
#include <pcl/filters/passthrough.h>
#include <pcl/segmentation/region_growing.h>
int main(int argc, char** argv)
{
DWORD t1, t2;
t1 = GetTickCount();//以上两句和最后return 0 之上的为计时函数。
//点云的类型
pcl::PointCloud<pcl::PointXYZ>::Ptr cloud(new pcl::PointCloud<pcl::PointXYZ>);
//打开点云
if (pcl::io::loadPCDFile <pcl::PointXYZ>("C:/Users/10859/Desktop/pointcloudsdata/bunny.pcd", *cloud) == -1)//改成想要输入的点云名称...*cloud就是把输入的点云记录到变量指针cloud中。
{
std::cout << "Cloud reading failed." << std::endl;
return (-1);
}
//设置搜索的方式或者说是结构
pcl::search::Search<pcl::PointXYZ>::Ptr tree = boost::shared_ptr<pcl::search::Search<pcl::PointXYZ> >(new pcl::search::KdTree<pcl::PointXYZ>);
//求法线
pcl::PointCloud <pcl::Normal>::Ptr normals(new pcl::PointCloud <pcl::Normal>);
pcl::NormalEstimation<pcl::PointXYZ, pcl::Normal> normal_estimator;
normal_estimator.setSearchMethod(tree);
normal_estimator.setInputCloud(cloud);
normal_estimator.setKSearch(50);
normal_estimator.compute(*normals);
//直通滤波在Z轴的0到1米之间
pcl::IndicesPtr indices(new std::vector <int>);
pcl::PassThrough<pcl::PointXYZ> pass;
pass.setInputCloud(cloud);
pass.setFilterFieldName("z");
pass.setFilterLimits(0.0, 1.0);
pass.filter(*indices);
//聚类对象<点,法线>
pcl::RegionGrowing<pcl::PointXYZ, pcl::Normal> reg; //首先建立reg寄存器(区域曾长的对象)
reg.setMinClusterSize(50); //最小的聚类的点数(小于这参数的平面被忽略不计)
reg.setMaxClusterSize(1000000); //最大的(一般随便设置)
reg.setSearchMethod(tree); //搜索方式(采用的默认是K—d树法)
reg.setNumberOfNeighbours(30); //设置搜索的邻域点的个数,周围多少个点决定这是一个平面(决定容错率,设置大时有倾斜也可接受,设置小时检测到的平面会很小)
reg.setInputCloud(cloud); //输入点
//reg.setIndices (indices);
reg.setInputNormals(normals); //输入的法线
reg.setSmoothnessThreshold(3.0 / 180.0 * M_PI); //设置平滑度(设置两个法线在多大夹角内可当做是共面的)
reg.setCurvatureThreshold(1.0); //设置曲率的阈值
//最后也是一个弯曲的阈值,这个决定了比当前考察的点和平均的法线角度,决定是否还有继续探索下去的必要。
//(也就是假设每个点都是平稳弯曲的,那么normal的夹角都很小,但是时间长了偏移的就大了,这个参数就是限制这个用的)
std::vector <pcl::PointIndices> clusters;
reg.extract(clusters);
//把结果输出到一个簇里面,这个簇会自动把每个平面分成一个vector,可以打印下来看看
std::cout << "Number of clusters is equal to " << clusters.size() << std::endl;
std::cout << "First cluster has " << clusters[0].indices.size() << " points." << endl;
std::cout << "These are the indices of the points of the initial" <<
std::endl << "cloud that belong to the first cluster:" << std::endl;
//int counter = 0;
//while (counter < clusters[0].indices.size())
//{
// std::cout << clusters[0].indices[counter] << ", ";
// counter++;
// if (counter % 10 == 0)
// std::cout << std::endl;
//}
//std::cout << std::endl;
//可视化聚类的结果
pcl::PointCloud <pcl::PointXYZRGB>::Ptr colored_cloud = reg.getColoredCloud();
pcl::visualization::CloudViewer viewer("Cluster viewer");
viewer.showCloud(colored_cloud);
while (!viewer.wasStopped())
{
}
pcl::PCDWriter writer;//将点云写入磁盘
writer.write("C:/Users/10859/Desktop/pointcloudsdata/bunnyresult.pcd", *colored_cloud, false);//改成想要输出的点云名称
t2 = GetTickCount(); //从这句到return 0之间的两句为计时函数
printf("Use Time:%f\n", (t2 - t1)*1.0 / 1000);
system("pause");
return (0);
}
运行结果:
可以看到,bunny的头部、耳朵、尾巴等都被较好的区分开来。
参考:
《PCL点云库学习&VS2010(X64)》Part 27 PCL中的区域增长之—Region_Growing算法
https://blog.csdn.net/sinat_24206709/article/details/69814380
PCL实现区域增长算法(代码详细解析)
https://blog.csdn.net/RNG_uzi_/article/details/88422523