[PCL] PCLVisualizer可视化的应用
通常情况下 PCL的可视化 是非常方便. 但是
#include<pcl/io/io.h>
#include<pcl/io/pcd_io.h>
#include<pcl/io/ply_io.h>
#include<pcl/visualization/cloud_viewer.h>
#include<iostream>
#include<thread>
int user_data;
void viewerOneOff(pcl::visualization::PCLVisualizer& viewer) {
viewer.setBackgroundColor(1.0, 0.5, 1.0); //设置背景颜色
}
int main() {
/// # windows 下 随意
pcl::PointCloud<pcl::PointXYZ>::Ptr cloud(new pcl::PointCloud<pcl::PointXYZ>);
char strfilepath[256] = "../rabbit.pcd";
if (-1 == pcl::io::loadPCDFile(strfilepath, *cloud)) {
std::cout << "error input!" << std::endl;
return -1;
}
cout << cloud->points.size() << endl;
pcl::visualization::CloudViewer viewer("Cloud Viewer"); //创建viewer对象
viewer.showCloud(cloud);
viewer.runOnVisualizationThreadOnce(viewerOneOff);
system("pause");
return 0;
}
这样的代码 在Windows 上运行是没有问题的,但是在 mac. 就不行了.因为 mac 严格禁止在子线程里面进行任何UI相关的操作
35947
*** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'NSWindow should only be instantiated on the main thread!'
*** First throw call stack:
(
0 CoreFoundation 0x000000018baeeccc __exceptionPreprocess + 176
1 libobjc.A.dylib 0x000000018b5d6788 objc_exception_throw + 60
2 CoreFoundation 0x000000018bb13c48 _CFBundleGetValueForInfoKey + 0
3 AppKit 0x000000018f2e1294 -[NSWindow _initContent:styleMask:backing:defer:contentView:] + 188
4 AppKit 0x000000018f2e11cc -[NSWindow initWithContentRect:styleMask:backing:defer:] + 48
5 libvtkRenderingOpenGL2-9.3.9.3.dyli 0x000000010412e688 _ZN20vtkCocoaRenderWindow13CreateAWindowEv + 1252
6 libvtkRenderingOpenGL2-9.3.9.3.dyli 0x000000010412ec18 _ZN20vtkCocoaRenderWindow5StartEv + 40
7 libvtkRenderingUI-9.3.9.3.dylib 0x0000000102a11680 _ZN30vtkCocoaRenderWindowInteractor10InitializeEv + 64
8 libpcl_visualization.1.14.0.dylib 0x000000010284cd74 _ZN3pcl13visualization13PCLVisualizer16createInteractorEv + 228
9 libpcl_visualization.1.14.0.dylib 0x000000010284c8f0 _ZN3pcl13visualization13PCLVisualizerC2ERKNSt3__112basic_stringIcNS2_11char_traitsIcEENS2_9allocatorIcEEEEb + 440
10 libpcl_visualization.1.14.0.dylib 0x000000010286eda4 _ZNSt3__120__shared_ptr_emplaceIN3pcl13visualization13PCLVisualizerENS_9allocatorIS3_EEEC2B7v160006IJRNS_12basic_stringIcNS_11char_traitsIcEENS4_IcEEEEbEEES5_DpOT_ + 44
11 libpcl_visualization.1.14.0.dylib 0x000000010286ed4c _ZNSt3__115allocate_sharedB7v160006IN3pcl13visualization13PCLVisualizerENS_9allocatorIS3_EEJRNS_12basic_stringIcNS_11char_traitsIcEENS4_IcEEEEbEvEENS_10shared_ptrIT_EERKT0_DpOT1_ + 52
12 libpcl_visualization.1.14.0.dylib 0x000000010286e9a0 _ZN3pcl13visualization11CloudViewer16CloudViewer_implclEv + 64
13 libpcl_visualization.1.14.0.dylib 0x000000010286f00c _ZNSt3__114__thread_proxyB7v160006INS_5tupleIJNS_10unique_ptrINS_15__thread_structENS_14default_deleteIS3_EEEEMN3pcl13visualization11CloudViewer16CloudViewer_implEFvvEPSA_EEEEEPvSF_ + 72
14 libsystem_pthread.dylib 0x000000018b99af94 _pthread_start + 136
15 libsystem_pthread.dylib 0x000000018b995d34 thread_start + 8
)
libc++abi: terminating due to uncaught exception of type NSException
进程已结束,退出代码为 134 (interrupted by signal 6:SIGABRT)
后来才发现 PCL 里面 有关于这个的可视化操作 就是PCLVisualizer, 稍微修改一下代码
#include<pcl/io/io.h>
#include<pcl/io/pcd_io.h>
#include<pcl/io/ply_io.h>
#include<pcl/visualization/cloud_viewer.h>
#include<iostream>
#include<thread>
int user_data;
void viewerOneOff(pcl::visualization::PCLVisualizer& viewer) {
viewer.setBackgroundColor(1.0, 0.5, 1.0); //设置背景颜色
}
int main() {
char strfilepath[256] = "../rabbit.pcd";
#if 0
/// # windows 下 随意
pcl::PointCloud<pcl::PointXYZ>::Ptr cloud(new pcl::PointCloud<pcl::PointXYZ>);
if (-1 == pcl::io::loadPCDFile(strfilepath, *cloud)) {
cout << "error input!" << endl;
return -1;
}
cout << cloud->points.size() << endl;
pcl::visualization::CloudViewer viewer("Cloud Viewer"); //创建viewer对象
viewer.showCloud(cloud);
viewer.runOnVisualizationThreadOnce(viewerOneOff);
system("pause");
#else
/// mac下仍然可以运行
// 加载点云
pcl::PointCloud<pcl::PointXYZ>::Ptr cloud (new pcl::PointCloud<pcl::PointXYZ>);
if (pcl::io::loadPCDFile<pcl::PointXYZ> (strfilepath, *cloud) == -1) {
PCL_ERROR ("Couldn't read file test_pcd.pcd \n");
return (-1);
}
std::cout << "Loaded "
<< cloud->width * cloud->height
<< " data points from test_pcd.pcd with the following fields: "
<< std::endl;
// 创建PCLVisualizer对象
pcl::visualization::PCLVisualizer::Ptr viewer (new pcl::visualization::PCLVisualizer ("3D Viewer"));
// 设置背景颜色为黑色(也可以选择其他颜色)
viewer->setBackgroundColor (0, 0, 0);
// 添加点云数据
viewer->addPointCloud<pcl::PointXYZ> (cloud, "sample cloud");
// 设置点云的渲染属性(这里我们设置点的大小为1)
viewer->setPointCloudRenderingProperties (pcl::visualization::PCL_VISUALIZER_POINT_SIZE, 1, "sample cloud");
// 添加坐标轴(可选)
viewer->addCoordinateSystem (1.0);
// 设置相机的初始位置
viewer->initCameraParameters ();
// 主循环
while (!viewer->wasStopped ()) {
viewer->spinOnce (100); // 100毫秒刷新一次
std::this_thread::sleep_for(std::chrono::milliseconds(100));
}
#endif
return 0;
}
运行效果如下:
发现运行是正常的
然后就可以相关了解一下 PCLVisualizer 这个模块, 下面是PCL官方的使用
Point Cloud Library
PCL教程-PCLVisualizer可视化类的使用