从pcl点云获取RGB信息

时间:2019-08-07 14:37:42

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

我想可视化从opencv Mat类型到pcl :: point云类型的视频流文件。

我能够将视频文件显示为pcl :: poinxyz类型。

但是颜色信息(RGB)仍然丢失。

依赖关系:pcl 1.8,opencv 3.4,msvc 2013

我尝试了以下代码:

            // when color needs to be added:
            //uint32_t rgb = (static_cast<uint32_t>(pr) << 16 | static_cast<uint32_t>(pg) << 8 | static_cast<uint32_t>(pb));
            // point.rgb = *reinterpret_cast<float*>(&rgb);
            //std::cout << point << std::endl;
            point_cloud_ptr->points.push_back(point);
        }
    }
    //point_cloud_ptr->width = (int)point_cloud_ptr->points.size();
    //point_cloud_ptr->height = 1;

    return point_cloud_ptr;
}

这是我的代码,用于从opencv Mat到pcl类型的可视化(仅位置而不是RGB信息):

pcl::PointCloud<pcl::PointXYZ>::Ptr MatToPoinXYZ(cv::Mat OpencVPointCloud)
{

    pcl::PointCloud<pcl::PointXYZ>::Ptr point_cloud_ptr(new pcl::PointCloud<pcl::PointXYZ>);//(new pcl::pointcloud<pcl::pointXYZ>);

    for (int i = 0; i < OpencVPointCloud.rows; i++)
    {
        for (int j = 0; j < OpencVPointCloud.cols; j++)
        {
            //std::cout<<i<<endl;

        pcl::PointXYZ point;
        point.x = OpencVPointCloud.at<cv::Vec3f>(i, j)[0] / 1000;
        point.y = OpencVPointCloud.at<cv::Vec3f>(i, j)[1] / 1000;
        point.z = OpencVPointCloud.at<cv::Vec3f>(i, j)[2] / 1000;
        }
    }
    return point_cloud_ptr;
}


int main(int argc, char *argv[])

    // main loop pulling data
    pcl::visualization::CloudViewer viewer("Simple");

    auto futureNextFrame = Scoped(synch->NextFrame());
    for (; g_stayInMainLoop && WasSuccessful(*futureNextFrame); futureNextFrame = synch->NextFrame())
    {

        std::vector<svp2_api::Image> pointClouds;

        for (auto &&s : sensors)
        {

            auto const W = Scoped(s->GetData(svp2_api::PointCloud));
            if (W != nullptr)
            {
                // convert point cloud data from intenta type to opencv type
                pointClouds.emplace_back(W->GetImage());
                int type = GetMatChannelType(pointClouds.back());
                std::cout << "Viz point Cloud\n";
                std::cout << "Channel " << pointClouds.back().channelCount << endl;

                cv::Mat tmp = cv::Mat(pointClouds.back().height, pointClouds.back().width, CV_16SC3, pointClouds.back().data);
                //std::cout << tmp << std::endl;
                if (tmp.type() == CV_16SC3)
                {
                    tmp.convertTo(tmp, CV_32FC3);
                }
                imshow("Point Cloud 1", tmp);

                // convert point cloud data from opencv type to pcl type

                MatToPoinXYZ(tmp);

                viewer.showCloud(MatToPoinXYZ(tmp));


                cv::waitKey(1);
            }

        }

    }

我希望输出带有RGB信息,但是我只得到点云(仅白色)。

我尝试了前面提到的代码,但我真的不知道如何在pcl中拆分通道以添加像素值以获得点云RGB信息。

0 个答案:

没有答案