RGB通过转换成HSV增加明度和饱和度
来源:互联网 发布:不倒翁沙袋淘宝 编辑:程序博客网 时间:2024/06/07 19:02
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增加明度和饱和度
- RGB和HSV转换
- RGB和HSV颜色转换
- RGB和HSV颜色转换
- RGB和HSV颜色转换
- RGB和HSV颜色转换
- 色相(H)饱和度(S)明度(L)与RGB的转换以及在android上的试验
- UIColor:RGB和HSV互相转换
- rgb和hsv的互相转换
- UIColor:RGB和HSV互相转换
- RGB和HSV区别与互相转换
- RGB和HSV空间的转换
- RGB与HSV转换
- RGB与HSV转换
- RGB-HSV转换
- HSV RGB 转换
- 通过RGB或YUV改变图像的色度和饱和度
- UnityShader:HSV(色相,饱和度,亮度)转换
- Android+JNI调用–文件操作
- JavaIO总结Demo大全(3)
- ble学习笔记二-----------------点亮led
- JavaIO总结Demo大全(2)
- Newtonsoft将对象或对象集合转换成JSON数据时,日期格式的设置
- RGB通过转换成HSV增加明度和饱和度
- POJ 2954 Triangle(求整顶点多边形包含的整点数)
- 模仿细胞的分裂--彩图,动态
- 记一次修改linux键盘映射的过程
- 最长公共子序列——NYOJ 36
- fir 提交的版本的iPhone4 iPhone4S不能安装 iPhone5 5s padAir可以安装解决方案
- 2015华为机试题整理
- UIView animate动画的写法
- ble学习笔记三---------------------------控制led