PCL point cloud library (2) — IO module

Table of contents

2.1 IO module interface

2.2 PCD data reading and writing

(1) PCD data analysis

(2) PCD file reading and writing example

2.3 PLY data reading and writing

(1) PLY data analysis

(2) PLY file reading and writing examples

2.4 OBJ data reading and writing

(1) OBJ data analysis

(2) OBJ file reading and writing example

2.5 VTK data reading and writing

(1) VTK data analysis

(2) VTK file reading and writing example

2.6 Save as PNG


2.1 IO module interface

Reference article: Summary of PCL function library - IO module_pcl/io_Youyuanzhikong's Blog-CSDN Blog

(1)Class pcl::FileReader

Class FileReader defines the reading interface of PCD files and is mainly used as the parent class of other reading classes.

(2)Class pcl::FileWriter

Class FileWriter corresponds to FileReader and is a class interface definition for writing PCD files and can be used as the parent class of other writing classes.

(3)Class pcl::Grabber

Class Grabber is the base class definition of the device driver interface corresponding to PCL1.X.

(4)Class openni_wrapper::OpenNIDevice

Class OpenNIDevice defines the base class of OpenNI devices. Inheriting this base class can implement different OpenNI device subclasses, which are used to obtain infrared data, RGB data, depth image data, etc.

(5)Class openni_wrapper::DeviceKinect

(6)Class openni_wrapper::DevicePrimesense

(7)Class openni_wrapper::DeviceXtionPro

The above three classes respectively encapsulate the implementation of Kinect, Primesense, and XtionPro related device operations and data acquisition operations. For detailed interfaces, please refer to the key function description of its parent class OpenNIDevice.

(8)Class openni_wrapper::DeviceONI

It encapsulates the operation and data acquisition operation implementation of the virtual kinect device using ONI file playback. For its detailed interface, please refer to the key function description of its parent class OpenNIDevice.

(9)Class openni_wrapper::OpenNIDriver

The class OpenNIDriver uses a singleton mode to realize the encapsulation of the underlying driver, which contains a xn::Context object, which is provided to all devices. This class provides method implementations to enumerate and access all devices.

(10)Class openni_wrapper::OpenNIException

Class OpenNIException encapsulates general exception handling implementation.

(11)Class openni_wrapper::Image

Class Image is a simple image data encapsulation base class.

(12)Class openni_wrapper::ImageBayerGRBG

(13)Class openni_wrapper::ImageRGB24

(14)Class openni_wrapper::ImageYUV422 Class Reference

The above three classes respectively implement the interface for converting raw data from BayerGRBG, RGB24, and YUV422 to images. For details, refer to the key function descriptions of their parent classes.

(15)Class pcl::OpenNIGrabber

Class OpenNIGrabber implements the data collection interface for OpenNI devices (such as Primesense PSDK, Microsoft Kinect, Asus XTion Pro/Live). For details, please refer to the key function description of its parent class Grabber.

(16)Class pcl::PCDReader

(17)Class pcl::PLYReader

The above two classes are respectively the implementations of the PCD and PLY file format read-in interfaces. For details, refer to its parent class pcl: :FileReader.

(18)Class pcl::PLYWriter

(19)Class pcl::PCDWriter

The above two classes are the implementation of the PCD and PLY file format writing interfaces respectively. For details, please refer to its parent class pcl: :FileWriter.

(20)Class pcl::io::IOException

Class pcl::io::IOException is an I/O-related exception handling interface implementation. For details, please refer to its parent class PCLEx-ception.

(21) Other key members of the I/O module

2.2 PCD data reading and writing

(1) PCD data analysis

