简体   繁体   中英

How does cv2.IMREAD_GRAYSCALE convert 16 bit images to 8 bit (OpenCV python)?

I am importing images in python using OpenCV.

import cv2
img = cv2.imread(img, cv2.IMREAD_GRAYSCALE)

The image is a 16 bit .png or .tif , and is converted to 8 bit by cv2.IMREAD_GRAYSCALE (as expected). I am happy that OpenCV converts my image. I am aware that I can use cv2.IMREAD_UNCHANGED if I want a 16bit image.

I just want to know how OpenCV is converting my image from 16 to 8 bit. Eg via typical normalisation, or in some other way that might saturate pixels? For my downstream problems it is more important that the general range is preserved.

I have checked the OpenCV documentation and cannot find an explanation for this. I cannot find a similar question.

EDIT: My images are single channel.

The uint 16 image ( or the 16 bit image ) used values from 0 to 2^16-1, while the uint8 images ( or the 8 bit image ) uses a range from, 0 to 2^8-1 only. If you just cast the original values to uint8, the saturation will destroy a lot of information. This is seen in the form of quality degradation. This happens due to bit depth.


“Bit-depth” determines the smallest changes you can make, relative to some range of values. If our scale is brightness from pure black to pure white, then the 4 values we get from a 2-bit number would include: black, dark midtones, light midtones, and white. This is a pretty lumpy scale and not very useful for a photograph. But if we have enough bits, we have enough gray values to make what appears to be a perfectly smooth gradient from black to white.

Here's an example comparing a black to white gradient at different bit depths. The embedded image here is just an example, click here to see the full resolution image in the JPEG2000 format with bit depths up to 14-bits. Depending on the quality of your monitor, you can probably only display differences up to 8-10 bits.


查看此图片以更好地理解


All software designs use and implement the same process.

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