@@ -29,9 +29,11 @@ def extract_central(img, boxSize = None):
29
29
30
30
Keyword Arguments:
31
31
boxSize : size of cropping square, default is largest possible
32
+
33
+ Returns:
34
+ ndarray, cropped image
32
35
"""
33
36
34
-
35
37
w = np .shape (img )[0 ]
36
38
h = np .shape (img )[1 ]
37
39
@@ -46,71 +48,74 @@ def extract_central(img, boxSize = None):
46
48
return imgOut
47
49
48
50
49
- def to8bit (img , ** kwargs ):
51
+ def to8bit (img , minVal = None , maxVal = None ):
50
52
""" Returns an 8 bit representation of image. If min and max are specified,
51
53
these pixel values in the original image are mapped to 0 and 255
52
54
respectively, otherwise the smallest and largest values in the
53
55
whole image are mapped to 0 and 255, respectively.
54
56
55
57
Arguments:
56
- img : input image as 2D numpy array
58
+ img : ndarray
59
+ input image as 2D numpy array
57
60
58
61
Keyword Arguments:
59
- minVal : optional, pixel value to scale to 0
60
- maxVal : optional, pixel value to scale to 255
62
+ minVal : float
63
+ optional, pixel value to scale to 0
64
+ maxVal : float
65
+ optional, pixel value to scale to 255
61
66
"""
62
- minV = kwargs .get ("minVal" , None )
63
- maxV = kwargs .get ("maxVal" , None )
64
-
65
-
67
+
66
68
img = img .astype ('float64' )
67
69
68
- if minV is None :
69
- minV = np .min (img )
70
+ if minVal is None :
71
+ minVal = np .min (img )
70
72
71
- img = img - minV
73
+ img = img - minVal
72
74
73
- if maxV is None :
74
- maxV = np .max (img )
75
+ if maxVal is None :
76
+ maxVal = np .max (img )
75
77
else :
76
- maxV = maxV - minV
78
+ maxVal = maxVal - minVal
77
79
78
- img = img / maxV * 255
80
+ img = img / maxVal * 255
79
81
img = img .astype ('uint8' )
80
82
81
83
return img
82
84
83
85
84
- def to16bit (img , ** kwargs ):
86
+ def to16bit (img , minVal = None , maxVal = None ):
85
87
""" Returns an 16 bit representation of image. If min and max are specified,
86
88
these pixel values in the original image are mapped to 0 and 2^16
87
89
respectively, otherwise the smallest and largest values in the
88
90
whole image are mapped to 0 and 2^16 - 1, respectively.
89
91
90
92
Arguments:
91
- img : input image as 2D numpy array
93
+ img : ndarray
94
+ input image as 2D numpy array
92
95
93
96
Keyword Arguments:
94
- minVal : optional, pixel value to scale to 0
95
- maxVal : optional, pixel value to scale to 2^16 - 1
96
- """
97
- minV = kwargs .get ("minVal" , None )
98
- maxV = kwargs .get ("maxVal" , None )
99
-
97
+ minVal : float
98
+ optional, pixel value to scale to 0
99
+ maxVal : float
100
+ optional, pixel value to scale to 2^16 - 1
101
+
102
+ Returns:
103
+ ndarray, 16 bit image
104
+ """
100
105
101
106
img = img .astype ('float64' )
102
107
103
- if minV is None :
104
- minV = np .min (img )
108
+ if minVal is None :
109
+ minVal = np .min (img )
105
110
106
- img = img - minV
111
+ img = img - minVal
107
112
108
- if maxV is None :
109
- maxV = np .max (img )
113
+ if maxVal is None :
114
+ maxVal = np .max (img )
110
115
else :
111
- maxV = maxV - minV
116
+ maxVal = maxVal - minVal
112
117
113
- img = img / maxV * (2 ^ 16 - 1 )
118
+ img = img / maxVal * (2 ^ 16 - 1 )
114
119
img = img .astype ('uint16' )
115
120
116
121
return img
@@ -123,9 +128,14 @@ def radial_profile(img, centre):
123
128
Returns radial profile as 1D numpy array
124
129
125
130
Arguments:
126
- img : input image as 2D numpy array
127
- centre : centre point for radial profile, tuple of (x,y)
131
+ img : ndarray
132
+ input image as 2D numpy array
133
+ centre : (int, int)
134
+ centre point for radial profile, tuple of (x,y)
135
+ Returns:
136
+ ndarray, 1D profile
128
137
"""
138
+
129
139
y , x = np .indices ((img .shape ))
130
140
r = np .sqrt ((x - centre [1 ])** 2 + (y - centre [0 ])** 2 )
131
141
r = r .astype (int )
@@ -140,18 +150,30 @@ def radial_profile(img, centre):
140
150
141
151
142
152
def save_image8 (img , filename ):
143
- """ Saves image as 8 bit tif without scaling"""
153
+ """ Saves image as 8 bit tif without scaling.
154
+
155
+ Arguments:
156
+ img : ndarray,
157
+ input image as 2D numpy array
158
+
159
+ filename : str
160
+ path to save to, folder must exist
161
+ """
162
+
144
163
im = Image .fromarray (img .astype ('uint8' ))
145
164
im .save (filename )
146
165
147
166
148
167
149
168
def save_image16 (img , filename ):
150
169
""" Saves image as 16 bit tif without scaling.
151
-
170
+
152
171
Arguments:
153
- img: image as 2D/3D numpy array
154
- filname : str, path to file. Folder must exist.
172
+ img : ndarray,
173
+ input image as 2D numpy array
174
+
175
+ filename : str
176
+ path to save to, folder must exist
155
177
"""
156
178
im = Image .fromarray (img .astype ('uint16' ))
157
179
im .save (filename )
@@ -160,10 +182,13 @@ def save_image16(img, filename):
160
182
161
183
def save_image8_scaled (img , filename ):
162
184
""" Saves image as 8 bit tif with scaling to use full dynamic range.
163
-
185
+
164
186
Arguments:
165
- img: image as 2D/3D numpy array
166
- filname : str, path to file. Folder must exist.
187
+ img : ndarray,
188
+ input image as 2D numpy array
189
+
190
+ filename : str
191
+ path to save to, folder must exist
167
192
"""
168
193
169
194
im = Image .fromarray (to8bit (img ))
@@ -172,12 +197,15 @@ def save_image8_scaled(img, filename):
172
197
173
198
def save_image16_scaled (img , filename ):
174
199
""" Saves image as 16 bit tif with scaling to use full dynamic range.
175
-
200
+
176
201
Arguments:
177
- img: image as 2D/3D numpy array
178
- filname : str, path to file. Folder must exist.
202
+ img : ndarray,
203
+ input image as 2D numpy array
204
+
205
+ filename : str
206
+ path to save to, folder must exist
179
207
"""
180
-
208
+
181
209
im = Image .fromarray (to16bit (img )[0 ])
182
210
im .save (filename )
183
211
@@ -188,7 +216,11 @@ def average_channels(img):
188
216
189
217
190
218
Arguments:
191
- img: image as 2D/3D numpy array
219
+ img: ndarray
220
+ image as 2D/3D numpy array
221
+
222
+ Returns:
223
+ ndarray, averaged image
192
224
"""
193
225
194
226
if img .ndim == 3 :
@@ -203,16 +235,31 @@ def max_channels(img):
203
235
204
236
205
237
Arguments:
206
- img: image as 2D/3D numpy array
238
+ img: ndarray
239
+ image as 2D/3D numpy array
240
+
241
+ Returns:
242
+ ndarray, max value image
207
243
"""
208
244
209
245
if img .ndim == 3 :
210
246
return np .max (img , 2 )
211
247
else :
212
248
return img
213
249
250
+
214
251
def resample (img , factor ):
252
+ """ Resizes an image by a factor.
215
253
254
+ Arguments:
255
+ img : ndarray
256
+ image as 2D numpy array
257
+ factor : float,
258
+ resize factor
259
+
260
+ Returns:
261
+ ndarray, resmaple images
262
+ """
216
263
h ,w = np .shape (img )
217
264
img = cv .resize (img , ( int (w * factor ), int (h * factor )))
218
265
0 commit comments