RGB色彩

RGB色彩模式是工业界的一种颜色标准,是通过对红(R)、绿(G)、蓝(B)三个颜色通道的变化以及它们相互之间的叠加来得到各式各样的颜色的,RGB即是代表红、绿、蓝三个通道的颜色。

HSL色彩( HSL color )

HSL色彩模式是工业界的一种颜色标准,是通过对色相(H)、饱和度(S)、亮度(L)三个颜色通道的变化以及它们相互之间的叠加来得到各式各样的颜色的,HSL即是代表色相,饱和度,亮度三个通道的颜色。

H: Hue 色相

S:Saturation 饱和度

L :Lightness 亮度

HSL的H(hue)分量,代表的是人眼所能感知的颜色范围,这些颜色分布在一个平面的色相环上,取值范围是0°到360°的圆心角,每个角度可以代表一种颜色。色相值的意义在于,我们可以在不改变光感的情况下,通过旋转色相环来改变颜色。

HSL的S(saturation)分量,指的是色彩的饱和度,它用0%至100%的值描述了相同色相、亮度下色彩纯度的变化。数值越大,颜色中的灰色越少,颜色越鲜艳,呈现一种从理性(灰度)到感性(纯色)的变化。

HSL的L(lightness)分量,指的是色彩的亮度,作用是控制色彩的亮暗变化。它同样使用了0%至100%的取值范围。数值越小,色彩越暗,越接近于黑色;数值越大,色彩越亮,越接近于白色。

RGB转HSL算法

(1)把RGB值转成区间[0,1]内的数值。

(2)找出R,G和B中的最大值。

(3)计算亮度:L=(maxcolor + mincolor)/2

(4)如果最大和最小的颜色值相同,即表示灰色,那么S定义为0,而H未定义并在程序中通常写成0。

(5)否则,根据亮度L计算饱和度S:

If L<0.5, S=(maxcolor-mincolor)/(maxcolor + mincolor)

If L>=0.5, S=(maxcolor-mincolor)/(2.0-maxcolor-mincolor)

(6)计算色调H:

If R=maxcolor, H=(G-B)/(maxcolor-mincolor)

If G=maxcolor, H=2.0+(B-R)/(maxcolor-mincolor)

If B=maxcolor, H=4.0+(R-G)/(maxcolor-mincolor)

H=H*60.0,如果H为负值,则加360。

HSL转RGB算法。

(1)If S=0,表示灰色,定义R,G和B都为L.

(2)否则,测试L:

If L<0.5,temp2=L*(1.0+S)

If L>=0.5,temp2=L+S-L*S

(3)temp1=2.0*L-temp2

(4)把H转换到0~1。

(5)对于R,G,B,计算另外的临时值temp3。方法如下:

for R, temp3=H+1.0/3.0

for G, temp3=H

for B, temp3=H-1.0/3.0

if temp3<0, temp3=temp3+1.0

if temp3>1, temp3=temp3-1.0

(6)对于R,G,B做如下测试:

If 6.0*temp3<1,color=temp1+(temp2-temp1)*6.0*temp3

Else if 2.0*temp3<1,color=temp2

Else if 3.0*temp3<2,

color=temp1+(temp2-temp1)*((2.0/3.0)-temp3)*6.0

Else color=temp1

色彩转换JAVA实现代码

