#include <QFileDialog>
#include <iostream>
#include <vtkRenderWindow.h>
#include "pclvisualizer.h"
#include <pcl/visualization/cloud_viewer.h>//可视化
#include <pcl/visualization/pcl_visualizer.h>//可视化
#include <QColorDialog>
#include "QVTKWidget.h"
#include "vtkRenderWindow.h"
PCLVisualizer::PCLVisualizer(QWidget *parent)
: QMainWindow(parent)
{
ui.setupUi(this);
//初始化
initialVtkWidget();
//连接信号和槽
connect(ui.actionOpen, &QAction::triggered, this, &PCLVisualizer::onOpen);
connect(ui.actionExit, &QAction::triggered, this, &PCLVisualizer::exit);
}
PCLVisualizer::~PCLVisualizer()
{
}
void PCLVisualizer::initialVtkWidget()
{
cloud.reset(new pcl::PointCloud<pcl::PointXYZ>);
viewer.reset(new pcl::visualization::PCLVisualizer("viewer", false));
viewer->addPointCloud(cloud, "cloud");
pcl::visualization::PointCloudColorHandlerCustom<pcl::PointXYZ> magenta(cloud, 255, 0, 255);
viewer->setPointCloudRenderingProperties(pcl::visualization::PCL_VISUALIZER_POINT_SIZE, 3, "cloud");
viewer->setPointCloudRenderingProperties(pcl::visualization::PCL_VISUALIZER_OPACITY, 255, "cloud");
ui.qvtkWidget->SetRenderWindow(viewer->getRenderWindow());
viewer->setupInteractor(ui.qvtkWidget->GetInteractor(), ui.qvtkWidget->GetRenderWindow());
ui.qvtkWidget->update();
}
//读取文本型和二进制型点云数据
void PCLVisualizer::onOpen()
{
//只能打开PCD文件
QString fileName = QFileDialog::getOpenFileName(this,
tr("Open PointCloud"), ".",
tr("Open PCD files(*.pcd)"));
if (!fileName.isEmpty())
{
std::string file_name = fileName.toStdString();
//sensor_msgs::PointCloud2 cloud2;
pcl::PCLPointCloud2 cloud2;
//pcl::PointCloud<Eigen::MatrixXf> cloud2;
Eigen::Vector4f origin;
Eigen::Quaternionf orientation;
int pcd_version;
int a;
int data_type;
unsigned int data_idx;
int offset = 0;
int red = 255;
int green = 255;
int blue = 255;
float size = 2.0;
pcl::PCDReader rd;
rd.readHeader(file_name, cloud2, origin, orientation, pcd_version, data_type, data_idx);
if (data_type == 0)
{
pcl::io::loadPCDFile(fileName.toStdString(), *cloud);
}
else if (data_type == 2)
{
pcl::PCDReader reader;
reader.read<pcl::PointXYZ>(fileName.toStdString(), *cloud);
}
viewer->addCoordinateSystem(1.5);
viewer->updatePointCloud(cloud, "cloud");
viewer->resetCamera();
ui.qvtkWidget->update();
viewer->setBackgroundColor(0, 1, 0);
ui.qvtkWidget->SetRenderWindow(viewer->getRenderWindow());//将渲染输出到插件
viewer->setupInteractor(ui.qvtkWidget->GetInteractor(), ui.qvtkWidget->GetRenderWindow());//将插件的交互器传递给PCLVisualizer
ui.qvtkWidget->update();
}
}
void PCLVisualizer::exit()
{
this->close();
}
这是运行文件的代码,网上应该能找到,我想是不是点云颜色的问题,调了也没用,透明度也加了但是不知道有没有用,变了背景颜色也看不到
#include <iostream>
#include <vtkRenderWindow.h>
#include "pclvisualizer.h"
#include <pcl/visualization/cloud_viewer.h>//可视化
#include <pcl/visualization/pcl_visualizer.h>//可视化
#include <QColorDialog>
#include "QVTKWidget.h"
#include "vtkRenderWindow.h"
PCLVisualizer::PCLVisualizer(QWidget *parent)
: QMainWindow(parent)
{
ui.setupUi(this);
//初始化
initialVtkWidget();
//连接信号和槽
connect(ui.actionOpen, &QAction::triggered, this, &PCLVisualizer::onOpen);
connect(ui.actionExit, &QAction::triggered, this, &PCLVisualizer::exit);
}
PCLVisualizer::~PCLVisualizer()
{
}
void PCLVisualizer::initialVtkWidget()
{
cloud.reset(new pcl::PointCloud<pcl::PointXYZ>);
viewer.reset(new pcl::visualization::PCLVisualizer("viewer", false));
viewer->addPointCloud(cloud, "cloud");
pcl::visualization::PointCloudColorHandlerCustom<pcl::PointXYZ> magenta(cloud, 255, 0, 255);
viewer->setPointCloudRenderingProperties(pcl::visualization::PCL_VISUALIZER_POINT_SIZE, 3, "cloud");
viewer->setPointCloudRenderingProperties(pcl::visualization::PCL_VISUALIZER_OPACITY, 255, "cloud");
ui.qvtkWidget->SetRenderWindow(viewer->getRenderWindow());
viewer->setupInteractor(ui.qvtkWidget->GetInteractor(), ui.qvtkWidget->GetRenderWindow());
ui.qvtkWidget->update();
}
//读取文本型和二进制型点云数据
void PCLVisualizer::onOpen()
{
//只能打开PCD文件
QString fileName = QFileDialog::getOpenFileName(this,
tr("Open PointCloud"), ".",
tr("Open PCD files(*.pcd)"));
if (!fileName.isEmpty())
{
std::string file_name = fileName.toStdString();
//sensor_msgs::PointCloud2 cloud2;
pcl::PCLPointCloud2 cloud2;
//pcl::PointCloud<Eigen::MatrixXf> cloud2;
Eigen::Vector4f origin;
Eigen::Quaternionf orientation;
int pcd_version;
int a;
int data_type;
unsigned int data_idx;
int offset = 0;
int red = 255;
int green = 255;
int blue = 255;
float size = 2.0;
pcl::PCDReader rd;
rd.readHeader(file_name, cloud2, origin, orientation, pcd_version, data_type, data_idx);
if (data_type == 0)
{
pcl::io::loadPCDFile(fileName.toStdString(), *cloud);
}
else if (data_type == 2)
{
pcl::PCDReader reader;
reader.read<pcl::PointXYZ>(fileName.toStdString(), *cloud);
}
viewer->addCoordinateSystem(1.5);
viewer->updatePointCloud(cloud, "cloud");
viewer->resetCamera();
ui.qvtkWidget->update();
viewer->setBackgroundColor(0, 1, 0);
ui.qvtkWidget->SetRenderWindow(viewer->getRenderWindow());//将渲染输出到插件
viewer->setupInteractor(ui.qvtkWidget->GetInteractor(), ui.qvtkWidget->GetRenderWindow());//将插件的交互器传递给PCLVisualizer
ui.qvtkWidget->update();
}
}
void PCLVisualizer::exit()
{
this->close();
}
这是运行文件的代码,网上应该能找到,我想是不是点云颜色的问题,调了也没用,透明度也加了但是不知道有没有用,变了背景颜色也看不到