PCL+C++ point cloud window display example

Featured program examples

PCL+C++ point cloud window display example

If you need to install the operating environment or remote debugging, see the personal QQ business card at the bottom of the article, and professional and technical personnel will assist remotely!

foreword

This blog writes code for <<PCL+C++ Point Cloud Window Display Example>>, the code is neat, regular and easy to read. The first choice for learning and application recommendation.


Article directory

1. Required tool software

2. Use steps

        1. Import library

        2. Code implementation

        3. Running results

3. Online assistance

1. Required tool software

1. VS, Qt

2. PCL

2. Use steps

1. Import library

#include <QtWidgets/QMainWindow>

#include "ui_PointCloudVision.h"

#include <pcl/point_types.h>					//点云数据类型
#include <pcl/point_cloud.h>					//点云类
#include <pcl/visualization/pcl_visualizer.h>	//点云可视化类
#include <vtkRenderWindow.h>					//vtk可视化窗口
#include <pcl/common/common.h>					//点云极值
#include "QHeightRampDlg.h"						//高度渲染
#include <pcl/filters/passthrough.h>			//直通滤波

#include <QFileDialog>
#include <pcl/io/pcd_io.h>
#include <pcl/point_types.h>
#include <pcl/visualization/pcl_visualizer.h>
#include <vtkRenderWindow.h>

2. Code implementation

code show as below:

#include "PointCloudVision.h"
#include <QFileDialog>
#include <QFile>
#include <QMessageBox>
#include <QColorDialog>

PointCloudVision::PointCloudVision(QWidget* parent)
	: QMainWindow(parent)
{
	ui.setupUi(this);

	//初始化
	init();
}



//打开点云
void PointCloudVision::on_action_open_triggered()
{
	init();

	QString fileName = QFileDialog::getOpenFileName(this, "Open PointCloud", ".", "Open PCD files(*.pcd)");
	if (!fileName.isEmpty())
	{
		std::string file_name = fileName.toStdString();

		pcl::PCLPointCloud2 cloud2;
		Eigen::Vector4f origin;
		Eigen::Quaternionf orientation;
		int pcd_version;
		int data_type;


		viewer->setPointCloudRenderingProperties(pcl::visualization::PCL_VISUALIZER_POINT_SIZE, 3, "cloud");
		viewer->updatePointCloud(m_currentCloud, "cloud");

		// 获取点云数据的最小和最大坐标值
		pcl::getMinMax3D(*m_currentCloud, p_min, p_max);

		viewer->resetCamera();
		ui.qvtkWidget->update();
	}
}



//俯视图
void PointCloudVision::on_action_up_triggered()
{
	if (!m_currentCloud->empty())
	{
		viewer->setCameraPosition(0.5 * (p_min.x + p_max.x), 0.5 * (p_min.y + p_max.y), p_max.z + 2 * maxLen, 0.5 * (p_min.x + p_max.x), 0.5 * (p_min.y + p_max.y), 0, 0, 1, 0);
		viewer->updatePointCloud(m_currentCloud, "cloud");
		ui.qvtkWidget->update();

		on_action_reset_triggered();
	}
}

//前视图
void PointCloudVision::on_action_front_triggered()
{
	if (!m_currentCloud->empty())
	{
		viewer->setCameraPosition(0.5 * (p_min.x + p_max.x), p_min.y - 2 * maxLen, 0.5 * (p_min.z + p_max.z), 0.5 * (p_min.x + p_max.x), 0, 0.5 * (p_min.z + p_max.z), 0, 0, 1);
		viewer->updatePointCloud(m_currentCloud, "cloud");
		ui.qvtkWidget->update();

		on_action_reset_triggered();
	}
}

//左视图
void PointCloudVision::on_action_left_triggered()
{
	if (!m_currentCloud->empty())
	{
		viewer->setCameraPosition(p_min.x - 2 * maxLen, 0.5 * (p_min.y + p_max.y), 0.5 * (p_min.z + p_max.z), 0, 0.5 * (p_min.y + p_max.y), 0.5 * (p_min.z + p_max.z), 0, 0, 1);
		viewer->updatePointCloud(m_currentCloud, "cloud");
		ui.qvtkWidget->update();

		on_action_reset_triggered();
	}
}

//后视图
void PointCloudVision::on_action_back_triggered()
{
	if (!m_currentCloud->empty())
	{
		viewer->setCameraPosition(0.5 * (p_min.x + p_max.x), p_max.y + 2 * maxLen, 0.5 * (p_min.z + p_max.z), 0.5 * (p_min.x + p_max.x), 0, 0.5 * (p_min.z + p_max.z), 0, 0, 1);
		viewer->updatePointCloud(m_currentCloud, "cloud");
		ui.qvtkWidget->update();

		on_action_reset_triggered();
	}
}

//右视图
void PointCloudVision::on_action_right_triggered()
{
	if (!m_currentCloud->empty())
	{
		viewer->setCameraPosition(p_max.x + 2 * maxLen, 0.5 * (p_min.y + p_max.y), 0.5 * (p_min.z + p_max.z), 0, 0.5 * (p_min.y + p_max.y), 0.5 * (p_min.z + p_max.z), 0, 0, 1);
		viewer->updatePointCloud(m_currentCloud, "cloud");
		ui.qvtkWidget->update();

		on_action_reset_triggered();
	}
}

//底视图
void PointCloudVision::on_action_bottom_triggered()
{
	if (!m_currentCloud->empty())
	{
		viewer->setCameraPosition(0.5 * (p_min.x + p_max.x), 0.5 * (p_min.y + p_max.y), p_min.z - 2 * maxLen, 0.5 * (p_min.x + p_max.x), 0.5 * (p_min.y + p_max.y), 0, 0, 1, 0);
		viewer->updatePointCloud(m_currentCloud, "cloud");
		ui.qvtkWidget->update();

		on_action_reset_triggered();
	}
}

//前轴测
void PointCloudVision::on_action_frontIso_triggered()
{
	if (!m_currentCloud->empty())
	{
		viewer->setCameraPosition(p_min.x - 2 * maxLen, p_min.y - 2 * maxLen, p_max.z + 2 * maxLen, 0.5 * (p_min.x + p_max.x), 0.5 * (p_min.y + p_max.y), 0.5 * (p_min.z + p_max.z), 1, 1, 0);
		viewer->updatePointCloud(m_currentCloud, "cloud");
		ui.qvtkWidget->update();

		on_action_reset_triggered();
	}
}

//后轴测
void PointCloudVision::on_action_backIso_triggered()
{
	viewer->setCameraPosition(p_max.x + 2 * maxLen, p_max.y + 2 * maxLen, p_max.z + 2 * maxLen, 0.5 * (p_min.x + p_max.x), 0.5 * (p_min.y + p_max.y), 0.5 * (p_min.z + p_max.z), -1, -1, 0);
	viewer->updatePointCloud(m_currentCloud, "cloud");
	ui.qvtkWidget->update();

	on_action_reset_triggered();
}

//设置点云颜色
void PointCloudVision::on_action_setColor_triggered()
{
	QColor color = QColorDialog::getColor(Qt::white, this, "设置点云颜色", QColorDialog::ShowAlphaChannel);

	viewer->removeAllPointClouds();
	pcl::visualization::PointCloudColorHandlerCustom<PointT> singleColor(m_currentCloud, color.red(), color.green(), color.blue());
	viewer->addPointCloud(m_currentCloud, singleColor, "myCloud", 0);
	viewer->setPointCloudRenderingProperties(pcl::visualization::PCL_VISUALIZER_OPACITY, color.alpha() * 1.0 / 255, "myCloud");

	ui.qvtkWidget->update();

}

//设置高度渲染
void PointCloudVision::on_action_heightRamp_triggered()
{
	heightRampDlg.show();
}

//进行高度渲染
void PointCloudVision::setHeightRamp(int dir, double height)
{
	//清空点云
	viewer->removeAllPointClouds();
	m_heightCloudList.clear();

	double min_range = 0;
	double max_range = 0;
	std::string field = "x";

	switch (dir)
	{
	case 0:
		min_range = p_min.x;
		max_range = p_max.x;
		field = "x";
		break;

	case 1:
		min_range = p_min.y;
		max_range = p_max.y;
		field = "y";
		break;

	case 2:
		min_range = p_min.z;
		max_range = p_max.z;
		field = "z";
		break;
	default:
		break;
	}

	for (double i = min_range - 1; i < max_range + height;)
	{
		PointCloudT::Ptr cloudTemp(new PointCloudT());

		pcl::PassThrough<PointT> pass;			//直通滤波器对象
		pass.setInputCloud(m_currentCloud);		//输入点云
		pass.setFilterFieldName(field);			//设置过滤字段
		pass.setFilterLimits(i, i + height);	//设置过滤范围
		pass.setFilterLimitsNegative(false);	//设置保留字段
		pass.filter(*cloudTemp);				//执行滤波

		i += height;

		m_heightCloudList.append(cloudTemp);
	}

	//分段渲染
	for (int j = 0; j < m_heightCloudList.size(); j++)
	{
		pcl::visualization::PointCloudColorHandlerGenericField<PointT> fieldColor(m_heightCloudList.at(j), field);
		std::string index = std::to_string(j);
		viewer->addPointCloud(m_heightCloudList.at(j), fieldColor, index);
	}

}

3. Running results

 

3. Online assistance:

If you need to install the operating environment or remote debugging, see the personal QQ business card at the bottom of the article, and professional and technical personnel will assist remotely!
1) Remote installation and operation environment, code debugging
2) Qt, C++, Python entry guide
3) Interface beautification
4) Software production

Current article link: Python+Qt desktop and webpage human customer service communication tool_alicema1111's blog-CSDN blog

Blogger recommended article: python face recognition statistics qt form - CSDN Blog

Blogger recommended article: Python Yolov5 flame smoke recognition source code sharing - CSDN blog

                         Python OpenCV recognizes the number of people entering and exiting the pedestrian entrance - python recognizes the number of people - CSDN Blog

Personal blog homepage: alicema1111's blog_CSDN blog-Python, C++, bloggers in the field of web pages

Click here for all the blogger's articles : alicema1111's blog_CSDN blog-Python, C++, bloggers in the field of web pages

Guess you like

Origin blog.csdn.net/alicema1111/article/details/131858247