public class TransformColor {

public static final double MaxRGB = 255.0;

public void rgbToHsl(Pixel pixel) {

int red = pixel.red;

int blue = pixel.blue;

int green = pixel.green;

double b, delta, g, max, min, r;

double hue, saturation, luminosity;

/*

* Convert RGB to HSL colorspace.

*/

r = (double) red / MaxRGB;

g = (double) green / MaxRGB;

b = (double) blue / MaxRGB;

max = Math.max(r, Math.max(g, b));

min = Math.min(r, Math.min(g, b));

hue = 0.0;

saturation = 0.0;

luminosity = (min + max) / 2.0;

delta = max - min;

if (delta == 0.0) {

pixel.hue = hue;

pixel.saturation = saturation;

pixel.luminosity = luminosity;

return;

}

saturation = delta / ((luminosity <= 0.5) ? (min + max) : (2.0 - max - min));

if (r == max)

hue = (g == min ? 5.0 + (max - b) / delta : 1.0 - (max - g) / delta);

else if (g == max)

hue = (b == min ? 1.0 + (max - r) / delta : 3.0 - (max - b) / delta);

else

hue = (r == min ? 3.0 + (max - g) / delta : 5.0 - (max - r) / delta);

hue /= 6.0;

pixel.hue = hue;

pixel.saturation = saturation;

pixel.luminosity = luminosity;

}

public void hslToRgb(Pixel pixel) {

double hue, saturation, luminosity;

// int red, green, blue;

double b, g, r, v, x, y, z;

hue = pixel.hue;

saturation = pixel.saturation;

luminosity = pixel.luminosity;

/*

* Convert HSL to RGB colorspace.

*/

v = (luminosity <= 0.5) ? (luminosity * (1.0 + saturation))

: (luminosity + saturation - luminosity * saturation);

if (saturation == 0.0) {

pixel.red = (int) (MaxRGB * luminosity + 0.5);

pixel.green = (int) (MaxRGB * luminosity + 0.5);

pixel.blue = (int) (MaxRGB * luminosity + 0.5);

return;

}

y = 2.0 * luminosity - v;

x = y + (v - y) * (6.0 * hue - Math.floor(6.0 * hue));

z = v - (v - y) * (6.0 * hue - Math.floor(6.0 * hue));

switch ((int) (6.0 * hue)) {

case 0:

r = v;

g = x;

b = y;

break;

case 1:

r = z;

g = v;

b = y;

break;

case 2:

r = y;

g = v;

b = x;

break;

case 3:

r = y;

g = z;

b = v;

break;

case 4:

r = x;

g = y;

b = v;

break;

case 5:

r = v;

g = y;

b = z;

break;

default:

r = v;

g = x;

b = y;

break;

}

pixel.red = (int) (MaxRGB * r + 0.5);

pixel.green = (int) (MaxRGB * g + 0.5);

pixel.blue = (int) (MaxRGB * b + 0.5);

}

}

辅助类Pixel代码:

import java.awt.image.*;

public class Pixel {

public int red;

public int green;

public int blue;

public int alpha=0xFF;

public double hue;

public double saturation;

public double luminosity;

private int rgb;

public Pixel() {

}

public void setRGB(int rgb) {

red = (rgb & 0x00FF0000) / 0x00010000;

green = (rgb & 0x0000FF00) / 0x00000100;

blue = rgb & 0x000000FF;

alpha = (rgb >> 24)&0x0ff;

this.rgb = rgb;

}

public int getRGB() {

rgb = alpha<<24 | 0x00010000 * red | 0x00000100 * green | blue;

return this.rgb;

}

public static void setRgb(BufferedImage image, int x, int y, int red, int green, int blue) {

int rgb = 0xFF000000 | red * 0x00010000 | green * 0x00000100 | blue;

image.setRGB(x, y, rgb);

}

public static int pixelIntensity(int rgb) {

int red = (rgb&0x00FF0000)/0x00010000;

int green = (rgb&0x0000FF00)/0x00000100;

int blue = rgb&0x000000FF;

return (int) (0.299 * red + 0.587 * green + 0.114 * blue + 0.5);

}

}

改变图像亮度、对比度饱和度

我们可以通过把对HSL颜色的处理实现对图像亮度,对比度饱和度的调节

调节亮度、色相、对比度代码如下

public static void modulate(double percent_hue, double percent_saturation, double percent_brightness, Pixel pixel) {

TransformColor transform = new TransformColor();

/*

* Increase or decrease color brightness, saturation, or hue.

*/

transform.rgbToHsl(pixel);

pixel.luminosity *= (0.01) * percent_brightness;

if (pixel.luminosity < 0.0)

pixel.luminosity = 0.0;

else if (pixel.luminosity > 1.0)

pixel.luminosity = 1.0;

pixel.saturation *= (0.01) * percent_saturation;

if (pixel.saturation < 0.0)

pixel.saturation = 0.0;

else if (pixel.saturation > 1.0)

pixel.saturation = 1.0;

pixel.hue *= (0.01) * percent_hue;

if (pixel.hue < 0.0)

pixel.hue += 1.0;

else if (pixel.hue > 1.0)

pixel.hue -= 1.0;

transform.hslToRgb(pixel);

}

