基于区域增长的分割

本文深入探讨了基于点云的区域增长分割算法,该算法利用点的曲率值和法线角度差,从最平坦区域开始,逐步合并相似点形成平面簇。通过设定阈值和点数限制,实现复杂场景的有效分割。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

算法核心:该算法是基于点法线之间角度的比较,企图将满足平滑约束的相邻点合并在一起,以一簇点集的形式输出。每簇点集被认为是属于相同平面。

工作原理:首先需要明白,区域增长是从有最小曲率值(curvature value)的点开始的。因此,我们必须计算出所有曲率值,并对它们进行排序。这是因为曲率最小的点位于平坦区域,而从最平坦的区域增长可以减少区域的总数。现在我们来具体描述这个过程:

1.点云中有未标记点,按照点的曲率值对点进行排序,找到最小曲率值点,并把它添加到种子点集;

2.对于每个种子点,算法都会发现周边的所有近邻点。1)计算每个近邻点与当前种子点的法线角度差(reg.setSmoothnessThreshold),如果差值小于设置的阈值,则该近邻点被重点考虑,进行第二步测试;2)该近邻点通过了法线角度差检验,如果它的曲率小于我们设定的阈值(reg.setCurvatureThreshold),这个点就被添加到种子点集,即属于当前平面。

3.通过两次检验的点,被从原始点云去除。

4.设置最小点簇的点数min(reg.setMinClusterSize),最大点簇为max(reg.setMaxClusterSize)。

4.重复1-3步,算法会生成点数在min和max的所有平面,并对不同平面标记不同颜色加以区分。

5.直到算法在剩余点中生成的点簇不能满足min,算法停止工作
 

附上一个基于区域增长的分割:

#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>
#include<sstream>
#include"IO.h"
using namespace std;

int GetRandomNumber()
{
	//srand((unsigned)time(NULL));
	int RandomNumber;
	RandomNumber = rand() % (250) + 0;//0到255之间选择颜色
	//生成其他范围的数字:RandomNumber = rand() % (b-a+1) + a;
	return RandomNumber;
}

int
main(int argc, char** argv)
{
	pcl::PointCloud<pcl::PointXYZ>::Ptr cloud(new pcl::PointCloud<pcl::PointXYZ>);
	if (pcl::io::loadPCDFile <pcl::PointXYZ>("D:\\建筑物屋面提取测试数据\\build.pcd", *cloud) == -1)
	{
		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);

	

	pcl::RegionGrowing<pcl::PointXYZ, pcl::Normal> reg;//创建区域分割对象
	reg.setMinClusterSize(2);//设置一个类别需要的最小点数
	reg.setMaxClusterSize(1000000);//设置一个类别需要的最多点数
	reg.setSearchMethod(tree);//设置搜索方法
	reg.setNumberOfNeighbours(30);//搜索近邻点数目
	reg.setInputCloud(cloud);//设置输入点云
	//reg.setIndices (indices);
	reg.setInputNormals(normals);//输入法向量
	reg.setSmoothnessThreshold(2/ 180.0 * M_PI);//设置平滑阈值
	reg.setCurvatureThreshold(1.0);//设置曲率阈值

	std::vector <pcl::PointIndices> clusters;
	reg.extract(clusters);

	
	


	//分割结果显示
	pcl::visualization::PCLVisualizer viewer("区域增长提取平面");
	viewer.setBackgroundColor(0, 0, 0);//黑色背景
	int ii = 0;
	IO IOExample;
	for (int i = 0; i < clusters.size(); i++)//每一块进行显示
	{
		
		vector<pcl::PointXYZ> TempCluster;
		for (int i = 0; i < clusters[ii].indices.size(); i++)
		{
			TempCluster.push_back(cloud->points[clusters[ii].indices[i]]);
		}
		ii = ii + 1;
		stringstream ss;
		ss << ii;
		string str;
		ss >> str;
		
		pcl::PointCloud<pcl::PointXYZ>::Ptr cloud_SinglePlane(new pcl::PointCloud<pcl::PointXYZ>);
		cloud_SinglePlane = IOExample.PointXYZ2Ptr(TempCluster);
		
		int R = GetRandomNumber();
		int G = GetRandomNumber();
		int B = GetRandomNumber();
		
		pcl::visualization::PointCloudColorHandlerCustom<pcl::PointXYZ> single_color(cloud_SinglePlane, R, G, B);
		viewer.addPointCloud<pcl::PointXYZ>(cloud_SinglePlane, single_color, str);
		viewer.setPointCloudRenderingProperties(pcl::visualization::PCL_VISUALIZER_POINT_SIZE, 3, str);
		
	}


	pcl::visualization::PCLVisualizer viewerBeforeRegionGrowing("区域增长分割前点云");
	viewerBeforeRegionGrowing.setBackgroundColor(0, 0, 0);//黑色背景
	pcl::visualization::PointCloudColorHandlerCustom<pcl::PointXYZ> single_color_base(cloud, 255, 0, 0);
	viewerBeforeRegionGrowing.addPointCloud<pcl::PointXYZ>(cloud, single_color_base, "origin");
	viewerBeforeRegionGrowing.setPointCloudRenderingProperties(pcl::visualization::PCL_VISUALIZER_POINT_SIZE, 3, "origin");


	while (!viewer.wasStopped() || !viewerBeforeRegionGrowing.wasStopped())
	{
		viewer.spinOnce(1);
		viewerBeforeRegionGrowing.spinOnce(1);
	}





	
	return (0);
}

