RGB与HSI空间相互转换

void HSItrans(char tp)
{
/*
** input: tp
** tp--varible of char type,it is used to judge whether HSI-RGB or RGB-HSI.
** h:[0,360]
** s:[0,1]
** I:[0,1]

** r:[0,255]
** g:[0,255]
** b:[0,255]
*/

 if (myHSIArray==NULL) myHSIArray= new sHSI[myPHeight*myPWidth];

 double tH,tS,fR,fG,fB;
 BYTE R,G,B,tI;
 int i,j;

 
 switch (tp)
 {
 case 't'://RGB-HSI
  for(i=0; i<(int)myPHeight; i++)
  {
   for (j=0; j<(int)myPWidth; j++)
   {    
    R=GetRValue(myPArray[i*myPWidth+j]);
    G=GetGValue(myPArray[i*myPWidth+j]);
    B=GetBValue(myPArray[i*myPWidth+j]);

    int  col1,col2,angle;
    int  mx,mn;

    mx=max(max(R,G),B);
    mn=min(min(R,G),B); 
    if (mx==mn)
    {
      myHSIArray[i*myPWidth+j].H=0;   
      myHSIArray[i*myPWidth+j].S=0;
    }
    else {
      if (mn==B) {
       angle=0;    col1=G;    col2=R;
      }
      else if (mn==R) {
       angle=120;  col1=B;    col2=G;
      }
      else if (mn==G) {
       angle=240;  col1=R;    col2=B;
      }
      myHSIArray[i*myPWidth+j].H=calchue(angle,col1,col2,mn);
      myHSIArray[i*myPWidth+j].S=(/*255.0**/float(mx-mn)/float(mx));
     } 
    myHSIArray[i*myPWidth+j].I=mx;
   }
  }
  break;

 case 'v':    //con't  HSI-RGB
  for(i=0; i<(int)myPHeight; i++)
  {
   for (j=0; j<(int)myPWidth; j++)
   {

    tH=myHSIArray[i*myPWidth+j].H;
    tS=255.0*myHSIArray[i*myPWidth+j].S;
    tI=myHSIArray[i*myPWidth+j].I;

    int min,max;
    tH=(360.0f*tH/252.0f);
    max=tI;

    if (tS==0) {
     R=G=B=max;
    }
    else {
     min=(int)((255.0-tS)*max/255.0);
     if (tH<=120) {
      B=min;
      if (tH<=60) {
       R=max;
       G=(int)(min+(double)tH*(max-min)/(120.0-tH));
      }
      else {
       G=max;
       R=(int)(min+(120.0-tH)*(max-min)/tH);
      }
     }
     else if (tH<=240) {
      R=min;
      if (tH<=180) {
       G=max;
       B=(int)(min+(tH-120.0)*(max-min)/(240.0-tH));
      }
      else {
       B=max;
       G=(int)(min+(240.0-tH)*(max-min)/(tH-120.0));
      }
     }
     else {
      G=min;
      if (tH<=300) {
       B=max;
       R=(int)(min+(tH-240.0)*(max-min)/(360.0-tH));
      }
      else {
       R=max;
       B=(int)(min+(360.0-tH)*(max-min)/(tH-240.0));
      }
      }
    myPArray[i*myPWidth+j]=RGB(R,G,B);    
    }

    
   }
  }
  break;
 }
 
}

int calchue(int angle, int col1, int col2, int min)
{
 int  rcode;
 
 rcode=(int) (252.0*(angle+((120.0)*(col1-min)/
  (col1+col2-(min+min))))/360.0);
   return(rcode);
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值