PCLlab—PCD文件的打開和保存操作


序:我們這里實現的功能是打開.pcd文件並顯示(顯示功能已經在上一節介紹過來),然后選擇要另存為的類型,這里支持ply和pcd格式的文件。

1. 文件打開

1.1. MFC設置

1.2. 代碼編輯

void CPCLlabDoc::OnFileOpen()
{
    // TODO: Add your command handler code here
    CFileDialog dlg(true, NULL, NULL, OFN_HIDEREADONLY | OFN_OVERWRITEPROMPT,
        (CString)"pointcloud(*.pcd)|*.pcd||",NULL);

    if(dlg.DoModal() != IDOK)
        return;
    CString filename = dlg.GetPathName();
    g_pcd_io.cloud_ = g_pcd_io.getData(filename.GetString());

    this->UpdateAllViews(0,0,0);//把文檔被修改的信息通知給每個視圖,刷新顯示窗口
}

注:上述代碼中我們定義了全局的類對象extern PCD_IO g_pcd_io;其變量定義和getData函數如下

//Variables
public:
    pcl::PCLPointCloud2::Ptr cloud_;
pcl::PCLPointCloud2::Ptr PCD_IO::getData(std::string filename)
{
    pcl::PCLPointCloud2::Ptr cloud (new pcl::PCLPointCloud2);
    if(pcl::io::loadPCDFile(filename, *cloud) == -1) // load the file
    {
        PCL_ERROR ("Couldn't read file");
    }
    
    return cloud;
}

2. 文件保存

2.1. MFC設置

2.2. 代碼編輯

void CPCLlabDoc::OnFileSave()
{
    // TODO: Add your command handler code here
    CFileDialog dlg(false, CString(".ply"), CString("pcd.ply"), OFN_HIDEREADONLY | OFN_OVERWRITEPROMPT,
        (CString)"polygon(*.ply)|*.ply|pointcloud(*.pcd)|*.pcd||",NULL);

    if(dlg.DoModal() != IDOK)
        return;
    CString filename = dlg.GetPathName();

    bool binary = false;
    bool use_camera = true;
    int pos = filename.ReverseFind('.');
    if(pos == -1)
        return;

    CString ext = filename.Right(filename.GetLength() - pos);
    if(ext == ".ply")
    {
        pcl::PointCloud<pcl::PointXYZRGB>::Ptr out(new pcl::PointCloud<pcl::PointXYZRGB>);
        pcl::PointCloud<pcl::PointXYZRGB>::Ptr result(new pcl::PointCloud<pcl::PointXYZRGB>);
        std::vector<int> index;
        pcl::fromPCLPointCloud2(*g_pcd_io.cloud_, *out);
        pcl::removeNaNFromPointCloud(*out, *result, index);//刪除nan的點,否則meshlab無法打開
        pcl::PLYWriter writer;
        writer.write(filename.GetString(), *result);
        //writer.write (filename.GetString(), m_pcd_io.cloud_, Eigen::Vector4f::Zero (), Eigen::Quaternionf::Identity (), binary, use_camera);
    }
    else if(ext == ".pcd")
    {
        pcl::io::savePCDFile(filename.GetString(), *g_pcd_io.cloud_, Eigen::Vector4f::Zero (), Eigen::Quaternionf::Identity (), binary);
    }
}

 

轉載請注明http://www.cnblogs.com/pcl-lab/articles/3965081.html


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM