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图像改变亮度和饱和度的。大家可以参考下。
所以先将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图像改变亮度和饱和度的。大家可以参考下。
相关文章推荐
- 色相(H)饱和度(S)明度(L)与RGB的转换以及在android上的试验
- » RGB与HSV(HSB)的转换公式 mybeky’s blog
- RGB和HSV颜色转换
- 颜色空间RGB与HSV(HSL)的转换
- 问题记录--通过jaxb转换的xml增加namespace信息
- 颜色空间RGB与HSV(HSL)的转换
- Rgb和Hsv相互转换的方法!
- RGB与HSV转换
- 从 RGB 到 HSL 或 HSV 的转换
- 计算机中的颜色IX——总述:RGB和HSV快速转换
- 基于OpenCV的RGB和HSV色彩空间相互转换C++程序
- [导入]RGB和HSV颜色转换
- HSV与RGB互相转换
- 计算机中的颜色IX——总述:RGB和HSV快速转换
- RGB HSV HLS三种色彩模式转换(C语言实现)
- RGB和HSV颜色转换
- RGB转换为HSV的VC++代码
- UIColor:RGB和HSV互相转换
- RGB 到HSV转换 摘自wiki百科
- RGB颜色空间到HSV和YUV的转换关系(含VC代码)