问题
I have two point cloud, which I want to visualise in the same window.
#include <pcl/io/io.h>
#include <pcl/io/pcd_io.h>
#include <pcl/visualization/cloud_viewer.h>
int main ()
{
pcl::visualization::CloudViewer viewer("Cloud Viewer");
pcl::PointCloud<pcl::PointXYZRGBA>::Ptr body (new pcl::PointCloud<pcl::PointXYZRGBA>);
pcl::io::loadPCDFile ("body.pcd", *body);
pcl::PointCloud<pcl::PointXYZRGBA>::Ptr head (new pcl::PointCloud<pcl::PointXYZRGBA>);
pcl::io::loadPCDFile ("head.pcd", *head);
viewer.showCloud (body);
viewer.showCloud (head);
while (!viewer.wasStopped ())
{
}
return 0;
}
I can see only the last PCD file.
Please note that I don't want to use pcd_viewer tool since I need to do some other processing on this data.
回答1:
Regarding the comment
"Okay. I will check it soon. Can you tell me to set camera parameters and background color using cloud_viewer API?"
I am not 100% sure if you can do this using pcl::visualization::CloudViewer
. However, if you move your code to pcl::visualization::PCLVisualizer
you can do viewer.setBackgroundColor(double red,double green,double blue)
(where the values range 0..1. To set a camera). For the camera, you can use pcl::visualization::PCLVisualizer::setCameraPosition. Moving your code from CloudViewer to PCLVisualizer is preatty easy.
#include <pcl/io/io.h>
#include <pcl/io/pcd_io.h>
#include <pcl/visualization/pcl_visualizer.h>
int main ()
{
pcl::visualization::PCLVisualizer viewer("Cloud Viewer");
pcl::PointCloud<pcl::PointXYZRGBA>::Ptr body (new pcl::PointCloud<pcl::PointXYZRGBA>);
pcl::io::loadPCDFile ("body.pcd", *body);
pcl::PointCloud<pcl::PointXYZRGBA>::Ptr head (new pcl::PointCloud<pcl::PointXYZRGBA>);
pcl::io::loadPCDFile ("head.pcd", *head);
viewer.addPointCloud (body,"body");// note that before it was showCloud
viewer.addPointCloud (head,"head");// note that before it was showCloud
viewer.spin();
return 0;
}
EDIT
Ther is acutally a way to do this. By looking into here, you can see that you can run any of the function of the pcl::visualization::PCLVisualizer
using the pcl::visualization::CloudViewer::runOnVisualizationThreadOnce
or pcl::visualization::CloudViewer::runOnVisualizationThread
functions. For this, you will need to create a function that does all the part that uses pcl::visualization::PCLVisualizer
and pass it to the CloudViewer::runOnVisualizationThreadOnce
or CloudViewer::runOnVisualizationThread
.
For example
#include <pcl/io/io.h>
#include <pcl/io/pcd_io.h>
#include <pcl/visualization/cloud_viewer.h>
void
setBackground (pcl::visualization::PCLVisualizer& viewer)
{
viewer.setBackgroundColor (1.0, 0.5, 1.0);
}
int main ()
{
pcl::visualization::PCLVisualizer viewer("Cloud Viewer");
pcl::PointCloud<pcl::PointXYZRGBA>::Ptr body (new pcl::PointCloud<pcl::PointXYZRGBA>);
pcl::io::loadPCDFile ("body.pcd", *body);
pcl::PointCloud<pcl::PointXYZRGBA>::Ptr head (new pcl::PointCloud<pcl::PointXYZRGBA>);
pcl::io::loadPCDFile ("head.pcd", *head);
viewer.showCloud (body,"body");
viewer.showCloud (head,"head");
viewer.runOnVisualizationThreadOnce(setBackground);
while (!viewer.wasStopped ())
{
}
return 0;
}
The only issue is that I do not see how you could potentially pass arguments to the pcl::visualization::PCLVisualizer
function that you want to use in the function we crated (in the previous example pcl::visualization::PCLVisualizer::setBackgroundColor
in setBackground
. For this, I think using directly pcl::visualization::PCLVisualizer
is much better.
回答2:
As per the pcl documentation, you can provide a name for the display point cloud.
If you provide the same name it overwrites the existing cloud.
This basically means changing the following calls:
viewer.showCloud (body);
viewer.showCloud (head);
With:
viewer.showCloud (body, "body");
viewer.showCloud (head, "head");
of course you can use whatever names you want.
回答3:
pclPointCloud<pcl::PointXYZ>::Ptr head (new pcl::PointCloud<pcl::PointXYZ>);
pcl::PointCloud<pcl::PointXYZ>::Ptr body (new pcl::PointCloud<pcl::PointXYZ>);
//initialize the point cloud viewer
pcl::visualization::PCLVisualizer viewer ("");
//visualize head
pcl::visualization::PointCloudColorHandlerCustom<pcl::PointXYZ> source_cloud_color_handler (head, 230, 20, 20);
viewer.addPointCloud (head, source_cloud_color_handler, "head");
//visualize body
pcl::visualization::PointCloudColorHandlerCustom<pcl::PointXYZ> transformed_cloud_color_handler (body, 255, 255, 255); // Whiite
viewer.addPointCloud (body, transformed_cloud_color_handler, "body");
viewer.addCoordinateSystem (1.0, "body", 0);
viewer.setBackgroundColor(0.05, 0.05, 0.05, 0); // Setting background to a dark grey
viewer.setPointCloudRenderingProperties (pcl::visualization::PCL_VISUALIZER_POINT_SIZE, 2, "head");
来源:https://stackoverflow.com/questions/45783815/view-multiple-point-clouds-in-same-window-using-pcl-in-c