public static BufferedImage hslImage(BufferedImage image, double satuPer, double huePer, double lumPer) {

BufferedImage bimg = new BufferedImage(image.getWidth(), image.getHeight(), BufferedImage.TYPE_INT_RGB);

Pixel pixel = new Pixel();

for (int y = 0; y < bimg.getHeight(); y++) {

for (int x = 0; x < bimg.getWidth(); x++) {

pixel.setRGB(image.getRGB(x, y));

modulate(huePer, satuPer, lumPer, pixel);

bimg.setRGB(x, y, pixel.getRGB());

}

}

return bimg;

}

调节对比度代码如下:

public static void contrast(double percent, Pixel pixel) {

TransformColor transform = new TransformColor();

double alpha = percent / 100.0;

/*

* Enhance contrast: dark color become darker, light color become lighter.

*/

transform.rgbToHsl(pixel);

pixel.luminosity += alpha * (alpha * (Math.sin(Math.PI * (pixel.luminosity - alpha)) + 1.0) - pixel.luminosity);

if (pixel.luminosity > 1.0)

pixel.luminosity = 1.0;

else if (pixel.luminosity < 0.0)

pixel.luminosity = 0.0;

transform.hslToRgb(pixel);

}

public static BufferedImage contractImage(BufferedImage image, double percent) {

BufferedImage bimg = new BufferedImage(image.getWidth(), image.getHeight(), BufferedImage.TYPE_INT_RGB);

Pixel pixel = new Pixel();

for (int y = 0; y < bimg.getHeight(); y++) {

for (int x = 0; x < bimg.getWidth(); x++) {

pixel.setRGB(image.getRGB(x, y));

contrast(percent, pixel);

bimg.setRGB(x, y, pixel.getRGB());

}

}

return bimg;

}

实现效果

以下是对图像亮度,饱和度、对比度分别增加和减少的测试代码:

