点云包围盒AABB and OBB
太惨了,之前半年的笔记几乎都在一个文档里放着,结果不知道啥时候可能手误干了个啥,早上打开后只有空空如也的白色界面,让我先默哀两分钟 。
是的,这是小白的笔记。
正文:
代码在各大道友的博客里都能找到,真好。
运行时会出现异常或者出现空的命令行,查找未果,走过路过的道友看到了给我说一下为啥,谢谢。
(0x00007FFC0F84F621 (ucrtbased.dll) (AABB_box_pcl.exe 中)处有未经处理的异常: 0xC0000005: 读取位置 0x0000000000000000 时发生访问冲突。)
经各种尝试,此法可解一时之忧:在生成的.exe文件的目录下进入命令行,运行AABB_box_pcl.exe文件,同时需要输入需要建立包围盒的点云。
addcube方法结果如下(一个AABB的cube,一个OBB的cube):
addLine方法结果如下:
总结:
做小白好难啊!有没有大佬带?
4.13更新
之前在网上找的代码,后来发现pcl源代码中就有,这里补充下代码。代码在第十章第六节moment of inertia中。
壹、
大体思路是找到最大xyz和最小的xyz得到八个顶点:
(max_x,max_y,max_z)(min_x,max_y,max_z)
(max_x,min_y,max_z)(max_x,max_y,min_z)
(max_x,min_y,min_z)(min_x,max_y,min_z)
(min_x,min_y,max_z)(min_x,min_y,min_z)
//定义储存极值的两个点,box_min_max.cpp
pcl::PointXYZ minPoint, maxPoint;
pcl::getMinMax3D(*cloud, minPoint, maxPoint);
贰、
但实际上要比这个复杂,使用PCA(Principal Component Analysis主成分分析)主方向。
因为如果直接在原来的点云上的那8个点,而不去转到主方向的话,那就是在原来相机的x,y,z坐标系下得到最大的x,y,z和最小的x,y,z也就是说最后得到的盒子的棱是平行于你相机坐标系的x,y,z的,这样就不是最小的盒子了,举例如下图:对于类似平面的点云a,b,c,d,如果根据主方向画盒子如左图,如果按之前的坐标系,如图。
常见的包围盒算法有轴对齐AABB包围盒、包围球、方向包围盒OBB以及固定方向凸包FDH。碰撞检测问题在虚拟现实、计算机辅助设计与制造、游戏及机器人等领域有着广泛的应用甚至成为关键技术。而包围盒算法是进行碰撞干涉初步检测的重要方法之一。
AABB,OBB应用广泛,AABB更常见,因为生成简单,与坐标轴对其。但也有缺点,当老虎沿着z站立,AABB和老虎比较贴合,但老虎旋转角度后,包围盒空间变大,就需要OBB。
最小包围盒的计算过程大致如下:
1.利用PCA主元分析法获得点云的三个主方向,获取质心,计算协方差,获得协方差矩阵,求取协方差矩阵的特征值和特征向量,特征向量即为主方向。
2.利用1中获得的主方向和质心,将输入点云转换至原点,且主方向与坐标系方向重回,建立变换到原点的点云的包围盒。
3.给输入点云设置主方向和包围盒,通过输入点云到原点点云变换的逆变换实现.
叁、
代码:
#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>
int main (int argc, char** argv)
{
if (argc != 2)
return (0);
pcl::PointCloud<pcl::PointXYZ>::Ptr cloud (new pcl::PointCloud<pcl::PointXYZ> ());
if (pcl::io::loadPCDFile (argv[1], *cloud) == -1)
return (-1);
//moment of inertia 转动惯量
//MomentOfInertiaEstimation--执行提取基于转动惯量的特征,可以计算AABB/OBB,以及被投影云的偏心率
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 ("点云库PCL学习教程第二版-基于惯性矩与偏心率的描述子"));
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;
//Eigen::Vector3f p1 (min_point_OBB.x, min_point_OBB.y, min_point_OBB.z);
//Eigen::Vector3f p2 (min_point_OBB.x, min_point_OBB.y, max_point_OBB.z);
//Eigen::Vector3f p3 (max_point_OBB.x, min_point_OBB.y, max_point_OBB.z);
//Eigen::Vector3f p4 (max_point_OBB.x, min_point_OBB.y, min_point_OBB.z);
//Eigen::Vector3f p5 (min_point_OBB.x, max_point_OBB.y, min_point_OBB.z);
//Eigen::Vector3f p6 (min_point_OBB.x, max_point_OBB.y, max_point_OBB.z);
//Eigen::Vector3f p7 (max_point_OBB.x, max_point_OBB.y, max_point_OBB.z);
//Eigen::Vector3f p8 (max_point_OBB.x, max_point_OBB.y, min_point_OBB.z);
//p1 = rotational_matrix_OBB * p1 + position;
//p2 = rotational_matrix_OBB * p2 + position;
//p3 = rotational_matrix_OBB * p3 + position;
//p4 = rotational_matrix_OBB * p4 + position;
//p5 = rotational_matrix_OBB * p5 + position;
//p6 = rotational_matrix_OBB * p6 + position;
//p7 = rotational_matrix_OBB * p7 + position;
//p8 = rotational_matrix_OBB * p8 + position;
//pcl::PointXYZ pt1 (p1 (0), p1 (1), p1 (2));
//pcl::PointXYZ pt2 (p2 (0), p2 (1), p2 (2));
//pcl::PointXYZ pt3 (p3 (0), p3 (1), p3 (2));
//pcl::PointXYZ pt4 (p4 (0), p4 (1), p4 (2));
//pcl::PointXYZ pt5 (p5 (0), p5 (1), p5 (2));
//pcl::PointXYZ pt6 (p6 (0), p6 (1), p6 (2));
//pcl::PointXYZ pt7 (p7 (0), p7 (1), p7 (2));
//pcl::PointXYZ pt8 (p8 (0), p8 (1), p8 (2));
//viewer->addLine (pt1, pt2, 1.0, 0.0, 0.0, "1 edge");
//viewer->addLine (pt1, pt4, 1.0, 0.0, 0.0, "2 edge");
//viewer->addLine (pt1, pt5, 1.0, 0.0, 0.0, "3 edge");
//viewer->addLine (pt5, pt6, 1.0, 0.0, 0.0, "4 edge");
//viewer->addLine (pt5, pt8, 1.0, 0.0, 0.0, "5 edge");
//viewer->addLine (pt2, pt6, 1.0, 0.0, 0.0, "6 edge");
//viewer->addLine (pt6, pt7, 1.0, 0.0, 0.0, "7 edge");
//viewer->addLine (pt7, pt8, 1.0, 0.0, 0.0, "8 edge");
//viewer->addLine (pt2, pt3, 1.0, 0.0, 0.0, "9 edge");
//viewer->addLine (pt4, pt8, 1.0, 0.0, 0.0, "10 edge");
//viewer->addLine (pt3, pt4, 1.0, 0.0, 0.0, "11 edge");
//viewer->addLine (pt3, pt7, 1.0, 0.0, 0.0, "12 edge");
while(!viewer->wasStopped())
{
viewer->spinOnce (100);
boost::this_thread::sleep (boost::posix_time::microseconds (100000));
}
return (0);
}
实验结果:上面更新之前的是AABB_box_pcl.exe的结果。
example2/bounding_box.cpp结果:
这是上面代码的结果:红色AABB 蓝色OBB
肆、坐标系相关
PCL点云库visualization模块中的PCLVisualizer类,它是PCL可视化3D点云的主要类。其中addCoordinateSystem()函数可以在可视化窗口中的坐标原点(0,0,0)处添加一个红绿蓝三色的三维指示坐标轴,红色是X轴,绿色是Y轴,蓝色是Z,也就是说PCL点云库中使用的是右手三维坐标系。
参考:
https://blog.csdn.net/josslyn/article/details/79902729
https://blog.csdn.net/ruoqiqingchedi/article/details/83959224
https://blog.csdn.net/u013541523/article/details/82982522
https://blog.csdn.net/qq_33624918/article/details/80488590