简体   繁体   中英

How to create three band combination images from multiple gray scale images for CNN

I am using CNN in Keras. I have 21 gray scale GeoTiff images and need to create 450 three layered band combinations to be used as 'rgb' in the code below.

The first 5 gray scale images are main data and the rest are ancillary images. The combination member has either a) one member from main images and two members from ancillary images or b) two members from main images and one member from ancillary images.

The code to create possible three layer combinations from two lists can be found below. How to identify 3 layer combinations from different lists

My question is that how I can apply it to images and create band combinations consisting of three grayscale image?

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)

testing the solution for 5 images:

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()

Not too sure which part you are having difficulty with, but here are some thoughts.


First, make primary and ancillary lists to use with the code from your other linked question:

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

Now, rather than writing out the code 21 times to read your images, use a loop something like this:

# 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)

Next, call the code you linked to (passing primary and ancillary ) to get the desired combinations and iterate over them in a loop.


For each combination, merge the desired three single-channel images ( channel1 , channel2 and channel3 ) into one 3-channel image:

import numpy as np

merged = np.dstack((channel1, channel2, channel3))

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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