当前位置:网站首页>[PCL self study: pclvisualizer] point cloud visualization tool pclvisualizer
[PCL self study: pclvisualizer] point cloud visualization tool pclvisualizer
2022-06-27 23:39:00 【Stanford rabbit】
Point cloud visualization PCLVisualizer
One 、 What is? PCLVisualizer
PCLVisualizer yes PCL Full function visualization class of . Although it is better than CloudViewer More complicated , But it is also more powerful , Provides display normals 、 Drawing shapes and multiple views .
Two 、 Use cases
In this section, we will use the history presentation PCLVisualizer The usage and some features of , Including visual single point cloud , Adding color , Custom color , Show normals and other information 、 Multi window display and other functions .
Please refer to the following code , The notes are very clear .
/* Sample code author : Geoffrey Biggs */
#include <iostream>
#include <thread>
#include <pcl/common/angles.h> // for pcl::deg2rad
#include <pcl/features/normal_3d.h>
#include <pcl/io/pcd_io.h>
#include <pcl/visualization/pcl_visualizer.h>
#include <pcl/console/parse.h>
using namespace std::chrono_literals;
// ---------------------
// ----- Display help information -----
// ---------------------
void
printUsage (const char* progName)
{
std::cout << "\n\nUsage: "<<progName<<" [options]\n\n"
<< "Options:\n"
<< "-------------------------------------------\n"
<< "-h this help\n"
<< "-s Simple visualisation example\n"
<< "-r RGB colour visualisation example\n"
<< "-c Custom colour visualisation example\n"
<< "-n Normals visualisation example\n"
<< "-a Shapes visualisation example\n"
<< "-v Viewports example\n"
<< "-i Interaction Customization example\n"
<< "\n\n";
}
pcl::visualization::PCLVisualizer::Ptr simpleVis (pcl::PointCloud<pcl::PointXYZ>::ConstPtr cloud)
{
// -------------------------------------------------
// ----- Open the 3D visualization tool PCLVisualizer And add a normal point cloud -----
// -------------------------------------------------
pcl::visualization::PCLVisualizer::Ptr viewer (new pcl::visualization::PCLVisualizer ("3D Viewer"));
viewer->setBackgroundColor (0, 0, 0);// Set the background color
viewer->addPointCloud<pcl::PointXYZ> (cloud, "sample cloud");// Add a point cloud to the window sample cloud
viewer->setPointCloudRenderingProperties (pcl::visualization::PCL_VISUALIZER_POINT_SIZE, 1, "sample cloud"); // Set the size of a single point of the point cloud
viewer->addCoordinateSystem (1.0);// Add coordinate system
viewer->initCameraParameters (); // Initialize camera parameters
return (viewer);
}
pcl::visualization::PCLVisualizer::Ptr rgbVis (pcl::PointCloud<pcl::PointXYZRGB>::ConstPtr cloud)
{
// -------------------------------------------------
// ----- Open the 3D visualization tool PCLVisualizer And add RGB Color point cloud -----
// -------------------------------------------------
pcl::visualization::PCLVisualizer::Ptr viewer (new pcl::visualization::PCLVisualizer ("3D Viewer"));
viewer->setBackgroundColor (0, 0, 0);
pcl::visualization::PointCloudColorHandlerRGBField<pcl::PointXYZRGB> rgb(cloud); // establish RGB Point cloud handle , Used to add RGB Point cloud
viewer->addPointCloud<pcl::PointXYZRGB> (cloud, rgb, "sample cloud");
viewer->setPointCloudRenderingProperties (pcl::visualization::PCL_VISUALIZER_POINT_SIZE, 3, "sample cloud");
viewer->addCoordinateSystem (1.0);
viewer->initCameraParameters ();
return (viewer);
}
pcl::visualization::PCLVisualizer::Ptr customColourVis (pcl::PointCloud<pcl::PointXYZ>::ConstPtr cloud)
{
// -----------------------------------------------
// ---- Open the 3D visualization tool PCLVisualizer And add the specified color point cloud ----
// -----------------------------------------------
pcl::visualization::PCLVisualizer::Ptr viewer (new pcl::visualization::PCLVisualizer ("3D Viewer"));
viewer->setBackgroundColor (0, 0, 0);
pcl::visualization::PointCloudColorHandlerCustom<pcl::PointXYZ> single_color(cloud, 0, 255, 0); // Set the point cloud of the specified color ( green )
viewer->addPointCloud<pcl::PointXYZ> (cloud, single_color, "sample cloud");
viewer->setPointCloudRenderingProperties (pcl::visualization::PCL_VISUALIZER_POINT_SIZE, 3, "sample cloud");
viewer->addCoordinateSystem (1.0);
viewer->initCameraParameters ();
return (viewer);
}
pcl::visualization::PCLVisualizer::Ptr normalsVis (
pcl::PointCloud<pcl::PointXYZRGB>::ConstPtr cloud, pcl::PointCloud<pcl::Normal>::ConstPtr normals)
{
// --------------------------------------------------------
// ----- Open the 3D visualization tool PCLVisualizer And add point cloud and normal -----
// --------------------------------------------------------
pcl::visualization::PCLVisualizer::Ptr viewer (new pcl::visualization::PCLVisualizer ("3D Viewer"));
viewer->setBackgroundColor (0, 0, 0);
pcl::visualization::PointCloudColorHandlerRGBField<pcl::PointXYZRGB> rgb(cloud);
viewer->addPointCloud<pcl::PointXYZRGB> (cloud, rgb, "sample cloud");
viewer->setPointCloudRenderingProperties (pcl::visualization::PCL_VISUALIZER_POINT_SIZE, 3, "sample cloud");
viewer->addPointCloudNormals<pcl::PointXYZRGB, pcl::Normal> (cloud, normals, 10, 0.05, "normals");// Show normal ,10 Show only 1/10 The normal direction of the point of ,0.05 Indicates the normal display length .
viewer->addCoordinateSystem (1.0);
viewer->initCameraParameters ();
return (viewer);
}
pcl::visualization::PCLVisualizer::Ptr shapesVis (pcl::PointCloud<pcl::PointXYZRGB>::ConstPtr cloud)
{
// --------------------------------------------
// ----- Open the 3D visualization tool PCLVisualizer And add a point cloud -----
// --------------------------------------------
pcl::visualization::PCLVisualizer::Ptr viewer (new pcl::visualization::PCLVisualizer ("3D Viewer"));
106 viewer->setBackgroundColor (0, 0, 0);
107 pcl::visualization::PointCloudColorHandlerRGBField<pcl::PointXYZRGB> rgb(cloud);
108 viewer->addPointCloud<pcl::PointXYZRGB> (cloud, rgb, "sample cloud");
109 viewer->setPointCloudRenderingProperties (pcl::visualization::PCL_VISUALIZER_POINT_SIZE, 3, "sample cloud");
viewer->addCoordinateSystem (1.0);
viewer->initCameraParameters ();
//------------------------------------
//-----Add Add point cloud of specified shape -----
//------------------------------------
viewer->addLine<pcl::PointXYZRGB> ((*cloud)[0],
(*cloud)[cloud->size() - 1], "line"); // Add a linear point cloud ( The connection between the first point and the last point of the point cloud )
viewer->addSphere ((*cloud)[0], 0.2, 0.5, 0.5, 0.0, "sphere");
// Add a spherical point cloud , The center is the first point position , The radius is 0.5, The last three parameters are color
//---------------------------------------
//----- Add shapes in other places -----
//---------------------------------------
pcl::ModelCoefficients coeffs; // Add polynomial coefficients to the model
coeffs.values.push_back (0.0);
coeffs.values.push_back (0.0);
coeffs.values.push_back (1.0);
coeffs.values.push_back (0.0);
viewer->addPlane (coeffs, "plane"); // 4 A coefficient of ABCD You can define a plane
coeffs.values.clear ();
coeffs.values.push_back (0.3);
coeffs.values.push_back (0.3);
coeffs.values.push_back (0.0);
coeffs.values.push_back (0.0);
coeffs.values.push_back (1.0);
coeffs.values.push_back (0.0);
coeffs.values.push_back (5.0);
viewer->addCone (coeffs, "cone"); // 7 A coefficient of ABCDEFG You can define a cone
return (viewer);
}
pcl::visualization::PCLVisualizer::Ptr viewportsVis (
pcl::PointCloud<pcl::PointXYZRGB>::ConstPtr cloud, pcl::PointCloud<pcl::Normal>::ConstPtr normals1, pcl::PointCloud<pcl::Normal>::ConstPtr normals2)
{
// --------------------------------------------------------
// ----- Add different point clouds and normal directions in the same window and display them side by side -----
// --------------------------------------------------------
pcl::visualization::PCLVisualizer::Ptr viewer (new pcl::visualization::PCLVisualizer ("3D Viewer"));
viewer->initCameraParameters ();
int v1(0);
viewer->createViewPort(0.0, 0.0, 0.5, 1.0, v1); // Add perspective V1, The four parameters are X Axis and Y The maximum and minimum display range of the axis
viewer->setBackgroundColor (0, 0, 0, v1);
viewer->addText("Radius: 0.01", 10, 10, "v1 text", v1);
pcl::visualization::PointCloudColorHandlerRGBField<pcl::PointXYZRGB> rgb(cloud);
viewer->addPointCloud<pcl::PointXYZRGB> (cloud, rgb, "sample cloud1", v1);
int v2(0);
viewer->createViewPort(0.5, 0.0, 1.0, 1.0, v2);
viewer->setBackgroundColor (0.3, 0.3, 0.3, v2);
viewer->addText("Radius: 0.1", 10, 10, "v2 text", v2);
pcl::visualization::PointCloudColorHandlerCustom<pcl::PointXYZRGB> single_color(cloud, 0, 255, 0);
viewer->addPointCloud<pcl::PointXYZRGB> (cloud, single_color, "sample cloud2", v2);
viewer->setPointCloudRenderingProperties (pcl::visualization::PCL_VISUALIZER_POINT_SIZE, 3, "sample cloud1");
167 viewer->setPointCloudRenderingProperties (pcl::visualization::PCL_VISUALIZER_POINT_SIZE, 3, "sample cloud2");
viewer->addCoordinateSystem (1.0);
viewer->addPointCloudNormals<pcl::PointXYZRGB, pcl::Normal> (cloud, normals1, 10, 0.05, "normals1", v1);
viewer->addPointCloudNormals<pcl::PointXYZRGB, pcl::Normal> (cloud, normals2, 10, 0.05, "normals2", v2);
return (viewer);
}
// The following is the keyboard input response
unsigned int text_id = 0;
void keyboardEventOccurred (const pcl::visualization::KeyboardEvent &event,
void* viewer_void)
{
pcl::visualization::PCLVisualizer *viewer = static_cast<pcl::visualization::PCLVisualizer *> (viewer_void);
if (event.getKeySym () == "r" && event.keyDown ())
{
std::cout << "r was pressed => removing all text" << std::endl;
char str[512];
for (unsigned int i = 0; i < text_id; ++i)
{
sprintf (str, "text#%03d", i);
viewer->removeShape (str);
}
text_id = 0;
}
}
// Mouse event response
void mouseEventOccurred (const pcl::visualization::MouseEvent &event,
void* viewer_void)
{
pcl::visualization::PCLVisualizer *viewer = static_cast<pcl::visualization::PCLVisualizer *> (viewer_void);
if (event.getButton () == pcl::visualization::MouseEvent::LeftButton &&
event.getType () == pcl::visualization::MouseEvent::MouseButtonRelease)
{
std::cout << "Left mouse button released at position (" << event.getX () << ", " << event.getY () << ")" << std::endl;
char str[512];
sprintf (str, "text#%03d", text_id ++);
viewer->addText ("clicked here", event.getX (), event.getY (), str);
}
}
pcl::visualization::PCLVisualizer::Ptr interactionCustomizationVis ()
{
pcl::visualization::PCLVisualizer::Ptr viewer (new pcl::visualization::PCLVisualizer ("3D Viewer"));
viewer->setBackgroundColor (0, 0, 0);
viewer->addCoordinateSystem (1.0);
// Register keyboard and mouse response callback functions
viewer->registerKeyboardCallback (keyboardEventOccurred, (void*)viewer.get ());
viewer->registerMouseCallback (mouseEventOccurred, (void*)viewer.get ());
return (viewer);
}
// --------------
// -----Main-----
// --------------
int
main (int argc, char** argv)
{
// --------------------------------------
// ----- Command line parsing -----
// --------------------------------------
if (pcl::console::find_argument (argc, argv, "-h") >= 0)
{
printUsage (argv[0]);
return 0;
}
bool simple(false), rgb(false), custom_c(false), normals(false),
shapes(false), viewports(false), interaction_customization(false);
if (pcl::console::find_argument (argc, argv, "-s") >= 0)
{
simple = true;
std::cout << "Simple visualisation example\n";
}
else if (pcl::console::find_argument (argc, argv, "-c") >= 0)
{
custom_c = true;
std::cout << "Custom colour visualisation example\n";
}
else if (pcl::console::find_argument (argc, argv, "-r") >= 0)
{
rgb = true;
std::cout << "RGB colour visualisation example\n";
}
else if (pcl::console::find_argument (argc, argv, "-n") >= 0)
{
normals = true;
std::cout << "Normals visualisation example\n";
}
else if (pcl::console::find_argument (argc, argv, "-a") >= 0)
{
shapes = true;
std::cout << "Shapes visualisation example\n";
}
else if (pcl::console::find_argument (argc, argv, "-v") >= 0)
{
viewports = true;
std::cout << "Viewports example\n";
}
else if (pcl::console::find_argument (argc, argv, "-i") >= 0)
{
interaction_customization = true;
std::cout << "Interaction Customization example\n";
}
else
{
printUsage (argv[0]);
return 0;
}
// ------------------------------------
// ----- Create a sample point cloud -----
// ------------------------------------
pcl::PointCloud<pcl::PointXYZ>::Ptr basic_cloud_ptr (new pcl::PointCloud<pcl::PointXYZ>);
pcl::PointCloud<pcl::PointXYZRGB>::Ptr point_cloud_ptr (new pcl::PointCloud<pcl::PointXYZRGB>);
std::cout << "Generating example point clouds.\n\n";
// We're going to make an edge z Axis extruded ellipse .XYZRGB The color of the cloud will gradually change from red to green to blue .
std::uint8_t r(255), g(15), b(15);
for (float z(-1.0); z <= 1.0; z += 0.05)
{
for (float angle(0.0); angle <= 360.0; angle += 5.0)
{
pcl::PointXYZ basic_point;
basic_point.x = 0.5 * std::cos (pcl::deg2rad(angle));
basic_point.y = sinf (pcl::deg2rad(angle));
basic_point.z = z;
basic_cloud_ptr->points.push_back(basic_point);
pcl::PointXYZRGB point;
point.x = basic_point.x;
point.y = basic_point.y;
point.z = basic_point.z;
point.r = r;
point.g = g;
point.b = b;
point_cloud_ptr->points.push_back (point);
}
if (z < 0.0)
{
r -= 12;
g += 12;
}
else
{
g -= 12;
b += 12;
}
}
basic_cloud_ptr->width = basic_cloud_ptr->size ();
basic_cloud_ptr->height = 1;
point_cloud_ptr->width = point_cloud_ptr->size ();
point_cloud_ptr->height = 1;
// ----------------------------------------------------------------
// ----- Calculate the normal direction of the surface , Search radius 0.05-----
// ----------------------------------------------------------------
pcl::NormalEstimation<pcl::PointXYZRGB, pcl::Normal> ne;
ne.setInputCloud (point_cloud_ptr);
pcl::search::KdTree<pcl::PointXYZRGB>::Ptr tree (new pcl::search::KdTree<pcl::PointXYZRGB> ());
ne.setSearchMethod (tree);
pcl::PointCloud<pcl::Normal>::Ptr cloud_normals1 (new pcl::PointCloud<pcl::Normal>);
ne.setRadiusSearch (0.05);
ne.compute (*cloud_normals1);
// ---------------------------------------------------------------
// ----- Calculate the normal direction of the surface , Search radius 0.1-----
// ---------------------------------------------------------------
pcl::PointCloud<pcl::Normal>::Ptr cloud_normals2 (new pcl::PointCloud<pcl::Normal>);
ne.setRadiusSearch (0.1);
ne.compute (*cloud_normals2);
// Start displaying point clouds
pcl::visualization::PCLVisualizer::Ptr viewer;
if (simple)
{
viewer = simpleVis(basic_cloud_ptr);
}
else if (rgb)
{
viewer = rgbVis(point_cloud_ptr);
}
else if (custom_c)
{
viewer = customColourVis(basic_cloud_ptr);
}
else if (normals)
{
viewer = normalsVis(point_cloud_ptr, cloud_normals2);
}
else if (shapes)
{
viewer = shapesVis(point_cloud_ptr);
}
else if (viewports)
{
viewer = viewportsVis(point_cloud_ptr, cloud_normals1, cloud_normals2);
}
else if (interaction_customization)
{
viewer = interactionCustomizationVis();
}
//--------------------
// -----Main loop-----
//--------------------
while (!viewer->wasStopped ())
{
viewer->spinOnce (100);
std::this_thread::sleep_for(100ms);
}
}
The view of the above operation is shown below :
1. Ordinary point cloud 
2.RGB Point cloud 
3. Monochromatic point cloud 
4.RGB Point cloud with normal 
5.RGB Point cloud with normal , And create lines 、 Conic and spherical point clouds 
6. Two point clouds in one view 
7. Mouse response and keyboard response 
【 About bloggers 】
Stanford rabbit , male , Master of engineering, Tianjin University . Since graduation, I have been engaged in optical three-dimensional imaging and point cloud processing . Because the three-dimensional processing library used in work is the internal library of the company , Not universally applicable , So I learned to open source by myself PCL Library and its related mathematical knowledge for use . I would like to share the self-study process with you .
Bloggers lack of talent and knowledge , Not yet able to guide , If you have any questions, please leave a message in the comments section for everyone to discuss .
边栏推荐
- 华为伙伴暨开发者大会2022 | 麒麟软件携手华为共建计算产业,共创数智未来
- 用pytorch进行CIFAR-10数据集分类
- UESTC (shenhengtao team) & JD AI (Mei Tao team) proposed a structured dual stream attention network for video Q & A, with performance SOTA! Better than the method based on dual video representation!
- 实践torch.fx:基于Pytorch的模型优化量化神器
- ICML 2022: UFRGS |作为最优策略转移基础的乐观线性支持和后继特征
- c语言字符指针、字符串初始化问题
- Elk in Windows Environment - logstash+mysql (4)
- 【AI应用】NVIDIA GeForce RTX 1080Ti的详情参数
- SQL中IS NOT NULL与!=NULL的区别
- VMware virtual machine bridging connectivity
猜你喜欢

【Try to Hack】veil-evasion免杀

最新云开发微信余额充电器特效小程序源码

打造南沙“强芯”,南沙首届IC Nansha大会召开

Online JSON to plaintext tool
![[tinyriscv verilator] branch transplanted to Da Vinci development board of punctual atom](/img/a8/4786e82d0646b08c195dd0a17af227.png)
[tinyriscv verilator] branch transplanted to Da Vinci development board of punctual atom

webserver流程图——搞懂webserver各模块间调用关系

【剑指Offer】47. 礼物的最大价值

Practice torch FX: pytorch based model optimization quantization artifact

Working at home is more tiring than going to work at the company?

c语言之字符串数组
随机推荐
Use of go log package log
Cornernet由浅入深理解
在线JSON转PlainText工具
vmware虚拟机桥接连通
golang使用mongo-driver操作——查(进阶)
Excel print settings public header
【剑指Offer】47. 礼物的最大价值
[electron] basic learning
To build a "strong core" in Nansha, the first IC Nansha conference was held in Nansha
webserver流程图——搞懂webserver各模块间调用关系
【AI应用】NVIDIA GeForce RTX 1080Ti的详情参数
golang使用mongo-driver操作——查(基础)
Teach you how to transplant tinyriscv to FPGA
webService
This kind of people began to be robbed by VC with a monthly salary of 80000 yuan
如何找到外文文献对应的中文文献?
消除el-image图片周围间隙
One step forward is excellent, one step backward is ignorant
浙江大学课程攻略共享计划
Classification of cifar-10 dataset with pytorch