@@ -15,25 +15,22 @@ def percentile_normalize(
1515) -> np .ndarray :
1616 """Channelwise percentile normalization to range [0, 1].
1717
18- Parameters
19- ----------
20- img : np.ndarray:
18+ Parameters:
19+ img (np.ndarray):
2120 Input image to be normalized. Shape (H, W, C)|(H, W).
22- lower : float, default=0.01:
21+ lower ( float, default=0.01) :
2322 The lower percentile
24- upper : float, default=99.99:
23+ upper ( float, default=99.99) :
2524 The upper percentile
26- copy : bool, default=False
25+ copy ( bool, default=False):
2726 If True, normalize the copy of the input.
2827
29- Returns
30- -------
28+ Returns:
3129 np.ndarray:
3230 Normalized img. Same shape as input. dtype: float32.
3331
34- Raises
35- ------
36- ValueError
32+ Raises:
33+ ValueError:
3734 If input image does not have shape (H, W) or (H, W, C).
3835 """
3936 axis = (0 , 1 )
@@ -53,34 +50,30 @@ def percentile_normalize(
5350 upercentile = np .percentile (im , upper )
5451 lpercentile = np .percentile (im , lower )
5552
56- # return np.interp(im, (lpercentile, upercentile), axis).astype(np.float32)
57- return np .interp (im , (lpercentile , upercentile ), axis ) # .astype(np.float32)
53+ return np .interp (im , (lpercentile , upercentile ), axis ).astype (np .float32 )
5854
5955
6056def percentile_normalize99 (
6157 img : np .ndarray , amin : float = None , amax : float = None , copy : bool = False
6258) -> np .ndarray :
6359 """Channelwise 1-99 percentile normalization. Optional clamping.
6460
65- Parameters
66- ----------
67- img : np.ndarray:
61+ Parameters:
62+ img (np.ndarray)
6863 Input image to be normalized. Shape (H, W, C)|(H, W).
69- amin : float, optional
64+ amin ( float, default=None)
7065 Clamp min value. No clamping performed if None.
71- amax : float, optional
66+ amax ( float, default=None):
7267 Clamp max value. No clamping performed if None.
73- copy : bool, default=False
68+ copy ( bool, default=False):
7469 If True, normalize the copy of the input.
7570
76- Returns
77- -------
71+ Returns:
7872 np.ndarray:
7973 Normalized image. Same shape as input. dtype: float32.
8074
81- Raises
82- ------
83- ValueError
75+ Raises:
76+ ValueError:
8477 If input image does not have shape (H, W) or (H, W, C).
8578 """
8679 axis = (0 , 1 )
@@ -99,7 +92,7 @@ def percentile_normalize99(
9992 percentile99 = np .percentile (im , q = 99 , axis = axis )
10093 num = im - percentile1
10194 denom = percentile99 - percentile1
102- im = num / denom if denom != 0 else np . zeros_like ( im )
95+ im = num / denom
10396
10497 # clamp
10598 if not any (x is None for x in (amin , amax )):
@@ -117,27 +110,24 @@ def normalize(
117110) -> np .ndarray :
118111 """Channelwise mean centering or standardizing of an image. Optional clamping.
119112
120- Parameters
121- ----------
122- img : np.ndarray
113+ Parameters:
114+ img (np.ndarray):
123115 Input image to be normalized. Shape (H, W, C)|(H, W).
124- standardize: bool, default=True
116+ standardize ( bool, default=True):
125117 If True, divide with standard deviation after mean centering
126- amin : float, optional
118+ amin ( float, default=None):
127119 Clamp min value. No clamping performed if None.
128- amax : float, optional
120+ amax ( float, default=None):
129121 Clamp max value. No clamping performed if None.
130- copy : bool, default=False
122+ copy ( bool, default=False):
131123 If True, normalize the copy of the input.
132124
133- Returns
134- -------
125+ Returns:
135126 np.ndarray:
136127 Normalized image. Same shape as input. dtype: float32.
137128
138- Raises
139- ------
140- ValueError
129+ Raises:
130+ ValueError:
141131 If input image does not have shape (H, W) or (H, W, C).
142132 """
143133 axis = (0 , 1 )
@@ -157,7 +147,7 @@ def normalize(
157147
158148 if standardize :
159149 std = im .std (axis = axis , keepdims = True )
160- im = im / std if std != 0 else np . zeros_like ( im )
150+ im = np . divide ( im , std , where = std != 0 )
161151
162152 # clamp
163153 if not any (x is None for x in (amin , amax )):
@@ -171,25 +161,22 @@ def minmax_normalize(
171161) -> np .ndarray :
172162 """Min-max normalization per image channel. Optional clamping.
173163
174- Parameters
175- ----------
176- img : np.ndarray:
164+ Parameters:
165+ img (np.ndarray):
177166 Input image to be normalized. Shape (H, W, C)|(H, W).
178- amin : float, optional
167+ amin ( float, default=None):
179168 Clamp min value. No clamping performed if None.
180- amax : float, optional
169+ amax ( float, default=None):
181170 Clamp max value. No clamping performed if None.
182- copy : bool, default=False
171+ copy ( bool, default=False):
183172 If True, normalize the copy of the input.
184173
185- Returns
186- -------
174+ Returns:
187175 np.ndarray:
188176 Min-max normalized image. Same shape as input. dtype: float32.
189177
190- Raises
191- ------
192- ValueError
178+ Raises:
179+ ValueError;
193180 If input image does not have shape (H, W) or (H, W, C).
194181 """
195182 if img .ndim not in (2 , 3 ):
@@ -206,7 +193,7 @@ def minmax_normalize(
206193 max = im .max ()
207194 denom = max - min
208195 num = im - min
209- im = num / denom if denom != 0 else np . zeros_like ( im )
196+ im = num / denom
210197
211198 # clamp
212199 if not any (x is None for x in (amin , amax )):
@@ -221,16 +208,14 @@ def float2ubyte(mat: np.ndarray, normalize: bool = False) -> np.ndarray:
221208 Float matrix values need to be in range [-1, 1] for img_as_ubyte so
222209 the image is normalized or clamped before conversion.
223210
224- Parameters
225- ----------
226- mat : np.ndarray
211+ Parameters:
212+ mat (np.ndarray):
227213 A float64 matrix. Shape (H, W, C).
228214 normalize (bool, default=False):
229215 Normalizes input to [0, 1] first. If not True,
230216 clips values between [-1, 1].
231217
232- Returns
233- -------
218+ Returns:
234219 np.ndarray:
235220 A uint8 matrix. Shape (H, W, C). dtype: uint8.
236221 """
0 commit comments