Sobel

The sobel operator is used to obtain a first order gradient image of a digital image and a common application is edge detection. The algorithm is based on the weighted difference of the gray values of the four fields of each pixel in the image, up, down, left and right, to reach the extreme values at the edges thus detecting the edges.

Operator Effect

Input ImageParameterOutput Image
imagekernel=[101202101]\begin{aligned}kernel = \begin{bmatrix}-1 & 0 & 1 \\ -2 & 0 & 2 \\ -1 & 0 & 1 \end{bmatrix}\end{aligned}image

Principle

The 3X3 convolution kernel is shown below, which is only applicable to gradient computation in one direction. The first convolution kernel applies to the convolution in the X direction, when the difference between the pixel values to the left and to the right of the pixel point is large, the convolution results in a larger target pixel value, i.e., a convex outline. The second convolution kernel is used for convolution in the Y direction, and the principle is the same.

kernelXdirection=[101202101]kernelYdirection=[121000121]\begin{aligned}&kernel\quad X\quad direction = \begin{bmatrix}-1 & 0 & 1\\ -2 & 0 & 2\\ -1 & 0 & 1 \end{bmatrix}\\ &kernel\quad Y\quad direction = \begin{bmatrix} -1 & -2 & -1 \\ 0 & 0 & 0 \\ 1 & 2 & 1 \end{bmatrix}\end{aligned}

API Interface

int32_t hbVPSobel(hbUCPTaskHandle_t *taskHandle, hbVPImage *dstImg, hbVPImage const *srcImg, hbVPSobelParam const *sobelParam);

For detailed interface information, please refer to hbVPSobel.

Usage

// Include the header #include "hobot/hb_ucp.h" #include "hobot/vp/hb_vp.h" #include "hobot/vp/hb_vp_sobel.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_S16C1, dst_width, dst_height, dst_stride, dst_mem.virAddr, dst_mem.phyAddr, nullptr, 0, 0}; // init param hbVPSobelParam sobel_param; sobel_param.scale = 1; sobel_param.delta = 0; sobel_param.dx = 1; sobel_param.dy = 0; sobel_param.kernelSize = 3; sobel_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 hbVPSobel(&task_handle, &dst_img, &src_img, &sobel_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);