Skip to content

Instantly share code, notes, and snippets.

@markwbrown
Forked from bikz05/rgb2Lab.py
Created March 14, 2016 07:22
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save markwbrown/a65b88a1b5c46a4c7980 to your computer and use it in GitHub Desktop.
Save markwbrown/a65b88a1b5c46a4c7980 to your computer and use it in GitHub Desktop.
Python Script to convert color from RGB ColorSpace to CIE Lab Color Space
import numpy as np
def func(t):
if (t > 0.008856):
return np.power(t, 1/3.0);
else:
return 7.787 * t + 16 / 116.0;
#Conversion Matrix
matrix = [[0.412453, 0.357580, 0.180423],
[0.212671, 0.715160, 0.072169],
[0.019334, 0.119193, 0.950227]]
# RGB values lie between 0 to 1.0
rgb = [1.0, 0, 0] # RGB
cie = np.dot(matrix, rgb);
cie[0] = cie[0] /0.950456;
cie[2] = cie[2] /1.088754;
# Calculate the L
L = 116 * np.power(cie[1], 1/3.0) - 16.0 if cie[1] > 0.008856 else 903.3 * cie[1];
# Calculate the a
a = 500*(func(cie[0]) - func(cie[1]));
# Calculate the b
b = 200*(func(cie[1]) - func(cie[2]));
# Values lie between -128 < b <= 127, -128 < a <= 127, 0 <= L <= 100
Lab = [b , a, L];
# OpenCV Format
L = L * 255 / 100;
a = a + 128;
b = b + 128;
Lab_OpenCV = [b , a, L];
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment