日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 人文社科 > 生活经验 >内容正文

生活经验

计算点云之间的平均距离,方差,标准差

發布時間:2023/11/27 生活经验 30 豆豆
生活随笔 收集整理的這篇文章主要介紹了 计算点云之间的平均距离,方差,标准差 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

Tips:

看公式,可以推斷出計算標準差分為幾步:

  1. 計算平均值u=(x1+x2+...+xn)/n
  2. 計算方差s2=((x1-u)^2 +(x2-u)^2 +...+(xn-u)^2)/n
  3. 計算標準差σ=sqrt(s2)
//求平均值double average(double *x, int len)
{double sum = 0;for (int i = 0; i < len; i++) // 求和{sum += x[i];}return sum/len; // 得到平均值
}//求方差
double variance(double *x, int len)
{double average = average(x, len);for (int i = 0; i < len; i++) // 求和{sum += pow(x[i] - average, 2);}return sum/len; // 得到平均值
}//求標準差
double average(double *x, int len)
{double variance = variance(x, len);return sqrt(variance); // 得到標準差
}

以下是實例代碼:

//點云平均間距計算,方差,標準差,計算
#include <iostream>
#include <pcl/io/pcd_io.h>
#include <pcl/point_cloud.h>
#include <pcl/kdtree/kdtree_flann.h>
#include<vector>
求平均值
//float means_re(pcl::PointCloud<pcl::PointXYZ>::Ptr cloud)//參數為輸入點云
//{
//	float res = 0.0;//定義平均距離
//	int n_points = 0;//定義記錄點云數量
//	int nres;//定義鄰域查找數量
//	//vector是順序容器的一種。vector 是可變長的動態數組
//	std::vector<int> indices(2);//創建一個包含2個int類型數據的vector //創建一個動態數組,存儲查詢點近鄰索引 //等價于這兩行代碼 using std::vector; vector<int> indices(2);
//	std::vector<float> sqr_distances(2);//存儲近鄰點對應平方距離
//	pcl::KdTreeFLANN<pcl::PointXYZ> tree;//以k-d tree方式查找
//	tree.setInputCloud(cloud);
//
//	for (size_t i = 0; i < cloud->size(); ++i)//循環遍歷每一個點
//	{
//		if (!pcl_isfinite(cloud->points[i].x))//pcl_isfinite函數返回一個布爾值,檢查某個值是不是正常數值
//		{
//			continue;
//		}
//		//Considering the second neighbor since the first is the point itself.
//		// kdtree.nearestKSearch (searchPoint, K, pointIdxNKNSearch, pointNKNSquaredDistance) 
//		//這是執行 K 近鄰查找的成員函數(其中,當k為1的時候,就是最近鄰搜索。當k大于1的時候,就是多個最近鄰搜索,此處k為2)
//		//K為要搜索的鄰居數量(k the number of neighbors to search for)
//		nres = tree.nearestKSearch(i, 2, indices, sqr_distances);//函數返回值(返回找到的鄰域數量),return number of neighbors found
//		if (nres == 2)//如果為兩個點之間
//		{
//			res += sqrt(sqr_distances[1]);//sqrt()函數,返回sqr_distances[1]的開平方數
//			std::cout << "sqr_distances[1]:" << sqr_distances[1] << std::endl;//打印與臨近點距離的平方值
//			++n_points;
//		}
//	}
//	std::cout << "nres:" << nres << std::endl;
//	std::cout << "點云總數量n_points:" << n_points << std::endl;
//	if (n_points != 0)
//	{
//		res /= n_points;
//	}
//	std::cout <<"平均距離:"<<res <<std::endl;
//	return res;
//}
//求方差 平均距離 標準差
float variance(pcl::PointCloud<pcl::PointXYZ>::Ptr cloud)
{   float res = 0.0;//定義平均距離float var = 0.0;//定義方差float standard_deviation = 0.0;int n_points = 0;//定義記錄點云數量int nres;//定義鄰域查找數量//vector是順序容器的一種。vector 是可變長的動態數組std::vector<int> indices(2);//創建一個包含2個int類型數據的vector //創建一個動態數組,存儲查詢點近鄰索引 //等價于這兩行代碼 using std::vector; vector<int> indices(2);std::vector<float> sqr_distances(2);//存儲近鄰點對應平方距離pcl::KdTreeFLANN<pcl::PointXYZ> tree;//以k-d tree方式查找tree.setInputCloud(cloud);for (size_t i = 0; i < cloud->size(); ++i)//循環遍歷每一個點{if (!pcl_isfinite(cloud->points[i].x))//pcl_isfinite函數返回一個布爾值,檢查某個值是不是正常數值{continue;}//Considering the second neighbor since the first is the point itself.// kdtree.nearestKSearch (searchPoint, K, pointIdxNKNSearch, pointNKNSquaredDistance) //這是執行 K 近鄰查找的成員函數(其中,當k為1的時候,就是最近鄰搜索。當k大于1的時候,就是多個最近鄰搜索,此處k為2)//K為要搜索的鄰居數量(k the number of neighbors to search for)nres = tree.nearestKSearch(i, 2, indices, sqr_distances);//函數返回值(返回找到的鄰域數量),return number of neighbors foundif (nres == 2)//如果為兩個點之間{res += sqrt(sqr_distances[1]);//sqrt()函數,返回sqr_distances[1]的開平方數//std::cout << "sqr_distances[1]:" << sqr_distances[1] << std::endl;//打印與臨近點距離的平方值++n_points;}}std::cout << "nres:" << nres << std::endl;std::cout << "點云總數量n_points:" << n_points << std::endl;if (n_points != 0){res /= n_points;for (size_t i = 0; i < cloud->size(); ++i){if (!pcl_isfinite(cloud->points[i].x)){continue;}nres = tree.nearestKSearch(i, 2, indices, sqr_distances);if (nres == 2){var += pow(sqrt(sqr_distances[1]) - res, 2);++n_points;}}	if (n_points != 0){var /= n_points;standard_deviation = sqrt(var);}}std::cout << "平均距離:" << res << std::endl;std::cout << "方差:" << var << std::endl;std::cout << "標準差:" << standard_deviation << std::endl;return res;
}int main(int argc, char** argv)
{pcl::PointCloud<pcl::PointXYZ>::Ptr cloud(new pcl::PointCloud<pcl::PointXYZ>);//Laden der PCD-Files pcl::io::loadPCDFile(argv[1], *cloud);variance(cloud); return 0;}

在項目屬性-->調試-->命令參數-->輸入點云路徑即可。

結果如下:(針對于不同的搜索鄰域點數k的變化)

? ? ?

(此處可以看出,當K為1時,表明計算的自身距離,搜索點數為1,是自身)

計算方差,跟標準差結果如下:

(此處方差跟標準差的計算自己推導的應該是對的??如有不對可以指出)

?

總結

以上是生活随笔為你收集整理的计算点云之间的平均距离,方差,标准差的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。