Geographic Information Systems Asked on April 1, 2021
I want to generate a set of samples from a Sentinel-2 image by tiling it, using mainly gdal_translate
. The tiling is ok, but its histogram seems to vary tile to tile. So, what I thought was to apply the same min/max from original image to each tile, but didn’t workout.
After to search and use examples found here and here, the tiles still present variations. The code used:
name, file_extension = os.path.splitext(image)
image_path = os.path.join(image_folder, image)
ds = gdal.Open(image_path)
stats = [ds.GetRasterBand(i + 1).GetStatistics(True, True) for i in range(ds.RasterCount)]
vmin, vmax, vmean, vstd = zip(*stats)
rows = ds.RasterXSize
cols = ds.RasterYSize
tiles_cols = cols / width
tiles_rows = rows / height
logging.info(">>>> Tiling image {}. {} x {} pixels. Estimated {} tiles of {} x {}..."
.format(image, rows, cols, round(tiles_cols * tiles_rows), width, height))
gdal.UseExceptions()
for i in range(0, rows, width):
for j in range(0, cols, height):
try:
output = os.path.join(output_folder, name + "_" + str(i) + "_" + str(j) + file_extension)
gdal.Translate(output, ds, format='GTIFF', srcWin=[i, j, width, height],
outputType=gdal.GDT_UInt16, scaleParams=[[list(zip(*[vmin, vmax]))]], options=['-epo'])
except RuntimeError as error:
logging.info(">>>>>> Partially outside the image. {} Not saved!".format(error))
when I open the tiles individually, it seems to have different histograms, even the stretching parameters are unique (calculated from original image, see stats
) to every generated tile.
Am I missing something or should I see a uniform tiled-image?
Get help from others!
Recent Answers
Recent Questions
© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP