Search code examples
opencvimagemagickrgbsrgb

opencv sRGB to RGB Conversion


I have iphone photos which are non-linear sRGB format. I need linear RGB. I couldnt find a way in opencv(python) to convert them to linear RGB.

Also tried ImageMagick convert :

convert output0.jpg  -colorspace RGB output0.jpg

Still same sRGB no effect.

Any ideas ?


Solution

  • Only a very few image formats truly support linear RGB. PNG does, but not JPG. You can tell if the gamma value is 1, it is linear. You can process in linear colorspace with ImageMagick, but saving to JPG may show the proper linear pixel values, but the JPG meta data does not store the fact that it is linear RGB. You can tell if the pixel data has been changed to linear if the image is darker. You could assign a linear profile to the JPG, but I am not sure where you would find one. Note also in very old version of ImageMagick -colorspace RGB was actually producing sRGB and vice-versa

    For example, you command makes no change, since JPG does not truly support linear RGB.

    Input:

    enter image description here

    convert logo.jpg -colorspace RGB logoRGB.jpg
    

    enter image description here


    But PNG does:

    convert logo.jpg -colorspace RGB logoRGB.png
    

    enter image description here

    Colorspace: RGB
    Gamma: 1
    

    In order to force the conversion to linear RGB in JPG, you can do the following to make the pixel data linear. But the meta data will still show non-linear sRGB, gamma=0.4545

    convert logo.jpg -colorspace RGB -set colorspace sRGB logoRGB2.jpg
    

    enter image description here

    Colorspace: sRGB
    Gamma: 0.454545
    

    EDIT: The following link may be useful if one wants a Linear RGB profile. See http://fnordware.blogspot.com/2008/05/making-linear-icc-profile.html