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

Daniel Castro picture Daniel Castro · Jan 18, 2014 · Viewed 30.6k times · Source

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!

Answer

M4rtini picture M4rtini · Jan 19, 2014

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.