Box Filter

盒滤波是一种低通滤波器,它通过将像素点周围的像素加权平均后得到生成像素值,常用来消除图片细节、去噪声、模糊边缘等。

算子效果

输入图像参数输出图像
imagekernelHeight = 3
kernelWidth = 3
pointLocX = -1
pointLocY = -1
normalize = 1
borderType = 0
image

原理

盒滤波使用以下滤波核,将滤波窗口内的像素值平均加权,计算得到输出图片的像素值:

boxm,n=1mn[111111111]\begin{aligned} box_{m,n}=\frac{1}{mn}\begin{bmatrix}1 & 1 & \cdots & 1\\1 & 1 & \cdots & 1\\\vdots & \vdots & \ddots & \vdots\\1 & 1 & \cdots & 1\\\end{bmatrix} \end{aligned}

其中 mmnn 为滤波核尺寸。

API接口

int32_t hbVPBoxFilter(hbUCPTaskHandle_t *taskHandle,
                      hbVPImage *dstImg,
                      hbVPImage const *srcImg,
                      hbVPBoxFilterParam const *boxParam);

详细接口信息请查看 hbVPBoxFilter

使用方法

// Include the header
#include "hobot/hb_ucp.h"
#include "hobot/vp/hb_vp.h"
#include "hobot/vp/hb_vp_box_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
hbVPBoxFilterParam box_filter_param;
box_filter_param.kernelHeight = 3;
box_filter_param.kernelWidth = 3;
box_filter_param.pointLocX = -1;
box_filter_param.pointLocY = -1;
box_filter_param.normalize = 1;
box_filter_param.borderType = HB_VP_BORDER_CONSTANT;

// 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
hbVPBoxFilter(&task_handle, &dst_img, &src_img, &box_filter_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);