《PCL点云库学习VS2010(X64)》Part 23 快速双边滤波算法之三线插值

本文主要是介绍《PCL点云库学习VS2010(X64)》Part 23 快速双边滤波算法之三线插值,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

《PCL点云库学习&VS2010(X64)》Part 23 快速双边滤波算法之三线插值


一、源代码

       快速双边滤波算法的精华部分在3D网格分配、控制网格延伸范围、下采样和上采样线性插值三个主要部分。

核心代码:

#ifndef PCL_FILTERS_IMPL_FAST_BILATERAL_HPP_
#define PCL_FILTERS_IMPL_FAST_BILATERAL_HPP_#include <pcl/common/io.h>//
template <typename PointT> void
pcl::FastBilateralFilter<PointT>::applyFilter (PointCloud &output)
{if (!input_->isOrganized ()){PCL_ERROR ("[pcl::FastBilateralFilter] Input cloud needs to be organized.\n");return;}copyPointCloud (*input_, output);float base_max = -std::numeric_limits<float>::max (),base_min = std::numeric_limits<float>::max ();bool found_finite = false;for (size_t x = 0; x < output.width; ++x){for (size_t y = 0; y < output.height; ++y){if (pcl_isfinite (output (x, y).z)){if (base_max < output (x, y).z)base_max = output (x, y).z;if (base_min > output (x, y).z)base_min = output (x, y).z;found_finite = true;}}}if (!found_finite){PCL_WARN ("[pcl::FastBilateralFilter] Given an empty cloud. Doing nothing.\n");return;}for (size_t x = 0; x < output.width; ++x)for (size_t y = 0; y < output.height; ++y)if (!pcl_isfinite (output (x, y).z))output (x, y).z = base_max;const float base_delta = base_max - base_min;const size_t padding_xy = 2;const size_t padding_z  = 2;const size_t small_width  = static_cast<size_t> (static_cast<float> (input_->width  - 1) / sigma_s_) + 1 + 2 * padding_xy;const size_t small_height = static_cast<size_t> (static_cast<float> (input_->height - 1) / sigma_s_) + 1 + 2 * padding_xy;const size_t small_depth  = static_cast<size_t> (base_delta / sigma_r_)   + 1 + 2 * padding_z;Array3D data (small_width, small_height, small_depth);for (size_t x = 0; x < input_->width; ++x){const size_t small_x = static_cast<size_t> (static_cast<float> (x) / sigma_s_ + 0.5f) + padding_xy;for (size_t y = 0; y < input_->height; ++y){const float z = output (x,y).z - base_min;const size_t small_y = static_cast<size_t> (static_cast<float> (y) / sigma_s_ + 0.5f) + padding_xy;const size_t small_z = static_cast<size_t> (static_cast<float> (z) / sigma_r_ + 0.5f) + padding_z;Eigen::Vector2f& d = data (small_x, small_y, small_z);d[0] += output (x,y).z;d[1] += 1.0f;}}std::vector<long int> offset (3);offset[0] = &(data (1,0,0)) - &(data (0,0,0));offset[1] = &(data (0,1,0)) - &(data (0,0,0));offset[2] = &(data (0,0,1)) - &(data (0,0,0));Array3D buffer (small_width, small_height, small_depth);for (size_t dim = 0; dim < 3; ++dim){const long int off = offset[dim];for (size_t n_iter = 0; n_iter < 2; ++n_iter){std::swap (buffer, data);for(size_t x = 1; x < small_width - 1; ++x)for(size_t y = 1; y < small_height - 1; ++y){Eigen::Vector2f* d_ptr = &(data (x,y,1));Eigen::Vector2f* b_ptr = &(buffer (x,y,1));for(size_t z = 1; z < small_depth - 1; ++z, ++d_ptr, ++b_ptr)*d_ptr = (*(b_ptr - off) + *(b_ptr + off) + 2.0 * (*b_ptr)) / 4.0;}}}if (early_division_){for (std::vector<Eigen::Vector2f >::iterator d = data.begin (); d != data.end (); ++d)*d /= ((*d)[0] != 0) ? (*d)[1] : 1;for (size_t x = 0; x < input_->width; x++)for (size_t y = 0; y < input_->height; y++){const float z = output (x,y).z - base_min;const Eigen::Vector2f D = data.trilinear_interpolation (static_cast<float> (x) / sigma_s_ + padding_xy,static_cast<float> (y) / sigma_s_ + padding_xy,z / sigma_r_ + padding_z);output(x,y).z = D[0];}}else{for (size_t x = 0; x < input_->width; ++x)for (size_t y = 0; y < input_->height; ++y){const float z = output (x,y).z - base_min;const Eigen::Vector2f D = data.trilinear_interpolation (static_cast<float> (x) / sigma_s_ + padding_xy,static_cast<float> (y) / sigma_s_ + padding_xy,z / sigma_r_ + padding_z);output (x,y).z = D[0] / D[1];}}
}//
template <typename PointT> size_t
pcl::FastBilateralFilter<PointT>::Array3D::clamp (const size_t min_value,const size_t max_value,const size_t x)
{if (x >= min_value && x <= max_value){return x;}else if (x < min_value){return (min_value);}else{return (max_value);}
}//
template <typename PointT> Eigen::Vector2f
pcl::FastBilateralFilter<PointT>::Array3D::trilinear_interpolation (const float x,const float y,const float z)
{const size_t x_index  = clamp (0, x_dim_ - 1, static_cast<size_t> (x));const size_t xx_index = clamp (0, x_dim_ - 1, x_index + 1);const size_t y_index  = clamp (0, y_dim_ - 1, static_cast<size_t> (y));const size_t yy_index = clamp (0, y_dim_ - 1, y_index + 1);const size_t z_index  = clamp (0, z_dim_ - 1, static_cast<size_t> (z));const size_t zz_index = clamp (0, z_dim_ - 1, z_index + 1);const float x_alpha = x - static_cast<float> (x_index);const float y_alpha = y - static_cast<float> (y_index);const float z_alpha = z - static_cast<float> (z_index);return(1.0f-x_alpha) * (1.0f-y_alpha) * (1.0f-z_alpha) * (*this)(x_index, y_index, z_index) +x_alpha        * (1.0f-y_alpha) * (1.0f-z_alpha) * (*this)(xx_index, y_index, z_index) +(1.0f-x_alpha) * y_alpha        * (1.0f-z_alpha) * (*this)(x_index, yy_index, z_index) +x_alpha        * y_alpha        * (1.0f-z_alpha) * (*this)(xx_index, yy_index, z_index) +(1.0f-x_alpha) * (1.0f-y_alpha) * z_alpha        * (*this)(x_index, y_index, zz_index) +x_alpha        * (1.0f-y_alpha) * z_alpha        * (*this)(xx_index, y_index, zz_index) +(1.0f-x_alpha) * y_alpha        * z_alpha        * (*this)(x_index, yy_index, zz_index) +x_alpha        * y_alpha        * z_alpha        * (*this)(xx_index, yy_index, zz_index);
}#endif /* PCL_FILTERS_IMPL_FAST_BILATERAL_HPP_ */


二、代码分析——函数

根据上面的代码:主要有三个函数:

1、

template <typename PointT> void
pcl::FastBilateralFilter<PointT>::applyFilter (PointCloud &output)
2、

template <typename PointT> size_t
pcl::FastBilateralFilter<PointT>::Array3D::clamp (const size_t min_value,const size_t max_value,const size_t x)
3、

template <typename PointT> Eigen::Vector2f
pcl::FastBilateralFilter<PointT>::Array3D::trilinear_interpolation (const float x,const float y,const float z)


三、代码分析——关键代码

1、分配3D网格,其中的padding_xy和padding_z用来控制网格的延拓范围,代码如下:

 for (size_t x = 0; x < output.width; ++x)for (size_t y = 0; y < output.height; ++y)if (!pcl_isfinite (output (x, y).z))output (x, y).z = base_max;const float base_delta = base_max - base_min;const size_t padding_xy = 2;const size_t padding_z  = 2;const size_t small_width  = static_cast<size_t> (static_cast<float> (input_->width  - 1) / sigma_s_) + 1 + 2 * padding_xy;const size_t small_height = static_cast<size_t> (static_cast<float> (input_->height - 1) / sigma_s_) + 1 + 2 * padding_xy;const size_t small_depth  = static_cast<size_t> (base_delta / sigma_r_)   + 1 + 2 * padding_z;


2、对分割的网格进行下采样处理,其核心代码如下:

 Array3D data (small_width, small_height, small_depth);for (size_t x = 0; x < input_->width; ++x){const size_t small_x = static_cast<size_t> (static_cast<float> (x) / sigma_s_ + 0.5f) + padding_xy;for (size_t y = 0; y < input_->height; ++y){const float z = output (x,y).z - base_min;const size_t small_y = static_cast<size_t> (static_cast<float> (y) / sigma_s_ + 0.5f) + padding_xy;const size_t small_z = static_cast<size_t> (static_cast<float> (z) / sigma_r_ + 0.5f) + padding_z;Eigen::Vector2f& d = data (small_x, small_y, small_z);d[0] += output (x,y).z;d[1] += 1.0f;}}

