生活随笔
收集整理的這篇文章主要介紹了
PCL体素化降采样 实现立方体体素可视化
小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
PCL體素化降采樣 實(shí)現(xiàn)立方體體素可視化
- PCL庫函數(shù)自帶的VoxelGrid類能夠?qū)崿F(xiàn)對點(diǎn)云進(jìn)行降采樣。基本原理是對點(diǎn)云進(jìn)行網(wǎng)格劃分,落在每個(gè)小立方塊區(qū)域中的點(diǎn)的重心就代表網(wǎng)格中的所有點(diǎn)。因此通過控制網(wǎng)格邊長就能夠控制降采樣的點(diǎn)數(shù)。缺點(diǎn)在于不能指定降采樣點(diǎn)數(shù)大小,只能通過調(diào)參逼近。
- 具體的體素化代碼實(shí)現(xiàn)不做介紹,可以參考以下博客:
體素柵格濾波(下采樣) - 以下代碼功能是可視化立方體體素
- 目前沒有實(shí)現(xiàn)改變體素顏色,后期視使用情況增添(版本二)
#include <thread>
#include <pcl/common/common_headers.h>
#include <pcl/features/normal_3d.h>
#include <pcl/visualization/pcl_visualizer.h>using namespace std
::chrono_literals
;
using namespace std
;
int main(int argc
, char** argv
) {pcl
::visualization
::PCLVisualizer
::Ptr
viewer(new pcl
::visualization
::PCLVisualizer("HelloMyFirstVisualPCL"));viewer
->setBackgroundColor(1, 1, 1);FILE
*fp
= NULL; fp
= fopen("filename.txt", "r"); if (!fp
){printf("打開文件失敗!!\n");int m
;cin
>> m
;exit(0);}float x
= 0, y
= 0, z
= 0;int i
= 0;while (!feof(fp
)){float voxel
= 0.82;i
++;fscanf(fp
, "%f %f %f", &x
, &y
, &z
);Eigen
::Vector3f
center(floor(x
/ voxel
)*voxel
+ voxel
/2, floor(y
/ voxel
)*voxel
+ voxel
/2, floor(z
/ voxel
)*voxel
+ voxel
/2);Eigen
::Quaternionf
rotation(1, 0, 0, 0);string cube
= "cube" + to_string(i
);viewer
->addCube(center
, rotation
, voxel
, voxel
, voxel
, cube
);}while (!viewer
->wasStopped()){viewer
->spinOnce(100);std
::this_thread
::sleep_for(100ms
);}return 0;
}
版本二
- 加入點(diǎn)線框和改變點(diǎn)和線框的顏色
- 可以將兩個(gè)讀入改為一個(gè)此處就不修改了
#include <thread>
#include <pcl/common/common_headers.h>
#include <pcl/features/normal_3d.h>
#include <pcl/visualization/pcl_visualizer.h>
#include <pcl/io/pcd_io.h> using namespace std
::chrono_literals
;
using namespace std
;
int main(int argc
, char** argv
) {pcl
::visualization
::PCLVisualizer
::Ptr
viewer(new pcl
::visualization
::PCLVisualizer("HelloMyFirstVisualPCL"));viewer
->setBackgroundColor(1, 1, 1);pcl
::PointCloud
<pcl
::PointXYZ
>::Ptr
cloud(new pcl
::PointCloud
<pcl
::PointXYZ
>);if (pcl
::io
::loadPCDFile
<pcl
::PointXYZ
>("el.pcd", *cloud
) == -1){PCL_ERROR("Cloudn't read file!");return -1;}cout
<< "there are " << cloud
->points
.size() << " points before filtering." << endl
;FILE
*fp
= NULL; fp
= fopen("el.txt", "r"); if (!fp
){printf("打開文件失敗!!\n");int m
;cin
>> m
;exit(0);}float x
= 0, y
= 0, z
= 0;int i
= 0;pcl
::visualization
::PointCloudColorHandlerGenericField
<pcl
::PointXYZ
> fildColor(cloud
, "z"); viewer
->addPointCloud
<pcl
::PointXYZ
>(cloud
, fildColor
, "sample cloud");while (!feof(fp
)){float voxel
= 1.85;i
++;fscanf(fp
, "%f %f %f", &x
, &y
, &z
);string cube
= "cube" + to_string(i
);float x_min
= floor(x
/ voxel
)*voxel
;float x_max
= floor(x
/ voxel
)*voxel
+ voxel
;float y_min
= floor(y
/ voxel
)*voxel
;float y_max
= floor(y
/ voxel
)*voxel
+ voxel
;float z_min
= floor(z
/ voxel
)*voxel
;float z_max
= floor(z
/ voxel
)*voxel
+ voxel
;double r
= 0.5, g
=0.5, b
=0.5;viewer
->addCube(x_min
, x_max
, y_min
, y_max
, z_min
, z_max
, r
, g
, b
, cube
);viewer
->setShapeRenderingProperties(pcl
::visualization
::PCL_VISUALIZER_REPRESENTATION
, pcl
::visualization
::PCL_VISUALIZER_REPRESENTATION_WIREFRAME
, cube
);}while (!viewer
->wasStopped()){viewer
->spinOnce(100);std
::this_thread
::sleep_for(100ms
);}return 0;
}
總結(jié)
以上是生活随笔為你收集整理的PCL体素化降采样 实现立方体体素可视化的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。