Preprocessing

“Preprocessing” means cleaning up and preparing an image before you analyze it further — things like fixing brightness, resizing, or sharpening edges. It’s a built-in capability of SPICEData, reached through its preprocess property. Every spice.preprocess.<method>() call works directly on spice.data, changes it in place, automatically records what it did into spice.history (with the exact values you used), and returns spice itself, so you can call .preprocess again right after.


spice.preprocess

No separate object needs to be constructed — spice.preprocess is accessed directly on any loaded SPICEData:

from juspice.io import load_data

spice = load_data('image.tif')
spice.preprocess.equalize_histogram()
spice.preprocess.normalize()
spice.preprocess.binarize(threshold=0.5)

Each call returns spice, so calls can be chained by re-accessing .preprocess:

spice.preprocess.equalize_histogram() \
     .preprocess.normalize() \
     .preprocess.binarize(threshold=0.5)

Two methods are exceptions to the “returns spice” rule:

  • label_components() returns (num_labels, labels, stats, centroids) directly, since it has multiple outputs. spice.data is still updated in place to the labeled image.

  • show_image() and show_labels() are visualization-only and return None.

Chaining does not continue past either of these — re-access spice.preprocess afterward to keep going.

Available methods

  • equalize_histogram() — histogram equalisation for contrast enhancement.

  • normalize() — min–max rescaling to [0, 1].

  • standardize() — zero-mean, unit-variance rescaling.

  • resize() — resize to a target width/height.

  • downsample() — stride-based downsampling by an integer factor.

  • apply_filter() — 'blur', 'gaussian', or 'median' filtering.

  • edge_detection() — 'canny', 'sobel', or 'laplacian' edge detection.

  • morphological_operation() — 'dilation', 'erosion', 'opening', or 'closing'.

  • binarize() — threshold-based binarisation.

  • label_components() — connected-component labelling (4- or 8-connectivity).

  • show_image() / show_labels() — matplotlib visualization helpers.

Reproducibility

Operations applied through spice.preprocess are recorded into DatasetHistory as preprocess.<method> entries, using the actual runtime argument values. They appear in the <stem>.json sidecar and reconstruct as direct spice.preprocess.<method>(...) calls in to_lines() and to_script() — no intermediate preprocessor object or manual copy-back step is needed.

When used inside a Tracker recording block, every call is additionally captured verbatim in the session-level _history.py.


See also