3、其中的这段代码不是特别理解:

  std::vector<long int> offset (3);offset[0] = &(data (1,0,0)) - &(data (0,0,0));offset[1] = &(data (0,1,0)) - &(data (0,0,0));offset[2] = &(data (0,0,1)) - &(data (0,0,0));Array3D buffer (small_width, small_height, small_depth);for (size_t dim = 0; dim < 3; ++dim){const long int off = offset[dim];for (size_t n_iter = 0; n_iter < 2; ++n_iter){std::swap (buffer, data);for(size_t x = 1; x < small_width - 1; ++x)for(size_t y = 1; y < small_height - 1; ++y){Eigen::Vector2f* d_ptr = &(data (x,y,1));Eigen::Vector2f* b_ptr = &(buffer (x,y,1));for(size_t z = 1; z < small_depth - 1; ++z, ++d_ptr, ++b_ptr)*d_ptr = (*(b_ptr - off) + *(b_ptr + off) + 2.0 * (*b_ptr)) / 4.0;}}}


4、clamp函数:限制x的范围,插值过程中控制其size大小。代码如下:

template <typename PointT> size_t
pcl::FastBilateralFilter<PointT>::Array3D::clamp (const size_t min_value,const size_t max_value,const size_t x)
{if (x >= min_value && x <= max_value){return x;}else if (x < min_value){return (min_value);}else{return (max_value);}
}


5、三线插值:

      三线插值要先给出六面体的八个顶点;各个顶点的数值分别是:V000, V100, V010, ....V111
计算立方体里面的(x,y,z)的值Vxyz。


Vxyz = V000 (1 - x) (1 - y) (1 - z) +
            V100 x (1 - y) (1 - z) +
            V010 (1 - x) y (1 - z) +
            V001 (1 - x) (1 - y) z +
            V101 x (1 - y) z +
            V011 (1 - x) y z +
            V110 x y (1 - z) +
            V111 x y z

           对于更一般的情况就需要对各个维度进行一定的尺度变换操作。其示计算过程意图如下:



                   三线性插值的几何可视化。在期望点(黑色点)和整个体积值的乘积等于每个顶点处的值的乘积和对角处的部分体积的总和。

template <typename PointT> Eigen::Vector2f
pcl::FastBilateralFilter<PointT>::Array3D::trilinear_interpolation (const float x,const float y,const float z)
{const size_t x_index  = clamp (0, x_dim_ - 1, static_cast<size_t> (x));const size_t xx_index = clamp (0, x_dim_ - 1, x_index + 1);const size_t y_index  = clamp (0, y_dim_ - 1, static_cast<size_t> (y));const size_t yy_index = clamp (0, y_dim_ - 1, y_index + 1);const size_t z_index  = clamp (0, z_dim_ - 1, static_cast<size_t> (z));const size_t zz_index = clamp (0, z_dim_ - 1, z_index + 1);const float x_alpha = x - static_cast<float> (x_index);const float y_alpha = y - static_cast<float> (y_index);const float z_alpha = z - static_cast<float> (z_index);return(1.0f-x_alpha) * (1.0f-y_alpha) * (1.0f-z_alpha) * (*this)(x_index, y_index, z_index) +x_alpha        * (1.0f-y_alpha) * (1.0f-z_alpha) * (*this)(xx_index, y_index, z_index) +(1.0f-x_alpha) * y_alpha        * (1.0f-z_alpha) * (*this)(x_index, yy_index, z_index) +x_alpha        * y_alpha        * (1.0f-z_alpha) * (*this)(xx_index, yy_index, z_index) +(1.0f-x_alpha) * (1.0f-y_alpha) * z_alpha        * (*this)(x_index, y_index, zz_index) +x_alpha        * (1.0f-y_alpha) * z_alpha        * (*this)(xx_index, y_index, zz_index) +(1.0f-x_alpha) * y_alpha        * z_alpha        * (*this)(x_index, yy_index, zz_index) +x_alpha        * y_alpha        * z_alpha        * (*this)(xx_index, yy_index, zz_index);
}

四:《三维法线分布转换

The Three-Dimensional Normal-Distributions Transform

论文网址——p86-87





第一句说的很清楚,在空间细分为离散单元格后,单元格边缘处的曲面显示会不连续,所以由二维插值到三维插值的演变过程,出现了三线插值。

五、让该算法的加速计算可以利用线程技术,但是根据论文中的体积,插值过程是由卷积计算来完成的,当然插值的算法有很多,但是总体的思路很重要。


这篇关于《PCL点云库学习VS2010(X64)》Part 23 快速双边滤波算法之三线插值的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!


原文地址:
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.chinasem.cn/article/515320

相关文章

springboot+dubbo实现时间轮算法

《springboot+dubbo实现时间轮算法》时间轮是一种高效利用线程资源进行批量化调度的算法,本文主要介绍了springboot+dubbo实现时间轮算法,文中通过示例代码介绍的非常详细,对大家... 目录前言一、参数说明二、具体实现1、HashedwheelTimer2、createWheel3、n

使用Python实现快速搭建本地HTTP服务器

《使用Python实现快速搭建本地HTTP服务器》:本文主要介绍如何使用Python快速搭建本地HTTP服务器,轻松实现一键HTTP文件共享,同时结合二维码技术,让访问更简单,感兴趣的小伙伴可以了... 目录1. 概述2. 快速搭建 HTTP 文件共享服务2.1 核心思路2.2 代码实现2.3 代码解读3.

springboot security快速使用示例详解

《springbootsecurity快速使用示例详解》:本文主要介绍springbootsecurity快速使用示例,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝... 目录创www.chinasem.cn建spring boot项目生成脚手架配置依赖接口示例代码项目结构启用s

SpringBoot实现MD5加盐算法的示例代码

《SpringBoot实现MD5加盐算法的示例代码》加盐算法是一种用于增强密码安全性的技术,本文主要介绍了SpringBoot实现MD5加盐算法的示例代码,文中通过示例代码介绍的非常详细,对大家的学习... 目录一、什么是加盐算法二、如何实现加盐算法2.1 加盐算法代码实现2.2 注册页面中进行密码加盐2.

Java时间轮调度算法的代码实现

《Java时间轮调度算法的代码实现》时间轮是一种高效的定时调度算法,主要用于管理延时任务或周期性任务,它通过一个环形数组(时间轮)和指针来实现,将大量定时任务分摊到固定的时间槽中,极大地降低了时间复杂... 目录1、简述2、时间轮的原理3. 时间轮的实现步骤3.1 定义时间槽3.2 定义时间轮3.3 使用时

C++快速排序超详细讲解

《C++快速排序超详细讲解》快速排序是一种高效的排序算法,通过分治法将数组划分为两部分,递归排序,直到整个数组有序,通过代码解析和示例,详细解释了快速排序的工作原理和实现过程,需要的朋友可以参考下... 目录一、快速排序原理二、快速排序标准代码三、代码解析四、使用while循环的快速排序1.代码代码1.由快

Java进阶学习之如何开启远程调式

《Java进阶学习之如何开启远程调式》Java开发中的远程调试是一项至关重要的技能,特别是在处理生产环境的问题或者协作开发时,:本文主要介绍Java进阶学习之如何开启远程调式的相关资料,需要的朋友... 目录概述Java远程调试的开启与底层原理开启Java远程调试底层原理JVM参数总结&nbsMbKKXJx

Win32下C++实现快速获取硬盘分区信息

《Win32下C++实现快速获取硬盘分区信息》这篇文章主要为大家详细介绍了Win32下C++如何实现快速获取硬盘分区信息,文中的示例代码讲解详细,感兴趣的小伙伴可以跟随小编一起学习一下... 实现代码CDiskDriveUtils.h#pragma once #include <wtypesbase

如何通过Golang的container/list实现LRU缓存算法

《如何通过Golang的container/list实现LRU缓存算法》文章介绍了Go语言中container/list包实现的双向链表,并探讨了如何使用链表实现LRU缓存,LRU缓存通过维护一个双向... 目录力扣:146. LRU 缓存主要结构 List 和 Element常用方法1. 初始化链表2.

Spring AI与DeepSeek实战一之快速打造智能对话应用

《SpringAI与DeepSeek实战一之快速打造智能对话应用》本文详细介绍了如何通过SpringAI框架集成DeepSeek大模型,实现普通对话和流式对话功能,步骤包括申请API-KEY、项目搭... 目录一、概述二、申请DeepSeek的API-KEY三、项目搭建3.1. 开发环境要求3.2. mav