【发布时间】: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