【问题标题】:How can I get rgb value from point cloud如何从点云中获取 rgb 值
【发布时间】:2015-11-06 21:45:37
【问题描述】:

我有一个点云。我想得到它的RGB值。我该怎么做?
为了让我的问题更清楚,请查看代码。

// Load the first input file into a PointCloud<T> with an appropriate type : 
        pcl::PointCloud<pcl::PointXYZRGB>::Ptr cloud1 (new pcl::PointCloud<pcl::PointXYZRGB>);
        if (pcl::io::loadPCDFile<pcl::PointXYZRGB> ("../data/station1.pcd", *cloud1) == -1)
        {
            std::cout << "Error reading PCD file !!!" << std::endl;
            exit(-1);
        }

我想单独获取每个值

std::cout << " x = " << cloud1->points[11].x << std::endl;
std::cout << " y = " << cloud1->points[11].y << std::endl;
std::cout << " z = " << cloud1->points[11].z << std::endl;
std::cout << " r = " << cloud1->points[11].r << std::endl;
std::cout << " g = " << cloud1->points[11].g << std::endl;
std::cout << " b = " << cloud1->points[11].b << std::endl;

但结果我得到了类似的东西:

 x = 2.33672
 y = 3.8102
 z = 8.86153
 r = �
 g = w
 b = �

【问题讨论】:

    标签: c++ rgb point-cloud-library


    【解决方案1】:

    From the point cloud docs:

    表示欧几里得 xyz 坐标和RGB 颜色的点结构。

    由于历史原因(PCL 最初是作为 ROS 包开发的),RGB 信息被打包成整数并转换为浮点数。这是我们希望在不久的将来删除的内容,但与此同时,以下代码 sn-p 应该可以帮助您在 PointXYZRGB 结构中打包和解包 RGB 颜色:

    // pack r/g/b into rgb
    uint8_t r = 255, g = 0, b = 0;    // Example: Red color
    uint32_t rgb = ((uint32_t)r << 16 | (uint32_t)g << 8 | (uint32_t)b);
    p.rgb = *reinterpret_cast<float*>(&rgb);
    

    要将数据解压缩为单独的值,请使用:

    PointXYZRGB p;
    // unpack rgb into r/g/b
    uint32_t rgb = *reinterpret_cast<int*>(&p.rgb);
    uint8_t r = (rgb >> 16) & 0x0000ff;
    uint8_t g = (rgb >> 8)  & 0x0000ff;
    uint8_t b = (rgb)       & 0x0000ff;
    

    或者,从 1.1.0 开始,您可以直接使用 p.r、p.g 和 p.b。

    文件point_types.hpp559行的定义。

    【讨论】:

    • 谢谢@Kevin;但我仍然得到同样的错误;这是新代码:` uint32_t rgb = *reinterpret_cast(&cloud1->points[11].rgb); uint8_t r = (rgb >> 16) & 0x0000ff; uint8_t g = (rgb >> 8) & 0x0000ff; uint8_t b = (rgb) & 0x0000ff; std::cout points[11].x points[11].y points[11].z
    • 我成功解决了!实际上 R、G 和 B 的值是 unsigned char 类型的,因此我们需要将它们转换为 int 以查看它们的数值。我试试:std::cout &lt;&lt; " r = " &lt;&lt; int(cloud1-&gt;points[11].r) &lt;&lt; std::endl; 它可以工作
    • Make that (int)cloud1->points[11].r ;) 您现在正在调用 int 的构造函数(创建一个新变量并使用 cout 将其返回到输出),而不是强制转换值转换为 int 类型。
    猜你喜欢
    • 2015-06-10
    • 2010-10-01
    • 2021-08-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-06-21
    • 1970-01-01
    相关资源
    最近更新 更多