0

I am trying to do data augmentation on sentinel 2. Everything else is working and the only problem I have is the output I am getting is only one band when the input image has 10 bands. I also tried with rasterio but the results are still only one band.

Why am I only getting one band as output?

from keras.preprocessing.image import ImageDataGenerator, array_to_img, img_to_array, load_img
# import io
from PIL import Image
from osgeo import gdal
import cv2
import rasterio
import numpy as np
from osgeo import gdal

datagen = ImageDataGenerator(
        rotation_range=180,
        width_shift_range=0,
        height_shift_range=0,
        shear_range=0,
        zoom_range=0.5,
        
        horizontal_flip=True,
        vertical_flip = True,
        fill_mode='nearest')    #Also try nearest, constant, reflect, wrap


a = 1
while True:
    ds = gdal.Open("E:\\opencv\\12.tif")
    myarray = np.array(ds.ReadAsArray())
       ""Rasterio code"" 
    #fp = r'E:\\opencv\\12.tif'
    #img = rasterio.open(fp)
    #img = img.read()
    """"""
    x = img_to_array(myarray)
    x = x.reshape(x.shape + (1, ))  
    i = 1
    for batch in datagen.flow(x, batch_size=16,  
        save_to_dir='E:\\opencv\\', 
        save_prefix= a, 
        save_format='tif'):
        i += 1
        if i > 1:
            break 
    a += 1
    if a > 3:
        break

1 Answer 1

1

You will need to specify which band in the dataset you want to read as an array. I would also open your dataset outside of the while loop as this will prevent corruption

a = 1
ds = gdal.Open("E:\\opencv\\12.tif")

while True:
    band = ds.GetRasterBand(a)
    myarray = band.ReadAsArray()
    x = img_to_array(myarray)
    x = x.reshape(x.shape + (1, ))  
    i = 1
    for batch in datagen.flow(x, batch_size=16,  
        save_to_dir='E:\\opencv\\', 
        save_prefix= a, 
        save_format='tif'):
        i += 1
        if i > 1:
            break 
    a += 1
    if a > 12: #ensures you get all 12 bands
        break
del ds
0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.