Python 从 RGB 转换为 LAB 色彩空间 - 了解 L*A*B* 值的范围吗?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/21210479/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-18 22:18:40  来源:igfitidea点击:

Converting from RGB to LAB Colorspace - any insight into the range of L*A*B* values?

pythonopencvcolor-spacelab-color-space

提问by Daniel Castro

I was unable to find documentation on the range of L*A*B* values when converting an image from RGB to LAB in OpenCV (Python). Looking for some confirmation that my insight is correct, as the numbers are rather peculiar. My results for lightness were from 0-255, but for a and b I got 42-226 and 20-223 respectively. I understand that these values do not need to have a predetermined range, but could anyone shed some insight into why these ranges were picked?

在 OpenCV (Python) 中将图像从 RGB 转换为 LAB 时,我无法找到有关 L*A*B* 值范围的文档。寻找一些确认我的见解是正确的,因为这些数字相当奇特。我的亮度结果是 0-255,但对于 a 和 b,我分别得到 42-226 和 20-223。我知道这些值不需要有一个预先确定的范围,但有人能深入了解为什么选择这些范围吗?

For what its worth I am attempting to create color histograms in the LAB space and needed to know the range of values to store bin values in a space efficient way.

对于它的价值,我试图在 LAB 空间中创建颜色直方图,并且需要知道以空间有效的方式存储 bin 值的值范围。

import cv2
import numpy as np
import sys
import urllib

print cv2.__version__ # 2.4.7
print sys.version # 2.7.5+ (default, Sep 19 2013, 13:48:49) \n[GCC 4.8.1]

# Load an image that contains all possible colors.
request = urllib.urlopen('http://www.brucelindbloom.com/downloads/RGB16Million.png')
image_array = np.asarray(bytearray(request.read()), dtype=np.uint8)
image = cv2.imdecode(image_array, cv2.CV_LOAD_IMAGE_COLOR)

# I was uncertain if it was BGR or RGB but in this case it doesn't matter because
# of my input image.
lab_image = cv2.cvtColor(image, cv2.COLOR_BGR2LAB)
l_channel,a_channel,b_channel = cv2.split(lab_image)

# Print the minimum and maximum of lightness.
print np.min(l_channel) # 0
print np.max(l_channel) # 255

# Print the minimum and maximum of a.
print np.min(a_channel) # 42
print np.max(a_channel) # 226

# Print the minimum and maximum of b.
print np.min(b_channel) # 20
print np.max(b_channel) # 223

Thanks!

谢谢!

采纳答案by M4rtini

Looking at the OpenCV documentation(scroll down to where the conversion for RGB ? CIE L*a*b* is defined), we can see that the values are rescaled into the 0-255 range:

查看 OpenCV文档(向下滚动到定义 RGB 转换的位置?CIE L*a*b* 的位置),我们可以看到这些值被重新调整为 0-255 范围:

L ← L * 255/100 ; a ← a + 128 ; b ← b + 128

L ← L * 255/100 ; ← a + 128 ; b ← b + 128

And in addition: the LAB color space spans the whole perceivable spectrum of colors, RGB does not. So you will not see the whole range of values when converting from RGB.

此外:LAB 色彩空间涵盖整个可感知的色彩范围,而 RGB 则没有。因此,当从 RGB 转换时,您将看不到整个值范围。

回答by adrienlucca.wordpress.com

Actually there are no defined limits to a*and b*, -127 to 127are just conventions to easily fit 8-bit Lab* coding...

实际上,a*and没有定义的限制b*-127 to 127只是为了轻松适应 8 位 L ab* 编码的约定......

Following CIE specs, L*can vary between 0 and 116, however Photoshop and others stop at 100

遵循 CIE 规范,L*可以在 之间有所不同0 and 116,但是 Photoshop 和其他人止步于100

The reason why it's generally OK to define values between -127 and 127is that it generally fits the gamut of real colors, see "gamut real colors Pointer"on Google

通常可以在两者之间定义值的原因-127 and 127是它通常适合gamut of real colors,参见"gamut real colors Pointer"Google