python - 在保留颜色的同时将 3 channel 16 位图像转换为 8 位

标签 python image image-processing tiff

<分区>

我有一个 3 channel 16 位图像的 tiff 文件。我想将它们转换为 8 位 3 channel 图像,但是当我进行简单缩放时,我发现那些以红色为主的图像变成全黑。有没有办法在保留原始 16 位图像颜色的同时进行这种转换。现在我有这段代码。

for r in root_:
files = os.listdir(r)
for f in files:
    if "tif" in f[-3:]:
        filepath = r+"/"+f 
        tif = TIFFfile(filepath)
        samples, sample_names = tif.get_samples()
        test = np.moveaxis(samples[0], 0, 2)
        img8 = (test/256).astype('uint8')

最佳答案

我猜您想应用自适应范围调整。

在某些全局最小值和全局最大值之间进行线性“拉伸(stretch)”是一种简单的解决方案。
找到较低和较高的百分位数是比最小值和最大值更可靠的解决方案。

这是一个例子:

import cv2
import numpy as np

# Build input image for testing
test = cv2.imread('chelsea.png').astype(np.uint16) * 100

# lo - low value as percentile 0.1 (1/1000 of test values are below lo)
# hi - high value as percentile 99.9 (1/1000 of test values are above hi)
lo, hi = np.percentile(test, (0.1, 99.9))

# Apply linear "stretech" - lo goes to 0, and hi goes to 255
img8 = (test.astype(float) - lo) * (255/(hi-lo))

#Clamp range to [0, 255] and convert to uint8
img8 = np.maximum(np.minimum(img8, 255), 0).astype(np.uint8)

#Display images before and after linear "stretech":
cv2.imshow('test', test)
cv2.imshow('img8', img8)
cv2.waitKey(0)
cv2.destroyAllWindows()

结果:

测试:
enter image description here

img8:
enter image description here

尝试修改您的问题,以便减少猜测。

请告诉我我的猜测是否正确。

关于python - 在保留颜色的同时将 3 channel 16 位图像转换为 8 位,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60059626/

相关文章:

python - 导入 Django 2.0 实用函数(对比 django 1.1)

python - 使用 pygame 实现简单的 2D 重力;值(value)准则?

image - Canvas drawImage 在 CustomPainter 中缩放高度和宽度

css - 如何使用 <image> 标签回退缩放 SVG?

python - 使用相位相关和对数极坐标变换获得旋转位移

python - Django-根据查询集生成表单

python - 没有名为 ServerSocket 的模块

html - 垂直对齐的居中图像

image - 如何判断图像是否暗? (高对比度,低亮度)

c# - 文件去斑 - OCR