Qt PCL学习(三):点云滤波

注意事项

  • 版本一览:Qt 5.15.2 + PCL 1.12.1 + VTK 9.1.0
  • 前置内容:Qt PCL学习(一):环境搭建、Qt PCL学习(二):点云读取与保存

0. 效果演示

在这里插入图片描述

在这里插入图片描述

在这里插入图片描述

1. pcl_open_save.pro

QT       += core guigreaterThan(QT_MAJOR_VERSION, 4): QT += widgets// 添加下行代码(根据自己安装目录进行修改)
include(D:/PCL1.12.1/pcl1.12.1.pri)

2. mainwindow.h

#ifndef MAINWINDOW_H
#define MAINWINDOW_H#include <voxel_filtering.h>
#include <vector>
#include <QMainWindow>
#include <QDebug>
#include <QColorDialog>
#include <QMessageBox>
#include <QFileDialog>
#include <QTime>
#include <QDir>
#include <QFile>
#include <QtMath>
#include <QWindow>
#include <QAction>
#include <QHBoxLayout>
#include <QVBoxLayout>
#include <QIcon>
#include <QMenuBar>
#include <QMenu>
#include <QToolBar>
#include <QStatusBar>
#include <QFont>
#include <QString>
#include <QTextBrowser>
#include <QDirIterator>
#include <QStandardItemModel>
#include <QModelIndex>#include "QVTKOpenGLNativeWidget.h"
#include <vtkSphereSource.h>
#include <vtkPolyDataMapper.h>
#include <vtkActor.h>
#include <vtkRenderer.h>
#include <vtkRenderWindow.h>
#include <vtkGenericOpenGLRenderWindow.h>
#include <vtkNamedColors.h>
#include <vtkProperty.h>
#include <vtkSmartPointer.h>
#include "vtkAutoInit.h"VTK_MODULE_INIT(vtkRenderingOpenGL2);
VTK_MODULE_INIT(vtkInteractionStyle);
VTK_MODULE_INIT(vtkRenderingVolumeOpenGL2);
VTK_MODULE_INIT(vtkRenderingFreeType);#include <pcl/point_cloud.h>
#include <pcl/point_types.h>
#include <pcl/io/pcd_io.h>
#include <pcl/io/ply_io.h>
#include <pcl/visualization/pcl_visualizer.h>
#include <pcl/filters/voxel_grid.h>#ifndef TREE_ITEM_ICON_DataItem
#define TREE_ITEM_ICON_DataItem QStringLiteral("treeItem_folder")
#endiftypedef pcl::PointXYZ PointT;
typedef pcl::PointCloud<PointT> PointCloudT;
typedef pcl::visualization::PCLVisualizer PCLViewer;
typedef std::shared_ptr<PointCloudT> PointCloudPtr;QT_BEGIN_NAMESPACE
namespace Ui { class MainWindow; }
QT_END_NAMESPACEclass MainWindow : public QMainWindow {Q_OBJECTpublic:MainWindow(QWidget *parent = nullptr);~MainWindow();PointCloudT::Ptr pcl_voxel_filter(PointCloudT::Ptr cloud_in, float leaf_size);void view_updata(std::vector<PointCloudT::Ptr> vector_cloud, std::vector<int> index);private slots:void open_clicked();  // 打开文件void save_clicked();  // 保存文件void on_treeView_clicked(const QModelIndex &index);// 点云滤波void pressBtn_voxel();void voxel_clicked(QString data);private:Ui::MainWindow *ui;QMap<QString, QIcon> m_publicIconMap;   // 存放公共图标QStandardItemModel* model;QStandardItem* itemFolder;QModelIndex index_cloud;std::vector<PointCloudT::Ptr> cloud_vec;std::vector<int> cloud_index;// 点云名称std::vector<std::string> cloud_name{"0", "1", "2"};int point_size = 1;PointCloudPtr cloudptr;PCLViewer::Ptr cloud_viewer;voxel_filtering *dialog_voxel;
};
#endif // MAINWINDOW_H

