bmp图像插值算法近邻取样(效果最差,也最快),双线性插值(效果可以,速度一般),三次卷积插值(效果最好,速度最慢)

UNCHAR3   **   CContourJudge::ImgInterp(int   k,   float   imgfactor,   float   mode,UNCHAR3   **resimg)  
  {  
  int   i,j;  
  int   pos;  
  //UNCHAR3   **   reimg;  
  long   x,y;  
  long   newimgw,newimgh;  
  float   xScale,   yScale,   fX,   fY;  
  MYVOXEL   voxnode;  
  CMyPoint   tmpt,rgb1,rgb2,rgb3,rgb4,test;  
  newimgw   =   (long)(pDoc->xdim   *   imgfactor);  
  newimgh   =   (long)(pDoc->ydim   *   imgfactor);  
   
  xScale   =   (float)(pDoc->xdim)   /   (float)newimgw;  
  yScale   =   (float)(pDoc->ydim)   /   (float)newimgh;  
   
  for(i=0;   i<newimgh;   i++)  
  {  
  for(j=0;   j<newimgw;   j++)  
  {  
  resimg[i][j][0]   =   0;  
  resimg[i][j][1]   =   0;  
  resimg[i][j][2]   =   0;  
  }  
  }  
   
  //CContourListProc   m_cclistobj;  
   
  switch((long)mode)  
  {  
  case   1:     //nearest   neighbour  
  for(y=0;   y<newimgh;   y++)  
  {  
  fY   =   y   *   yScale;  
  for(x=0;   x<newimgw;   x++)  
  {  
  fX   =   x   *   xScale;  
   
  test.x   =   (double)fX;  
  test.y   =   (double)fY;  
  test.z   =   (double)k;  
  float   LayHeight=   (float)pDoc->layerthickness*k+pDoc->minz;  
   
   
  pos   =   pDoc->CompPos((long)fX,(long)fY,k);  
  voxnode   =   pDoc->myvoxarray[pos];  
  if(voxnode.flag   !=   255)  
  {  
  resimg[y][x][0]   =   (unsigned   char)   (255   *   voxnode.matvalue[0]);  
  resimg[y][x][1]   =   (unsigned   char)   (255   *   voxnode.matvalue[1]);  
  resimg[y][x][2]   =   (unsigned   char)   (255   *   voxnode.matvalue[2]);    
  }  
  else  
  {  
  resimg[y][x][0]   =   0;  
  resimg[y][x][1]   =   0;  
  resimg[y][x][2]   =   0;    
  }  

   
  }  
  }  
  break;  
  case   2:   //   bicubic   interpolation   by   Blake   L.   Carlson   <blake-carlson(at)uiowa(dot)edu  
  float   f_x,   f_y,   a,   b,   rr,   gg,   bb,   r1,   r2;  
  int       i_x,   i_y,   xx,   yy;  
  for(y=0;   y<newimgh;   y++)  
  {  
  f_y   =   (float)   y   *   yScale;  
  i_y   =   (int)   floor(f_y);  
  a       =   f_y   -   (float)floor(f_y);  
  for(x=0;   x<newimgw;   x++)  
  {  
  f_x   =   (float)   x   *   xScale;  
  i_x   =   (int)   floor(f_x);  
  b       =   f_x   -   (float)floor(f_x);  
   
  rr   =   gg   =   bb   =   0.0F ;  
  for(int   m=-1;   m<3;   m++)    
  {  
  r1   =   b3spline((float)   m   -   a);  
  for(int   n=-1;   n<3;   n++)    
  {  
  r2   =   b3spline( -1.0F *((float)n   -   b));    
  xx   =   i_x+n+2;  
  yy   =   i_y+m+2;  
  if   (xx<0)   xx=0;  
  if   (yy<0)   yy=0;  
  if   (xx>=pDoc->xdim)   xx=pDoc->xdim   -   1;  
  if   (yy>=pDoc->ydim)   yy=pDoc->ydim   -   1;  
   
  pos   =   pDoc->CompPos(xx,yy,k);  
   
  voxnode   =   pDoc->myvoxarray[pos];  
  if(voxnode.flag   !=   255)  
  {  
  tmpt.x   =   (unsigned   char)   (255   *   voxnode.matvalue[0]);  
  tmpt.y   =   (unsigned   char)   (255   *   voxnode.matvalue[1]);  
  tmpt.z   =   (unsigned   char)   (255   *   voxnode.matvalue[2]);  
   
  rr   +=   (float)tmpt.x   *   r1   *   r2;  
  gg   +=   (float)tmpt.y   *   r1   *   r2;  
  bb   +=   (float)tmpt.z   *   r1   *   r2;  
  }  
  }  
  }  
  resimg[y][x][0]   =   (unsigned   char)   rr;  
  resimg[y][x][1]   =   (unsigned   char)   gg;  
  resimg[y][x][2]   =   (unsigned   char)   bb;  
  }  
  }  
  break;  
  default:   //bilinear   interpolation  
  {  
  long   ifX,   ifY,   ifX1,   ifY1,   xmax,   ymax;  
  float   ir1,   ir2,   ig1,   ig2,   ib1,   ib2,   dx,   dy;  
  BYTE   r,g,b;  
   
  xmax   =   pDoc->xdim-1;  
  ymax   =   pDoc->ydim-1;  
  for(y=0;   y<newimgh-1;   y++){  
  fY   =   y   *   yScale;  
  ifY   =   (int)fY;  
  ifY1   =   min(ymax,   ifY+1);  
  dy   =   fY   -   ifY;  
  for(x=0;   x<newimgw-1;   x++){  
  fX   =   x   *   xScale;  
  ifX   =   (int)fX;  
  ifX1   =   min(xmax,   ifX+1);  
  dx   =   fX   -   ifX;  
   
  //   Interpolate   using   the   four   nearest   pixels   in   the   source  
  pos   =   pDoc->CompPos(ifX1,ifY1,k);  
  voxnode   =   pDoc->myvoxarray[pos];  
  rgb1.x   =   (unsigned   char)   (255   *   voxnode.matvalue[0]);  
  rgb1.y   =   (unsigned   char)   (255   *   voxnode.matvalue[1]);  
  rgb1.z   =   (unsigned   char)   (255   *   voxnode.matvalue[2]);  
   
  pos   =   pDoc->CompPos(ifX1+1,ifY1,k);  
  voxnode   =   pDoc->myvoxarray[pos];  
  rgb2.x   =   (unsigned   char)   (255   *   voxnode.matvalue[0]);  
  rgb2.y   =   (unsigned   char)   (255   *   voxnode.matvalue[1]);  
  rgb2.z   =   (unsigned   char)   (255   *   voxnode.matvalue[2]);  
   
  pos   =   pDoc->CompPos(ifX1,ifY1+1,k);  
  voxnode   =   pDoc->myvoxarray[pos];  
  rgb3.x   =   (unsigned   char)   (255   *   voxnode.matvalue[0]);  
  rgb3.y   =   (unsigned   char)   (255   *   voxnode.matvalue[1]);  
  rgb3.z   =   (unsigned   char)   (255   *   voxnode.matvalue[2]);  
   
  pos   =   pDoc->CompPos(ifX1+1,ifY1+1,k);  
  voxnode   =   pDoc->myvoxarray[pos];  
  rgb4.x   =   (unsigned   char)   (255   *   voxnode.matvalue[0]);  
  rgb4.y   =   (unsigned   char)   (255   *   voxnode.matvalue[1]);  
  rgb4.z   =   (unsigned   char)   (255   *   voxnode.matvalue[2]);  
   
  //   Interplate   in   x   direction:  
  ir1   =   (float)rgb1.x     *   (1   -   dy)   +   (float)rgb3.x   *   dy;  
  ig1   =   (float)rgb1.y     *   (1   -   dy)   +   (float)rgb3.y   *   dy;  
  ib1   =   (float)rgb1.z     *   (1   -   dy)   +   (float)rgb3.z   *   dy;  
  ir2   =   (float)rgb2.x     *   (1   -   dy)   +   (float)rgb4.x   *   dy;  
  ig2   =   (float)rgb2.y     *   (1   -   dy)   +   (float)rgb4.y   *   dy;  
  ib2   =   (float)rgb2.z     *   (1   -   dy)   +   (float)rgb4.z   *   dy;  
  //   Interpolate   in   y:  
  r   =   (BYTE)(ir1   *   (1   -   dx)   +   ir2   *   dx);  
  g   =   (BYTE)(ig1   *   (1   -   dx)   +   ig2   *   dx);  
  b   =   (BYTE)(ib1   *   (1   -   dx)   +   ib2   *   dx);  
  //   Set   output  
  resimg[y][x][0]   =   r;  
  resimg[y][x][1]   =   g;  
  resimg[y][x][2]   =   b;  
   
  }  
  }    
  }    
  break;  
  }  
   
  return   resimg;  
  }

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值