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 Image | Parameter | Output Image |
|---|
 | kernelSize = 5 sigmaColor = 15.0f sigmaSpace = 2.0f borderType = HB_VP_BORDER_REPLICATE |  |
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)=∑xi∈Ωfr(x,xi)gs(x,xi)1xi∈Ω∑src(xi)fr(x,xi)gs(x,xi)
Among them, dst is the output image, src is the input image, x is the currently computed pixel, and Ω is the current filter window.
fr(x,xi)=e−21(σr∣src(xi)−src(x)∣)2
gs(x,xi)=e−21(σd∥xi−x∥)2
Among them, ∣ is the absolute value, ∥ is the Euclidean distance, σr is the color sigma parameter for smoothing intensity differences across the filter window,
and σ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);