我想一个点云从PCL格式RGB信息转换为CV ::垫和回PCL。我发现convert mat to pointcloud上计算器。如何CV ::垫转换为PCL ::点云颜色
代码更新
我因此用在计算器为起点发现代码。现在有以下几点:
//input pcl::PointCloud<pcl::PointXYZRGB> point_cloud_ptr
cv::Mat OpenCVPointCloud(3, point_cloud_ptr.points.size(), CV_64FC1);
OpenCVPointCloudColor = cv::Mat(480, 640, CV_8UC3);
for(int y=0;y<OpenCVPointCloudColor.rows;y++)
{
for(int x=0;x<OpenCVPointCloudColor.cols;x++)
{
OpenCVPointCloud.at<double>(0,y*OpenCVPointCloudColor.cols+x) = point_cloud_ptr.points.at(y*OpenCVPointCloudColor.cols+x).x;
OpenCVPointCloud.at<double>(1,y*OpenCVPointCloudColor.cols+x) = point_cloud_ptr.points.at(y*OpenCVPointCloudColor.cols+x).y;
OpenCVPointCloud.at<double>(2,y*OpenCVPointCloudColor.cols+x) = point_cloud_ptr.points.at(y*OpenCVPointCloudColor.cols+x).z;
cv::Vec3b color = cv::Vec3b(point_cloud_ptr.points.at(y*OpenCVPointCloudColor.cols+x).b,point_cloud_ptr.points.at(y*OpenCVPointCloudColor.cols+x).g,point_cloud_ptr.points.at(y*OpenCVPointCloudColor.cols+x).r);
OpenCVPointCloudColor.at<cv::Vec3b>(cv::Point(x,y)) = color;
}
}
cv::imshow("view 2", OpenCVPointCloudColor);
cv::waitKey(30);
显示图像之上确保我的颜色正确提取(图像与原始图像相比眼睛)。然后我想将其转换回点云:
pcl::PointCloud<pcl::PointXYZRGB>::Ptr point_cloud_ptr(new pcl::PointCloud<pcl::PointXYZRGB>);
for(int y=0;y<OpenCVPointCloudColor.rows;y++)
{
for(int x=0;x<OpenCVPointCloudColor.cols;x++)
{
pcl::PointXYZRGB point;
point.x = OpencVPointCloud.at<double>(0,y*OpenCVPointCloudColor.cols+x);
point.y = OpencVPointCloud.at<double>(1,y*OpenCVPointCloudColor.cols+x);
point.z = OpencVPointCloud.at<double>(2,y*OpenCVPointCloudColor.cols+x);
cv::Vec3b color = OpenCVPointCloudColor.at<cv::Vec3b>(cv::Point(x,y));
//Try 1 not working
//uint8_t r = (color[2]);
//uint8_t g = (color[1]);
//uint8_t b = (color[0]);
//Try 2 not working
//point.r = color[2];
//point.g = color[1];
//point.b = color[0];
//Try 3 not working
//uint8_t r = (color.val[2]);
//uint8_t g = (color.val[1]);
//uint8_t b = (color.val[0]);
//test working WHY DO THE ABOVE CODES NOT WORK :/
uint8_t r = 255;
uint8_t g = 0;
uint8_t b = 0;
int32_t rgb = (r << 16) | (g << 8) | b;
point.rgb = *reinterpret_cast<float*>(&rgb);
point_cloud_ptr -> points.push_back(point);
}
}
任何人都可以解释为什么值明确指定255,0,0颜色一切红色。但是如果我从彩色图像中选择输出,那么云的颜色是错误的?
偶然this,我看不出有什么区别我的代码有其他比,则云类型是不同的?
更新
对PCL阅读this讨论,我切换到xyzrgba(也stackoverflow提到)结束。码转换回来时,我又试图为:
point.b = OpenCVPointCloudColor.at<cv::Vec3b>(x,y)[0];
point.g = OpenCVPointCloudColor.at<cv::Vec3b>(x,y)[1];
point.r = OpenCVPointCloudColor.at<cv::Vec3b>(x,y)[2];
point.a = 255;
产生的颜色云是从XYZRGB中创建的不同,但仍然是错误的:/ WTF?
额外
即使我强制红色到使用cvCloudColor.at<cv::Vec3f>(x,y) = cv::Vec3f(0,0,255);
然后从OpenCVPointCloudColor
阅读所有点OpenCVPointCloudColor
仍然会在PCL云错误的色彩信息。
在这个例子中,您似乎在尝试从'colorImage'读取它们时将这些点存储在'OpenCVPointCloudColor'中。这些都一样吗? – iamai
@iamai是的,我现在会更新这个问题,以便它们具有相同的变量名称。 (只有他们在两个函数调用)。 – JTIM
(与上面的代码无关)如果您尝试从使用OpenCV加载的图像读取它们,请注意默认颜色系统是BGR而不是RGB。这可能会影响颜色。 – ilke444