0

I am trying to do data augmentation on sentinel 2. Everything else is working the only problem I have is the output I am getting is only one band when the input image is with 10 band. I also tried with rasterio but the result are still only one band. Can someone tell me why i am only getting one band as ouptut. Thanks

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
0

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
3
  • Thank you for the reply.But i have one more question how can i create a composite of all the bands because the output will be many file of single band of different transformation.I wanted a single composite of all the band for each transformation
    – user123
    Apr 21 '21 at 12:24
  • You can use gdal_merge (gdal.org/programs/gdal_merge.html) with the - separate arguments to stack your different bands into 1 geotiff or create a new dataset in general gis.stackexchange.com/questions/164853/…. Apr 21 '21 at 13:32
  • I can use merge for single image but when i am working with multiple image it wont be possible to merge each band with each transformation individually.It would be really helpful if you tell me where to put the loop so that when i work with multiple image then the transformation will be done and i can get composite image of each transformation automatically, Sorry if my way question was wrong in the second comment
    – user123
    Apr 21 '21 at 14:15

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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