sobel算子用于获得数字图像的一阶梯度图像,常见的应用是边缘检测。算子根据图像中每个像素的上下左右四领域的灰度值加权差,在边缘处达到极值从而检测边缘的目的。
| 输入图像 | 参数 | 输出图像 |
|---|---|---|
![]() | kernel=−1−2−1000121 | ![]() |
3X3卷积核如下所示,其只适用于一个方向上的梯度计算。第一个卷积核适用于X方向上的卷积,当像素点左侧和右侧像素值差距较大时, 卷积后才会得到较大的目标像素值,即凸显的轮廓。第二个卷积核适用于Y方向上的卷积,原理相同。
kernelXdirection=−1−2−1000121kernelYdirection=−101−202−101
int32_t hbVPSobel(hbUCPTaskHandle_t *taskHandle,
hbVPImage *dstImg,
hbVPImage const *srcImg,
hbVPSobelParam const *sobelParam);
详细接口信息请查看 hbVPSobel。
// 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);

