【问题标题】:How to create three band combination images from multiple gray scale images for CNN如何从 CNN 的多个灰度图像中创建三波段组合图像
【发布时间】:2021-06-02 01:44:09
【问题描述】:

我在 Keras 中使用 CNN。我有 21 个灰度 GeoTiff 图像,需要创建 450 个三层波段组合,在下面的代码中用作“rgb”。

前5张灰度图为主数据,其余为辅助图。组合成员具有 a) 一个来自主图像的成员和两个来自辅助图像的成员,或者 b) 两个来自主图像的成员和一个来自辅助图像的成员。

可以在下面找到从两个列表创建可能的三层组合的代码。 How to identify 3 layer combinations from different lists

我的问题是如何将其应用于图像并创建由三个灰度图像组成的波段组合?

image1 = Image.open('ras1.tif')
# convert first image to numpy array
img1 = asarray(image1)
.
.
.
image21 = Image.open('ras21.tif')
# convert last image to numpy array
img21 = asarray(image21)

from keras.preprocessing.image import ImageDataGenerator
train_datagen = ImageDataGenerator(rescale=1./255)

img_train_generator = train_datagen.flow_from_directory(
                "train/image/",
                target_size=(64, 64),
                batch_size=batch_size,
                class_mode= None,
                color_mode='rgb',
                shuffle=False)

测试 5 张图片的解决方案:

import os
import numpy as np
from PIL import Image
import matplotlib.pyplot as plt

path= 'C:/dataset/test'
os.chdir(path)

primary   = list(range(1,4))           # primary = [1, 2, 3]
ancillary = list(range(4,6))          # ancillary = [4, 5]

# Make empty list of images
images = []

# Load all images, primary and ancillary
for i in [*primary, *ancillary]:
    filename = f'ras{i}.tif'
    print(filename)
    im = np.array(Image.open(filename))
    images.insert(i,im)

result:

ras1.tif
ras2.tif
ras3.tif
ras4.tif
ras5.tif

def apply_op(primary, ancillary):
    ret = []
    for i in range(len(primary)):
        for j in range(i + 1, len(primary)):
            for k in range(len(ancillary)):
                ret.append((primary[i], primary[j], ancillary[k]))
    for i in range(len(primary)):
        for j in range(len(ancillary)):
            for k in range(j + 1, len(ancillary)):
                ret.append((primary[i], ancillary[j], ancillary[k]))
    return ret

print(apply_op(primary, ancillary))

result:

[(1, 2, 4), (1, 2, 5), (1, 3, 4), (1, 3, 5), (2, 3, 4), (2, 3, 5), (1, 4, 5), (2, 4, 5), (3, 4, 5)]

merged = np.dstack((1, 2, 5))

plt.imshow(merged)
plt.show()

【问题讨论】:

    标签: python image numpy multidimensional-array conv-neural-network


    【解决方案1】:

    不太确定您在哪个部分遇到困难,但这里有一些想法。


    首先,制作主要和辅助列表以与您的其他链接问题中的代码一起使用:

    primary   = list(range(1,6))           # primary = [1, 2, 3, 4, 5]
    ancillary = list(range(6,22))          # ancillary = [6, 7, ..., 20, 21]
    

    现在,与其写出 21 次代码来读取图像,不如使用类似这样的循环:

    # Make empty list of images
    images = []
    
    # Load all images, primary and ancillary
    for i in [*primary, *ancillary]:
        filename = f'ras{i}.tif'
        print(filename)
        im = np.array(Image.open(filename))
        images.insert(i,im)
    

    接下来,调用您链接到的代码(传递primaryancillary)以获取所需的组合并在循环中对其进行迭代。


    对于每个组合,将所需的三个单通道图像(channel1channel2channel3)合并为一个 3 通道图像:

    import numpy as np
    
    merged = np.dstack((channel1, channel2, channel3))
    

    【讨论】:

    • @Mark Setchell,感谢您的回答。请看问题。我测试了 5 张图像的解决方案,但它最后返回黑色图像。我不确定如何解决。
    • 而不是np.dstack((,2,3))你需要做result = np.dstack((images[a], images[b], images[c]))
    • @Mark Setchell,您的代码有助于可视化波段组合。是否可以在 train_datagen 中使用“for loop”将每个组合用作“rgb”?还是我应该对所有组合一一重复?
    猜你喜欢
    • 2022-08-05
    • 1970-01-01
    • 2020-02-21
    • 1970-01-01
    • 1970-01-01
    • 2020-02-13
    • 2022-11-15
    • 2015-05-30
    • 1970-01-01
    相关资源
    最近更新 更多