简体   繁体   English

cv2.IMREAD_GRAYSCALE 如何将 16 位图像转换为 8 位(OpenCV python)?

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

I am importing images in python using OpenCV.我正在使用 OpenCV 在 python 中导入图像。

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).该图像是 16 位.png.tif ,并由cv2.IMREAD_GRAYSCALE转换为 8 位(如预期的那样)。 I am happy that OpenCV converts my image.我很高兴 OpenCV 转换了我的图像。 I am aware that I can use cv2.IMREAD_UNCHANGED if I want a 16bit image.我知道如果我想要 16 位图像,我可以使用cv2.IMREAD_UNCHANGED

I just want to know how OpenCV is converting my image from 16 to 8 bit.我只想知道 OpenCV如何将我的图像从 16 位转换为 8 位。 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.我检查了 OpenCV 文档,找不到对此的解释。 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. uint 16 图像(或 16 位图像)使用 0 到 2^16-1 的值,而 uint8 图像(或 8 位图像)仅使用 0 到 2^8-1 的范围。 If you just cast the original values to uint8, the saturation will destroy a lot of information.如果只是将原始值强制转换为 uint8,饱和会破坏大量信息。 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.如果我们的比例是从纯黑色到纯白色的亮度,那么我们从 2 位数字中得到的 4 个值将包括:黑色、深中间调、浅中间调和白色。 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.此处嵌入的图像只是一个示例,单击此处可查看位深高达 14 位的 JPEG2000 格式的全分辨率图像。 Depending on the quality of your monitor, you can probably only display differences up to 8-10 bits.根据显示器的质量,您可能最多只能显示 8-10 位的差异。


查看此图片以更好地理解


All software designs use and implement the same process.所有软件设计都使用和实现相同的过程。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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