normalize.py
Go to the documentation of this file.
1 import gdal
2 import numpy as np
3 import sys
4 from scipy.signal import savgol_filter
5 
6 inname = sys.argv[1]
7 outname = sys.argv[2]
8 
9 ds = gdal.Open(inname, gdal.GA_ReadOnly)
10 if not ds:
11  print("Could not open file ", inname)
12 
13 raster_count = ds.RasterCount
14 x_size = ds.RasterXSize
15 y_size = ds.RasterYSize
16 
17 min_channel = 1
18 max_channel = ds.RasterCount
19 if len(sys.argv) == 5:
20  min_channel = int(sys.argv[3])
21  max_channel = int(sys.argv[4])
22 
23 num_channels = max_channel - min_channel
24 
25 values = np.empty((raster_count, y_size, x_size), dtype=np.float32)
26 for i in range(0, num_channels):
27  values[i] = np.array(ds.GetRasterBand(i + 1).ReadAsArray()).astype(np.float32)
28 
29 ds = None
30 
31 # source: https://gitlab.informatik.uni-osnabrueck.de/figelbri/hyperspace
32 # normalize data
33 shape = values.shape
34 values = values.reshape(values.shape[0], -1)
35 means = values.mean(axis=0) # mean of every spectrum
36 mins = values.min(axis=0) # min of every spectrum
37 #maxs = values.max(axis=0)
38 vmax = np.amax(values)
39 values = ((values - mins) / (means - mins))
40 
41 mins = None
42 means = None
43 
44 #values = values * (maxs / 2.)
45 values = values * vmax / 2.
46 
47 values = values.reshape(shape)
48 
49 # source: https://gitlab.informatik.uni-osnabrueck.de/figelbri/hyperspace
50 # smooth spectral data
51 values = savgol_filter(values, 11, 2, axis=0)
52 
53 # write normalized dataset
54 driver = gdal.GetDriverByName("GTiff")
55 ds = driver.Create(outname, x_size, y_size, raster_count, gdal.GDT_UInt16)
56 for i in range(0, num_channels):
57  ds.GetRasterBand(i + 1).WriteArray(values[i])
58 


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