天天看點

讀取PCD檔案的XYZ與RGB資訊

讀取pcl::PointXYZRGB中的RGB資訊:

pcl::PointCloud<pcl::PointXYZRGB>::Ptr cloud (new pcl::PointCloud<pcl::PointXYZRGB>);
           

unpack rgb into r/g/b:

(1)

int r = cloud->points[i].r;
int g = cloud->points[i].g;
int b = cloud->points[i].b;
           

(2)

unsigned long rgb = *reinterpret_cast<int*>(&cloud->points[i].rgb);
int r = (rgb >> 16) & 0x0000ff;
int g = (rgb >> 8)  & 0x0000ff;
int b = (rgb)       & 0x0000ff;
           

備注:

typedef signed char int8_t;
typedef unsigned char uint8_t;

typedef int int16_t;
typedef unsigned int uint16_t;

typedef long int32_t;
typedef unsigned long uint32_t;

typedef long long int64_t;
typedef unsigned long long uint64_t;
           

代碼如下:

#include <iostream>
#include <fstream>
#include <pcl/point_types.h>
#include <pcl/io/pcd_io.h>
#include <pcl/visualization/cloud_viewer.h>
#include <pcl/visualization/pcl_visualizer.h>
#include <pcl/filters/statistical_outlier_removal.h>
#include <pcl/filters/voxel_grid.h>

using namespace std;

int main(int argc, char *argv[])
{
//  pcl::PointXYZRGBA也可
    pcl::PointCloud<pcl::PointXYZRGB>::Ptr cloud(new pcl::PointCloud<pcl::PointXYZRGB>);
    // Fill in the cloud data
    //    pcl::io::loadPCDFile<pcl::PointXYZRGB>("result1.pcd", *cloud);
    if (pcl::io::loadPCDFile<pcl::PointXYZRGB>("result.pcd", *cloud) == -1)
    {
        PCL_ERROR("Couldn't read file chuli.pcd\n");
        return (-1);
    }
    int N=cloud->points.size();
    cout <<N<<endl;
//    cout << "Loaded "
//     << cloud->width * cloud->height
//     << " data points from test_file.pcd ."
//     << endl;
    ofstream fout("result.txt");

    for (size_t i = 0; i < N; i++)
    {
//        fout <<cloud->points[i].x
//             << " " << cloud->points[i].y
//             << " " << cloud->points[i].z
//             << " " << int(cloud->points[i].r)
//             << " " << int(cloud->points[i].g)
//             << " " << int(cloud->points[i].b)
//             << " " << int(cloud->points[i].a)
//             << endl;
        if(i==0||i==N-1)
        {
            printf("%f %f %f %d %d %d %d\n",
                   cloud->points[i].x,
                   cloud->points[i].y,
                   cloud->points[i].z,
                   cloud->points[i].r,
                   cloud->points[i].g,
                   cloud->points[i].b,
                   cloud->points[i].a);
//            unsigned long rgb = *reinterpret_cast<int*>(&cloud->points[i].rgb);
//            int r = (rgb >> 16) & 0x0000ff;
//            int g = (rgb >> 8)  & 0x0000ff;
//            int b = (rgb)       & 0x0000ff;
//            cout<<r<< " "<<g<< " "<<b<<endl;
        }

//        cout<<cloud->points[i].getArray3fMap()<<endl;
//        cout<<cloud->points[i].getVector3fMap()<<endl;
//        cout<<cloud->points[i].getRGBVector3i()<<endl;
//        cout<<cloud->points[i].getRGBAVector4i()<<endl;

    }
    fout.close();
    cout << "trans has done!!!" << endl;
    //可視化1——————————
//    pcl::visualization::CloudViewer viewer("pcd viewer");
//    viewer.showCloud(cloud);
    //可視化2——————————
//    pcl::visualization::PCLVisualizer viewer("Cloud viewer");
//
//    viewer.setCameraPosition(0,0,-3.0,0,-1,0);
//    viewer.addCoordinateSystem(0.3);
//
//    viewer.addPointCloud(cloud);
//    while(!viewer.wasStopped())
//        viewer.spinOnce(100);
//    cin.get();
    return 0;
}
           

參考自:

https://blog.csdn.net/coldplayplay/article/details/79054606

https://blog.csdn.net/wkxxuanzijie920129/article/details/51433626

http://pointclouds.org/documentation/tutorials/pcd_file_format.php

http://docs.pointclouds.org/1.7.0/structpcl_1_1_point_x_y_z_r_g_b.html#a1678cfbe6e832aa61ec0de773cab15ae

http://docs.pointclouds.org/trunk/structpcl_1_1___point_x_y_z_r_g_b_a.html