Bilateral Filter

Bilinear filter is a nonlinear, edge-preserving smoothing filter that is often used to smooth pictures. It takes the weighted average of the intensity values of nearby pixels in each input image, and it calculates the weights by the Euclidean distance between pixels and color intensity differences, and finally obtains the picture processing effect of preserving the edges and smoothing the similar regions.

Operator Effect

Input ImageParameterOutput Image
imagekernelSize = 5
sigmaColor = 15.0f
sigmaSpace = 2.0f
borderType = HB_VP_BORDER_REPLICATE
image

Principle

The main computational process of bilinear filtering is to convolve the input picture using a filter kernel, the main equation is as follows:

dst(x)=1xiΩfr(x,xi)gs(x,xi)xiΩsrc(xi)fr(x,xi)gs(x,xi)dst(x)=\frac{1}{\sum_{x_i\in\Omega}{f_r(x,xi)g_s(x,xi)}}\displaystyle\sum_{x_i\in\Omega}{src(x_i)f_r(x,xi)g_s(x,xi)}

Among them, dstdst is the output image, srcsrc is the input image, xx is the currently computed pixel, and Ω\Omega is the current filter window.

fr(x,xi)=e12(src(xi)src(x)σr)2f_r(x,xi)=e^{-\frac{1}{2}(\frac{\lvert src(xi)-src(x)\rvert}{\sigma_r})^2}

gs(x,xi)=e12(xixσd)2g_s(x,xi)=e^{-\frac{1}{2}(\frac{\lVert xi-x \rVert}{\sigma_d})^2}

Among them, \vert is the absolute value, \Vert is the Euclidean distance, σr\sigma_r is the color sigma parameter for smoothing intensity differences across the filter window, and σd\sigma_d is the spatial sigma parameter for smoothing differences in spatial coordinates across the window.

API Interface

int32_t hbVPBilateralFilter(hbUCPTaskHandle_t *taskHandle, hbVPImage *dstImg, hbVPImage const *srcImg, hbVPBilateralFilterParam const *bilateralParam);

For detailed interface information, please refer to hbVPBilateralFilter.

Usage

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