中值滤波使用提供的2D滤波核对输入图像执行二维滤波操作,滤波核的尺寸即为滤波操作使用的像素邻域范围。 过滤器执行非线性操作,计算方式为按照滤波核权重把输入像素值加权计算,并取中值作为输出的结果。中值滤波操作常被用于脉冲降噪、图像平滑、分析等。
| 输入图像 | 参数 | 输出图像 |
|---|---|---|
![]() | - | ![]() |
中值滤波计算公式如下:
dst(x,y)=m∈n∈[0,kh−1][0,kw−1]median{K[m,n]∗src[x−(n−⌊w/2⌋),y−(m−⌊kh/2⌋)]}其中,dst 为输出图片,src 为输入图片,K 为滤波核,kw,kh 为滤波核的宽和高。 具体计算过程如图所示:

其中5X5的一种滤波核如下:
0010000100111110010000100int32_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);

