Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?

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.

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!

like image 272
Daniel Castro Avatar asked Jan 18 '14 22:01

Daniel Castro


2 Answers

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:

L ← L * 255/100 ; a ← 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.

like image 191
M4rtini Avatar answered Oct 27 '22 20:10

M4rtini


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

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

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

like image 43
adrienlucca.net Avatar answered Oct 27 '22 20:10

adrienlucca.net