public static void main(String[] argv) throws IOException {

BufferedImage img = read(new File("D:\\eclipse-workspace\\image-toolkit\\images\\girl.jpg"));

BufferedImage img2 = hslImage(img, 0, 0.5, 0);

ImageIO.write(img2, "jpeg", new File("girl-s+.jpg"));

img2 = hslImage(img, 0, -0.5, 0);

ImageIO.write(img2, "jpeg", new File("girl-s-.jpg"));

img2 = hslImage(img, 0, 0, 0.5);

ImageIO.write(img2, "jpeg", new File("girl-l+.jpg"));

img2 = hslImage(img, 0, 0, -0.5);

ImageIO.write(img2, "jpeg", new File(girl-l-.jpg"));

img2 = contractImage(img, 0.7);

ImageIO.write(img2, "jpeg", new File("girl-c+.jpg"));

img2 = contractImage(img, -0.7);

ImageIO.write(img2, "jpeg", new File("girl-c-.jpg"));

}

实现效果

其中第一张为原始图片,第二三张为饱和度调节图片,第四五张为亮度调节图片,第六七张为对比度调节图片

girl.jpg

girl-s+.jpg

girl-s-.jpg

girl-l+.jpg

girl-l-.jpg

girl-c+.jpg

girl-c-.jpg

java rgb转colour_JAVA图像处理系列(六)—— 色彩转换RGB vs HSL相关推荐

  1. JAVA面试常考系列六

    转载自 JAVA面试常考系列六 题目一 一个Applet有哪些生命周期? 一个Applet的生命周期分为以下四个阶段: Init 每次加载时都会初始化一个小程序.此方法通知Applet,方法已经被装入 ...

  2. ONNX系列六 --- 在Java中使用可移植的ONNX AI模型

    目录 安装和导入ONNX运行时 载入ONNX模型 使用ONNX运行时进行预测 摘要和后续步骤 参考文献 下载源547.1 KB 系列文章列表如下: ONNX系列一 --- 带有ONNX的便携式神经网络 ...

  3. java水泡_JAVA图像处理系列(八)——艺术效果:水泡

    艺术效果水泡 通过对图像进行变形或叠加其他图片,能够实现许多有趣的艺术效果,本文介绍类似水泡效果的实现方式,下面先看一下实现的效果. 第一张为原始图像,第二张为叠加水泡效果的图像. cat.jpg c ...

  4. Android音视频学习系列(六) — 掌握视频基础知识并使用OpenGL ES 2.0渲染YUV数据

    系列文章 Android音视频学习系列(一) - JNI从入门到精通 Android音视频学习系列(二) - 交叉编译动态库.静态库的入门 Android音视频学习系列(三) - Shell脚本入门 ...

  5. 数字图像处理——第六章 彩色图像处理

    数字图像处理--第六章 彩色图像处理 文章目录 数字图像处理--第六章 彩色图像处理 1 彩色模型 1.1 RGB彩色模型 1.2 CMY 和CMYK彩色模型 1.3 HSI彩色模型 2 伪彩色图像处 ...

  6. Java中实现六种图像处理的效果(灰度化、马赛克效果、去背景实现、珠纹化实现、黑白版画效果、油画效果)

    ** Java中实现六种图像处理的效果(灰度化.马赛克效果.去背景实现.珠纹化实现.黑白版画效果.油画效果) ** 本文的编程的思想: 先将实现这六种效果的方法写入一个名为pic_performanc ...

  7. android图像处理系列之五-- 给图片添加边框(中)

    前面一篇讲到给图片加边框的方式,只能给图片加一些有规则的边框,如果想加一些比较精美的效果,就有点麻烦了.下面就给出解决这个问题的思路. 思路是:一些比较精美的花边图片我们是很难用代码控制,就目前本人水 ...

  8. struts2官方 中文教程 系列六:表单验证

    先贴个本帖的地址,以免被爬:struts2教程 官方系列六:表单验证  即 http://www.cnblogs.com/linghaoxinpian/p/6906720.html 下载本章节代码 介 ...

  9. LINUX下简单实现ISP图像处理从RAW到RGB,BMP算法、RGB到JPEG库的使用(一)

    在这里分享一下相关的ISP的一些基本简单图像处理算法.在一般的市面上,相关的ISP算法都是非常复杂,且经过不同serson设备的情况进行固定参数并且固化在芯片内来实现.所以硬件ISP的效率会比软件算法 ...

最新文章

  1. php laravel 环境搭建
  2. Python 3.8即将发布,这几个变化你必须知道
  3. python释放类对象_Python 基本功: 10. 面对对象-类 Class
  4. 近一半企业曾遭遇云计算安全问题
  5. OEM中无法用sys用户登录
  6. 如何处理数据中心电缆管理问题?
  7. 你真的了解Lambda表达式吗?
  8. 《Effective C++》阅后心得
  9. Python中的xxx+=xxx和xxx=xxx+xxx一些区别及执行过程
  10. 两大图灵奖得主点赞中国用AI检测新冠,AI还能做什么?
  11. 《Python编程从入门到实践》记录之json模块(数据存储)
  12. linux内核开源不能仿照_Linux内核开发,开源生产力工具,使用Google应用程序创建自动日历等
  13. weka manual 3.6 翻译:1.1 引言
  14. 关于sql 资源竞争死锁现象
  15. 下单延迟10s撤单性能测试
  16. 配置项setOption -- title
  17. 博客阅读和思考——2018-3-5
  18. $stateParams 详解
  19. 浅谈Tomcat接收到一个请求后在其内部的执行流程(源码)
  20. Hacking the PS4, part 1

热门文章

  1. JUC锁框架AbstractQueuedSynchronizer详细分析
  2. fc重装机兵计算机密码,FC重装机兵 求打诺亚的四个密码
  3. MegaCli64工具磁盘修复处理
  4. js中引用script.aculo.us
  5. 学习淘淘商城第二十四课(前台系统工程搭建)
  6. 商鼎云 | 你的数据管家
  7. C 算法精介----链表-双向链表
  8. Lock锁-------tryLock()方法
  9. 幼儿园体质测试软件,幼儿园儿童体质测试标准
  10. Prescan许可证失效怎么办