Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/image/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 将3通道16位图像转换为8位,同时保留颜色_Python_Image_Image Processing_Tiff - Fatal编程技术网

Python 将3通道16位图像转换为8位,同时保留颜色

Python 将3通道16位图像转换为8位,同时保留颜色,python,image,image-processing,tiff,Python,Image,Image Processing,Tiff,我有一个3通道16位图像的tiff文件。我想把它们转换成8位3通道图像,但当我做一个简单的缩放时,我发现那些以红色为主的图像会变成全黑色。有没有一种方法可以在保持原始16位图像颜色的同时进行这种转换。现在我有这个密码 for r in root_: files = os.listdir(r) for f in files: if "tif" in f[-3:]: filepath = r+"/"+f tif = TIFFfile(filepath)

我有一个3通道16位图像的tiff文件。我想把它们转换成8位3通道图像,但当我做一个简单的缩放时,我发现那些以红色为主的图像会变成全黑色。有没有一种方法可以在保持原始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')

我将提取3个通道:

c1 = test[:,:][0]
c2 = test[:,:][1]
c3 = test[:,:][2]
c1new = bytescale(c1)
c2new = bytescale(c2)
c3new = bytescale(c3)
使用辅助功能将其缩放到8位:

def bytescale(image, cmin=None, cmax=None, high=255, low=0):

    if image.dtype == np.uint8:
        return image

    if high > 255:
        high = 255
    if low < 0:
        low = 0
    if high < low:
        raise ValueError("`high` should be greater than or equal to `low`.")

    if cmin is None:
        cmin = image.min()
    if cmax is None:
        cmax = image.max()

    cscale = cmax - cmin
    if cscale == 0:
        cscale = 1

    scale = float(high - low) / cscale
    bytedata = (image - cmin) * scale + low
    return (bytedata.clip(low, high) + 0.5).astype(np.uint8)
把所有这些放在一起:

x = np.array([c1new, c2new, c3new])

如果有帮助,请告诉我。

我猜您希望应用自适应范围调整

在某些全局最小值和全局最大值之间进行线性“拉伸”是一个简单的解决方案。
找到上百分位和下百分位是比最小值和最大值更稳健的解决方案

以下是一个例子:

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:

试着修改你的问题,这样就不需要猜测了


请让我知道我的猜测是否正确

这就是我想要的回答你的问题?嘿,谢谢,这看起来很有趣,我发布了答案,它符合我的要求,但这看起来像一个巧妙的小把戏,我一定会记住的谢谢,这看起来真的很巧妙