RGB通过转换成HSV增加明度和饱和度

RGB图像对亮度和饱和度的表示不是很直观。而HSV空间图像却可以很直观的表现出每个像素的亮度和饱和度

所以先将RGB图像转换为HSV图像,然后调整亮度和饱和度,最后将HSV图像转换为RGB图像。

参考公式:http://en.wikipedia.org/wiki/HSL_color_space


activity的代码:

public void remove(View v){
        Bitmap bitmap=BitmapFactory.decodeResource(getResources(),R.drawable.cc);
           int width = bitmap.getWidth();  
        int height = bitmap.getHeight();
        int[] inpixel=new int[width*height];
        int[] outpixel=new int[width*height];
        bitmap.getPixels(inpixel, 0, width, 0, 0, width, height);
        for(int y=0;y<height;y++){
           for(int x=0;x<width;x++){
               int a=inpixel[y*width+x]>>24&0xff;
               int r=inpixel[y*width+x]>>16&0xff;
               int g=inpixel[y*width+x]>>8&0xff;
               int b=inpixel[y*width+x]&0xff;
               
               //from RGB to HSL
               float H=0,S=0,V=0;
               float cMax=255.0f;
               float high=Math.max(r, Math.max(g,b));
               float low=Math.min(r, Math.min(g,b));
               float rng=high-low;
               //计算明度v
               V=high/cMax ;
               //计算饱和度S
               if(high>0){
                  S=(float)rng/high;
               }
               //计算色调H
               
               if(rng>0){
                   float rr=(float)(high-r)/rng;
                   float gg=(float)(high-g)/rng;
                   float bb=(float)(high-b)/rng;
                   float hh=0;
                   if(r==high){
                       hh=bb-gg;
                   }else if(g==high){
                       hh=rr-bb+2;
                   }else if(b==high){
                       hh=gg-rr+4;
                   }
                   
                   if(hh<0){
                       H=(hh+6)/6;
                   }else{
                       H=hh/6;
                   }
               }
              
               //application HSL
                H=H;
                S=S+0.01f;
                V=V+0.2f;
                if(H>1){
                    H=1;
                }
                if(S>1){
                    S=1;
                }
                if(V>1){
                    V=1;
                }
               //form HSL to RGB
               int pix=HSVtoRGB(H,S,V);
               pix=a<<24|pix;
               outpixel[y*width+x]=pix;
           }
        }
        Bitmap newBitmap=Bitmap.createBitmap(width, height,Config.RGB_565);
        newBitmap.setPixels(outpixel, 0, width, 0, 0, width, height);
        destinationImage.setImageBitmap(newBitmap);
    }


public int HSVtoRGB(float h,float s,float v){
        float rr=0,gg=0,bb=0;
        float hh=(6*h)%6;
        int c1=(int)hh;
        float c2=hh-c1;
        float x=(1-s)*v;
        float y=(1-s*c2)*v;
        float z=(1-(s*(1-c2)))*v;
        switch(c1){
            case 0: rr=v;gg=z;bb=x;break;
            case 1: rr=y;gg=v;bb=x;break;
            case 2: rr=x;gg=v;bb=z;break;
            case 3: rr=x;gg=y;bb=v;break;
            case 4: rr=z;gg=x;bb=v;break;
            case 5: rr=v;gg=x;bb=y;break;
        }
        int N=256;
        int r=Math.min(Math.round(rr*N),N-1);
        int g=Math.min(Math.round(gg*N),N-1);
        int b=Math.min(Math.round(bb*N),N-1);
        return r<<16|g<<8|b;
    }

得到的效果是:


然后在网上看见一篇博客:http://blog.csdn.net/jia20003/article/details/8026552

是采用将RGB图转换为HSL图像改变亮度和饱和度的。大家可以参考下。

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值