使用PCL将pgm文件转化为pcd文件

这里使用的是pcl-1.9版本

transform.cpp

transform.cpp
#include <pcl/io/pcd_io.h>
#include <pcl/point_types.h>
#include <opencv2/opencv.hpp>

int main()
{
    // 读取PGM文件
    cv::Mat pgmImage = cv::imread("/home/user/Desktop/map.pgm", cv::IMREAD_GRAYSCALE);

    // 创建PCL点云对象
    pcl::PointCloud<pcl::PointXYZ>::Ptr cloud(new pcl::PointCloud<pcl::PointXYZ>);

    // 将PGM图像转换为点云数据
    for (int row = 0; row < pgmImage.rows; ++row)
    {
        for (int col = 0; col < pgmImage.cols; ++col)
        {
            float intensity = static_cast<float>(pgmImage.at<uchar>(row, col));
            pcl::PointXYZ point;
            point.x = static_cast<float>(col);
            point.y = static_cast<float>(row);
            point.z = intensity;
            cloud->push_back(point);
        }
    }

    // 设置点云参数(可选)
    cloud->width = pgmImage.cols;
    cloud->height = pgmImage.rows;
    cloud->is_dense = true;

    // 保存点云数据为PCD文件
    pcl::io::savePCDFileASCII("/home/user/Desktop/map.pcd", *cloud);
    std::cout << "PCD file saved." << std::endl;

    return 0;
}

CMakeLists.txt

cmake_minimum_required(VERSION 2.8)
project(test_gls)
# 设置编译模式
set( CMAKE_BUILD_TYPE "Debug" )
set(CMAKE_CXX_FLAGS   "-std=c++11")
set(PCL_DIR "/usr/share/pcl-1.9")
set(OpenCV_DIR "/usr/share/opencv" CACHE PATH "Path to OpenCV")
find_package(PCL)
find_package(OpenCV REQUIRED)
 
 
include_directories(${PCL_INCLUDE_DIRS})
include_directories(${OpenCV_INCLUDE_DIRS})
link_directories(${PCL_LIBRARY_DIRS})
add_definitions(${PCL_DEFINITIONS})


add_executable(test transform.cpp)
 
target_link_libraries (test ${PCL_LIBRARIES} ${OpenCV_LIBS})

你可能感兴趣的:(PCL,PCL)