> Unify Compute Platform (UCP) > Vision Process > Vision Algorithm > Threshold
Thresholding function to process the image by different thresholding methods.
This function applies a fixed level of thresholding to a multi-channel array.
It is typically used to obtain a binary image from a grayscale image or for noise removal, i.e., filtering out pixels with values that are too small or too large.
This function currently supports the HB_VP_THRESH_TOZERO type of threshold.
Operator Effect
Principle
Different threshold types have different processing effects, which are based on the following principles:
The principle expression formula is as follows:
T H R E S H B I N A R Y : d s t ( x , y ) = { m a x v a l i f s r c ( x , y ) > t h r e s h 0 o t h e r w i s e T H R E S H B I N A R Y I N V : d s t ( x , y ) = { 0 i f s r c ( x , y ) > t h r e s h m a x v a l o t h e r w i s e T H R E S H T R U N C : d s t ( x , y ) = { t h r e s h o l d i f s r c ( x , y ) > t h r e s h s r c ( x , y ) o t h e r w i s e T H R E S H T O Z E R O : d s t ( x , y ) = { s r c ( x , y ) i f s r c ( x , y ) > t h r e s h 0 o t h e r w i s e T H R E S H T O Z E R O I N V : d s t ( x , y ) = { 0 i f s r c ( x , y ) > t h r e s h s r c ( x , y ) o t h e r w i s e \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} T H R E S H B I N A R Y : d s t ( x , y ) T H R E S H B I N A R Y I N V : d s t ( x , y ) T H R E S H T R U N C : d s t ( x , y ) T H R E S H T O Z E R O : d s t ( x , y ) T H R E S H T O Z E R O I N V : d s t ( x , y ) = { ma xv a l 0 i f sr c ( x , y ) > t h r es h o t h er w i se = { 0 ma xv a l i f sr c ( x , y ) > t h r es h o t h er w i se = { t h r es h o l d sr c ( x , y ) i f sr c ( x , y ) > t h r es h o t h er w i se = { sr c ( x , y ) 0 i f sr c ( x , y ) > t h r es h o t h er w i se = { 0 sr c ( x , y ) i f sr c ( x , y ) > t h r es h o t h er w i se
API Interface
int32_t hbVPThreshold ( hbUCPTaskHandle_t * taskHandle ,
hbVPImage * dstImg ,
hbVPImage const * srcImg ,
hbVPThresholdParam const * thresholdParam);
For detailed interface information, please refer to hbVPThreshold .
Usage
// 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_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
hbVPThresholdParam threshold_param;
threshold_param.thresh = 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);