> Unify Compute Platform (UCP) > Vision Process > Vision Algorithm > Resize
The Resize operator is used to resize the picture by interpolating the algorithm and scaling the picture to the target size according to the user's needs.
Operator Effect
Principle
The operator is formulated as follows, with different implementation details depending on the interpolation algorithm:
{ d s t w i d t h = x s c a l e ∗ s r c w i d t h d s t h e i g h t = y s c a l e ∗ s r c h e i g h t \begin{aligned}\begin{cases}dst_{width}=xscale*src_{width}\\dst_{height}=yscale*src_{height}\end{cases}\end{aligned} { d s t w i d t h = x sc a l e ∗ sr c w i d t h d s t h e i g h t = y sc a l e ∗ sr c h e i g h t
For the bilinear interpolation method, the pixel mapping principle is as follows:
d s t ( x , y ) = Q ∗ ( 1 − b ) + R ∗ b dst(x,y)=Q*(1-b)+R*b d s t ( x , y ) = Q ∗ ( 1 − b ) + R ∗ b
Q = s r c ( m , n ) ∗ ( 1 − a ) + s r c ( m , n + 1 ) ∗ a Q=src(m,n)*(1-a)+src(m,n+1)*a Q = sr c ( m , n ) ∗ ( 1 − a ) + sr c ( m , n + 1 ) ∗ a
R = s r c ( m + 1 , n ) ∗ ( 1 − a ) + s r c ( m + 1 , n + 1 ) ∗ a R=src(m+1,n)*(1-a)+src(m+1,n+1)*a R = sr c ( m + 1 , n ) ∗ ( 1 − a ) + sr c ( m + 1 , n + 1 ) ∗ a
Among them, s r c src sr c is the input image, d s t dst d s t is the output image, and m , n , x , y m,n,x,y m , n , x , y are the pixel point coordinates.
For the nearest neighbor interpolation method, the pixel mapping principle is as follows:
{ s r c x = x s c a l e ∗ d s t x s r c y = y s c a l e ∗ d s t y \begin{aligned}\begin{cases}src_x=xscale*dst_x\\src_y=yscale*dst_y\end{cases}\end{aligned} { sr c x = x sc a l e ∗ d s t x sr c y = y sc a l e ∗ d s t y
Among them, x , y x,y x , y are the coordinates of the picture.
API Interface
int32_t hbVPResize ( hbUCPTaskHandle_t * taskHandle ,
hbVPImage * dstImg ,
hbVPImage const * srcImg ,
hbVPInterpolationType interpolation);
For detailed interface information, please refer to hbVPResize .
Usage
// 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);