PCL Halcon 贪婪三角

 一、简介

使用intersect_plane_object_model_3d 这个算子,将其平面找出切面轮廓,但是再计算轮廓的前提就是三角化,所以我们先要将其三角化然后再计算mesh之后的模型与平面的切面。

二、PCL 贪婪三角

1、计算法向量

再三角化之前需要计算出点云的法向量,我这里是用的是omp 加速的方法计算法向量,代码如下

int  CalculateNormal(pcl::PointCloud<pcl::PointXYZRGB>::Ptr   cloud_in, pcl::PointCloud<pcl::Normal>::Ptr  normal_out)
{
	if (cloud_in->size()<3)
	{
		return  -1;
	}
	pcl::search::KdTree<pcl::PointXYZRGB> ::Ptr  tree(new pcl::search::KdTree<pcl::PointXYZRGB>);
	pcl::NormalEstimationOMP<pcl::PointXYZRGB, pcl::Normal> normalEstimation;
	normalEstimation.setInputCloud(cloud_in);
	normalEstimation.setNumberOfThreads(10);  //	设置加速线程书
	normalEstimation.setSearchMethod(tree);
	normalEstimation.setRadiusSearch(0.05f);
	normalEstimation.compute(*normal_out);
	return  0;
}

2、三角化

 步骤:

a、将原始点云和之前计算的法向量整合成一个点云,    

pcl::concatenateFields(*cloud_in,*normals, *cloud_normal);

b、设置三角化的搜索树

c、设置参数:最大搜索间距、搜索半径、最大点数、  三角化之后的内角范围、 表面法向量的偏差度数、法向量的朝向等参数

代码如下:


int  greedy_traingle_GenerateMesh(pcl::PointCloud<pcl::PointXYZRGB>::Ptr cloud_in, pcl::PointCloud<pcl::Normal>::Ptr normals, pcl::PolygonMesh &model)
{
	if (cloud_in->size()<3|| normals->size()<3)
	{
		return  -1;
	}
	// 将原始点云和计算的法向量整合到一个点云上面,这样的话这个点云就有 点、颜色、法向量、曲率等特征了
	pcl::PointCloud<pcl::PointXYZRGBNormal>::Ptr  cloud_normal(new pcl::PointCloud<pcl::PointXYZRGBNormal>);
	pcl::concatenateFields(*cloud_in,*normals, *cloud_normal);
	pcl::GreedyProjectionTriangulation<pcl::PointXYZRGBNormal> greedyProjectionTriangulation;

	// 定义一个平面三角化的搜索书
	pcl::search::KdTree<pcl::PointXYZRGBNormal> ::Ptr  tree(new 	pcl::search::KdTree<pcl::PointXYZRGBNormal>);
	tree->setInputCloud(cloud_normal);

	greedyProjectionTriangulation.setInputCloud(cloud_normal);
	greedyProjectionTriangulation.setSearchRadius(0.05f);//设置搜索时的半径,也就是KNN的球半径;   我的点云间隔是0.05f
	greedyProjectionTriangulation.setMu(2.5);//设置样本点搜索其近邻点的最远距离为2.5倍(典型值2.5-3),这样使得算法自适应点云密度的变化
	greedyProjectionTriangulation.setMaximumNearestNeighbors(100); // 邻域点最大个数
	greedyProjectionTriangulation.setMinimumAngle(M_1_PI/18);  // 三角之后内角最小度数  10度
	greedyProjectionTriangulation.setMaximumAngle(M_1_PI/3*2);   // 三角化之后内角的最大度数
	greedyProjectionTriangulation.setSearchMethod(tree);
	greedyProjectionTriangulation.setMaximumSurfaceAngle(M_1_PI/4);  // 设置某点法向量偏离样本法向量的最大度数,如何大约这个值,那么该店不考虑
	greedyProjectionTriangulation.setNormalConsistency(false);  //设置该参数为true保证法线朝向一致,设置为false的话不会进行法线一致性检查
	greedyProjectionTriangulation.reconstruct(model);  // 三角化 
	return  0;
}

