Skip to content

Commit 8933955

Browse files
committed
MAINT: Add logging possibility for setup of illumination correction
1 parent 3a1405e commit 8933955

File tree

1 file changed

+69
-0
lines changed

1 file changed

+69
-0
lines changed

src/darsia/corrections/color/illuminationcorrection.py

+69
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ def setup(
2828
interpolation: Literal["rbf", "quartic", "illumination"] = "quartic",
2929
show_plot: bool = False,
3030
rescale: bool = False,
31+
log: Optional[Path] = None,
3132
):
3233
"""Initialize an illumination correction.
3334
@@ -220,6 +221,74 @@ def objective_function(scaling):
220221
)
221222
plt.show()
222223

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+
223292
def correct_array(self, img: np.ndarray) -> np.ndarray:
224293
"""Rescale an array using local WB.
225294

0 commit comments

Comments
 (0)