phenotypic.measure.MeasureTexture#
- class phenotypic.measure.MeasureTexture(scale: int | List[int] = 5, quant_lvl: Literal[8, 16, 32, 64] = 32, enhance: bool = False, warn: bool = False)[source]
Bases:
MeasureFeaturesMeasure colony surface texture using Haralick features from gray-level co-occurrence matrices.
Compute 13 second-order Haralick texture features per colony at one or more pixel-offset scales, across four directional angles (0, 45, 90, 135 degrees), plus direction-averaged values. These features quantify surface roughness, regularity, and directional patterns that distinguish colony morphotypes.
- Args:
- scale: Pixel offset(s) for the co-occurrence matrix. A single
integer or list of integers. Small values (1–2) capture fine texture; large values (5–10) capture coarse patterns. Default:
5.- quant_lvl: Number of gray-level bins for quantization. Accepted
values:
8,16,32,64. Lower values are faster; higher values preserve texture nuance but are more noise-sensitive. Default:32.- enhance: Rescale each colony’s intensity to [0, 1] before
computing Haralick features. Improves contrast in low-variance regions but can bias cross-colony comparisons. Default:
False.- warn: Emit warnings when Haralick computation fails for
individual colonies (typically very small objects). Default:
False.
- Returns:
pd.DataFrame: Object-level texture measurements with columns:
Label: unique object identifier.
13 Haralick features x 4 angles = 52 directional columns per scale (e.g.,
Contrast-deg000-scale05).13 direction-averaged columns per scale (e.g.,
Contrast-avg-scale05).
- References:
[1] R. M. Haralick, K. Shanmugam, and I. Dinstein, “Textural features for image classification,” IEEE Trans. Syst., Man, Cybern., vol. SMC-3, no. 6, pp. 610–621, Nov. 1973.
- Best For:
Distinguishing smooth wild-type colonies from rough, wrinkled, or sporulated mutants.
Assessing mycelial organization in filamentous fungi (radial vs cottony growth).
Multi-feature phenotypic clustering when combined with size, shape, and color measurements.
- Consider Also:
MeasureShapefor geometric morphology metrics (circularity, Feret diameters) that complement texture.MeasureIntensityfor brightness statistics without spatial co-occurrence information.MeasureColorfor pigmentation-based phenotyping.
- See Also:
Tutorial 7: Measuring and Exporting for a walkthrough of measuring and exporting colony data. Measurement Metrics and Their Biological Meaning for interpreting texture metrics in a biological context.
Category: TEXTURE# Name
Description
AngularSecondMoment- Angular second moment (energy / uniformity). Measures the degree of local homogeneity
(Σ p(i,j)²). High values → uniform texture (e.g., smooth, yeast-like colonies with consistent mycelial density). Low values → heterogeneous surfaces (e.g., sectored, wrinkled, or mixed sporulation zones). Reflects colony surface regularity rather than brightness.
Contrast- Contrast (local intensity variation; Σ (i–j)² p(i,j)). High values indicate strong gray-level
differences (e.g., sharply defined rings, radial sectors, raised or folded regions). Low values indicate gradual tonal changes or uniformly pigmented colonies. Quantifies visual roughness and zonation amplitude.
Correlation- Linear gray-level correlation between neighboring pixels. Positive, high values suggest
structured spatial dependence (e.g., oriented radial hyphae or concentric patterns); near-zero values indicate uncorrelated, disordered growth (e.g., diffuse cottony mycelium). Sensitive to illumination gradients and directional GLCM computation.
HaralickVariance- GLCM variance (Σ (i–μ)² p(i,j)). Captures spread of co-occurring gray-level pairs, distinct
from raw intensity variance. High values → complex, multi-zone textures with variable hyphal/spore densities. Low values → consistent gray-level relationships and simpler colony surfaces.
InverseDifferenceMoment- Homogeneity (Σ p(i,j) / (1 + (i–j)²)). High values → smooth, locally uniform textures
(e.g., glabrous colonies, uniform aerial mycelium). Low values → abrupt gray-level changes (e.g., granular sporulation, wrinkled surfaces). Typically inversely correlated with Contrast.
SumAverage- Mean of gray-level sums (Σ k·p_{x+y}(k)). Reflects the average intensity combination of
neighboring pixels. In fungal colonies, can loosely parallel mean colony brightness when illumination and exposure are controlled, but remains a second-order rather than first-order intensity metric.
SumVariance- Variance of gray-level sum distribution. High values → heterogeneous brightness zones
(e.g., alternating dense/sparse or pigmented/non-pigmented regions). Low values → uniform tone across the colony. Often correlated with Contrast; use comparatively within one setup.
SumEntropy- Entropy of the gray-level sum distribution. High values → diverse brightness combinations
and irregular zonation. Low values → repetitive or periodic brightness patterns (e.g., evenly spaced rings). Indicates spatial unpredictability of summed intensities.
Entropy- Global GLCM entropy (–Σ p(i,j)·log p(i,j)). Measures total texture disorder and information
content. High values → complex, irregular colony surfaces (powdery, fuzzy, or sectored growth). Low values → simple, smooth, predictable patterns (glabrous or uniform colonies). Sensitive to gray-level quantization and image dynamic range.
DiffVariance- Variance of gray-level difference distribution. High values → mixture of smooth and textured
regions (e.g., smooth margins with wrinkled centers). Low values → consistent edge content. Highlights heterogeneity in edge magnitude across the colony.
DiffEntropy- Entropy of gray-level difference distribution. High values → irregular, unpredictable
intensity transitions (e.g., random sporulation or uneven mycelial networks). Low values → regular periodic transitions (e.g., concentric zonation). Reflects randomness of local contrast rather than its magnitude.
InfoCorrelation1- Information measure of correlation 1. Compares joint vs marginal entropies to quantify
mutual dependence between gray levels. Positive values → structured, predictable textures (e.g., organized radial growth); near-zero → independence between adjacent regions. Direction of sign varies with implementation.
InfoCorrelation2- Information measure of correlation 2 (√[1 – exp(–2 (H_xy2–H_xy))]). Always ≥ 0.
Values approaching 1 → strong spatial dependence and organized architecture (e.g., symmetric rings, radial structure). Values near 0 → random, independent patterns. Captures nonlinear organization missed by linear correlation.
Methods
Initializes an object with specific configurations for scale, quantization level, enhance, and warning behaviors.
Execute the measurement operation on a detected-object image.
- __init__(scale: int | List[int] = 5, quant_lvl: Literal[8, 16, 32, 64] = 32, enhance: bool = False, warn: bool = False)[source]
Initializes an object with specific configurations for scale, quantization level, enhance, and warning behaviors. This constructor ensures that the ‘scale’ parameter is always stored as a list.
- Parameters:
scale (int | List[int]) – A single integer or a list of integers representing the scale configuration. If a single integer is provided, it will be converted into a list containing that integer.
quant_lvl (Literal[8, 16, 32, 64]) – The quantization level. A higher level adds more computational complexity but captures more discrete texture changes. A higher value is not always more meaningful. Think of this like sensitivity to texture. Acceptable values are either 8, 16, 32, or 64.
enhance (bool) – A flag indicating whether to enhance the image before measuring texture. This can increase the amount of detail captured but can bias the measurements in cases where the relative variance between pixel intensities of an object is small.
warn (bool) – A flag indicating whether warnings should be issued.
- __del__()
Automatically stop tracemalloc when the object is deleted.
- measure(image, include_meta=False)
Execute the measurement operation on a detected-object image.
This is the main public API method for extracting measurements. It handles: input validation, parameter extraction via introspection, calling the subclass-specific _operate() method, optional metadata merging, and exception handling.
How it works (for users):
Pass your processed Image (with detected objects) to measure()
The method calls your subclass’s _operate() implementation
Results are validated and returned as a pandas DataFrame
If include_meta=True, image metadata (filename, grid info) is merged in
How it works (for developers):
When you subclass MeasureFeatures, you only implement _operate(). This measure() method automatically:
Extracts __init__ parameters from your instance (introspection)
Passes matched parameters to _operate() as keyword arguments
Validates the Image has detected objects (objmap)
Wraps exceptions in OperationFailedError with context
Merges grid/object metadata if requested
- Parameters:
image (Image) – A PhenoTypic Image object with detected objects (must have non-empty objmap from a prior detection operation).
include_meta (bool, optional) – If True, merge image metadata columns (filename, grid position, etc.) into the results DataFrame. Defaults to False.
- Returns:
Measurement results with structure:
First column: OBJECT.LABEL (integer IDs from image.objmap[:])
Remaining columns: Measurement values (float, int, or string)
One row per detected object
If include_meta=True, additional metadata columns are prepended before OBJECT.LABEL (e.g., Filename, GridRow, GridCol).
- Return type:
pd.DataFrame
- Raises:
OperationFailedError – If _operate() raises any exception, it is caught and re-raised as OperationFailedError with details including the original exception type, message, image name, and operation class. This provides consistent error handling across all measurers.
Notes
This method is the main entry point; do not override in subclasses
Subclasses implement _operate() only, not this method
Automatic memory profiling is available via logging configuration
Image must have detected objects (image.objmap should be non-empty)
Examples
Basic measurement extraction:
>>> from phenotypic import Image >>> from phenotypic.measure import MeasureSize >>> from phenotypic.detect import OtsuDetector >>> # Load and detect >>> image = Image('plate.jpg') >>> image = OtsuDetector().operate(image) >>> # Extract measurements >>> measurer = MeasureSize() >>> df = measurer.measure(image) >>> print(df.head())
Include metadata in measurements:
>>> # With image metadata (filename, grid info) >>> df_with_meta = measurer.measure(image, include_meta=True) >>> print(df_with_meta.columns) # Output: ['Filename', 'GridRow', 'GridCol', 'OBJECT.LABEL', # 'Area', 'IntegratedIntensity', ...]