# .PCD v0.7 - Point Cloud Data file format
VERSION 0.7
FIELDS x y z _
SIZE 4 4 4 1
TYPE F F F U
COUNT 1 1 1 4
WIDTH 112099
HEIGHT 1
VIEWPOINT 0 0 0 1 0 0 0
POINTS 112099
DATA binary
  • VERSION 0.7: Specifies the version of the pcd file.
  • FIELDS: Specify the dimensions that each point can have, and what each dimension represents. For example: FIELDS xyzrgb represents the position information (x, y, z) and color information (r, g, b) of the point.
  • SIZE: Specify the memory occupied by each data in bytes.
  • TYPE: Specifies the data type of each data. Where invalid points are usually stored as NAN type.

            I: can represent int8, int16, int32.
            U: can represent uint8, unit16, uint32.
            F: represents float (the one used in the above figure is a floating point type).

  • COUNT: Specifies how many elements each dimension has. For example xyz data usually has only one element.
  • WIDTH: Specifies the width of the data points. It contains two meanings: (1) The total number of point clouds can be specified (same as POINTS), which is used for unorganized data. (2) The width of the organized point cloud data (total number of consecutive points) can be specified.
  • HEIGTH:  Specify the height of the data points, which contains two meanings: (1) You can specify the height of the organized point cloud data (total number of rows). (2) For unorganized data, it is set to 1.
  • POINTS: Specifies the total number of point clouds.
  • VIEWPOINT: The viewpoint when collecting data (composed of translation tx, ty, tz and quaternion qw, qx, qy, qz).
  • DATA: The data type of point cloud data storage (supports ascii and binary). If in ASCII form, each point occupies a new line.

(2) PCD file reading and writing example

cmake_minimum_required(VERSION 2.6)
project(pcd)
 
find_package(PCL 1.10 REQUIRED)
 
include_directories(${PCL_INCLUDE_DIRS})
link_directories(${PCL_LIBRARY_DIRS})
add_definitions(${PCL_DEFINITIONS})
 
add_executable(pcd pcd.cpp)
 
target_link_libraries (pcd ${PCL_LIBRARIES})
 
install(TARGETS pcd RUNTIME DESTINATION bin)
#include <iostream>
#include <pcl/point_cloud.h>
#include <pcl/io/pcd_io.h>
#include <pcl/visualization/pcl_visualizer.h>

using namespace std;

int main()
{
    pcl::PCDReader reader;
    pcl::PCLPointCloud2 org;
    pcl::io::loadPCDFile("../pcdfile.pcd",org);

    for(auto &f : org.fields)
    cout << f.name;

    pcl::PointCloud<pcl::PointXYZ> cloud;
    pcl::fromPCLPointCloud2<pcl::PointXYZ>(org,cloud);

    pcl::PCDWriter writer;
    pcl::io::savePCDFileBinaryCompressed("../savepcdfile.pcd",cloud);
    pcl::visualization::PCLVisualizer::Ptr viewer(new pcl::visualization::PCLVisualizer);
    viewer->setWindowName("PCDFile");
    viewer->addPointCloud(cloud.makeShared());
    while (!viewer->wasStopped())
    {
        viewer->spinOnce(100);
    }
    return 0;
}

2.3 PLY data reading and writing

(1) PLY data analysis

Typical PLY file structure:

  • head
  • vertex list
  • List of dough pieces
  • List of other elements
ply
format ascii 1.0
element vertex 14806
property float x
property float y
property float z
property float nx
property float ny
property float nz
element face 0
property list uchar int vertex_indices
end_header
0.91441 -0.536438 0.822624 -0.0442205 -0.930906 0.362575
0.933494 -0.545228 0.820276 0.073409 -0.981856 0.174844
...

(2) PLY file reading and writing examples

cmake_minimum_required(VERSION 2.6)
project(ply)
 
find_package(PCL 1.10 REQUIRED)
 
include_directories(${PCL_INCLUDE_DIRS})
link_directories(${PCL_LIBRARY_DIRS})
add_definitions(${PCL_DEFINITIONS})
 
add_executable(ply ply.cpp)
 
target_link_libraries (ply ${PCL_LIBRARIES})
 
install(TARGETS ply RUNTIME DESTINATION bin)
#include <iostream>
#include <pcl/point_cloud.h>
#include <pcl/io/ply_io.h>
#include <pcl/visualization/pcl_visualizer.h>

using namespace std;

int main()
{
    pcl::PCLPointCloud2 cloud;
    pcl::io::loadPLYFile("../ply.ply",cloud);

    pcl::PLYReader reader;
    pcl::PLYWriter writer;

    pcl::PointCloud<pcl::PointXYZ> cloud1;
    pcl::fromPCLPointCloud2<pcl::PointXYZ>(cloud,cloud1);

    pcl::io::savePLYFile("saveply.ply",cloud,Eigen::Vector4f::Zero (),
                         Eigen::Quaternionf::Identity (),
                         true);

    pcl::visualization::PCLVisualizer::Ptr viewer(new pcl::visualization::PCLVisualizer);
    viewer->setWindowName("PLYFile");
    pcl::visualization::PointCloudColorHandlerGenericField<pcl::PointXYZ> color(cloud1.makeShared(), "y");
    viewer->addPointCloud(cloud1.makeShared(),color);
    viewer->setPointCloudRenderingProperties(pcl::visualization::PCL_VISUALIZER_POINT_SIZE,2);

    while(!viewer->wasStopped())
        viewer->spinOnce(100);

    return 0;
}

