ch08——8.3.7CropHull任意多边形内部点云提取

点云库PCL学习教程 专栏收录该内容
51 篇文章 13 订阅

ch08——8.3.7CropHull任意多边形内部点云提取
1.代码

#include <pcl/visualization/cloud_viewer.h>
#include <iostream>
#include <pcl/io/io.h>
#include <pcl/io/pcd_io.h>
#include <pcl/point_cloud.h>
#include <pcl/point_types.h>
#include <pcl/filters/crop_hull.h>
#include <pcl/surface/concave_hull.h>
using namespace std;
using namespace pcl;
#include <vector>

int main() {
	PointCloud<PointXYZ>::Ptr cloud(new PointCloud<PointXYZ>);
	PCDReader reader;
	reader.read("D:\\pcd\\pig.pcd",*cloud);

	PointCloud<PointXYZ>::Ptr boundingbox_ptr(new PointCloud<PointXYZ>);
	//输入2D平面点云
	boundingbox_ptr->push_back(pcl::PointXYZ(0.1, 0.1, 0));
	boundingbox_ptr->push_back(pcl::PointXYZ(0.1, -0.1, 0));
	boundingbox_ptr->push_back(pcl::PointXYZ(-0.1, 0.1, 0));
	boundingbox_ptr->push_back(pcl::PointXYZ(-0.1, -0.1, 0));
	boundingbox_ptr->push_back(pcl::PointXYZ(0.15, 0.1, 0));

	//创建凸包对象
	ConcaveHull<PointXYZ> hull;
	hull.setInputCloud(boundingbox_ptr);//设置输入点云
	hull.setDimension(2);//设置凸包维度
	
	//我自己加的,不加的话凸包运行不出来
	hull.setAlpha(1);
	
	vector<Vertices> polygons;//用于保存凸包顶点
	PointCloud<PointXYZ>::Ptr surface_hull(new PointCloud<PointXYZ>);//该点云用于描述凸包形状
	hull.reconstruct(*surface_hull,polygons);//计算凸包结果

	PointCloud<PointXYZ>::Ptr objects(new PointCloud<PointXYZ>);
	CropHull<PointXYZ> bb_filter;//创建CropHull对象
	bb_filter.setDim(2);//设置维度  应与凸包维度一致
	bb_filter.setInputCloud(cloud);//设置需要滤波的点云
	bb_filter.setHullIndices(polygons);//输入封闭多变形的顶点
	bb_filter.setHullCloud(surface_hull);//输入封闭多变形的形状
	bb_filter.filter(*objects);//执行CropHull滤波,存储结果到objects
	cout << objects->size() << endl;

	//可视化
	boost::shared_ptr<visualization::PCLVisualizer> for_visualizer_v(new visualization::PCLVisualizer("crophull display"));
	for_visualizer_v->setBackgroundColor(255, 255, 255);
	int v1(0);
	for_visualizer_v->createViewPort(0.0, 0.0, 0.33, 1, v1);
	for_visualizer_v->setBackgroundColor(255, 255, 255, v1);
	for_visualizer_v->addPointCloud(cloud, "cloud", v1);
	
	//版本不一样要修改 /**< 3 floats (R, G, B) going from 0.0 (dark) to 1.0 (light) */
	for_visualizer_v->setPointCloudRenderingProperties(visualization::PCL_VISUALIZER_COLOR, 1, 0, 0, "cloud");
	//for_visualizer_v->setPointCloudRenderingProperties(visualization::PCL_VISUALIZER_COLOR, 255, 0, 0, "cloud");
	for_visualizer_v->setPointCloudRenderingProperties(visualization::PCL_VISUALIZER_POINT_SIZE, 3, "cloud");
	for_visualizer_v->addPolygon<pcl::PointXYZ>(surface_hull, 0, .069 * 255, 0.2 * 255, "backview_hull_polyline1", v1);

	int v2(0);
	for_visualizer_v->createViewPort(0.33, 0.0, 0.66, 1, v2);
	for_visualizer_v->setBackgroundColor(255, 255, 255, v2);
	for_visualizer_v->addPointCloud(surface_hull, "surface_hull", v2);
	for_visualizer_v->setPointCloudRenderingProperties(visualization::PCL_VISUALIZER_COLOR, 1, 0, 0, "surface_hull");
	for_visualizer_v->setPointCloudRenderingProperties(visualization::PCL_VISUALIZER_POINT_SIZE, 8, "surface_hull");
	for_visualizer_v->addPolygon<pcl::PointXYZ>(surface_hull, 0, .069 * 255, 0.2 * 255, "backview_hull_polyline", v2);

	int v3(0);
	for_visualizer_v->createViewPort(0.66, 0.0, 1, 1, v3);
	for_visualizer_v->setBackgroundColor(255, 255, 255, v3);
	for_visualizer_v->addPointCloud(objects, "objects", v3);
	for_visualizer_v->setPointCloudRenderingProperties(visualization::PCL_VISUALIZER_COLOR, 1, 0, 0, "objects");
	for_visualizer_v->setPointCloudRenderingProperties(visualization::PCL_VISUALIZER_POINT_SIZE, 3, "objects");

	while (!for_visualizer_v->wasStopped())
	{

		for_visualizer_v->spinOnce(1000);
	}
	system("pause");
}

2.显示
在这里插入图片描述

  • 0
    点赞
  • 1
    评论
  • 1
    收藏
  • 打赏
    打赏
  • 扫一扫,分享海报

©️2022 CSDN 皮肤主题:游动-白 设计师:我叫白小胖 返回首页

打赏作者

CV-点云

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

¥2 ¥4 ¥6 ¥10 ¥20
输入1-500的整数
余额支付 (余额:-- )
扫码支付
扫码支付:¥2
获取中
扫码支付

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

打赏作者

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

抵扣说明:

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

余额充值