Java 如何确定颜色更接近白色还是黑色?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9780632/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How do I determine if a color is closer to white or black?
提问by Aziz
I am dealing with images and would like to determine if a set of pixels are closer to white or black.
我正在处理图像并想确定一组像素是否更接近白色或黑色。
So given a set of colors/pixles, how does one determine if they closer to a white or a black?
那么给定一组颜色/像素,如何确定它们更接近白色还是黑色?
I have tried some noobish algorithms, does anyone know how I can do this?
我尝试了一些 noobish 算法,有谁知道我该怎么做?
采纳答案by Hyman
I would say that you can first convert the color to gray scale and then check if it's nearer to black or white.
我会说您可以先将颜色转换为灰度,然后检查它是否更接近黑色或白色。
First convert the RGB color value to compute luminance by the following formula
首先通过以下公式将RGB颜色值转换为计算亮度
Y = 0.2126*R + 0.7152*G + 0.0722*B
Then check if the value is nearer to 0 or to 255 and choose black or white accordingly
然后检查该值是否更接近 0 或 255 并相应地选择黑色或白色
color c = Y < 128 ? black : white
Mind that this works well if the color space is not gamma compressed, otherwise you will have to add a step before calculating the luminance which is a gamma expansion, compute Y, then perform a gamma compressionto obtain a non-linear luminance value that you can then use to decide if color is nearer to black or white.
请注意,如果颜色空间没有经过伽马压缩,这很有效,否则您必须在计算亮度之前添加一个步骤,这是伽马扩展,计算 Y,然后执行伽马压缩以获得非线性亮度值然后可以用来决定颜色是否更接近黑色或白色。
回答by mauris
Take a look at YCbCr. Since Java and most computer processes colors in RGB format, you will need to do some conversion from RGB to YCbCr. There are many formulas to convert RGB to YCbCr.
看看YCbCr。由于 Java 和大多数计算机以 RGB 格式处理颜色,因此您需要进行一些从 RGB 到 YCbCr 的转换。有许多公式可以将 RGB 转换为 YCbCr。
Once you get the YCbCr value, you can check the luminance value (the value Y in YCbCr).
获得YCbCr 值后,您可以检查亮度值(YCbCr 中的Y 值)。
回答by hkf
Convert the colour to grayscale (a grayscale colour has all 3 RGB components equal, see convert color image to grayscale).
Check if grayscale is closer to black (0) or white (255)
将颜色转换为灰度(灰度颜色的所有 3 个 RGB 分量都相等,请参阅将彩色图像转换为灰度)。
检查灰度是否更接近黑色 (0) 或白色 (255)
回答by mikera
There are two potential meanings of white and black:
白色和黑色有两种潜在的含义:
- Colour spectrum of visible light
- Human skin tones as determined by race, amount of tan etc.
- 可见光的色谱
- 由种族、棕褐色量等决定的人类肤色。
The former is easy: convert to greyscale range 0-255. 127 or less is closer to black (0), 128 or above is closer to white (255).
前者很容易:转换为灰度范围 0-255。127 或更低更接近黑色 (0),128 或更高更接近白色 (255)。
I use the following function to convert to greyscale using luminance values (assuming an int ARGB format for input colour):
我使用以下函数使用亮度值转换为灰度(假设输入颜色为 int ARGB 格式):
public static int getLuminance(int argb) {
int lum= ( 77 * ((argb>>16)&255)
+ 150 * ((argb>>8)&255)
+ 29 * ((argb)&255))>>8;
return lum;
}
The latter definition (human skin tones) is impossible to do with a simple algorithm as it depends on light condition, camera settings, exposure etc. An RGB vlaue of (190,115,60) is probably approximately the midpoint in typical conditions
后一种定义(人类肤色)无法用简单的算法来实现,因为它取决于光照条件、相机设置、曝光等。 (190,115,60) 的 RGB 值可能大约是典型条件下的中点