C++ 使用pcl 把pcd点云文件保存为图片,只有intensity的值
时间: 2024-03-13 07:45:29 浏览: 60
使用C++与PCL详细过程版实现的点云转强度图像
5星 · 资源好评率100%
可以使用PCL自带的可视化库来实现将pcd点云文件保存为图片,只保存intensity的值。具体步骤如下:
1. 加载pcd文件:
```c++
pcl::PointCloud<pcl::PointXYZI>::Ptr cloud(new pcl::PointCloud<pcl::PointXYZI>);
pcl::io::loadPCDFile("input.pcd", *cloud);
```
2. 创建可视化对象和渲染器:
```c++
pcl::visualization::PCLVisualizer viewer("Cloud Viewer");
viewer.addPointCloud(cloud);
viewer.setPointCloudRenderingProperties(pcl::visualization::PCL_VISUALIZER_POINT_SIZE, 1, "cloud");
viewer.setBackgroundColor(1, 1, 1);
```
3. 定义颜色映射表:
```c++
pcl::visualization::LookupTable lut;
lut.insert(std::make_pair(0.0, cv::Vec3b(0, 0, 0)));
lut.insert(std::make_pair(1.0, cv::Vec3b(255, 255, 255)));
```
4. 将intensity的值转换为0到1之间的浮点数,并映射到颜色映射表上:
```c++
cv::Mat intensity_image(cloud->height, cloud->width, CV_8UC3);
for (int y = 0; y < intensity_image.rows; y++) {
for (int x = 0; x < intensity_image.cols; x++) {
float intensity = cloud->at(x, y).intensity;
cv::Vec3b color = lut.getColor(intensity);
intensity_image.at<cv::Vec3b>(y, x) = color;
}
}
```
5. 保存图片:
```c++
cv::imwrite("output.png", intensity_image);
```
完整代码如下:
```c++
#include <pcl/io/pcd_io.h>
#include <pcl/point_types.h>
#include <pcl/visualization/pcl_visualizer.h>
#include <opencv2/opencv.hpp>
int main(int argc, char** argv) {
pcl::PointCloud<pcl::PointXYZI>::Ptr cloud(new pcl::PointCloud<pcl::PointXYZI>);
pcl::io::loadPCDFile("input.pcd", *cloud);
pcl::visualization::PCLVisualizer viewer("Cloud Viewer");
viewer.addPointCloud(cloud);
viewer.setPointCloudRenderingProperties(pcl::visualization::PCL_VISUALIZER_POINT_SIZE, 1, "cloud");
viewer.setBackgroundColor(1, 1, 1);
pcl::visualization::LookupTable lut;
lut.insert(std::make_pair(0.0, cv::Vec3b(0, 0, 0)));
lut.insert(std::make_pair(1.0, cv::Vec3b(255, 255, 255)));
cv::Mat intensity_image(cloud->height, cloud->width, CV_8UC3);
for (int y = 0; y < intensity_image.rows; y++) {
for (int x = 0; x < intensity_image.cols; x++) {
float intensity = cloud->at(x, y).intensity;
cv::Vec3b color = lut.getColor(intensity);
intensity_image.at<cv::Vec3b>(y, x) = color;
}
}
cv::imwrite("output.png", intensity_image);
return 0;
}
```
阅读全文