normalize_multiple.py
Go to the documentation of this file.
1 import gdal
2 import numpy as np
3 import sys
4 import os
5 
6 num_sets = len(sys.argv) - 1
7 inputs = sys.argv[1:]
8 mins = np.empty((num_sets), dtype=np.float32)
9 means = np.empty((num_sets), dtype=np.float32)
10 maxs = np.empty((num_sets), dtype=np.float32)
11 
12 # determine min, max and mean of each dataset
13 for i in range(0, num_sets):
14  ds = gdal.Open(inputs[i], gdal.GA_ReadOnly)
15 
16  raster_count = ds.RasterCount
17  x_size = ds.RasterXSize
18  y_size = ds.RasterYSize
19  num_channels = raster_count - 1
20 
21  values = np.empty((raster_count, y_size, x_size), dtype=np.float32)
22  for j in range(0, num_channels):
23  values[j] = np.array(ds.GetRasterBand(j + 1).ReadAsArray()).astype(np.float32)
24 
25  mins[i] = np.amin(values)
26  means[i] = np.mean(values)
27  maxs[i] = np.amax(values)
28 
29  ds = None
30  values = None
31 
32 # determine global min, max and mean
33 all_min = np.amin(mins)
34 print("Calculated minimum value over data sets: ", all_min)
35 all_mean = np.mean(means)
36 print("Calculated mean value over data sets: ", all_mean)
37 all_max = np.amax(maxs)
38 print("Calculated maximum value over data sets: ", all_max)
39 denominator = all_mean - all_min
40 
41 # perform normalization for each dataset
42 for i in range(0, num_sets):
43  ds = gdal.Open(inputs[i], gdal.GA_ReadOnly)
44 
45  raster_count = ds.RasterCount
46  x_size = ds.RasterXSize
47  y_size = ds.RasterYSize
48  num_channels = raster_count - 1
49 
50  values = np.empty((raster_count, y_size, x_size), dtype=np.float32)
51  for j in range(0, num_channels):
52  values[j] = np.array(ds.GetRasterBand(j + 1).ReadAsArray()).astype(np.float32)
53  ds = None
54 
55  values = ((values - all_min) / denominator)
56  values = values * (all_max / 2.)
57  values = values.astype(np.int)
58 
59  driver = gdal.GetDriverByName("GTiff")
60  filename, file_extension = os.path.splitext(inputs[i])
61  ds = driver.Create(filename + "_normed" + file_extension, x_size, y_size, raster_count, gdal.GDT_UInt16)
62  for j in range(0, num_channels):
63  ds.GetRasterBand(j + 1).WriteArray(values[j])
64 
65  values = None
66  ds = None


lvr2
Author(s): Thomas Wiemann , Sebastian Pütz , Alexander Mock , Lars Kiesow , Lukas Kalbertodt , Tristan Igelbrink , Johan M. von Behren , Dominik Feldschnieders , Alexander Löhr
autogenerated on Wed Mar 2 2022 00:37:24