Bilateral Filter
双线性滤波是一种非线性、边缘保留的平滑滤波器,通常来对图片进行平滑处理。它将每个输入图像中附近像素的强度值加权平均值, 并通过像素间的欧式距离和颜色强度差异计算权重,最终得到保留边缘并平滑相似区域的图片处理效果。
算子效果
| 输入图像 | 参数 | 输出图像 |
|---|
 | kernelSize = 5 sigmaColor = 15.0f sigmaSpace = 2.0f borderType = HB_VP_BORDER_REPLICATE |  |
原理
双线性滤波的主要计算过程为使用滤波核对输入图片进行卷积,主体公式如下:
dst(x)=∑xi∈Ωfr(x,xi)gs(x,xi)1xi∈Ω∑src(xi)fr(x,xi)gs(x,xi)
其中, dst 是输出图片,src 是输入图片,x 是当前计算的像素,Ω 是当前滤波窗口。
fr(x,xi)=e−21(σr∣src(xi)−src(x)∣)2
gs(x,xi)=e−21(σd∥xi−x∥)2
其中,∣ 为绝对值,∥ 为欧氏距离,σr 为颜色sigma参数,用于平滑滤波器窗口的强度差异,σd 为空间sigma参数,用于平滑跨窗口的空间坐标差异。
API接口
int32_t hbVPBilateralFilter(hbUCPTaskHandle_t *taskHandle,
hbVPImage *dstImg,
hbVPImage const *srcImg,
hbVPBilateralFilterParam const *bilateralParam);
详细接口信息请查看 hbVPBilateralFilter 。
使用方法
// Include the header
#include "hobot/hb_ucp.h"
#include "hobot/vp/hb_vp.h"
#include "hobot/vp/hb_vp_bilateral_filter.h"
// init Image, allocate memory for image data
hbUCPSysMem src_mem;
hbUCPMallocCached(&src_mem, src_stride * src_height, 0);
hbVPImage src_img{HB_VP_IMAGE_FORMAT_Y,
HB_VP_IMAGE_TYPE_U8C1,
src_width,
src_height,
src_stride,
src_mem.virAddr,
src_mem.phyAddr,
nullptr,
0,
0};
hbUCPSysMem dst_mem;
hbUCPMallocCached(&dst_mem, dst_stride * dst_height, 0);
hbVPImage dst_img{HB_VP_IMAGE_FORMAT_Y,
HB_VP_IMAGE_TYPE_U8C1,
dst_width,
dst_height,
dst_stride,
dst_mem.virAddr,
dst_mem.phyAddr,
nullptr,
0,
0};
// init param
hbVPBilateralFilterParam vp_param;
vp_param.sigmaColor = 20.0f;
vp_param.sigmaSpace = 5.0f;
vp_param.kernelSize = 5;
vp_param.borderType = HB_VP_BORDER_REPLICATE;
// init task handle and schedule param
hbUCPTaskHandle_t task_handle{nullptr};
hbUCPSchedParam sched_param;
HB_UCP_INITIALIZE_SCHED_PARAM(&sched_param);
sched_param.backend = HB_UCP_DSP_CORE_0;
// create task
hbVPBilateralFilter(&task_handle, &dst_img, &src_img, &vp_param);
// submit task
hbUCPSubmitTask(task_handle, &sched_param);
// wait for task done
hbUCPWaitTaskDone(task_handle, 0);
// release task handle
hbUCPReleaseTask(task_handle);
// release memory
hbUCPFree(&src_mem);
hbUCPFree(&dst_mem);