3. mainwindow.cpp

#pragma execution_character_set("utf-8")#include "mainwindow.h"
#include "ui_mainwindow.h"MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent), ui(new Ui::MainWindow) {ui->setupUi(this);// 设置窗口标题和 logothis->setWindowTitle("CloudOne");this->setWindowIcon(QIcon(":/resourse/icon.ico"));m_publicIconMap[TREE_ITEM_ICON_DataItem] = QIcon(QStringLiteral(":/resourse/folder.png"));model = new QStandardItemModel(ui->treeView);model->setHorizontalHeaderLabels(QStringList()<<QStringLiteral("--cloud--DB-Tree--"));ui->treeView->setHeaderHidden(true);ui->treeView->setModel(model);ui->treeView->setSelectionMode(QAbstractItemView::ExtendedSelection);  // 设置多选cloudptr.reset(new PointCloudT);cloud_viewer.reset(new pcl::visualization::PCLVisualizer("viewer", false));vtkNew<vtkGenericOpenGLRenderWindow> window;window->AddRenderer(cloud_viewer->getRendererCollection()->GetFirstRenderer());ui->openGLWidget->setRenderWindow(window.Get());cloud_viewer->setupInteractor(ui->openGLWidget->interactor(), ui->openGLWidget->renderWindow());ui->openGLWidget->update();// 创建菜单栏QMenuBar *menu_bar = new QMenuBar(this);this->setMenuBar(menu_bar);menu_bar->setStyleSheet("font-size : 16px");// 1、File 下拉列表QMenu *file_menu = new QMenu("File", menu_bar);QAction *open_action = new QAction("Open File");QAction *save_action = new QAction("Save File");QAction *exit_action = new QAction("Exit");// 添加动作到文件菜单file_menu->addAction(open_action);file_menu->addAction(save_action);file_menu->addSeparator();  // 添加菜单分隔符将 exit 单独隔离开file_menu->addAction(exit_action);// 把 File 添加到菜单栏menu_bar->addMenu(file_menu);// 2、Filter 下拉列表QMenu *filter_menu = new QMenu("Filter", menu_bar);QAction *voxel_action = new QAction("Voxel Filtering");filter_menu->addAction(voxel_action);menu_bar->addMenu(filter_menu);// 信号与槽函数链接connect(open_action, SIGNAL(triggered()), this, SLOT(open_clicked()));  // 打开文件connect(save_action, SIGNAL(triggered()), this, SLOT(save_clicked()));  // 保存文件connect(exit_action, SIGNAL(triggered()), this, SLOT(close()));         // 退出connect(voxel_action, SIGNAL(triggered()), this, SLOT(pressBtn_voxel()));
}MainWindow::~MainWindow() {delete ui;
}PointCloudT::Ptr MainWindow::pcl_voxel_filter(PointCloudT::Ptr cloud_in, float leaf_size) {pcl::VoxelGrid<PointT> voxel_grid;voxel_grid.setLeafSize(leaf_size, leaf_size, leaf_size);voxel_grid.setInputCloud(cloud_in);PointCloudT::Ptr cloud_out (new PointCloudT()) ;voxel_grid.filter(*cloud_out);return cloud_out;
}void MainWindow::pressBtn_voxel() {dialog_voxel = new voxel_filtering();connect(dialog_voxel, SIGNAL(sendData(QString)), this, SLOT(voxel_clicked(QString)));if (dialog_voxel->exec() == QDialog::Accepted){}delete dialog_voxel;
}// 体素采样
void MainWindow::voxel_clicked(QString data) {if (cloudptr->empty()) {QMessageBox::warning(this, "Warning", "None point cloud!");return;} else {if (data.isEmpty()) {QMessageBox::warning(this, "Warning", "Wrong format!");return;}float size = data.toFloat();auto cloud_out = pcl_voxel_filter(cloudptr, size);cloudptr = cloud_out;int size1 = static_cast<int>(cloudptr->size());QString PointSize = QString("%1").arg(size1);ui->textBrowser_2->clear();ui->textBrowser_2->insertPlainText("PCD number: " + PointSize);ui->textBrowser_2->setFont(QFont("Arial", 9, QFont::Normal));cloud_viewer->removeAllPointClouds();cloud_viewer->removeAllShapes();cloud_viewer->addPointCloud<pcl::PointXYZ>(cloudptr->makeShared(), std::to_string(cloud_vec.size()-1));cloud_viewer->setPointCloudRenderingProperties(pcl::visualization::PCL_VISUALIZER_POINT_SIZE, point_size, std::to_string(cloud_vec.size()-1));cloud_viewer->resetCamera();// 设置颜色处理器,将点云数据添加到 cloud_viewer 中const std::string axis = "z";pcl::visualization::PointCloudColorHandlerGenericField<PointT> color_handler(cloudptr, axis);cloud_viewer->addPointCloud(cloudptr, color_handler, "cloud");cloud_viewer->addPointCloud(cloudptr, "cloud");}
}void MainWindow::open_clicked() {// this:代表当前对话框的父对象;tr("open file"):作为对话框的标题显示在标题栏中,使用 tr 函数表示这是一个需要翻译的文本// "":代表对话框的初始目录,这里为空表示没有指定初始目录// tr("pcb files(*.pcd *.ply *.txt) ;;All files (*.*)"):过滤器,决定在对话框中只能选择指定类型的文件QString fileName = QFileDialog::getOpenFileName(this, tr("open file"), "", tr("point cloud files(*.pcd *.ply) ;; All files (*.*)"));if (fileName.isEmpty()) {return;}if (fileName.endsWith("ply")) {qDebug() << fileName;if (pcl::io::loadPLYFile(fileName.toStdString(), *cloudptr) == -1) {qDebug() << "Couldn't read .ply file \n";return ;}} else if (fileName.endsWith("pcd")) {qDebug() << fileName;if (pcl::io::loadPCDFile(fileName.toStdString(), *cloudptr) == -1) {qDebug() << "Couldn't read .pcd file \n";return;}} else {QMessageBox::warning(this, "Warning", "Wrong format!");}cloud_vec.push_back(cloudptr->makeShared());cloud_index.push_back(1);itemFolder = new QStandardItem(m_publicIconMap[QStringLiteral("treeItem_folder")], QStringLiteral("cloud%1").arg(cloud_vec.size()-1));itemFolder->setCheckable(true);itemFolder->setCheckState(Qt::Checked);  // 获取选中状态model->appendRow(itemFolder);int size = static_cast<int>(cloudptr->size());QString PointSize = QString("%1").arg(size);ui->textBrowser_2->clear();ui->textBrowser_2->insertPlainText("PCD number: " + PointSize);ui->textBrowser_2->setFont(QFont("Arial", 9, QFont::Normal));cloud_viewer->addPointCloud<pcl::PointXYZ>(cloudptr->makeShared(), std::to_string(cloud_vec.size()-1));// 设置点云大小cloud_viewer->setPointCloudRenderingProperties(pcl::visualization::PCL_VISUALIZER_POINT_SIZE, point_size, std::to_string(cloud_vec.size()-1));cloud_viewer->resetCamera();ui->openGLWidget->renderWindow()->Render();ui->openGLWidget->update();// 设置颜色处理器,将点云数据添加到 cloud_viewer 中const std::string axis = "z";pcl::visualization::PointCloudColorHandlerGenericField<PointT> color_handler(cloudptr, axis);cloud_viewer->addPointCloud(cloudptr, color_handler, "cloud");cloud_viewer->addPointCloud(cloudptr, "cloud");
}void MainWindow::save_clicked() {int return_status;QString filename = QFileDialog::getSaveFileName(this, tr("Open point cloud"), "", tr("Point cloud data (*.pcd *.ply)"));if (cloudptr->empty()) {return;} else {if (filename.isEmpty()) {return;}if (filename.endsWith(".pcd", Qt::CaseInsensitive)) {return_status = pcl::io::savePCDFileBinary(filename.toStdString(), *cloudptr);} else if (filename.endsWith(".ply", Qt::CaseInsensitive)) {return_status = pcl::io::savePLYFileBinary(filename.toStdString(), *cloudptr);} else {filename.append(".ply");return_status = pcl::io::savePLYFileBinary(filename.toStdString(), *cloudptr);}if (return_status != 0) {PCL_ERROR("Error writing point cloud %s\n", filename.toStdString().c_str());return;}}
}void MainWindow::view_updata(std::vector<PointCloudT::Ptr> vector_cloud, std::vector<int> index) {cloud_viewer.reset(new pcl::visualization::PCLVisualizer("viewer", false));vtkNew<vtkGenericOpenGLRenderWindow> window;window->AddRenderer(cloud_viewer->getRendererCollection()->GetFirstRenderer());ui->openGLWidget->setRenderWindow(window.Get());cloud_viewer->removeAllPointClouds();cloud_viewer->removeAllShapes();for (int i = 0; i<vector_cloud.size(); i++) {if (index[i] == 1) {pcl::visualization::PointCloudColorHandlerGenericField<pcl::PointXYZ>render(vector_cloud[i], "intensity");cloud_viewer->addPointCloud<pcl::PointXYZ>(vector_cloud[i], render, std::to_string(i));cloud_viewer->setPointCloudRenderingProperties (pcl::visualization::PCL_VISUALIZER_POINT_SIZE, point_size, std::to_string(i));}}cloud_viewer->resetCamera();ui->openGLWidget->update();
}// 确定 index
void MainWindow::on_treeView_clicked(const QModelIndex &index) {QStandardItem* item = model->itemFromIndex(index);// 点云数量更改QStandardItemModel* model = static_cast<QStandardItemModel*>(ui->treeView->model());QModelIndex index_temp = ui->treeView->currentIndex();int size = static_cast<int>(cloud_vec[index_temp.row()]->size());QString PointSize = QString("%1").arg(size);ui->textBrowser_2->clear();ui->textBrowser_2->insertPlainText("Point cloud number: " + PointSize);ui->textBrowser_2->setFont(QFont("Arial", 9, QFont::Normal));// 可视化更改if (item == nullptr)return;if (item->isCheckable()) {//判断状态Qt::CheckState state = item->checkState();  // 获取当前的选择状态if (Qt::Checked == state) {cloud_index[index.row()] = 1;}if (Qt::Unchecked == state) {cloud_index[index.row()] = 0;}view_updata(cloud_vec, cloud_index);}
}

4. voxel_filtering.h

#ifndef VOXEL_FILTERING_H
#define VOXEL_FILTERING_H#include <QDialog>
#include <QString>namespace Ui {
class voxel_filtering;
}class voxel_filtering : public QDialog {Q_OBJECTsignals:void sendData(QString data);public:explicit voxel_filtering(QWidget *parent = nullptr);~voxel_filtering();private slots:void on_buttonBox_accepted();private:Ui::voxel_filtering *ui;
};#endif // VOXEL_FILTERING_H

