如何转换成彩***像灰度?如果每个彩色像素由三重(R,G,B)(红,绿,蓝)的强度描述,
如何讲(R,G,B)映射到一个单独的数字作为的灰度值?在GIMP图像处理软件有三种算法。
lightness方法:是取最突触颜色和最不突出颜色的平均值: (max(R, G, B) + min(R, G, B)) / 2.
average方法:最简单取R,G,B的平均值:(R+G+B)/3 .
luminosity方法:是平均方法的一个更复杂的版本。它也是平均值,但它通过加权平均来解释人类感知。我们对绿色比其他颜色更敏感,所以绿色加权最大。其计算公式为亮度为0.21 R +0.72 G +0.07 B.
下面向日葵图片示例来自GIM文档:
lightness方法倾向于降低对比度。luminosity方法效果最好,如果你使用GIMP改变一个图片从RGB到灰度图片通过Image->ModeMenu,该方法是默认使用的方法。
然而,一些图像看起来更好地利用其他算法之一,有时三种方法产生非常相似的结果。
附:
1. 调色板:https://www.google.com/design/spec/style/color.html#color-color-palette
2. GIMP(GNU Image Manipulation Program):开源图片处理工具 开源免费跨平台。
3. jscience 开源库提供的灰阶计算加权值常量文档
4. Java封装GIMP和Jscience提供的灰阶计算方法,代码示例:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
|
/**
* Compute method about grayscale from
* <p/>
* gimp website
* http://docs.gimp.org/2.6/en/gimp-tool-desaturate.html
* http://www.gimp.org/
* <p/>
* http://www.johndcook.com/blog/2009/08/24/algorithms-convert-color-grayscale/
* http://www.johndcook.com/blog/2009/08/24/more-on-colors-and-grayscale/
*/
public
static
class
GrayScaleUtil {
interface
GrayScaleCompute {
int
grayScale(
int
r,
int
g,
int
b);
}
public
enum
GrayScale {
Lightness(
new
GrayScaleCompute() {
@Override
public
int
grayScale(
int
r,
int
g,
int
b) {
return
GrayScaleUtil.lightness(r, g, b);
}
}),
Average(
new
GrayScaleCompute() {
@Override
public
int
grayScale(
int
r,
int
g,
int
b) {
return
GrayScaleUtil.average(r, g, b);
}
}),
Luminosity(
new
GrayScaleCompute() {
@Override
public
int
grayScale(
int
r,
int
g,
int
b) {
return
GrayScaleUtil.luminosity(r, g, b);
}
}),
BT709(
new
GrayScaleCompute() {
@Override
public
int
grayScale(
int
r,
int
g,
int
b) {
return
GrayScaleUtil.BT709(r, g, b);
}
}),
RMY(
new
GrayScaleCompute() {
@Override
public
int
grayScale(
int
r,
int
g,
int
b) {
return
GrayScaleUtil.RMY(r, g, b);
}
}),
Y(
new
GrayScaleCompute() {
@Override
public
int
grayScale(
int
r,
int
g,
int
b) {
return
GrayScaleUtil.Y(r, g, b);
}
});
private
GrayScaleCompute gc;
GrayScale(GrayScaleCompute gc) {
this
.gc = gc;
}
public
int
grayScale(
int
r,
int
g,
int
b) {
return
this
.gc.grayScale(r, g, b);
}
}
//Lightness = (max(r,g,b)+min(r,g,b))/2
public
static
int
lightness(
int
r,
int
g,
int
b) {
return
(Math.max(Math.max(r, g), b) + Math.min(Math.min(r, g), b)) /
2
;
}
// Average Brightness = (r+g+b)/3
public
static
int
average(
int
r,
int
g,
int
b) {
return
(r + g + b) /
3
;
}
//Luminosity =(0.21*r+0.72*g+0.07*b)
public
static
int
luminosity(
int
r,
int
g,
int
b) {
return
(
int
) (
0.21
* r +
0.72
* g +
0.07
* b);
}
/**
* Magic number about grayscale from http://jscience.org/experimental/javadoc/org/jscience/computing/ai/vision/GreyscaleFilter.html
*/
//BT709 Greyscale: Red: 0.2125 Green: 0.7154 Blue: 0.0721
public
static
int
BT709(
int
r,
int
g,
int
b) {
return
(
int
) (
0.2125
* r +
0.7154
* g +
0.0721
* b);
}
//RMY Greyscale: Red: 0.5 Green: 0.419 Blue: 0.081
public
static
int
RMY(
int
r,
int
g,
int
b) {
return
(
int
) (
0.5
* r +
0.419
* g +
0.081
* b);
}
//Y-Greyscale (YIQ/NTSC): Red: 0.299 Green: 0.587 Blue: 0.114
public
static
int
Y(
int
r,
int
g,
int
b) {
return
(
int
) (
0.299
* r +
0.587
* g +
0.114
* b);
}
}
|
本文转自 secondriver 51CTO博客,原文链接:http://blog.51cto.com/aiilive/1718960,如需转载请自行联系原作者