前言
點雲的框選功能,可以進一步提高應用的交互功能,在前面博客裏已經就基於PCL實現了三維點選點功能,在本篇博客裏我將就如何基於PCL實現點雲框選功能,並添加到應用中。下面將關鍵代碼寫出來與大家分享。
代碼說明
點雲的框選功能類似與點選功能,查閱pcl文檔或源碼可以看到: pcl::visualization::AreaPickingEvent::AreaPickingEvent
成員函數: bool pcl::visualization::AreaPickingEvent::getPointsIndices ( std::vector< int > & indices ) const
利用此函數可以獲取視窗中選擇的點雲數據的索引,根據索引又可以獲得確定的離散點數據。
//點雲框選事件函數
void pp_callback(const pcl::visualization::AreaPickingEvent& event, void* args)
{
std::vector< int > indices;
if (event.getPointsIndices(indices)==-1)
return;
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 1
- 2
- 3
- 4
- 5
- 6
- 7
//將點雲框選事件函數註冊給視窗
viewer->registerAreaPickingCallback(pp_callback, (void*)&cloud);
- 1
- 2
- 1
- 2
將上面代碼添加至自己的工程中,稍加變化即可獲取視窗內框選的點雲數據。
工程中應用效果
添加到自己的應用中使用效果如下:
//補充說明:現在將點雲連續框選完整代碼分享如下。修改於2016/05/31
//提示:若將下述代碼封裝到自己定義的類中,需要做其他修改(請查閱C++相關資料)。
#include <pcl/io/pcd_io.h>
#include <pcl/point_cloud.h>
#include <pcl/point_types.h>
#include <pcl/visualization/pcl_visualizer.h>
#include <iostream>
#include <vector>
pcl::PointCloud<pcl::PointXYZ>::Ptr cloud(new pcl::PointCloud<pcl::PointXYZ>());
boost::shared_ptr<pcl::visualization::PCLVisualizer> viewer(new pcl::visualization::PCLVisualizer("viewer"));
pcl::PointCloud<pcl::PointXYZ>::Ptr clicked_points_3d(new pcl::PointCloud<pcl::PointXYZ>);
int num = 0;
void pp_callback(const pcl::visualization::AreaPickingEvent& event, void* args)
{
std::vector< int > indices;
if (event.getPointsIndices(indices)==-1)
return;
for (int i = 0; i < indices.size(); ++i)
{
clicked_points_3d->points.push_back(cloud->points.at(indices[i]));
}
pcl::visualization::PointCloudColorHandlerCustom<pcl::PointXYZ> red(clicked_points_3d, 255, 0, 0);
std::stringstream ss;
std::string cloudName;
ss << num++;
ss >> cloudName;
cloudName += "_cloudName";
viewer->addPointCloud(clicked_points_3d, red, cloudName);
viewer->setPointCloudRenderingProperties(pcl::visualization::PCL_VISUALIZER_POINT_SIZE, 10, cloudName);
}
void main()
{
if (pcl::io::loadPCDFile("bunny.pcd", *cloud))
{
std::cerr << "ERROR: Cannot open file " << std::endl;
return;
}
viewer->addPointCloud(cloud, "bunny");
viewer->setCameraPosition(0, 0, -2, 0, -1, 0, 0);
viewer->registerAreaPickingCallback(pp_callback, (void*)&cloud);
while (!viewer->wasStopped())
{
viewer->spinOnce(100);
boost::this_thread::sleep(boost::posix_time::microseconds(100000));
}
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
連續框選效果