5. voxel_filtering.cpp

#include "voxel_filtering.h"
#include "ui_voxel_filtering.h"voxel_filtering::voxel_filtering(QWidget *parent) : QDialog(parent), ui(new Ui::voxel_filtering) {ui->setupUi(this);
}voxel_filtering::~voxel_filtering() {delete ui;
}void voxel_filtering::on_buttonBox_accepted() {emit sendData(ui->lineEdit->text());this->close();
}

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/675999.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

包装效果图为何要用云渲染100?渲染100邀请码1a12

包装效果图能吸引用户注意力&#xff0c;提升销量&#xff0c;随着技术的发展&#xff0c;越来越多的设计师开始使用云渲染来处理效果图&#xff0c;云渲染有什么优势呢&#xff1f;以渲染100为例我来说下。 1、节省时间和成本 渲染100拥有超过10万台的高性能渲染节点&#x…

【Larry】英语学习笔记语法篇——换一种方式理解词性

目录 一、换一种方式理解词性 1、名词、形容词、副词&#xff0c;这就是一切 2、词性之间的修饰关系 3、介词其实很简单 形容词属性的介词短语 副词属性的介词短语 ①修饰动词 ②修饰形容词 ③修饰其他副词 一、换一种方式理解词性 1、名词、形容词、副词&#xff0c…