简单分割结果

### 基于区域增长分割法的图像分割代码示例 以下是基于 Python 和 OpenCV 的区域增长分割法的代码示例。此代码实现了基本的区域增长算法,用于对输入图像进行简单的分割。 #### 区域增长分割法的核心逻辑 区域增长分割法依赖于定义一个一致性准则,通常是一个像素值范围(如灰度或颜色)。如果某个像素与其邻居之间的差异在这个范围内,则将其合并到当前区域内[^4]。 ```python import cv2 import numpy as np def region_growing(image, seed_point, threshold=10): """ 实现区域增长算法。 参数: image (numpy.ndarray): 输入的单通道灰度图像。 seed_point (tuple): 种子点坐标 (y, x)。 threshold (int): 生长阈值,默认为 10。 返回: result_image (numpy.ndarray): 处理后的二值化图像。 """ height, width = image.shape[:2] visited = np.zeros((height, width), dtype=bool) segmented = np.zeros((height, width), dtype=np.uint8) stack = [seed_point] while len(stack) > 0: current_point = stack.pop() y, x = current_point if not (0 <= y < height and 0 <= x < width): continue if visited[y, x]: continue visited[y, x] = True intensity_difference = abs(int(image[y, x]) - int(image[seed_point])) if intensity_difference < threshold: segmented[y, x] = 255 neighbors = [(y-1, x), (y+1, x), (y, x-1), (y, x+1)] stack.extend(neighbors) return segmented # 加载图像并转换为灰度图 image_path = 'input_image.jpg' # 替换为您的图片路径 original_image = cv2.imread(image_path, cv2.IMREAD_GRAYSCALE) if original_image is None: raise ValueError("无法加载图像,请检查文件路径") # 设置种子点和阈值 seed_point = (100, 100) # 示例种子点位置 threshold_value = 10 # 示例阈值 # 执行区域增长算法 segmented_image = region_growing(original_image, seed_point, threshold=threshold_value) # 显示结果 cv2.imshow('Original Image', original_image) cv2.imshow('Segmented Image', segmented_image) cv2.waitKey(0) cv2.destroyAllWindows() ``` --- ### 关键说明 1. **种子点的选择**: 上述代码中 `(100, 100)` 是一个示例种子点的位置。可以根据具体需求调整种子点的位置。 2. **阈值设置**: `threshold` 控制着区域增长的速度和大小。较小的阈值会导致更精细的分割,而较大的阈值可能导致过度生长。 3. **性能优化**: 对于大规模图像,可以通过优先队列或其他数据结构进一步优化栈操作效率[^4]。 --- ###
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

点云实验室lab

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值