您的位置:首页 > 其它

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

2014-09-14 13:13 176 查看
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图像改变亮度和饱和度的。大家可以参考下。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: