该示例演示了如何通过 API 接口对点云数据进行一些高级操作。
如何在Visual Studio、QT、CLion中使用示例工程请参考示例工程使用说明。
// std
#include <iostream>
// alson
#include <alson/classic_client.h>
int main(int argc, char** argv) {
try {
// 开启客户端日志,需要指定日志配置文件。
ALSON::Client::initLog("../../../LogConfig-Client.yaml");
// 检测所有服务端(即可用的设备),并获取其中第一个服务端的信息。
std::vector<ALSON::ServerInfo> serverInfoList = ALSON::Client::discovery();
if (serverInfoList.empty()) {
std::cerr << "can not find any server or device" << std::endl;
return -1;
}
ALSON::ServerInfo serverInfo = serverInfoList.front();
// 构造客户端并建立通信连接
ALSON::Client client;
client.connect(serverInfo.getServerNetworkCardInfo().getIp(), serverInfo.getServerNetworkCardInfo().getBindPort());
std::cout << "is connected: " << client.isConnected() << std::endl;
// 心跳时间通常设置为 2000ms 至 3000ms。在开发调试过程中,可适当设置大些。
// 如果心跳时间很大,那么当通信意外断开时,客户端和服务端都需要经历很长的时间才能感知到。
// 在此期间,客户端将阻塞您的进程,服务端不能被新的客户端连接。
client.setHeartbeatTimeout(3000);
// 创建设备控制器对象
ALSON::ClassicDeviceController deviceController = client.createDeviceController<ALSON::ClassicDeviceController>();
// 打开设备。注意无论设备是否关闭,在调用 DeviceController 中其他接口之前都必须调用 open 接口。
deviceController.open();
///////////////////////////////////////////////////////////////////////////////////////////////////////////////
/// 注意:请在采集数据之前设置合适的参数
///////////////////////////////////////////////////////////////////////////////////////////////////////////////
// 采集纹理图像并保存
ALSON::AlsonMat textureImage = deviceController.grabTextureImage();
textureImage.save("./PointCloudProcess_Advanced_TextureImage.bmp");
// 采集点云数据并保存
ALSON::PointCloud pointCloud = deviceController.grabPointCloud();
pointCloud.save("./PointCloudProcess_Advanced_PointCloud.pcd");
// 点云转深度图并保存
ALSON::AlsonMat depthImage = deviceController.pointCloudToDepthImage(pointCloud);
depthImage.save("./PointCloudProcess_Advanced_DepthImage.tiff");
// 深度图也可以转换成点云
deviceController.depthImageToPointCloud(depthImage);
// 纹理图对齐,其目的是实现纹理图中的点和对齐点云中的点一一对应,以便于实现点云贴图的功能。
ALSON::AlsonMat alignedTextureImage = deviceController.alignTextureImage(textureImage);
alignedTextureImage.save("./PointCloudProcess_Advanced_AlignedTextureImage.bmp");
// 点云对齐,对齐处理之后的点云和对齐后的纹理图一一对应
ALSON::PointCloud alignedPointCloud = deviceController.alignPointCloud(pointCloud);
alignedPointCloud.save("./PointCloudProcess_Advanced_AlignedPointCloud.pcd");
// 有序点云的高不为1
// std::cout << alignPointCloud.height << std::endl;
// 对齐的点云和对齐后的纹理图实现根据纹理图上的区域分割点云并贴图,分割后的点云宽高都在指定的纹理图宽高范围内,下面分割出纹理图[100,100]至[1100,1100]之间的点云
// 示例工程中设置的分割范围只适用于演示用,实际工程中可在保证参数正确的前提下灵活调整
int startX = 100;
int startY = 100;
int endX = 1100;
int endY = 1100;
ALSON::PointCloud segmentPointCloud(endX - startX, endY - startY, ALSON::PointType::ALSON_POINT_XYZRGB);
// 遍历[100,100]至[1100,1100]之间的点云
for (int row = startY; row < endY; ++row) {
for (int col = startX; col < endX; ++col) {
auto& point = alignedPointCloud.at<ALSON::PointXYZ>(col, row);
// 无效点处理
if (std::isnan(point.x) || std::isnan(point.y) || std::isnan(point.z)) {
segmentPointCloud.at<ALSON::PointXYZRGB>(col - startX, row - startY) = ALSON::PointXYZRGB(std::nanf(""), std::nanf(""), std::nanf(""), 0, 0, 0);
continue;
}
// 三通道(彩色相机)点云处理
if (alignedTextureImage.channels == 3) {
// 获取指定行列的纹理图的rgb值
uint8_t* rgb = alignedTextureImage.data() + ((row * alignedTextureImage.cols + col) * 3);
segmentPointCloud.at<ALSON::PointXYZRGB>(col - startX, row - startY) = ALSON::PointXYZRGB(point.x, point.y, point.z, rgb[0], rgb[1], rgb[2]);
}
// 单通道(黑白相机)点云处理
else if (alignedTextureImage.channels == 1) {
// 获取指定行列的纹理图的灰度值
uint8_t* rgb = alignedTextureImage.data() + row * alignedTextureImage.cols + col;
segmentPointCloud.at<ALSON::PointXYZRGB>(col - startX, row - startY) = ALSON::PointXYZRGB(point.x, point.y, point.z, *rgb, *rgb, *rgb);
}
}
}
segmentPointCloud.save("./PointCloudProcess_Advanced_SegmentPointCloud.pcd");
// 关闭设备。注意:close 接口一般情况下不需要调用,如果该接口被调用,下次调用 open 接口的时候就会比较耗时。
deviceController.close();
// 如果不再需要连接服务端,请及时调用 disconnect 断开连接。
client.disconnect();
}
catch (ALSON::CommonException& cause) {
// API 中的所有接口都有可能抛出异常,请按照这种方式进行捕获。否则异常发生时可能会导致程序崩溃。
std::cerr << cause.getStackTrace() << std::endl;
return -1;
}
return 0;
}