phenotypic.enhance.ContrastStretching#
- class phenotypic.enhance.ContrastStretching(lower_percentile: int = 2, upper_percentile: int = 98)[source]
Bases:
ImageEnhancerStretch the intensity range of detect_mat to fill the full dynamic range.
Rescales pixel values based on lower and upper percentiles, compressing outliers (specular highlights, deep shadows) while expanding the range where colony intensities reside. Simpler and faster than CLAHE, with no local tile artifacts.
- Parameters:
- Returns:
Input image with
detect_matrescaled to the full dynamic range.rgbandgrayare unchanged.- Return type:
Image
- Best For:
Plates with narrow histograms (under-exposed or low-contrast).
Normalizing exposure across different imaging sessions.
Quick preprocessing before global thresholding (Otsu, Triangle).
- Consider Also:
CLAHEwhen illumination varies spatially across the plate.HomomorphicFilterwhen the primary issue is a brightness gradient rather than narrow dynamic range.
See also
How To: Enhance Low-Contrast Images for a comparison of contrast enhancement methods. What Enhancement Actually Does for how enhancement fits into the pipeline model.
Methods
Applies the operation to an image, either in-place or on a copy.
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.
- Parameters:
image (Image) – The arr image to apply the operation on.
inplace (bool) – If True, modifies the image in place; otherwise, operates on a copy of the image.
- Returns:
The modified image after applying the operation.
- Return type:
Image
- widget(image: Image | None = None, show: bool = False) Widget
Return (and optionally display) the root widget.
- Parameters:
image (Image | None) – Optional image to visualize. If provided, visualization controls will be added to the widget.
show (bool) – Whether to display the widget immediately. Defaults to False.
- Returns:
The root widget.
- Return type:
ipywidgets.Widget
- Raises:
ImportError – If ipywidgets or IPython are not installed.