I am wondering whether OpenCV has functions to handle the non-linearities in the sRGB color space.
Say I want to convert an JPEG image from sRGB color space into XYZ color space. As specified in this Wiki page, one needs to first undo the nonlinearities to convert to linear RGB space, and then multiply with the 3x3 color transform matrix. However, I couldn't find any such discussions in the cvtColor documentation. Did I miss something?
Thanks a lot in advance!
Changing Color-space For color conversion, we use the function cv. cvtColor(input_image, flag) where flag determines the type of conversion. For HSV, hue range is [0,179], saturation range is [0,255], and value range is [0,255]. Different software use different scales.
BGR color space: OpenCV's default color space is RGB. However, it actually stores color in the BGR format. It is an additive color model where the different intensities of Blue, Green and Red give different shades of color.
To decode a sRGB encoded color, raise the rgb values to the power of 2.2 . Once you have decoded the color, you are now free to add, subtract, multiply, and divide it. By raising the color values to the power of 2.2 , you're converting them from sRGB to RGB or linear color space.
Simply, it means that numerical intensity values correspond proportionally to their perceived intensity. This means that the colors can be added and multiplied correctly. A color space without that property is called ”non-linear”.
It's not explicitly stated in the documentation, so you're not missing anything, but OpenCV does not perform gamma correction in its RGB2XYZ/BGR2XYZ
color conversions. You can confirm this by looking at the source code for cvtColor
in
<OpenCV_dir>/modules/imgproc/src/color.cpp
If you look at the RGB <-> XYZ
section you'll see that the input RGB values are simply multiplied by the coefficient matrix.
I have also not found any existing method to perform gamma correction on an RGB image.
Interestingly, a custom RGB -> XYZ
conversion is done as a preliminary step for converting to both L*a*b*
and L*u*v*
, and in both cases it performs gamma correction.
Unfortunately, this isn't accessible from RGB2XYZ
code, but you might be able to reuse it in your own code. I've also seen several code samples on the web, mostly using look-up tables for CV_8U
depth images.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With