【點雲PCL入門】PCL+QT+VTK顯示點雲

奮鬥的大慶發表於2020-12-15

1.讀取TXT格式點雲(Qt ifstream)

標頭檔案

#include <vtkAutoInit.h>
VTK_MODULE_INIT(vtkRenderingOpenGL)
VTK_MODULE_INIT(vtkInteractionStyle)

#include <vtkRenderWindow.h>
#include<pcl/point_types.h>
#include<pcl/visualization/pcl_visualizer.h>
#include<pcl/io/pcd_io.h>
    ui.setupUi(this);

	QString str = tr("PCL+QT+VTK點雲顯示");
	this->setWindowTitle(str);

	//pcl::PointCloud<pcl::PointXYZRGB>::Ptr cloud;
	//boost::shared_ptr<pcl::visualization::PCLVisualizer> viewer;
	cloud.reset(new pcl::PointCloud<pcl::PointXYZRGB>);
	viewer.reset(new pcl::visualization::PCLVisualizer("viewer", false));

	/*QString filename = "./1.pcd";
	if(pcl::io::loadPCDFile(filename.toStdString(),*cloud)==-1)
	{
		cout << "載入文件失敗" << endl;
	}*/
  //讀TXT點雲文件
	std::string filename = "./1.txt";
	ifstream infile;
	infile.open(filename.data());
	assert(infile.is_open());
	std::string s;
    while (getline(infile,s))
    {
		QString ss, s1, s2, s3;
		ss = tr(s.c_str());
		s1 = ss.section(" ", 0, 0);
		s2 = ss.section(" ", 1, 1);
		s3 = ss.section(" ", 2, 2);
		pcl::PointXYZRGB current_point;
		current_point.x = s1.toFloat();
		current_point.y = s2.toFloat();
		current_point.z = s3.toFloat();
		cloud->points.push_back(current_point);
    }
	infile.close();

	//初始化顏色及大小資訊
	int red = 255;
	int green = 255;
	int blue = 255;
	float size = 2.0;
	//將PCLVisualizer與QVTKWidget聯絡起來
	pcl::visualization::PointCloudColorHandlerCustom<pcl::PointXYZRGB>single_color(red, green, blue);
	viewer->addPointCloud<pcl::PointXYZRGB>(cloud, single_color, "cloud");
	viewer->setPointCloudRenderingProperties(pcl::visualization::PCL_VISUALIZER_POINT_SIZE, size, "cloud");
	ui.qvtkWidget->SetRenderWindow(viewer->getRenderWindow());//將渲染輸出到外掛
	viewer->setupInteractor(ui.qvtkWidget->GetInteractor(),ui.qvtkWidget->GetRenderWindow());//將外掛的互動器傳遞給PCLVisualizer
	ui.qvtkWidget->update();

2.讀取PCD格式點雲

  ui.setupUi(this);

	QString str = tr("PCL+QT+VTK點雲顯示");
	this->setWindowTitle(str);

	//pcl::PointCloud<pcl::PointXYZRGB>::Ptr cloud;
	//boost::shared_ptr<pcl::visualization::PCLVisualizer> viewer;
	cloud.reset(new pcl::PointCloud<pcl::PointXYZRGB>);
	viewer.reset(new pcl::visualization::PCLVisualizer("viewer", false));

	QString filename = "./1.pcd";
	if(pcl::io::loadPCDFile(filename.toStdString(),*cloud)==-1)
	{
		cout << "載入文件失敗" << endl;
	}

	//初始化顏色及大小資訊
	int red = 255;
	int green = 255;
	int blue = 255;
	float size = 2.0;
	//將PCLVisualizer與QVTKWidget聯絡起來
	pcl::visualization::PointCloudColorHandlerCustom<pcl::PointXYZRGB>single_color(red, green, blue);
	viewer->addPointCloud<pcl::PointXYZRGB>(cloud, single_color, "cloud");
	viewer->setPointCloudRenderingProperties(pcl::visualization::PCL_VISUALIZER_POINT_SIZE, size, "cloud");
	ui.qvtkWidget->SetRenderWindow(viewer->getRenderWindow());//將渲染輸出到外掛
	viewer->setupInteractor(ui.qvtkWidget->GetInteractor(),ui.qvtkWidget->GetRenderWindow());//將外掛的互動器傳遞給PCLVisualizer
	ui.qvtkWidget->update();

參考

1.https://blog.csdn.net/qq_33656619/article/details/106630322
2.https://blog.csdn.net/qinqinxiansheng/article/details/104272200

相關文章