Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Logging of setup for illumination correction #388

Merged
merged 2 commits into from
Jan 23, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
69 changes: 69 additions & 0 deletions src/darsia/corrections/color/illuminationcorrection.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ def setup(
interpolation: Literal["rbf", "quartic", "illumination"] = "quartic",
show_plot: bool = False,
rescale: bool = False,
log: Optional[Path] = None,
):
"""Initialize an illumination correction.

Expand Down Expand Up @@ -220,6 +221,74 @@ def objective_function(scaling):
)
plt.show()

# Logging
if log:

# Create a directory for the log
(log / "illumination_correction").mkdir(parents=True, exist_ok=True)

# Log the original image, with patches and the determined scaling
plt.figure("Log samples")
# Plot the base image
plt.imshow(base[0].img)
# Overlay with ~mask
if mask is not None:
plt.imshow(mask, alpha=0.5)
# Plot the patches as red boxes, and fill with the determined scaling
for sample in samples:
plt.plot(
[
sample[1].start,
sample[1].start,
sample[1].stop,
sample[1].stop,
sample[1].start,
],
[
sample[0].start,
sample[0].stop,
sample[0].stop,
sample[0].start,
sample[0].start,
],
"r-",
)
plt.fill_between(
[sample[1].start, sample[1].stop],
sample[0].start,
sample[0].stop,
color="r",
alpha=0.2,
)
plt.text(
0.5 * (sample[1].start + sample[1].stop),
0.5 * (sample[0].start + sample[0].stop),
f"{scaling[samples.index(sample)]}",
color="w",
ha="center",
va="center",
fontsize=5,
)
plt.savefig(log / "illumination_correction" / "samples.png")
plt.close()

# Log the before and after scaling in a side-by-side plot
fig, ax = plt.subplots(1, 3)
ax[0].imshow(base[0].img)
ax[0].set_title("Original")
ax[1].imshow(self.correct_array(base[0].img))
ax[1].set_title("Corrected")
ax[2].imshow(self.local_scaling[0].img, vmin=0, vmax=2)
ax[2].set_title("Scaling")
fig.colorbar(
ax[2].imshow(self.local_scaling[0].img),
ax=ax[2],
orientation="vertical",
fraction=0.05,
)
plt.savefig(log / "illumination_correction" / "scaling.png")
plt.close()

def correct_array(self, img: np.ndarray) -> np.ndarray:
"""Rescale an array using local WB.

Expand Down
16 changes: 7 additions & 9 deletions src/darsia/utils/interpolation.py
Original file line number Diff line number Diff line change
Expand Up @@ -232,15 +232,13 @@ def interpolate_to_image(
interpolated_image.coordinatesystem,
)
elif method.lower() in ["linear", "quadratic", "cubic", "quartic"]:
degree = (
1
if method.lower() == "linear"
else (
2
if method.lower() == "quadratic"
else 3 if method.lower() == "cubic" else 4
)
)
degrees = {
"linear": 1,
"quadratic": 2,
"cubic": 3,
"quartic": 4,
}
degree = degrees[method.lower()]
interpolated_image.img = polynomial_interpolation(
data,
interpolated_image.coordinatesystem,
Expand Down
Loading