Qt Load and Save PCL/PLY 加载和保存点云

时间:2022-07-19 09:37:56

Qt可以跟VTK和PCL等其他库联合使用,十分强大,下面的代码展示了如何使用Qt联合PCL库来加载和保存PCL/PLY格式的点云:

通过按钮加载点云:

void QMainWindow::on_pb_load_clicked() {
QString filename = QFileDialog::getOpenFileName (this, tr ("Open point cloud"), "", tr ("Point cloud data (*.pcd *.ply)")); PCL_INFO("File chosen: %s\n", filename.toStdString ().c_str ());
PointCloudT::Ptr cloud_tmp (new PointCloudT); if (filename.isEmpty ())
return; int return_status;
if (filename.endsWith (".pcd", Qt::CaseInsensitive))
return_status = pcl::io::loadPCDFile (filename.toStdString (), *cloud_tmp);
else
return_status = pcl::io::loadPLYFile (filename.toStdString (), *cloud_tmp); if (return_status != )
{
PCL_ERROR("Error reading point cloud %s\n", filename.toStdString ().c_str ());
return;
}
}

保存点云:

void QMainWindow::on_pb_save_clicked() {
QString filename = QFileDialog::getSaveFileName(this, tr ("Open point cloud"), "", tr ("Point cloud data (*.pcd *.ply)")); PCL_INFO("File chosen: %s\n", filename.toStdString ().c_str ()); if (filename.isEmpty ())
return; int return_status;
if (filename.endsWith (".pcd", Qt::CaseInsensitive))
return_status = pcl::io::savePCDFileBinary (filename.toStdString (), *cloud_);
else if (filename.endsWith (".ply", Qt::CaseInsensitive))
return_status = pcl::io::savePLYFileBinary (filename.toStdString (), *cloud_);
else
{
filename.append(".ply");
return_status = pcl::io::savePLYFileBinary (filename.toStdString (), *cloud_);
} if (return_status != )
{
PCL_ERROR("Error writing point cloud %s\n", filename.toStdString ().c_str ());
return;
}
}

注意savePCDFileBinary是以二进制的方式来储存,这样保存出来的点云占的硬盘空间较小,但是使用例如notepad++等软件打开ply文件,无法真长显示数据。我们也可以使用savePLYFileASCII来以ascii码形式来保存点云,这样我们即使使用文本文件打开,也能显示数据了。