点云库PCL学习:提取最小包围盒(AABB、OBB)

最小包围盒介绍

包围盒也叫外接最小矩形,是一种求解离散点集最优包围空间的算法,基本思想是用体积稍大且特性简单的几何体(称为包围盒)来近似地代替复杂的几何对象。

常见的包围盒算法有AABB包围盒、包围球、方向包围盒OBB以及固定方向凸包FDH。碰撞检测问题在虚拟现实、计算机辅助设计与制造、游戏及机器人等领域有着广泛的应用,甚至成为关键技术。而包围盒算法是进行碰撞干涉初步检测的重要方法之一。
在这里插入图片描述

AABB、OBB包围盒的提取

#include <vtkAutoInit.h>
VTK_MODULE_INIT(vtkRenderingOpenGL);
VTK_MODULE_INIT(vtkInteractionStyle);
#include <pcl/features/moment_of_inertia_estimation.h>
#include <vector>
#include <pcl/io/pcd_io.h>
#include <pcl/point_types.h>
#include <pcl/visualization/cloud_viewer.h>
#include <boost/thread/thread.hpp>
#include <pcl/visualization/cloud_viewer.h>
 
int main(int argc, char** argv)
{
       pcl::PointCloud<pcl::PointXYZ>::Ptr cloud(new pcl::PointCloud<pcl::PointXYZ>());
       if (pcl::io::loadPCDFile("test.pcd", *cloud) == -1)
       {
              PCL_ERROR("Cloudn't read file!");
              system("pause");
              return -1;
       }
       pcl::MomentOfInertiaEstimation <pcl::PointXYZ> feature_extractor;
       feature_extractor.setInputCloud(cloud);
       feature_extractor.compute();
       std::vector <float> moment_of_inertia;  
       std::vector <float> eccentricity;  
       pcl::PointXYZ min_point_AABB;
       pcl::PointXYZ max_point_AABB;
       pcl::PointXYZ min_point_OBB;
       pcl::PointXYZ max_point_OBB;
       pcl::PointXYZ position_OBB;
       Eigen::Matrix3f rotational_matrix_OBB;
       float major_value, middle_value, minor_value; 
       Eigen::Vector3f major_vector, middle_vector, minor_vector;
       Eigen::Vector3f mass_center; 
       feature_extractor.getMomentOfInertia(moment_of_inertia);
       feature_extractor.getEccentricity(eccentricity);
       feature_extractor.getAABB(min_point_AABB,max_point_AABB); 
       feature_extractor.getOBB(min_point_OBB,max_point_OBB, position_OBB, rotational_matrix_OBB); 
       feature_extractor.getEigenValues(major_value,middle_value, minor_value); 
       feature_extractor.getEigenVectors(major_vector,middle_vector, minor_vector); 
       feature_extractor.getMassCenter(mass_center);                                                                                   
      boost::shared_ptr<pcl::visualization::PCLVisualizer> viewer(new pcl::visualization::PCLVisualizer("3D Viewer"));
       viewer->setBackgroundColor(1,1, 1); 
       viewer->addCoordinateSystem(1.0);
       viewer->initCameraParameters();  
       viewer->addPointCloud<pcl::PointXYZ>(cloud,pcl::visualization::PointCloudColorHandlerCustom<pcl::PointXYZ>(cloud, 0,255, 0), "sample cloud");
       viewer->setPointCloudRenderingProperties(pcl::visualization::PCL_VISUALIZER_POINT_SIZE,
5, "sample cloud");
       viewer->addCube(min_point_AABB.x,max_point_AABB.x,min_point_AABB.y,max_point_AABB.y,min_point_AABB.z,max_point_AABB.z, 1.0, 0.0, 0.0, "AABB"); 
       viewer->setShapeRenderingProperties(pcl::visualization::PCL_VISUALIZER_OPACITY,
0.1, "AABB");  
       viewer->setShapeRenderingProperties(pcl::visualization::PCL_VISUALIZER_LINE_WIDTH,
4, "AABB");  
       Eigen::Vector3f position(position_OBB.x, position_OBB.y, position_OBB.z); 
       std::cout<< "position_OBB: " << position_OBB << endl;
       std::cout<< "mass_center: " << mass_center << endl;
       Eigen::Quaternionf quat(rotational_matrix_OBB);  
       viewer->addCube(position,quat, max_point_OBB.x - min_point_OBB.x, max_point_OBB.y - min_point_OBB.y,max_point_OBB.z - min_point_OBB.z, "OBB");
       viewer->setShapeRenderingProperties(pcl::visualization::PCL_VISUALIZER_COLOR,
0, 0, 1, "OBB");
       viewer->setShapeRenderingProperties(pcl::visualization::PCL_VISUALIZER_OPACITY,
0.1, "OBB");       
       viewer>setShapeRenderingProperties(pcl::visualization::PCL_VISUALIZER_LINE_WIDTH,
4, "OBB");
       viewer->setRepresentationToWireframeForAllActors();
       pcl::PointXYZ center(mass_center(0), mass_center(1), mass_center(2));
       pcl::PointXYZ x_axis(major_vector(0) + mass_center(0), major_vector(1) + mass_center(1), major_vector(2) + mass_center(2));
       pcl::PointXYZ y_axis(middle_vector(0) + mass_center(0), middle_vector(1) +mass_center(1),middle_vector(2) + mass_center(2));
       pcl::PointXYZ z_axis(minor_vector(0) + mass_center(0), minor_vector(1) +mass_center(1),minor_vector(2) + mass_center(2));
       viewer->addLine(center,x_axis, 1.0f, 0.0f, 0.0f, "major eigen vector");
       viewer->addLine(center,y_axis, 0.0f, 1.0f, 0.0f, "middle eigen vector");
       viewer->addLine(center,z_axis, 0.0f, 0.0f, 1.0f, "minor eigen vector");
       std::cout<< "size of cloud :" << cloud->points.size() <<endl;
       std::cout << "moment_of_inertia :" << moment_of_inertia.size() << endl;
       std::cout<< "eccentricity :" << eccentricity.size() << endl; 
       viewer->setCameraPosition(0,0, 0, 0, 156, -20, 0, 0, 1, 0);//设置相机位置,焦点,方向
       while (!viewer->wasStopped())
       {
              viewer->spinOnce(100);
              boost::this_thread::sleep(boost::posix_time::microseconds(100000));
       }
       system("pause");
       return (0);
}

提取结果

在这里插入图片描述

  • 13
    点赞
  • 89
    收藏
    觉得还不错? 一键收藏
  • 12
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 12
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值