我有一个 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()
结果:
测试:
img8:
尝试修改您的问题,以便减少猜测。
请告诉我我的猜测是否正确。