3、显示:


int  ShowCloudAndMesh(pcl::PointCloud<pcl::PointXYZRGB>::Ptr  cloud_normal, pcl::PolygonMesh &model)
{
	// 创建可视化对象
	pcl::visualization::PCLVisualizer viewer("viewer");

	// 将当前窗口,拆分成横向的2个可视化窗口,以viewport区分(v1/v2)
	int v1;
	int v2;
	//窗口参数分别对应 x_min, y_min, x_max, y_max, viewport
	viewer.createViewPort(0.0, 0.0, 0.5, 1.0, v1);
	viewer.createViewPort(0.5, 0.0, 1.0, 1.0, v2);

	// 添加2d文字标签
	viewer.addText("v1", 10, 10, 20, 1, 0, 0, "viewport_v1", v1);
	viewer.addText("v2", 10, 10, 20, 0, 1, 0, "viewport_v2", v2);

	// 添加坐标系
	viewer.addCoordinateSystem(0.5);    // 单位:m

										// 设置可视化窗口背景色
	viewer.setBackgroundColor(0.2, 0.2, 0.2);     // r,g,b  0~1之间

												  // 向v1窗口中添加点云
	viewer.addPointCloud(cloud_normal, "cloud_src", v1);

	// 向v2窗口中添加PolygonMesh对象
	viewer.addPolygonMesh(model, "mesh_src", v2);

	// // 设置可视化窗口内的所有mesh为线框模式,部分情况下,便于可视化debug
	// viewer.setRepresentationToWireframeForAllActors();

	// 关闭窗口则退出
	while (!viewer.wasStopped()) {
		viewer.spinOnce(100);
		boost::this_thread::sleep(boost::posix_time::microseconds(100000));
	}
	return 0;

}

 三、Halcon 三角化

Halcon 案例:

triangulate_object_model_3d_greedy

The operator triangulate_object_model_3d generates a surface of triangular faces for the 3D object model ObjectModel3D and returns the resulting surface in TriangulatedObjectModel3D. Currently, the operator offers four methods for the triangulation that can be selected in Method: 'polygon_triangulation', 'xyz_mapping', 'greedy' and 'implicit'. 'polygon_triangulation' is a simple method for the conversion of a polygonal to a triangular face representation in a 3D object model. 'xyz_mapping' triangulates the points in 2D according to a 2D mapping. The other two methods are rather complex algorithms that are used to calculate triangular faces from pure 3D point data with unknown surface topology. A detailed comparison of the 'greedy' and 'implicit' algorithm is provided in the paragraph "Comparison of the triangulation methods" below

大概意思是有4中方法,最常用的和最复杂的方法就是“greedy”和“implicit”方法。

Halcon 3D 三维重建_Σίσυφος1900的博客-CSDN博客

triangulate_object_model_3d (RawPointData3D, 'greedy', [ParameterNames,'information'], [ParameterValues,'verbose'], Surface3DMorphology, Info)
count_seconds (End)
TimeMorphology := End - Start
force_pause (TimeMorphology, ForcePause)
* 
* Display results
VisParNames := ['color_0','color_1','alpha','disp_background']
if (OpenGLSupported)
    Objects3D := [Surface3DMorphology,Surface3DDefault]
    VisParValues := ['green','red',0.5,'true']
else
    Objects3D := [Surface3DDefault,Surface3DMorphology]
    VisParValues := ['red','green',0.5,'true']
endif
Status := 'Execution time (with mesh morphology): ' + TimeMorphology$'.2' + 's'
Message := ['After mesh morphology (' + VisParValues[0] + ')','Default settings (' + VisParValues[1] + ')']
delayed_clear_titled_window (WindowHandle, Message, 65, 12, OpenGLSupported)
visualize_object_model_3d (WindowHandle, Objects3D, [], Pose, VisParNames, VisParValues, [DescriptionText,Status], [], Instructions, Pose)
* 

猜你喜欢

转载自blog.csdn.net/weixin_39354845/article/details/131050242
pcl