Threshold

阈值函数,通过不同的阈值方法对图像进行处理。该函数将固定级别的阈值应用于多通道数组,通常用于从灰度图像中获取二值图像或用于去除噪声,即过滤掉值过小或过大的像素。 该函数当前支持HB_VP_THRESH_TOZERO类型的阈值。

算子效果

输入图像参数输出图像
imagetype = HB_VP_THRESH_TOZERO
thresh = 0
maxVal = 0
image

原理

不同的阈值类型有不同的处理效果,其原理如下:

VP5threshold1

原理表达公式如下:

THRESHBINARY:dst(x,y)={maxvalifsrc(x,y)>thresh0otherwiseTHRESHBINARYINV:dst(x,y)={0ifsrc(x,y)>threshmaxvalotherwiseTHRESHTRUNC:dst(x,y)={thresholdifsrc(x,y)>threshsrc(x,y)otherwiseTHRESHTOZERO:dst(x,y)={src(x,y)ifsrc(x,y)>thresh0otherwiseTHRESHTOZEROINV:dst(x,y)={0ifsrc(x,y)>threshsrc(x,y)otherwise\begin{aligned}\begin{aligned}THRESH\quad BINARY : \quad dst(x,y)&=\begin{cases} maxval & if src(x,y)>thresh \\ 0 & otherwise \end{cases} \\THRESH\quad BINARY\quad INV : \quad dst(x,y)&=\begin{cases} 0 & if src(x,y)>thresh \\ maxval & otherwise \end{cases} \\THRESH\quad TRUNC : \quad dst(x,y)&=\begin{cases} threshold & if src(x,y)>thresh \\ src(x,y) & otherwise \end{cases} \\THRESH\quad TOZERO: \quad dst(x,y)&=\begin{cases} src(x,y) & if src(x,y)>thresh \\ 0 & otherwise \end{cases} \\THRESH\quad TOZERO\quad INV: \quad dst(x,y)&=\begin{cases} 0 & if src(x,y)>thresh \\ src(x,y) & otherwise \end{cases}\end{aligned}\end{aligned}

API接口

int32_t hbVPThreshold(hbUCPTaskHandle_t *taskHandle, hbVPImage *dstImg, hbVPImage const *srcImg, hbVPThresholdParam const *thresholdParam);

详细接口信息请查看 hbVPThreshold

使用方法

// Include the header #include "hobot/hb_ucp.h" #include "hobot/vp/hb_vp.h" #include "hobot/vp/hb_vp_threshold.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_width, 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 hbVPThresholdParam threshold_param; threshold_param.thresh = 0; threshold_param.maxVal = 0; threshold_param.type = HB_VP_THRESH_TOZERO; // 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 hbVPThreshold(&task_handle, &dst_img, &src_img, &threshold_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);