@@ -28,6 +28,7 @@ def setup(
28
28
interpolation : Literal ["rbf" , "quartic" , "illumination" ] = "quartic" ,
29
29
show_plot : bool = False ,
30
30
rescale : bool = False ,
31
+ log : Optional [Path ] = None ,
31
32
):
32
33
"""Initialize an illumination correction.
33
34
@@ -220,6 +221,74 @@ def objective_function(scaling):
220
221
)
221
222
plt .show ()
222
223
224
+ # Logging
225
+ if log :
226
+
227
+ # Create a directory for the log
228
+ (log / "illumination_correction" ).mkdir (parents = True , exist_ok = True )
229
+
230
+ # Log the original image, with patches and the determined scaling
231
+ plt .figure ("Log samples" )
232
+ # Plot the base image
233
+ plt .imshow (base [0 ].img )
234
+ # Overlay with ~mask
235
+ if mask is not None :
236
+ plt .imshow (mask , alpha = 0.5 )
237
+ # Plot the patches as red boxes, and fill with the determined scaling
238
+ for sample in samples :
239
+ plt .plot (
240
+ [
241
+ sample [1 ].start ,
242
+ sample [1 ].start ,
243
+ sample [1 ].stop ,
244
+ sample [1 ].stop ,
245
+ sample [1 ].start ,
246
+ ],
247
+ [
248
+ sample [0 ].start ,
249
+ sample [0 ].stop ,
250
+ sample [0 ].stop ,
251
+ sample [0 ].start ,
252
+ sample [0 ].start ,
253
+ ],
254
+ "r-" ,
255
+ )
256
+ plt .fill_between (
257
+ [sample [1 ].start , sample [1 ].stop ],
258
+ sample [0 ].start ,
259
+ sample [0 ].stop ,
260
+ color = "r" ,
261
+ alpha = 0.2 ,
262
+ )
263
+ plt .text (
264
+ 0.5 * (sample [1 ].start + sample [1 ].stop ),
265
+ 0.5 * (sample [0 ].start + sample [0 ].stop ),
266
+ f"{ scaling [samples .index (sample )]} " ,
267
+ color = "w" ,
268
+ ha = "center" ,
269
+ va = "center" ,
270
+ fontsize = 5 ,
271
+ )
272
+ plt .savefig (log / "illumination_correction" / "samples.png" )
273
+ plt .close ()
274
+
275
+ # Log the before and after scaling in a side-by-side plot
276
+ fig , ax = plt .subplots (1 , 3 )
277
+ ax [0 ].imshow (base [0 ].img )
278
+ ax [0 ].set_title ("Original" )
279
+ ax [1 ].imshow (self .correct_array (base [0 ].img ))
280
+ ax [1 ].set_title ("Corrected" )
281
+ ax [2 ].imshow (self .local_scaling [0 ].img , vmin = 0 , vmax = 2 )
282
+ ax [2 ].set_title ("Scaling" )
283
+ fig .colorbar (
284
+ ax [2 ].imshow (self .local_scaling [0 ].img ),
285
+ ax = ax [2 ],
286
+ orientation = "vertical" ,
287
+ fraction = 0.05 ,
288
+ )
289
+ plt .savefig (log / "illumination_correction" / "scaling.png" )
290
+ plt .close ()
291
+
223
292
def correct_array (self , img : np .ndarray ) -> np .ndarray :
224
293
"""Rescale an array using local WB.
225
294
0 commit comments