Median Blur

中值滤波使用提供的2D滤波核对输入图像执行二维滤波操作,滤波核的尺寸即为滤波操作使用的像素邻域范围。 过滤器执行非线性操作,计算方式为按照滤波核权重把输入像素值加权计算,并取中值作为输出的结果。中值滤波操作常被用于脉冲降噪、图像平滑、分析等。

算子效果

输入图像参数输出图像
image-image

原理

中值滤波计算公式如下:

dst(x,y)=medianm[0,kh1]n[0,kw1]{K[m,n]src[x(nw/2),y(mkh/2)]}dst(x,y)= \operatorname*{median}\limits_{\begin{aligned}m\in&[0,k_h-1]\\n\in&[0,k_w-1]\end{aligned}}\{K[m,n]*src[x-(n-\lfloor_w/2 \rfloor),y-(m-\lfloor k_h/2\rfloor)] \}

其中,dstdst 为输出图片,srcsrc 为输入图片,KK 为滤波核,kw,khk_w, k_h 为滤波核的宽和高。 具体计算过程如图所示:

image

其中5X5的一种滤波核如下:

[0010000100111110010000100]\begin{aligned}\begin{bmatrix}0 & 0 & 1 & 0 & 0 \\ 0 & 0 & 1 & 0 & 0 \\ 1 & 1 & 1 & 1 & 1 \\ 0 & 0 & 1 & 0 & 0 \\ 0 & 0 & 1 & 0 & 0 \end{bmatrix}\end{aligned}

API接口

int32_t hbVPMedianBlur(hbUCPTaskHandle_t *taskHandle,
                       hbVPImage *dstImg,
                       hbVPImage const *srcImg,
                       int8_t maskWidth);

详细接口信息请查看 hbVPMedianBlur

使用方法

// Include the header
#include "hobot/hb_ucp.h"
#include "hobot/vp/hb_vp.h"
#include "hobot/vp/hb_vp_median_blur.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
int8_t mask_width{5};

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

// 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);