繁体   English   中英

从 1 个通道到 3 个通道的灰度图像,2 个额外的通道仅由零构成

[英]Gray scaled image from 1 channel to 3 channels, with the 2 extra channels made only by zeros

是否可以通过添加 2 个仅包含零的通道将具有 1 个通道的图像制作为具有 3 个通道的图像?

这是我想通过添加 2 个仅由零构成的额外通道将图像转换为 3 通道图像的文件

images.shape
(100, 120, 120, 1)

这是我现在用来在 1 个通道中导入图像的代码

def loadImages():

    dirname = '/c/disk/img_t1/'
    x_orig = np.zeros((100, 120, 120), dtype=np.float32) 

    for f in range(x_orig.shape[0]):
        img    = Image.open(dirname + 'img_t1_%05d.tiff' % (f))  
        img    = np.array(img)
        x_orig[f] = img

    
    path = '/c/labels.csv'    
    labels = pd.read_csv(path, usecols=["proportional", "category"],
                       sep=";" )
    y_orig = np.array(labels['category'])

    return x_orig, y_orig


x, y = loadImages()
plt.imshow(x[3])

这就是图像的样子

在此处输入图片说明

如果从相同的代码我只是从包 PIL 添加 .convert('RGB) 来添加其他 2 个通道

def loadImages():

    dirname = '/c/disk/img_t1/'
    x_orig = np.zeros((100, 120, 120,3), dtype=np.float32) 

    for f in range(x_orig.shape[0]):
        img    = Image.open(dirname + 'img_t1_%05d.tiff' % (f)).convert('RGB')  
        img    = np.array(img)
        x_orig[f] = img

    
    path = '/c/labels.csv'    
    labels = pd.read_csv(path, usecols=["proportional", "category"],
                       sep=";" )
    y_orig = np.array(labels['category'])

    return x_orig, y_orig


x, y = loadImages()
plt.imshow(x[3])

图像看起来像这样

在此处输入图片说明

所以我想要这样的东西

images.shape
(100, 120, 120, 3)

我想你正在尝试为机器学习或类似的东西做一些培训。

我认为您的文件可能是一个单通道的 32 位浮点 TIFF - 尽管您实际上共享了一个 PNG 文件。

我认为问题在于 PIL/Pillow 不支持 RGB(3 通道)浮动 - 请参阅文档

我不知道你接下来打算用你的数据做什么,所以我不知道你需要什么图像处理功能。 但我怀疑您可能需要转移到不同的库 - OpenCV、Wand、pyvips 或 ...

暂无
暂无

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

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