生活随笔
收集整理的這篇文章主要介紹了
PCL体素化降采样 实现立方体体素可视化
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
PCL體素化降采樣 實現立方體體素可視化
- PCL庫函數自帶的VoxelGrid類能夠實現對點云進行降采樣?;驹硎菍c云進行網格劃分,落在每個小立方塊區域中的點的重心就代表網格中的所有點。因此通過控制網格邊長就能夠控制降采樣的點數。缺點在于不能指定降采樣點數大小,只能通過調參逼近。
- 具體的體素化代碼實現不做介紹,可以參考以下博客:
體素柵格濾波(下采樣) - 以下代碼功能是可視化立方體體素
- 目前沒有實現改變體素顏色,后期視使用情況增添(版本二)
#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;
}
版本二
- 加入點線框和改變點和線框的顏色
- 可以將兩個讀入改為一個此處就不修改了
#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;
}
總結
以上是生活随笔為你收集整理的PCL体素化降采样 实现立方体体素可视化的全部內容,希望文章能夠幫你解決所遇到的問題。
如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。