(基础算法)归并排序

1.确定分界点 mid &#xff08;lr&#xff09;/2 2.递归排序左右两段 3.归并----合二为一 #include<iostream> using namespace std; //归并排序----分治 const int N10010; int n; int q[N],tmp[N];//需要一个额外数组void mergesort(int q[],int l,int r)//l左边界&a…

TCP 传输控制协议——详细

目录 1 TCP 1.1 TCP 最主要的特点 1.2 TCP 的连接 TCP 连接&#xff0c;IP 地址&#xff0c;套接字 1.3 可靠传输的工作原理 1.3.1 停止等待协议 &#xff08;1&#xff09;无差错情况 &#xff08;2&#xff09;出现差错 &#xff08;3&#xff09;确认丢失和确认迟到…

【MySQL进阶之路】BufferPool底层设计(下)

欢迎关注公众号&#xff08;通过文章导读关注&#xff1a;【11来了】&#xff09;&#xff0c;及时收到 AI 前沿项目工具及新技术的推送&#xff01; 在我后台回复 「资料」 可领取编程高频电子书&#xff01; 在我后台回复「面试」可领取硬核面试笔记&#xff01; 文章导读地址…

Linux运用fork函数创建进程

fork函数&#xff1a; 函数原型&#xff1a; pid_t fork(void); 父进程调用fork函数创建一个子进程&#xff0c;子进程的用户区父进程的用户区完全一样&#xff0c;但是内核区不完全一样&#xff1b;如父进程的PID和子进程的PID不一样。 返回值&#xff1a; RETURN VALUEO…