2.4 OBJ data reading and writing

(1) OBJ data analysis

mtllib cube.mtl
g default
v -0.500000 -0.500000 0.500000
v 0.500000 -0.500000 0.500000
v -0.500000 0.500000 0.500000
v 0.500000 0.500000 0.500000
v -0.500000 0.500000 -0.500000
v 0.500000 0.500000 -0.500000
v -0.500000 -0.500000 -0.500000
v 0.500000 -0.500000 -0.500000
vt 0.001992 0.001992
vt 0.998008 0.001992
vt 0.001992 0.998008
vt 0.998008 0.998008
...

mtllib: represents the material library, usually pointing to an mtl file

  • v(vertices): vertices of geometric shapes, because objects are made of surfaces, and surfaces are made of lines, and lines are made of points, so no matter what shape it is, it must have geometric vertices; some applications support vertex color , represented by xyz followed by red, green, blue values. Color values ​​range from 0 to 1.0.
  • vt (vertex texture): Vertex texture, representing which pixel of the texture map the current vertex corresponds to, usually 0-1. If it is greater than 1, it is equivalent to re-expanding the texture and then taking the value, such as mirror filling, flip filling, etc. Then calculate the specific pixel position based on the width and height of the texture map
  • vn (vertex normal): vertex normal. In physics, it is said that the eye sees an object because light is reflected from the surface of the object to the eye, so this normal is the normal used to calculate the reflected light through the incident light.
  • f(face): Most geometries include faces, except for models like hair, which only contain the vertices of one hair, and the hair of most models is also rendered using the patch method.

        Other references: obj format analysis_obj format detailed explanation_Fengyi Bingzhou's blog-CSDN blog

(2) OBJ file reading and writing example

cmake_minimum_required(VERSION 2.6)
project(obj)
 
find_package(PCL 1.10 REQUIRED)
 
include_directories(${PCL_INCLUDE_DIRS})
link_directories(${PCL_LIBRARY_DIRS})
add_definitions(${PCL_DEFINITIONS})
 
add_executable(obj obj.cpp)
 
target_link_libraries (obj ${PCL_LIBRARIES})
 
install(TARGETS obj RUNTIME DESTINATION bin)
#include <iostream>

#include <pcl/point_cloud.h>
#include <pcl/io/obj_io.h>
#include <pcl/visualization/pcl_visualizer.h>

using namespace std;

int main()
{
    pcl::PolygonMesh mesh;
    pcl::PCLPointCloud2 cloud;
    pcl::TextureMesh tmesh;

#if 0
    pcl::io::loadOBJFile("../obj.obj",cloud);
    pcl::io::loadOBJFile("../obj.obj",mesh);
    pcl::io::loadOBJFile("../obj.obj",tmesh);
#else
    pcl::OBJReader objreader;
    objreader.read("../obj.obj",cloud);
    objreader.read("../obj.obj",mesh);
    objreader.read("../obj.obj",tmesh);
#endif

    pcl::io::saveOBJFile("../saveobj.obj",mesh);

    pcl::PointCloud<pcl::PointXYZ> cloudxyz;
    pcl::fromPCLPointCloud2<pcl::PointXYZ>(cloud,cloudxyz);

    pcl::visualization::PCLVisualizer::Ptr viewer(new pcl::visualization::PCLVisualizer);
    viewer->setWindowName("OBJFile");

    pcl::visualization::PointCloudColorHandlerGenericField<pcl::PointXYZ> color(cloudxyz.makeShared(), "z");
    viewer->addPointCloud(cloudxyz.makeShared(),color);
    viewer->setPointCloudRenderingProperties(pcl::visualization::PCL_VISUALIZER_POINT_SIZE,2);
    viewer->addPolygonMesh(mesh);

    while(!viewer->wasStopped())
        viewer->spinOnce(100);

    return 0;
}

2.5 VTK data reading and writing

(1) VTK data analysis

