c++ - 从 PointCloud 到 Mat 的转换

标签 c++ opencv image-processing computer-vision point-cloud-library

假设我初始化了一个点云。我想将其 RGB channel 存储在 opencv 的 Mat 数据类型中。我该怎么做?

pcl::PointCloud<pcl::PointXYZRGBA>::Ptr cloud (new pcl::PointCloud<pcl::PointXYZRGBA>);   //Create a new cloud
pcl::io::loadPCDFile<pcl::PointXYZRGBA> ("cloud.pcd", *cloud);

最佳答案

我的理解是否正确,您只对点云的 RGB 值感兴趣而不关心它的 XYZ 值?

然后你可以这样做:

pcl::PointCloud<pcl::PointXYZRGBA>::Ptr cloud (new pcl::PointCloud<pcl::PointXYZRGBA>); 
//Create a new cloud
pcl::io::loadPCDFile<pcl::PointXYZRGBA> ("cloud.pcd", *cloud);

cv::Mat result; 

if (cloud->isOrganized()) {
    result = cv::Mat(cloud->height, cloud->width, CV_8UC3);

    if (!cloud->empty()) {

        for (int h=0; h<result.rows; h++) {
            for (int w=0; w<result.cols; w++) {
                pcl::PointXYZRGBA point = cloud->at(w, h);

                Eigen::Vector3i rgb = point.getRGBVector3i();

                result.at<cv::Vec3b>(h,w)[0] = rgb[2];
                result.at<cv::Vec3b>(h,w)[1] = rgb[1];
                result.at<cv::Vec3b>(h,w)[2] = rgb[0];
            }
        }
    }
}

我认为足以展示基本思想。

但这只有在你的点云是有组织的情况下才有效:

An organized point cloud dataset is the name given to point clouds that resemble an organized image (or matrix) like structure, where the data is split into rows and columns. Examples of such point clouds include data coming from stereo cameras or Time Of Flight cameras. The advantages of a organized dataset is that by knowing the relationship between adjacent points (e.g. pixels), nearest neighbor operations are much more efficient, thus speeding up the computation and lowering the costs of certain algorithms in PCL. (Source)

关于c++ - 从 PointCloud 到 Mat 的转换,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15716900/

相关文章:

python - 将调色板颜色的 16x16 真彩色图像转换为实际的调色板文件

c++ - 有没有关于如何将包含文件夹和文件的 zip 文件解压缩到某个目录并通过 MiniZip 将它们打包回来的教程?

c++ - 如何在不使用 C++0x auto 的情况下实现这种类型特定的对象生成器

android - Skia 中的非均匀文本缩放(FreeType 2 后端)

arrays - 从 opencv::Mat 中删除选定的行?

python - 如何为基于 Numpy/Opencv 的 Python 应用程序构建浏览器界面

c++ - C++中的Lua匿名函数存储

c# - 线程导致应用程序在 iOS 上滞后,但在 Android 上不会

python - 如何检测图像中的一种颜色?

matlab - matlab中循环中矩阵子集的平均值