压敏电阻简介

压敏电阻 原理 压敏电阻器是一种具有瞬态电压抑制功能的元件&#xff0c;可以用来代替瞬态抑制二极管、齐纳二极管和电容器的组合。压敏电阻器可以对IC及其它设备的电路进行保护&#xff0c;防止因静电放电、浪涌及其它瞬态电流&#xff08;如雷击等&#xff09;而造成对它们…

node网站 宝塔 面板配置 防止刷新404

1.问题 我现在配置了一个网站 后台项目 放到了宝塔上 将相应的域名和项目都配置好了 域名也可以访问 但是有的时候 出现了404 类似这种404 这个资源找不到 2.说明 其实这个问题的原因是nginx 的问题 反向代理的原因 3.解决 在这个配置文件中 有个配置文件 # 防止刷新404l…

前端使用pdf.js进行pdf文件预览的第二种方式:Viewer.html

背景 最近需要实现一个PDF文档预览的功能&#xff0c;按理说&#xff0c;如果只是简单的预览&#xff0c;使用<embed>、<object>等就可以实现。 但是&#xff0c;我们的需求要实现搜索&#xff01;而且&#xff0c;文档还都超大&#xff0c;均300页以上。那<e…

斯巴鲁Subaru EDI需求分析

斯巴鲁Subaru是日本运输集团斯巴鲁公司&#xff08;前身为富士重工&#xff09;的汽车制造部门&#xff0c;以性能而闻名&#xff0c;曾赢得 3 次世界拉力锦标赛和 10 次澳大利亚拉力锦标赛。 斯巴鲁Subaru EDI 需求分析 企业与斯巴鲁Subaru建立EDI连接&#xff0c;首先需要确…

Vue ECharts 散点图上画一个圆形 - 附完整示例

