显示激光3d点云一般使用pcl库,这里提供一种新的思路,使用opencv来做显示。
首先需要增加viz头文件
#include <opencv2/viz.hpp>
第一步读取点云数据
int num_points;
int pt_feature = 4;
std::ifstream in("../0000.bin", std::ios::binary);
if (!in.is_open()) {
std::cerr << "Could not open the scan!" << std::endl;
return 1;
}
in.seekg(0, std::ios::end);
num_points = in.tellg() / (pt_feature * sizeof(float));
in.seekg(0, std::ios::beg);
std::vector<float> pts(pt_feature * num_points);
in.read((char*)&pts[0], pt_feature * num_points * sizeof(float));
第二步转换成点云格式
std::vector<LidarPoint>lidar_pts;
for(int i = 0; i<num_points;i++){ // 解析激光点云数据
LidarPoint lidar_pt;
lidar_pt.fX = pts[i*4+0];
lidar_pt.fY = pts[i*4+1];
lidar_pt.fZ = pts[i*4+2]+2;
lidar_pt.nIntensity = pts[i*4+3];
lidar_pt.ntype = POINT_TYPE::PT_UNKNOWN;
lidar_pts.push_back(lidar_pt);
}
int point_num = lidar_pts.size();
第三步初始化显示窗口
viz::Viz3d window("window");
window.setBackgroundColor();
第四步转换点云格式并显示
Mat point_cloud = Mat::zeros(3, point_num, CV_32FC3);
//point cloud 赋值,每组第一个参数为x,第二个参数为y,第三个参数为z
for(int row = 0; row < point_num; row++) {
point_cloud.ptr<Vec3f>(0)[row][0] = lidar_pts[row].fX;
point_cloud.ptr<Vec3f>(0)[row][1] = lidar_pts[row].fY;
point_cloud.ptr<Vec3f>(0)[row][2] = lidar_pts[row].fZ;
}
cv::viz::WCloud cloud(point_cloud, viz::Viz3d::Color::white()); // 显示点云和颜色
window.showWidget("cloud",cloud);
第五步构造直角坐标系和目标框
window.showWidget("Coordinate", viz::WCoordinateSystem()); // 显示坐标
viz::WCube cube_widget(Point3f(51.0,-1.5,0.0), Point3f(55.0,0.5,1.5), true, viz::Color::red());
cube_widget.setRenderingProperty(viz::LINE_WIDTH, 1.0); // 设置线条粗细
window.showWidget("Cube Widget", cube_widget);
第六步显示所有结果
while (!window.wasStopped())
{
window.spinOnce(1, false);
}
最终显示结果如图
参考资料
OpenCV 3D显示Viz模块_Asimov_Liu的博客-CSDN博客
Opencv中的WMesh - 凤凰_1 - 博客园文章来源:https://uudwc.com/A/Nby1v
OpenCV的viz库学习(一)_负壹的博客-CSDN博客文章来源地址https://uudwc.com/A/Nby1v