how to convert an image from BGR to LAB with opencv 2.4 python 2.7 and numpy

By : Gianfra
Source: Stackoverflow.com
Question!

Hy

I am working with opencv 2.4 and numpy. I would like to open an image and get all the informations about it (8 bit - if its RGB-BGR etc) and also try to change the color space. I have this code:

if __name__ == '__main__':
    img =  cv2.imread('imL.png') 
    conv= cv2.cvtColor(img, cv2.COLOR_BGR2LAB)
        cv2.imwrite('lab.png', conv )

But when i open the file lab.png i get the image with different colors! I check tha value of BGR to LAB in: http://www.brucelindbloom.com/

For this i would like to know all the information about one image.

thanks

By : Gianfra


Answers
That rigtht you will get a different image colour because imwrite() saves file in the format specified (PNG, JPEG 2000, and TIFF) single-channel or 3-channel (with ‘BGR’ channel order). images can be saved using this function. imwrite() doesn't know the format LAB to save image as it always expect the image in BGR. If the format, depth or channel order is different, use Mat::convertTo() , and cvtColor() to convert it before saving.

By : isrish


Lab is another color space, like the BGR color space which is gained from cv2.imread(). It just like you convert temperature from Fahrenheit to Celsius. 32 Fahrenheit and 0 Celsius is the same temperature but in different unit.

cv2.imwrite() dose not know if the values are in BGR color space or not. When it get a 3 dimension array, it assume that it is a BGR color space while your conv variable contains Lab color space. This is why your color of your image is changed.

For your information, Each layer of BGR color space contains blue, green and red colors while layers of Lab contains lightness (0-100), a* and b* respectively. For more information, please see "Lab color space" in Wikipedia.



This video can help you solving your question :)
By: admin