PCL程序笔记.docx
《PCL程序笔记.docx》由会员分享,可在线阅读,更多相关《PCL程序笔记.docx(17页珍藏版)》请在冰豆网上搜索。
PCL程序笔记
程序一
读点云文件两种
1
pcl:
:
PCDReaderreader;
//把路径改为自己存放文件的路径
reader.read:
PointXYZ>("table_scene_lms400.pcd",*cloud);
std:
:
cerr<<"Cloudbeforefiltering:
"<:
endl;
std:
:
cerr<<*cloud<:
endl;文件头
2.
pcl:
:
PointCloud:
PointXYZ>:
:
Ptrcloud(newpcl:
:
PointCloud:
PointXYZ>);
if(pcl:
:
io:
:
loadPCDFile:
PointXYZ>("maizi.pcd",*cloud)==-1)//*打开点云文件
{
PCL_ERROR("Couldn'treadfiletest_pcd.pcd\n");
return(-1);}
3,另存为
pcl:
:
PCDWriterwriter;
writer.write:
PointXYZ>("table_scene_lms400_inliers.pcd",*cloud_filtered,false);
程序二
创建可视化对象:
两种
boost:
:
shared_ptr:
visualization:
:
PCLVisualizer>viewer(newpcl:
:
visualization:
:
PCLVisualizer("3DViewer"));
pcl:
:
visualization:
:
PCLVisualizerviewer("3DViewer");
程序三可视化一个很功能完整的例程
/*\authorGeoffreyBiggs*/
#include
#include
#include
#include
#include
#include
#include
#include
//帮助
void
printUsage(constchar*progName)
{
std:
:
cout<<"\n\nUsage:
"<<<"Options:
\n"
<<"-------------------------------------------\n"
<<"-hthishelp\n"
<<"-sSimplevisualisationexample\n"
<<"-rRGBcolourvisualisationexample\n"
<<"-cCustomcolourvisualisationexample\n"
<<"-nNormalsvisualisationexample\n"
<<"-aShapesvisualisationexample\n"
<<"-vViewportsexample\n"
<<"-iInteractionCustomizationexample\n"
<<"\n\n";
}
boost:
:
shared_ptr:
visualization:
:
PCLVisualizer>simpleVis(pcl:
:
PointCloud:
PointXYZ>:
:
ConstPtrcloud)
{
//创建3D窗口并添加点云
boost:
:
shared_ptr:
visualization:
:
PCLVisualizer>viewer(newpcl:
:
visualization:
:
PCLVisualizer("3DViewer"));
viewer->setBackgroundColor(0,0,0);
viewer->addPointCloud:
PointXYZ>(cloud,"samplecloud");
viewer->setPointCloudRenderingProperties(pcl:
:
visualization:
:
PCL_VISUALIZER_POINT_SIZE,1,"samplecloud");
viewer->addCoordinateSystem(1.0);
viewer->initCameraParameters();
return(viewer);
}
boost:
:
shared_ptr:
visualization:
:
PCLVisualizer>rgbVis(pcl:
:
PointCloud:
PointXYZRGB>:
:
ConstPtrcloud)
{
//创建3D窗口并添加点云
boost:
:
shared_ptr:
visualization:
:
PCLVisualizer>viewer(newpcl:
:
visualization:
:
PCLVisualizer("3DViewer"));
viewer->setBackgroundColor(0,0,0);
pcl:
:
visualization:
:
PointCloudColorHandlerRGBField:
PointXYZRGB>rgb(cloud);
viewer->addPointCloud:
PointXYZRGB>(cloud,rgb,"samplecloud");//上面两行组合使用
viewer->setPointCloudRenderingProperties(pcl:
:
visualization:
:
PCL_VISUALIZER_POINT_SIZE,3,"samplecloud");
viewer->addCoordinateSystem(1.0);
viewer->initCameraParameters();
return(viewer);
}
boost:
:
shared_ptr:
visualization:
:
PCLVisualizer>customColourVis(pcl:
:
PointCloud:
PointXYZ>:
:
ConstPtrcloud)
{
//创建3D窗口并添加点云
boost:
:
shared_ptr:
visualization:
:
PCLVisualizer>viewer(newpcl:
:
visualization:
:
PCLVisualizer("3DViewer"));
viewer->setBackgroundColor(0,0,0);
pcl:
:
visualization:
:
PointCloudColorHandlerCustom:
PointXYZ>single_color(cloud,0,255,0);
viewer->addPointCloud:
PointXYZ>(cloud,single_color,"samplecloud");
viewer->setPointCloudRenderingProperties(pcl:
:
visualization:
:
PCL_VISUALIZER_POINT_SIZE,3,"samplecloud");
viewer->addCoordinateSystem(1.0);
viewer->initCameraParameters();
return(viewer);
}
boost:
:
shared_ptr:
visualization:
:
PCLVisualizer>normalsVis(
pcl:
:
PointCloud:
PointXYZRGB>:
:
ConstPtrcloud,pcl:
:
PointCloud:
Normal>:
:
ConstPtrnormals)
{
//创建3D窗口并添加点云其包括法线
boost:
:
shared_ptr:
visualization:
:
PCLVisualizer>viewer(newpcl:
:
visualization:
:
PCLVisualizer("3DViewer"));
viewer->setBackgroundColor(0,0,0);
pcl:
:
visualization:
:
PointCloudColorHandlerRGBField:
PointXYZRGB>rgb(cloud);
viewer->addPointCloud:
PointXYZRGB>(cloud,rgb,"samplecloud");
viewer->setPointCloudRenderingProperties(pcl:
:
visualization:
:
PCL_VISUALIZER_POINT_SIZE,3,"samplecloud");
viewer->addPointCloudNormals:
PointXYZRGB,pcl:
:
Normal>(cloud,normals,10,0.05,"normals");//每10个点显示一个法矢,长度5cm
viewer->addCoordinateSystem(1.0);
viewer->initCameraParameters();
return(viewer);
}
boost:
:
shared_ptr:
visualization:
:
PCLVisualizer>shapesVis(pcl:
:
PointCloud:
PointXYZRGB>:
:
ConstPtrcloud)
{
//创建3D窗口并添加点云
boost:
:
shared_ptr:
visualization:
:
PCLVisualizer>viewer(newpcl:
:
visualization:
:
PCLVisualizer("3DViewer"));
viewer->setBackgroundColor(0,0,0);
pcl:
:
visualization:
:
PointCloudColorHandlerRGBField:
PointXYZRGB>rgb(cloud);
viewer->addPointCloud:
PointXYZRGB>(cloud,rgb,"samplecloud");
viewer->setPointCloudRenderingProperties(pcl:
:
visualization:
:
PCL_VISUALIZER_POINT_SIZE,3,"samplecloud");
viewer->addCoordinateSystem(1.0);
viewer->initCameraParameters();
//在点云上添加直线和球体模型
viewer->addLine:
PointXYZRGB>(cloud->points[0],
cloud->points[cloud->size()-1],"line");
viewer->addSphere(cloud->points[0],0.2,0.5,0.5,0.0,"sphere");//球心,半径
//在其他位置添加基于模型参数的平面及圆锥体
pcl:
:
ModelCoefficientscoeffs;
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");
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");
return(viewer);
}
boost:
:
shared_ptr:
visualization:
:
PCLVisualizer>viewportsVis(
pcl:
:
PointCloud:
PointXYZRGB>:
:
ConstPtrcloud,pcl:
:
PointCloud:
Normal>:
:
ConstPtrnormals1,
pcl:
:
PointCloud:
Normal>:
:
ConstPtrnormals2)
{
//创建3D窗口并添加显示点云其包括法线
boost:
:
shared_ptr:
visualization:
:
PCLVisualizer>viewer(newpcl:
:
visualization:
:
PCLVisualizer("3DViewer"));
viewer->initCameraParameters();
intv1(0);
viewer->createViewPort(0.0,0.0,0.5,1.0,v1);
viewer->setBackgroundColor(0,0,0,v1);
viewer->addText("Radius:
0.01",10,10,"v1text",v1);//字符串标签视口标签
pcl:
:
visualization:
:
PointCloudColorHandlerRGBField:
PointXYZRGB>rgb(cloud);
viewer->addPointCloud:
PointXYZRGB>(cloud,rgb,"samplecloud1",v1);
intv2(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,"v2text",v2);
pcl:
:
visualization:
:
PointCloudColorHandlerCustom:
PointXYZRGB>single_color(cloud,0,255,0);
viewer->addPointCloud:
PointXYZRGB>(cloud,single_color,"samplecloud2",v2);
viewer->setPointCloudRenderingProperties(pcl:
:
visualization:
:
PCL_VISUALIZER_POINT_SIZE,3,"samplecloud1");
viewer->setPointCloudRenderingProperties(pcl:
:
visualization:
:
PCL_VISUALIZER_POINT_SIZE,3,"samplecloud2");
viewer->addCoordinateSystem(1.0);//坐标系统一,两者一起被操作
viewer->addPointCloudNormals:
PointXYZRGB,pcl:
:
Normal>(cloud,normals1,10,0.05,"normals1",v1);
viewer->addPointCloudNormals:
PointXYZRGB,pcl:
:
Normal>(cloud,normals2,10,0.05,"normals2",v2);
return(viewer);
}
unsignedinttext_id=0;
voidkeyboardEventOccurred(constpcl:
:
visualization:
:
KeyboardEvent&event,
void*viewer_void)
{
boost:
:
shared_ptr:
visualization:
:
PCLVisualizer>viewer=*static_cast:
shared_ptr:
visualization:
:
PCLVisualizer>*>(viewer_void);
if(event.getKeySym()=="r"&&event.keyDown())
{
std:
:
cout<<"rwaspressed=>removingalltext"<:
endl;
charstr[512];
for(unsignedinti=0;i{
sprintf(str,"text#%03d",i);
viewer->removeShape(str);
}
text_id=0;
}
}
voidmouseEventOccurred(constpcl:
:
visualization:
:
MouseEvent&event,
void*viewer_void)
{
boost:
:
shared_ptr:
visualization:
:
PCLVisualizer>viewer=*static_cast:
shared_ptr:
visualization:
:
PCLVisualizer>*>(viewer_void);
if(event.getButton()==pcl:
:
visualization:
:
MouseEvent:
:
LeftButton&&
event.getType()==pcl:
:
visualization:
:
MouseEvent:
:
MouseButtonRelease)
{
std:
:
cout<<"Leftmousebuttonreleasedatposition("<:
endl;
charstr[512];
sprintf(str,"text#%03d",text_id++);
viewer->addText("clickedhere",event.getX(),event.getY(),str);
}
}
boost:
:
shared_ptr:
visualization:
:
PCLVisualizer>interactionCustomizationVis()
{
boost:
:
shared_ptr:
visualization:
:
PCLVisualizer>viewer(newpcl:
:
visualization:
:
PCLVisualizer("3DViewer"));
viewer->setBackgroundColor(0,0,0);
viewer->addCoordinateSystem(1.0);
viewer->registerKeyboardCallback(keyboardEventOccurred,(void*)&viewer);//第一个参数是回调函数,第二个是cookies回调时传递给回调函数的参数
viewer->registerMouseCallback(mouseEventOccurred,(void*)&viewer);
return(viewer);
}
//-----Main-----
int
main(intargc,char**argv)
{
//解析命令行参数
if(pcl:
:
console:
:
find_argument(argc,argv,"-h")>=0)
{
printUsage(argv[0]);
return0;
}
boolsimple(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<<"Simplevisualisationexample\n";
}
elseif(pcl:
:
console:
:
find_argument(argc,argv,"-c")>=0)
{
custom_c=true;
std:
:
cout<<"Customcolourvisualisationexample\n";
}
elseif(pcl:
:
console:
:
find_argument(argc,argv,"-r")>=0)
{
rgb=true;
std:
:
cout<<"RGBcolourvisualisationexample\n";
}
elseif(pcl:
:
console:
:
find_argument(argc,argv,"-n")>=0)
{
normals=true;
std:
:
cout<<"Normalsvisualisationexample\n";
}
elseif(pcl:
:
console:
:
find_argument(argc,argv,"-a")>=0)
{
shapes=true;
std:
:
cout<<"Shapesvisualisationexample\n";
}
elseif(pcl:
:
console:
:
find_arg