PCL点云滤波:双边滤波BilateralFilter和适用场景

目录

PCL双边滤波使用和结果显示

双边滤波实际计算过程(定义)

双边滤波适用场景和使用体验


PCL双边滤波使用和结果显示

先用最简单的例子来说明PCL双边滤波如何使用


/*20201022 by 手口一斤*/
#include 
#include 
#include 
#include 
#include 

typedef pcl::PointXYZI PointT;

int
main(int argc, char* argv[])
{
	std::string incloudfile = "selfgen.pcd";
	std::string outcloudfile ="testfilterssb.pcd";
	float sigma_s = 1.0;
	float sigma_r = 1.0;

	// 读入点云文件
	pcl::PointCloud::Ptr cloud(new pcl::PointCloud);
	pcl::io::loadPCDFile(incloudfile.c_str(), *cloud);

	pcl::PointCloud outcloud;
	
	// 建立kdtree
	pcl::search::KdTree::Ptr tree(new pcl::search::KdTree);//用一个子类作为形参传入

	pcl::BilateralFilter bf;
	bf.setInputCloud(cloud);
	bf.setSearchMethod(tree);
	bf.setHalfSize(sigma_s);
	bf.setStdDev(sigma_r);
	bf.filter(outcloud);

    //结果点云显示
	pcl::PointCloud::ConstPtr testpoint(&outcloud);
	pcl::visualization::CloudViewer viewer("Cloud Viewer");
	viewer.showCloud(cloud,"cloud");
	viewer.showCloud(testpoint,"cloud filtered");
	while (!viewer.wasStopped())
	{
	}

	// 保存滤波输出点云文件
	pcl::io::savePCDFile(outcloudfile.c_str(), outcloud);
	return (0);
}

 "selfgen.pcd"这个文件可以为自己的pcd文件,如果没有自己的点云文件,也可以看https://blog.csdn.net/h649070/article/details/111286598,中的genwrite函数得到一个正方体内的随机点生成的点云;

其中,需要注意,如果需要显示结果,需要对点云格式进行转换pcl::PointCloud::ConstPtr testpoint(&outcloud);

双边滤波实际计算过程(定义)

如果不会调参数,需要看双边滤波的原理,代码如下:

#include 
 #include 
 #include 

 typedef pcl::PointXYZI PointT;

 float
 G (float x, float sigma)
 {
   return std::exp (- (x*x)/(2*sigma*sigma));
 }

 int
 main (int argc, char *argv[])
 {
   std::string incloudfile = argv[1];
   std::string outcloudfile = argv[2];
   float sigma_s = atof (argv[3]);
   float sigma_r = atof (argv[4]);

   // Load cloud
   pcl::PointCloud::Ptr cloud (new pcl::PointCloud);
   pcl::io::loadPCDFile (incloudfile.c_str (), *cloud);
   int pnumber = (int)cloud->size ();

   // Output Cloud = Input Cloud
   pcl::PointCloud outcloud = *cloud;

   // Set up KDTree
   pcl::KdTreeFLANN::Ptr tree (new pcl::KdTreeFLANN);
   tree->setInputCloud (cloud);

   // Neighbors containers
   std::vector k_indices;
   std::vector k_distances;

   // Main Loop
   for (int point_id = 0; point_id < pnumber; ++point_id)
   {
     float BF = 0;
     float W = 0;

     tree->radiusSearch (point_id, 2 * sigma_s, k_indices, k_distances);

     // For each neighbor
     for (std::size_t n_id = 0; n_id < k_indices.size (); ++n_id)
     {
       float id = k_indices.at (n_id);
       float dist = sqrt (k_distances.at (n_id));
       float intensity_dist = std::abs ((*cloud)[point_id].intensity - (*cloud)[id].intensity);

       float w_a = G (dist, sigma_s);
       float w_b = G (intensity_dist, sigma_r);
       float weight = w_a * w_b;

       BF += weight * (*cloud)[id].intensity;
       W += weight;
     }

     outcloud[point_id].intensity = BF / W;
   }

   // Save filtered output
   pcl::io::savePCDFile (outcloudfile.c_str (), outcloud);
   return (0);
 }

双边滤波适用场景和使用体验

一种非线性滤波,可以达到保持边缘的同时对图像进行平滑的效果;

经过实验,时间复杂度较高,大的点云不适合;

去噪效果效果需要根据实际点云情况,不是所有需要平滑和保留边缘的情况使用;

有两个参数需要调整,需要多次实验;

 

参考:https://pcl.readthedocs.io/projects/tutorials/en/latest/writing_new_classes.html#writing-new-classes

你可能感兴趣的:(PCL,算法,计算机视觉,c++,图像处理)