Use opencv's viz module to display 3d point clouds

Displaying laser 3D point clouds generally uses the pcl library. Here is a new way of thinking, using opencv for display.

First you need to add the viz header file

#include <opencv2/viz.hpp>

The first step is to read point cloud data

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

The second step is to convert to point cloud format

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

The third step is to initialize the display window

viz::Viz3d window("window");

window.setBackgroundColor();

The fourth step is to convert the point cloud format and display

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

The fifth step is to construct the Cartesian coordinate system and the target box

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

The sixth step shows all results

while (!window.wasStopped())

{
    
    window.spinOnce(1, false);

}

The final display result is shown in the figure

References

OpenCV 3D display Viz module_Asimov_Liu's Blog-CSDN Blog

 WMesh in Opencv - Phoenix_1 - 博客园

OpenCV's viz library learning (1) - Negative One Blog - CSDN Blog

Guess you like

Origin blog.csdn.net/weixin_41691854/article/details/127265911