Median Blur

Median filtering performs a two-dimensional filtering operation on the input image using the provided 2D filter kernel, the size of which is the pixel neighborhood range used for the filtering operation. The filter performs a nonlinear operation, computed by weighting the input pixel values according to the filter kernel weights and taking the median value as the output. Median filtering operations are often used for impulse noise reduction, image smoothing, analysis, and so on.

Operator Effect

Input ImageParameterOutput Image
image-image

Principle

The median filtering formula is as follows:

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 is the output image, srcsrc is the input image, KK is the filter kernel, and kw,khk_w, k_h are the width and height of the filter kernel. The specific calculation process is shown as belows:

image

One of the 5X5 kind of filter kernel is shown below:

[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 Interface

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

For detailed interface information, please refer to hbVPMedianBlur.

Usage

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