phenotypic.enhance.ContrastStretching#
- class phenotypic.enhance.ContrastStretching(lower_percentile: int = 2, upper_percentile: int = 98)[source]#
Bases:
ImageEnhancerContrast stretching for plate images.
Rescales intensities based on chosen lower/upper percentiles so the bulk of pixel values expands to the full display range. For agar plate colony images, this often improves separability between colonies and agar by compressing extreme outliers (e.g., specular highlights, deep shadows) while expanding the dynamic range where colonies reside.
Use cases (agar plates): - Normalize exposure across scans or camera shots before thresholding. - Recover contrast on low-contrast plates without amplifying noise as much as
aggressive histogram equalization might.
Prepare images for global methods (e.g., Otsu) or for visualization.
Tuning and effects: - lower_percentile: Increasing this value clips more of the darkest pixels
(e.g., shadows/edge artifacts), which brightens the image and can reveal translucent colonies. Too high can erase true dark background structure.
upper_percentile: Decreasing this value clips more highlights (e.g., glare, dust reflections), preventing them from dominating contrast. Too low flattens bright colonies or pigmented regions.
Caveats: - If outliers are biological signals (very bright colonies), heavy clipping can
reduce their apparent intensity and bias measurements.
Contrast stretching is global; it will not fix spatially varying illumination on its own (consider GaussianSubtract or RollingBallRemoveBG).
- Parameters:
Methods
Applies the operation to an image, either in-place or on a copy.
Drop references to the UI widgets.
Push internal state into widgets.
Return (and optionally display) the root widget.
- __init__(lower_percentile: int = 2, upper_percentile: int = 98)[source]#
- Parameters:
lower_percentile (int) – Dark clipping point. Increase to suppress deep shadows/edge artifacts; too high may remove meaningful dark background structure. Typical range: 1–5.
upper_percentile (int) – Bright clipping point. Decrease to suppress glare/highlights; too low may flatten bright colonies. Typical range: 95–99.
- __del__()#
Automatically stop tracemalloc when the object is deleted.
- __getstate__()#
Prepare the object for pickling by disposing of any widgets.
This ensures that UI components (which may contain unpickleable objects like input functions or thread locks) are cleaned up before serialization.
Note
This method modifies the object state by calling dispose_widgets(). Any active widgets will be detached from the object.
- apply(image, inplace=False)#
Applies the operation to an image, either in-place or on a copy.
- widget(image: Image | None = None, show: bool = False) Widget#
Return (and optionally display) the root widget.
- Parameters:
- Returns:
The root widget.
- Return type:
ipywidgets.Widget
- Raises:
ImportError – If ipywidgets or IPython are not installed.