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

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>);

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);

//版本不一样要修改 /**< 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->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->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
收藏
• 打赏
• 扫一扫，分享海报

06-25 40万+
10-15

06-18 3374
10-11 2548
11-13 1万+
08-09 1383
09-06 6065
08-30 3392
12-20 3794
06-20 2948

CV-点云

¥2 ¥4 ¥6 ¥10 ¥20

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