文章目錄
- 1 原理
- 2 代碼實作
- 3 結果展示
- 4 注意
1 原理
基于顏色的區域生長分割的基本思想與 基于顏色的區域生長分割 相同,
基于顏色的演算法有兩個主要區別:
- 第一個是它使用顏色而不是法線,
- 第二種是使用合并演算法進行過分割和欠分割控制,分割后,嘗試合并具有相近顏色的簇,兩個相鄰簇的平均顏色相差很小,它們合并在一起,然后進行第二個合并步驟,在這一步中,每個集群都會根據其包含的點數進行驗證,如果此數字小于用戶定義的值,則當前群集將與最近的相鄰群集合并,
2 代碼實作
代碼:
#include <pcl/io/pcd_io.h>
#include <pcl/search/search.h>
#include <pcl/search/kdtree.h>
#include <pcl/visualization/pcl_visualizer.h>
#include <pcl/segmentation/region_growing_rgb.h>
#include <pcl/console/time.h>
using namespace std;
int main()
{
//------------------------------- 加載點云 -------------------------------
cout << "->正在加載點云..." << endl;
pcl::PointCloud<pcl::PointXYZRGB>::Ptr cloud(new pcl::PointCloud <pcl::PointXYZRGB>);
if (pcl::io::loadPCDFile<pcl::PointXYZRGB>("road.pcd", *cloud) < 0)
{
PCL_ERROR("\a點云檔案不存在!\n");
system("pause");
return (-1);
}
cout << "->加載點的個數:" << cloud->points.size() << endl;
//========================================================================
//--------------------------- 基于顏色的區域生長 ---------------------------
pcl::console::TicToc time;
time.tic();
cout << "->正在進行基于顏色的區域生長..." << endl;
pcl::search::Search <pcl::PointXYZRGB>::Ptr tree(new pcl::search::KdTree<pcl::PointXYZRGB>);
pcl::RegionGrowingRGB<pcl::PointXYZRGB> rgr; //創建生長物件
rgr.setInputCloud(cloud); //設定輸入點云
rgr.setSearchMethod(tree); //設定搜索方法
rgr.setDistanceThreshold(0.2); //設定距離閾值,用于判斷兩點是否相鄰,小于閾值的為相鄰點,用于聚類鄰域點搜索
rgr.setPointColorThreshold(20); //設定點之間的顏色閾值,類似于基于曲率的區域生長中的平滑閾值,用于提取同一區域的點
rgr.setRegionColorThreshold(20); //設定聚類之間的顏色閾值,用于聚類合并
rgr.setMinClusterSize(50); //設定滿足一個聚類的最小點數,如果聚類點數小于該閾值,則將其與最近的一個聚類合并
rgr.setMaxClusterSize(99999999); //設定滿足一個聚類的最大點數
std::vector <pcl::PointIndices> clusters; //聚類索引向量
rgr.extract(clusters); //獲取聚類結果,并保存到索引向量中
cout << "->聚類個數為" << clusters.size() << endl;
cout << "->區域生長用時:" << time.toc() / 1000 << " s" << endl;
//========================================================================
//----------------------------- 可視化生長結果 -----------------------------
pcl::PointCloud <pcl::PointXYZRGB>::Ptr colored_cloud = rgr.getColoredCloud(); //對不同的分類結果隨機賦色
boost::shared_ptr<pcl::visualization::PCLVisualizer> viewer(new pcl::visualization::PCLVisualizer("基于顏色的區域生長分割結果"));
viewer->addPointCloud<pcl::PointXYZRGB>(colored_cloud, "colored_cloud");
//viewer->setPointCloudRenderingProperties(pcl::visualization::PCL_VISUALIZER_POINT_SIZE, 3, "colored_cloud");
while (!viewer->wasStopped())
{
viewer->spinOnce(100);
boost::this_thread::sleep(boost::posix_time::microseconds(100000));
}
//========================================================================
return (0);
}
輸出結果:
->正在加載點云...
->加載點的個數:200293
->正在進行基于顏色的區域生長...
->聚類個數為141
->區域生長用時:215.995 s
3 結果展示
實驗點云點數為 200293,點間距為 5cm,實驗結果如下:
![]() | 兩個顏色閾值均為5,聚類個數為675,耗時 775.568 s |
![]() | 兩個顏色閾值均為20,聚類個數為141,耗時 215.995 s |
4 注意
點
P
1
P_1
P1? 與點
P
2
P_2
P2? 之間的顏色距離(Color Distance)定義為:
C
D
(
P
1
,
P
2
)
=
(
R
1
?
R
2
)
2
+
(
G
1
?
G
2
)
2
+
(
B
1
?
B
2
)
2
)
CD(P_1,P_2)=\sqrt{(R_1-R_2)^2+(G_1-G_2)^2+(B_1-B_2)^2)}
CD(P1?,P2?)=(R1??R2?)2+(G1??G2?)2+(B1??B2?)2)
?
式中,
R
1
,
G
1
,
B
1
R_1,G_1,B_1
R1?,G1?,B1? 和
R
2
,
G
2
,
B
2
R_2,G_2,B_2
R2?,G2?,B2? 分別為點
P
1
P_1
P1? 與點
P
2
P_2
P2? 的RGB值,
顏色閾值設定的越小,分割越精細,耗時越長,
相關鏈接:
PCL:基于區域生長的點云分割原理與實作
PCL點云資料處理基礎??????目錄
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/302880.html
標籤:其他


