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
PolyGeo
65.5k29 gold badges115 silver badges350 bronze badges
asked Apr 21, 2021 at 7:12

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
answered Apr 21, 2021 at 12:04
0

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.