18

I am trying to convert an RGB to the perceptually uniform color space, CIELAB. Wikipedia states:

"The RGB or CMYK values first must be transformed to a specific absolute color space, such as sRGB or Adobe RGB. This adjustment will be device-dependent, but the resulting data from the transform will be device-independent, allowing data to be transformed to the CIE 1931 color space and then transformed into L*a * b*."

I know there are some straightforward transformations once converting to sRGB, but I have not found any material to go from RGB to sRGB. So, what methods exist to do such a conversion?

1

1 Answer 1

17

No, you should not go from (linear) RGB to sRGB. In fact, it is the other way round. Following are the steps:

  1. Convert sRGB into linear RGB. sRGB image is a gamma encoded which means a camera applies gamma function pow(x, 1/2.2) onto the light signal. This sRGB is in gamma-space which is non-linear.

  2. Now, converting linear RGB to LAB involves two steps: first is converting linear RGB to XYZ color space (this is a basic color-space). This conversion is a linear operation, i.e., matrix multiplication. This is the reason why you would need linear RGB values not sRGB. It needs to be in linear space. Finally, XYZ values are converted into LAB values through a non-linear operation which contains some standard formulas (which you don't need to be worried about).

Interesting links:

(i) Understanding sRGB and linear RGB space: http://filmicgames.com/archives/299; http://www.cambridgeincolour.com/tutorials/gamma-correction.htm

(ii) MATLAB tutorial: https://de.mathworks.com/help/vision/ref/colorspaceconversion.html

(iii) Python package: http://pydoc.net/Python/pwkit/0.2.1/pwkit.colormaps/

(iv) C code: http://svn.int64.org/viewvc/int64/colors/color.c?view=markup

(v) OpenCV does not do this sRGB to linear RGB conversion but it does the conversion inside color.cpp code (OpenCV_DIR\modules\imgproc\src\color.cpp). Check out method called initLabTabs(), there is a gamma encoding and decoding. OpenCV color conversion API: http://docs.opencv.org/3.1.0/de/d25/imgproc_color_conversions.html

2
  • 2
    Thank you @Sanchit! So glad to finally understand how these color spaces work. Really appreciate the thoughtful answer and interesting links—especially the links. 🙏 For anyone else dealing with color space issues in SVG filter effects, this explains why you need to add color-interpolation-filters="sRGB". Otherwise filters default to linearRGB. That loss of gamma information causes images to appear washed out. To fix, update the color space of your filters. Jul 22, 2018 at 18:30
  • @TannerHodges: Your welcome :) I am really glad that you like my answer.
    – Sanchit
    Jul 23, 2018 at 8:37

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.