pyosv.pre.normalizer
1import numpy as np 2 3 4def percentile_prescaler(img : np.ndarray , perc : int, mmin : float = None) -> np.ndarray: 5 ''' 6 Clip image between minimum and maximum based on the percentile 7 8 Parameters: 9 ----------- 10 - img : np.ndarray 11 a WxHxB image, with W width, H height and B bands 12 - perc: int 13 the percentile value 14 - mmin : float 15 the minimum value to clip the img (default : None), if None mmin is calculated from img 16 17 Returns: 18 -------- 19 - img : np.ndarray 20 normalized WxHxB image, with W width, H height and B bands 21 22 Usage: 23 ------ 24 ```python 25 import numpy as np 26 27 img = np.array( 28 [[ 29 [0.1, 0.2, 0.3], 30 [0.4, 0.5, 0.6], 31 [0.7, 0.8, 0.9] 32 ], 33 [ 34 [1.1, 1.2, 1.3], 35 [1.4, 1.5, 1.6], 36 [1.7, 1.8, 1.9] 37 ] 38 ] 39 ) 40 41 # Making channels last 42 img = np.moveaxis(img, 0, -1) 43 44 img_n = percentile_prescaler(img, perc=95, mmin=0) 45 ``` 46 47 Output: 48 ------- 49 ``` 50 [[[0.1 1.1 ] 51 [0.2 1.2 ] 52 [0.3 1.3 ]] 53 [[0.4 1.4 ] 54 [0.5 1.5 ] 55 [0.6 1.6 ]] 56 [[0.7 1.7 ] 57 [0.8 1.8 ] 58 [0.9 1.815]]] 59 ``` 60 ''' 61 62 63 if mmin is None: mmin = np.nanmin(img) 64 mmax = np.nanpercentile(img, perc) 65 img = np.clip(img, mmin, mmax) 66 67 return img 68 69 70def minmax_scaler(img : np.ndarray, mmin : float = None, mmax : float = None, clip : list = [None, None]) -> np.ndarray: 71 ''' 72 Apply the min max scaler to the input img: 73 74 out = (img - minimum)/(maximum - minimum + E) (1) 75 76 where E stabilizes the division. 77 78 Parameters: 79 ----------- 80 - img : np.ndarray 81 a WxHxB image, with W width, H height and B bands 82 - mmin : list 83 the minimum in equation (1) (default : None) 84 - mmax : lsit 85 the maximum in equation (1) (default : None) 86 - clip : list 87 a list of two values used to constrain the image values (default : [None, None]) 88 89 Returns: 90 -------- 91 - img : np.ndarray 92 normalized WxHxB image, with W width, H height and B bands 93 94 Usage: 95 ------ 96 ```python 97 import numpy as np 98 99 img = np.array( 100 [[ 101 [0.1, 0.2, 0.3], 102 [0.4, 0.5, 0.6], 103 [0.7, 0.8, 0.9] 104 ], 105 [ 106 [1.1, 1.2, 1.3], 107 [1.4, 1.5, 1.6], 108 [1.7, 1.8, 1.9] 109 ] 110 ] 111 ) 112 113 # Making channels last 114 img = np.moveaxis(img, 0, -1) 115 116 img_n = minmax_scaler(img, mmin=0, mmax=1, clip = [0,0.5]) 117 ``` 118 119 Output: 120 ------- 121 ``` 122 [[[0.0999001 0.5 ] 123 [0.1998002 0.5 ] 124 [0.2997003 0.5 ]] 125 [[0.3996004 0.5 ] 126 [0.4995005 0.5 ] 127 [0.5 0.5 ]] 128 [[0.5 0.5 ] 129 [0.5 0.5 ] 130 [0.5 0.5 ]]] 131 ``` 132 ''' 133 134 if mmin == None: mmin = np.nanmin(img) 135 if mmax == None: mmax = np.nanmax(img) 136 num = (img - mmin) 137 den = (mmax - mmin) 138 img = np.divide(num, den, where=den != 0) 139 if (clip[0] is not None) and (clip[1] is not None): 140 img = np.clip(img, clip[0], clip[-1]) 141 142 return img 143 144 145def std_scaler(img : np.ndarray, mmean : float = None, sstd : float = None, clip : list = [None, None]) -> np.ndarray: 146 ''' 147 Apply the min max scaler to the input img: 148 149 out = (img - mean)/(std) (1) 150 151 Parameters: 152 ----------- 153 - img : np.ndarray 154 a WxHxB image, with W width, H height and B bands 155 - mmean : list 156 the mean in equation (1) (default : None) 157 - sstd : lsit 158 the standar deviation in equation (1) (default : None) 159 - clip : list 160 a list of two values used to constrain the image values (default : [None, None]) 161 162 Returns: 163 -------- 164 - img : np.ndarray 165 normalized WxHxB image, with W width, H height and B bands 166 167 Usage: 168 ------ 169 ```python 170 import numpy as np 171 172 img = np.array( 173 [[ 174 [0.1, 0.2, 0.3], 175 [0.4, 0.5, 0.6], 176 [0.7, 0.8, 0.9] 177 ], 178 [ 179 [1.1, 1.2, 1.3], 180 [1.4, 1.5, 1.6], 181 [1.7, 1.8, 1.9] 182 ] 183 ] 184 ) 185 186 # Making channels last 187 img = np.moveaxis(img, 0, -1) 188 189 img_n = std_scaler(img, None, None, clip = [0,0.5]) 190 ``` 191 192 Output: 193 ------- 194 ``` 195 [[[0. 0.17770466] 196 [0. 0.35540933] 197 [0. 0.5 ]] 198 [[0. 0.5 ] 199 [0. 0.5 ] 200 [0. 0.5 ]] 201 [[0. 0.5 ] 202 [0. 0.5 ] 203 [0. 0.5 ]]] 204 ``` 205 ''' 206 207 if mmean == None: mmean = np.nanmean(img) 208 if sstd == None: sstd = np.nanstd(img) 209 img = np.divide((img - mmean), std, where=std != 0) 210 211 if (clip[0] is not None) and (clip[1] is not None): 212 img = np.clip(img, clip[0], clip[-1]) 213 214 return img
def
percentile_prescaler(img: numpy.ndarray, perc: int, mmin: float = None) -> numpy.ndarray:
5def percentile_prescaler(img : np.ndarray , perc : int, mmin : float = None) -> np.ndarray: 6 ''' 7 Clip image between minimum and maximum based on the percentile 8 9 Parameters: 10 ----------- 11 - img : np.ndarray 12 a WxHxB image, with W width, H height and B bands 13 - perc: int 14 the percentile value 15 - mmin : float 16 the minimum value to clip the img (default : None), if None mmin is calculated from img 17 18 Returns: 19 -------- 20 - img : np.ndarray 21 normalized WxHxB image, with W width, H height and B bands 22 23 Usage: 24 ------ 25 ```python 26 import numpy as np 27 28 img = np.array( 29 [[ 30 [0.1, 0.2, 0.3], 31 [0.4, 0.5, 0.6], 32 [0.7, 0.8, 0.9] 33 ], 34 [ 35 [1.1, 1.2, 1.3], 36 [1.4, 1.5, 1.6], 37 [1.7, 1.8, 1.9] 38 ] 39 ] 40 ) 41 42 # Making channels last 43 img = np.moveaxis(img, 0, -1) 44 45 img_n = percentile_prescaler(img, perc=95, mmin=0) 46 ``` 47 48 Output: 49 ------- 50 ``` 51 [[[0.1 1.1 ] 52 [0.2 1.2 ] 53 [0.3 1.3 ]] 54 [[0.4 1.4 ] 55 [0.5 1.5 ] 56 [0.6 1.6 ]] 57 [[0.7 1.7 ] 58 [0.8 1.8 ] 59 [0.9 1.815]]] 60 ``` 61 ''' 62 63 64 if mmin is None: mmin = np.nanmin(img) 65 mmax = np.nanpercentile(img, perc) 66 img = np.clip(img, mmin, mmax) 67 68 return img
Clip image between minimum and maximum based on the percentile
Parameters:
- img : np.ndarray
a WxHxB image, with W width, H height and B bands
- perc: int
the percentile value
- mmin : float
the minimum value to clip the img (default : None), if None mmin is calculated from img
Returns:
- img : np.ndarray
normalized WxHxB image, with W width, H height and B bands
Usage:
import numpy as np
img = np.array(
[[
[0.1, 0.2, 0.3],
[0.4, 0.5, 0.6],
[0.7, 0.8, 0.9]
],
[
[1.1, 1.2, 1.3],
[1.4, 1.5, 1.6],
[1.7, 1.8, 1.9]
]
]
)
# Making channels last
img = np.moveaxis(img, 0, -1)
img_n = percentile_prescaler(img, perc=95, mmin=0)
Output:
[[[0.1 1.1 ]
[0.2 1.2 ]
[0.3 1.3 ]]
[[0.4 1.4 ]
[0.5 1.5 ]
[0.6 1.6 ]]
[[0.7 1.7 ]
[0.8 1.8 ]
[0.9 1.815]]]
def
minmax_scaler( img: numpy.ndarray, mmin: float = None, mmax: float = None, clip: list = [None, None]) -> numpy.ndarray:
71def minmax_scaler(img : np.ndarray, mmin : float = None, mmax : float = None, clip : list = [None, None]) -> np.ndarray: 72 ''' 73 Apply the min max scaler to the input img: 74 75 out = (img - minimum)/(maximum - minimum + E) (1) 76 77 where E stabilizes the division. 78 79 Parameters: 80 ----------- 81 - img : np.ndarray 82 a WxHxB image, with W width, H height and B bands 83 - mmin : list 84 the minimum in equation (1) (default : None) 85 - mmax : lsit 86 the maximum in equation (1) (default : None) 87 - clip : list 88 a list of two values used to constrain the image values (default : [None, None]) 89 90 Returns: 91 -------- 92 - img : np.ndarray 93 normalized WxHxB image, with W width, H height and B bands 94 95 Usage: 96 ------ 97 ```python 98 import numpy as np 99 100 img = np.array( 101 [[ 102 [0.1, 0.2, 0.3], 103 [0.4, 0.5, 0.6], 104 [0.7, 0.8, 0.9] 105 ], 106 [ 107 [1.1, 1.2, 1.3], 108 [1.4, 1.5, 1.6], 109 [1.7, 1.8, 1.9] 110 ] 111 ] 112 ) 113 114 # Making channels last 115 img = np.moveaxis(img, 0, -1) 116 117 img_n = minmax_scaler(img, mmin=0, mmax=1, clip = [0,0.5]) 118 ``` 119 120 Output: 121 ------- 122 ``` 123 [[[0.0999001 0.5 ] 124 [0.1998002 0.5 ] 125 [0.2997003 0.5 ]] 126 [[0.3996004 0.5 ] 127 [0.4995005 0.5 ] 128 [0.5 0.5 ]] 129 [[0.5 0.5 ] 130 [0.5 0.5 ] 131 [0.5 0.5 ]]] 132 ``` 133 ''' 134 135 if mmin == None: mmin = np.nanmin(img) 136 if mmax == None: mmax = np.nanmax(img) 137 num = (img - mmin) 138 den = (mmax - mmin) 139 img = np.divide(num, den, where=den != 0) 140 if (clip[0] is not None) and (clip[1] is not None): 141 img = np.clip(img, clip[0], clip[-1]) 142 143 return img
Apply the min max scaler to the input img:
out = (img - minimum)/(maximum - minimum + E) (1)
where E stabilizes the division.
Parameters:
- img : np.ndarray
a WxHxB image, with W width, H height and B bands
- mmin : list
the minimum in equation (1) (default : None)
- mmax : lsit
the maximum in equation (1) (default : None)
- clip : list
a list of two values used to constrain the image values (default : [None, None])
Returns:
- img : np.ndarray
normalized WxHxB image, with W width, H height and B bands
Usage:
import numpy as np
img = np.array(
[[
[0.1, 0.2, 0.3],
[0.4, 0.5, 0.6],
[0.7, 0.8, 0.9]
],
[
[1.1, 1.2, 1.3],
[1.4, 1.5, 1.6],
[1.7, 1.8, 1.9]
]
]
)
# Making channels last
img = np.moveaxis(img, 0, -1)
img_n = minmax_scaler(img, mmin=0, mmax=1, clip = [0,0.5])
Output:
[[[0.0999001 0.5 ]
[0.1998002 0.5 ]
[0.2997003 0.5 ]]
[[0.3996004 0.5 ]
[0.4995005 0.5 ]
[0.5 0.5 ]]
[[0.5 0.5 ]
[0.5 0.5 ]
[0.5 0.5 ]]]
def
std_scaler( img: numpy.ndarray, mmean: float = None, sstd: float = None, clip: list = [None, None]) -> numpy.ndarray:
146def std_scaler(img : np.ndarray, mmean : float = None, sstd : float = None, clip : list = [None, None]) -> np.ndarray: 147 ''' 148 Apply the min max scaler to the input img: 149 150 out = (img - mean)/(std) (1) 151 152 Parameters: 153 ----------- 154 - img : np.ndarray 155 a WxHxB image, with W width, H height and B bands 156 - mmean : list 157 the mean in equation (1) (default : None) 158 - sstd : lsit 159 the standar deviation in equation (1) (default : None) 160 - clip : list 161 a list of two values used to constrain the image values (default : [None, None]) 162 163 Returns: 164 -------- 165 - img : np.ndarray 166 normalized WxHxB image, with W width, H height and B bands 167 168 Usage: 169 ------ 170 ```python 171 import numpy as np 172 173 img = np.array( 174 [[ 175 [0.1, 0.2, 0.3], 176 [0.4, 0.5, 0.6], 177 [0.7, 0.8, 0.9] 178 ], 179 [ 180 [1.1, 1.2, 1.3], 181 [1.4, 1.5, 1.6], 182 [1.7, 1.8, 1.9] 183 ] 184 ] 185 ) 186 187 # Making channels last 188 img = np.moveaxis(img, 0, -1) 189 190 img_n = std_scaler(img, None, None, clip = [0,0.5]) 191 ``` 192 193 Output: 194 ------- 195 ``` 196 [[[0. 0.17770466] 197 [0. 0.35540933] 198 [0. 0.5 ]] 199 [[0. 0.5 ] 200 [0. 0.5 ] 201 [0. 0.5 ]] 202 [[0. 0.5 ] 203 [0. 0.5 ] 204 [0. 0.5 ]]] 205 ``` 206 ''' 207 208 if mmean == None: mmean = np.nanmean(img) 209 if sstd == None: sstd = np.nanstd(img) 210 img = np.divide((img - mmean), std, where=std != 0) 211 212 if (clip[0] is not None) and (clip[1] is not None): 213 img = np.clip(img, clip[0], clip[-1]) 214 215 return img
Apply the min max scaler to the input img:
out = (img - mean)/(std) (1)
Parameters:
- img : np.ndarray
a WxHxB image, with W width, H height and B bands
- mmean : list
the mean in equation (1) (default : None)
- sstd : lsit
the standar deviation in equation (1) (default : None)
- clip : list
a list of two values used to constrain the image values (default : [None, None])
Returns:
- img : np.ndarray
normalized WxHxB image, with W width, H height and B bands
Usage:
import numpy as np
img = np.array(
[[
[0.1, 0.2, 0.3],
[0.4, 0.5, 0.6],
[0.7, 0.8, 0.9]
],
[
[1.1, 1.2, 1.3],
[1.4, 1.5, 1.6],
[1.7, 1.8, 1.9]
]
]
)
# Making channels last
img = np.moveaxis(img, 0, -1)
img_n = std_scaler(img, None, None, clip = [0,0.5])
Output:
[[[0. 0.17770466]
[0. 0.35540933]
[0. 0.5 ]]
[[0. 0.5 ]
[0. 0.5 ]
[0. 0.5 ]]
[[0. 0.5 ]
[0. 0.5 ]
[0. 0.5 ]]]