简体   繁体   中英

Transform image from BGR to sRGB in Numpy/OpenCV

Given a numpy array of dimensions (width, height, 3) in BGR format, how can I transform this image into sRGB space? OpenCV ( cv2 ) does not implement this transformation, nor any transformation to sRGB for that matter.

I have conducted some research and could not find any function that does this, or even a formula that I can follow to implement my own function.

Any suggestion or guidance? And why is this transformation such an obscure thing on the internet?

BGR format is already sRGB with the channels reversed. OpenCV reads an input sRGB image into BGR format, but does not linearize the channels. The data is read as is, but the channels are swapped. When it writes an image out, it is then again in sRGB format. However, if the input image is linear RGB, then I suspect OpenCV would not change that and you would have to convert to non-linear sRGB, if you wanted that as output. As far as I know, OpenCV does not have a method of changing between non-linear and linear RGB in either direction. You might be able to do that with profiles in PIL.

Linear RGB is often used for certain kinds of processing such as resizing in order to avoid some kinds of subtle artifacts. But one starts with sRGB, converts to linear RGB, process the image, then converts back to sRGB. Linear does not mean or give poor quality. It is just darker.

Here is a non-linear sRGB image, which looks as expected:

在此处输入图像描述

Here is a linear RGB image, which is just darker:

在此处输入图像描述

The linear one comes from Imagemagick with the command:

convert lena.png -colorspace RGB lena_srgb.png

and the non-linear one is:

convert lena.png -colorspace sRGB lena_srgb.png

which does nothing to change the image, since it was already sRGB.

Most tools do not distinguish between RGB and sRGB. They often refer to sRGB as just RGB.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM