utils.py
1 #!/usr/bin/env python3 2 # -*- coding: utf-8 -*- 3 """ 4 Created on Wed May 18 14:50:06 2022 5 6 @author: aleoikon 7 """ 8 9 import numpy as np 10 from scipy.ndimage import zoom 11 12 def adjust_shape(I, s): 13 """Adjust shape of grayscale image I to s.""" 14 # crop if necesary 15 I = I[:s[0],:s[1]] 16 si = I.shape 17 18 # pad if necessary 19 p0 = max(0,s[0] - si[0]) 20 p1 = max(0,s[1] - si[1]) 21 22 return np.pad(I,((0,p0),(0,p1)),'edge') 23 24 def feature_scaling(img, method): 25 I = img 26 if method == "STAND": 27 I = (I - I.mean()) / I.std() 28 return I 29 if method == "MINMAX": 30 I = ((I - np.nanmin(I))/(np.nanmax(I) - np.nanmin(I))) 31 return I 32 else: 33 return I 34 35 def create_rgb(x,channel): 36 if channel == 'red': 37 r = x[:,:,1] 38 r = np.expand_dims(r, axis=2) 39 return r 40 if channel == 'green': 41 g = x[:,:,2] 42 g = np.expand_dims(g, axis=2) 43 return g 44 if channel == 'blue': 45 b = x[:,:,3] 46 b = np.expand_dims(b, axis=2) 47 return b 48 if channel == 'rgb': 49 r = x[:,:,1] 50 g = x[:,:,2] 51 b = x[:,:,3] 52 rgb = np.dstack((r,g,b)) 53 return(rgb) 54 if channel == 'rgbvnir': 55 r = x[:,:,1] 56 g = x[:,:,2] 57 b = x[:,:,3] 58 vnir = x[:,:,8] 59 rgbvnir = np.stack((r,g,b,vnir),axis=2).astype('float') 60 #rgb = np.dstack((r,g,b)) 61 return(rgbvnir) 62 if channel == 'eq20': 63 r = x[:,:,1] 64 s = r.shape 65 ir1 = adjust_shape(zoom(x[:,:,4],2),s) 66 ir2 = adjust_shape(zoom(x[:,:,5],2),s) 67 ir3 = adjust_shape(zoom(x[:,:,6],2),s) 68 nir2 = adjust_shape(zoom(x[:,:,8],2),s) 69 swir2 = adjust_shape(zoom(x[:,:,11],2),s) 70 swir3 = adjust_shape(zoom(x[:,:,12],2),s) 71 x = np.stack((ir1,ir2,ir3,nir2,swir2,swir3),axis=2).astype('float') 72 return x 73 else: 74 return x 75 print("NOT CORRECT CHANNELS")