ECharts&#xff1a;一个基于 JavaScript 的开源可视化图表库。 效果 一、介绍 1、官方文档&#xff1a;Apache ECharts Apache EChartsApache ECharts&#xff0c;一款基于JavaScript的数据可视化图表库&#xff0c;提供直观&#xff0c;生动&#xff0c;可交互&#xff0c;可…

Redis 单线程

文章目录 Redis单线程架构Redis 单线程访问速度IO多路复用原理 Redis单线程架构 Redis的单线程架构的效果为&#xff1a;Redis的单线程是对于服务端而言的&#xff0c;Redis允许多个Redis用户端同时在线操作&#xff0c;但同时只有一个用户端在和服务端交互。多个用户同时发送…

《Git 简易速速上手小册》第5章:高级 Git 技巧(2024 最新版)

文章目录 5.1 交互式暂存5.1.1 基础知识讲解5.1.2 重点案例&#xff1a;为 Python 项目分阶段提交5.1.3 拓展案例 1&#xff1a;细粒度控制更改5.1.4 拓展案例 2&#xff1a;处理遗漏的更改 5.2 使用 Rebase 优化提交历史5.2.1 基础知识讲解5.2.2 重点案例&#xff1a;整理 Pyt…

【工作学习 day04】 9. uniapp 页面和组件的生命周期

问题描述 uniapp常用的有&#xff1a;页面和组件&#xff0c;并且页面和组件各自有各自的生命周期函数&#xff0c;那么在页面/组件请求数据时&#xff0c;是用created呢&#xff0c;还是用onLoad呢&#xff1f; 先说结论: 组件使用组件的生命周期&#xff0c;页面使用页面的…

通俗易懂:快速排序算法全解析

快速排序&#xff08;Quick Sort&#xff09;是一种高效的分治排序算法&#xff0c;它以其出色的性能和广泛的应用而闻名。本文将深入讲解快速排序的原理、步骤和时间复杂度&#xff0c;并探讨其优势和应用场景。 快速排序原理 快速排序的核心思想是通过选择一个基准元素&…

Go语言每日一练——链表篇(四)

传送门 牛客面试笔试必刷101题 ----------------合并两个排序的链表 题目以及解析 题目 解题代码及解析 package main import _"fmt" import . "nc_tools" /** type ListNode struct{* Val int* Next *ListNode* }*//*** 代码中的类名、方法名、参…

【Docker】了解Docker Desktop桌面应用程序,TA是如何管理和运行Docker容器(2)

欢迎来到《小5讲堂》&#xff0c;大家好&#xff0c;我是全栈小5。 这是《Docker容器》系列文章&#xff0c;每篇文章将以博主理解的角度展开讲解&#xff0c; 特别是针对知识点的概念进行叙说&#xff0c;大部分文章将会对这些概念进行实际例子验证&#xff0c;以此达到加深对…

Sqlite3安装步骤

1、Sqlite3以下载文件&#xff0c;配置环境变量的方式进行安装。 2、下方链接为官方的下载地址。 sqlite下载地址 2.1、需要两个下载文件&#xff0c;解压后将他们放在一起&#xff0c;假设解压后的路径为E:\sqlite。 sqlite-dll-win-x64-3450100.zip sqlite-tools-win-x6…

【数据结构】数据结构

本文是基于中国MOOC平台上&#xff0c;华中科技大学的《数据结构》课程和浙江大学的《数据结构》课程所作的一篇课程笔记&#xff0c;便于后期讲行系统性查阅和复习。 从个人感受而言&#xff0c;华中科技大学的课程讲解更适合初学者&#xff08;缺点在于&#xff0c;从概念到…

linux 07 存储管理

02. ext4是一种索引文件系统 上面是索引节点inode&#xff0c;存放数据的元数据 下面是存储块block&#xff0c;主要存放有关的信息 03.linux上的inode 查看文件中的inode ll -i 文件名 磁盘中的inode与文件数量