# vtk DataFile Version 2.0
vtk output
ASCII
DATASET POLYDATA
POINTS 2312 float
0.263107 0 0.425176 0.33131 0 0.374478 0.389942 0 0.312962 
0.43731 0 0.242405 0.472045 0 0.164845 0.493143 0 0.0825238 
  • The first line is the version of the vtk file (this is the legacy version, in addition to the newer xml version)
  • The second line is the file description, write whatever you want
  • The third line is ASCII or BINARY
  • The fourth line describes the data type, including STRUCTURED_POINTS, STRUCTURED_GRID, RECTILINEAR_GRID, POLYDATA
  • Next is the data, which is divided into three parts: POINTS point data, CELSS grid data, CELL_TYPES grid type
     

(2) VTK file reading and writing example

cmake_minimum_required(VERSION 2.6)
project(vtkfile)
 
find_package(PCL 1.10 REQUIRED)
find_package(VTK REQUIRED)

include(${VTK_USE_FILE})
link_directories(${VTK_LIBRARY_DIRS})
 
include_directories(${PCL_INCLUDE_DIRS})
link_directories(${PCL_LIBRARY_DIRS})
add_definitions(${PCL_DEFINITIONS})

add_executable(vtkfile vtkfile.cpp)
 
target_link_libraries (vtkfile ${PCL_LIBRARIES} ${VTK_LIBRARIES})
target_link_libraries (vtkfile ${VTK_LIBRARIES})
 
install(TARGETS vtkfile RUNTIME DESTINATION bin)
#include <iostream>
#include <pcl/point_cloud.h>
#include <pcl/io/vtk_io.h>
#include <pcl/io/vtk_lib_io.h>
#include <pcl/visualization/pcl_visualizer.h>
#include <pcl/io/obj_io.h>

using namespace std;

int main()
{
    pcl::PCLPointCloud2 cloud;
    pcl::PolygonMesh mesh,mesh1,mesh2;

#if 0
    pcl::io::loadPolygonFile("../vtk.vt",mesh);
#else
    pcl::io::loadPolygonFileVTK("../vtk.vtk",mesh);
    pcl::io::loadPolygonFileVTK("../vtk.vtk",mesh2);
#endif
    
    pcl::PointCloud<pcl::PointXYZ> cloudxyz;
    pcl::fromPCLPointCloud2<pcl::PointXYZ>(mesh.cloud,cloudxyz);

    pcl::visualization::PCLVisualizer::Ptr viewer(new pcl::visualization::PCLVisualizer);
    viewer->setWindowName("VTKFile");

    pcl::visualization::PointCloudColorHandlerGenericField<pcl::PointXYZ> color(cloudxyz.makeShared(),"y");
    viewer->addPointCloud(cloudxyz.makeShared(),color);
    viewer->setPointCloudRenderingProperties(pcl::visualization::PCL_VISUALIZER_POINT_SIZE,3);
    viewer->addPolygonMesh(mesh);

    while(!viewer->wasStopped())
        viewer->spinOnce(100);

    return 0;
}

2.6 Save as PNG

#include <iostream>
#include <pcl/point_cloud.h>
#include <pcl/io/pcd_io.h>
#include <pcl/io/png_io.h>
#include <pcl/visualization/pcl_visualizer.h>

using namespace std;

int main()
{

    pcl::PCDReader reader;
    pcl::PCLPointCloud2 org;

    pcl::io::loadPCDFile("../cow.pcd",org);

    for(auto &f : org.fields)
        cout << f.name << endl;

    pcl::PointCloud<pcl::PointXYZ> cloud;
    pcl::fromPCLPointCloud2<pcl::PointXYZ>(org,cloud);

    pcl::io::savePNGFile("../savepng.png",cloud,"rgb");
    pcl::visualization::PointCloudColorHandlerGenericField<pcl::PointXYZ> color(cloud.makeShared(), "z");
    pcl::visualization::PCLVisualizer::Ptr viewer(new pcl::visualization::PCLVisualizer);
    viewer->setWindowName("savePNG");
    viewer->addPointCloud(cloud.makeShared(),color);
    viewer->setPointCloudRenderingProperties(pcl::visualization::PCL_VISUALIZER_POINT_SIZE,2);
    
    while(!viewer->wasStopped())
        viewer->spinOnce(100);

    return 0;
}}

 

Guess you like

Origin blog.csdn.net/qq_41921826/article/details/130286950