Erode

The erosion (corrosion) algorithm performs a 2D filtering operation on the input image using the provided 2D Boolean kernel. The Boolean kernel size defines the pixel neighborhood of the filtering operation. As a morphological operation operator, equivalent to a minimization operation, it reduces or shrinks the foreground area in the picture (in this case the white color in the figure below), the causes unwanted small or thin objects to disappear, recognizing and filling holes in the picture.

Operator Effect

Input ImageParameterOutput Image
image-image

Principle

dst(x,y)=min0i<ksize0j<ksizesrc(xksize12+i,yksize12+j)\begin{aligned}dst(x,y)=\min_{\begin{aligned} 0 \leqslant i<ksize \\ 0 \leqslant j < ksize\end{aligned}} src(x-\lfloor\frac{ksize-1}{2}\rfloor+i,y-\lfloor\frac{ksize-1}{2}\rfloor+j)\end{aligned}

The erosion filter is equivalent to the minimum operation and will shrink the bright structures in the image, another morphological filter is Dilate, which is equivalent to the maximum operation. Expansion filters may be used after erosion filters to form morphological closures of the image.

API Interface

int32_t hbVPErode(hbUCPTaskHandle_t *taskHandle, hbVPImage *dstImg, hbVPImage const *srcImg, hbVPFilterKernel const *erodeKernel, hbVPErodeParam const *erodeParam);

For detailed interface information, please refer to hbVPErode.

Usage

// Include the header #include "hobot/hb_ucp.h" #include "hobot/vp/hb_vp.h" #include "hobot/vp/hb_vp_erode.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 kernel hbUCPSysMem kernel_mem; hbUCPMallocCached(&kernel_mem, 3 * 3, 0); hbVPFilterKernel kernel; kernel.dataType = HB_VP_IMAGE_TYPE_U8C1; kernel.width = 3; kernel.height = 3; kernel.dataPhyAddr = kernel_mem.phyAddr; kernel.dataVirAddr = kernel_mem.virAddr; // init param hbVPErodeParam erode_param; erode_param.pointLocX = -1; erode_param.pointLocY = -1; erode_param.iterations = 1; erode_param.borderType = 0; erode_param.borderValue = 0; // 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 hbVPErode(&task_handle, &dst_img, &src_img, &kernel, &erode_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); hbUCPFree(&kernel_mem);