Resize

Resize算子通过插值算法,将图片按照需求缩放到目标尺寸,用以调整图片的大小。

算子效果

输入图像参数输出图像
imagexscale = 1.5
yscale = 1.5
interp = HB_VP_INTER_LINEAR
image

原理

算子公式如下,根据不同的插值算法有不同的实现细节:

{dstwidth=xscalesrcwidthdstheight=yscalesrcheight\begin{aligned}\begin{cases}dst_{width}=xscale*src_{width}\\dst_{height}=yscale*src_{height}\end{cases}\end{aligned}

对双线性插值方法,其像素映射原理如下:

image

dst(x,y)=Q(1b)+Rbdst(x,y)=Q*(1-b)+R*b

Q=src(m,n)(1a)+src(m,n+1)aQ=src(m,n)*(1-a)+src(m,n+1)*a

R=src(m+1,n)(1a)+src(m+1,n+1)aR=src(m+1,n)*(1-a)+src(m+1,n+1)*a

其中,srcsrc 为输入图片,dstdst 为输出图片,m,n,x,ym,n,x,y 为像素点坐标。

对最邻近插值方法,其像素映射原理如下:

{srcx=xscaledstxsrcy=yscaledsty\begin{aligned}\begin{cases}src_x=xscale*dst_x\\src_y=yscale*dst_y\end{cases}\end{aligned}

其中,x,yx,y 为图片的坐标。

API接口

int32_t hbVPResize(hbUCPTaskHandle_t *taskHandle, 
                   hbVPImage *dstImg, 
                   hbVPImage const *srcImg,
                   hbVPInterpolationType interpolation);

详细接口信息请查看 hbVPResize

使用方法

// Include the header
#include "hobot/hb_ucp.h"
#include "hobot/vp/hb_vp.h"
#include "hobot/vp/hb_vp_resize.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
hbVPInterpolationType interp{HB_VP_INTER_LINEAR};

// 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
hbVPResize(&task_handle, &dst_img, &src_img, interp);

// 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);