Bacteria
Per-cell brightness assignment for synthetic bacteria.
Computes an additive brightness adjustment for each cell in a synthetic image, either by matching measured intensities from a real microscope image or by interpolating between configurable brightness bounds based on cell age. Optionally overlays multi-scale spatial noise for intra-cell variation.
- extract_original_brightness(original_image: ndarray, original_mask: ndarray, colors: list) Dict[int, float]
Extract mean brightness for each original bacterium.
Handles color reuse in the original mask by using connected component analysis to separate bacteria with the same color.
- Parameters:
original_image (np.ndarray) – Original microscope image (grayscale or RGB).
original_mask (np.ndarray) – Original labeled mask - can be grayscale (H x W) with integer labels or RGB (H x W x 3) with unique colors per cell.
colors (list) – List of colors/labels from crm.extract_positions(), maps cell index to color (RGB tuple) or label (integer).
- Returns:
Dictionary mapping cell_index -> mean_brightness.
- Return type:
Dict[int, float]
- create_synthetic_brightness_map(synthetic_mask: ndarray, brightness_map: Dict[int, float], n_cells: int) ndarray
Create brightness map for synthetic bacteria.
Maps each synthetic bacterium to its target brightness value extracted from the corresponding original bacterium.
- Parameters:
synthetic_mask (np.ndarray) – Synthetic RGB mask where cell i has color crm.counter_to_color(i+1).
brightness_map (Dict[int, float]) – Mapping from cell_index -> mean_brightness.
n_cells (int) – Number of cells.
- Returns:
Brightness values at each pixel (H x W), dtype=float32.
- Return type:
np.ndarray
- add_brightness_noise(brightness_map: ndarray, synthetic_mask: ndarray, noise_strength: float, seed: int = None) ndarray
Add Perlin-like noise variation around mean brightness values.
Creates smooth spatial variations in brightness within each bacterium, making the result look more natural.
- Parameters:
brightness_map (np.ndarray) – Target brightness map (H x W).
synthetic_mask (np.ndarray) – Synthetic RGB mask to identify bacteria regions.
noise_strength (float) – Strength of noise variation (0-1). Typical range: 0.1-0.3.
seed (int) – Random seed for reproducibility. If None, uses random state.
- Returns:
Brightness map with added noise (H x W), dtype=float32.
- Return type:
np.ndarray
- apply_original_brightness(synthetic_image: ndarray, synthetic_mask: ndarray, original_image: ndarray, original_mask: ndarray, colors: list, noise_strength: float = 0.0, seed: int = None) ndarray
Apply brightness from original bacteria to synthetic bacteria.
Extracts mean brightness from each original bacterium and applies it to the corresponding synthetic bacterium. This creates realistic brightness variations that match the original microscope image.
- Parameters:
synthetic_image (np.ndarray) – Synthetic microscope image (H x W x C or H x W), uint8 or float.
synthetic_mask (np.ndarray) – Synthetic RGB labeled mask (H x W x 3) where cell i has color crm.counter_to_color(i+1).
original_image (np.ndarray) – Original microscope image (grayscale or RGB).
original_mask (np.ndarray) – Original RGB labeled mask (H x W x 3) - may have color reuse.
colors (list) – List of colors from crm.extract_positions(), maps cell index to original mask color.
noise_strength (float) – Strength of Perlin-like noise variation (0 = no noise, just mean). Typical range: 0.1-0.3 for subtle variation.
seed (int) – Random seed for reproducibility. If None, uses random state.
- Returns:
- Additive brightness adjustment map (same shape as synthetic_image).
Values to be added to the image to match original brightness.
- Return type:
np.ndarray
- compute_age_based_brightness(synthetic_mask: ndarray, cell_ages: Dict, cell_colors: Dict, brightness_range: tuple = (0.8, 0.3), max_age: int = None) ndarray
Compute brightness map based on cell age.
Creates a brightness map where younger cells are brighter and older cells are darker, using linear interpolation between brightness_range values.
- Parameters:
synthetic_mask (np.ndarray) – Synthetic RGB mask (H x W x 3) where each cell has a unique color.
cell_ages (Dict) – Dictionary mapping cell identifier to age (in iterations). Can use CellIdentifier objects or any hashable key.
cell_colors (Dict) – Dictionary mapping cell identifier to RGB color tuple. Must have same keys as cell_ages.
brightness_range (tuple) – (young_brightness, old_brightness). Young cells (age=0) get the first value, oldest cells get the second value. Linear interpolation in between.
max_age (int) – Maximum age for normalization. If None, uses max observed age from cell_ages. When max_age=0 or all cells have same age, all cells get young_brightness.
- Returns:
- Brightness values at each pixel (H x W), dtype=float32.
Values are in range [0, 1] representing brightness multiplier.
- Return type:
np.ndarray
- apply_age_based_brightness(synthetic_image: ndarray, synthetic_mask: ndarray, cell_ages: Dict, cell_colors: Dict, brightness_range: tuple = (0.8, 0.3), max_age: int = None, noise_strength: float = 0.0, seed: int = None) ndarray
Apply age-based brightness to synthetic bacteria image.
Creates realistic brightness variations based on cell age, where older cells appear darker than younger cells.
- Parameters:
synthetic_image (np.ndarray) – Synthetic microscope image (H x W x C or H x W), uint8 or float.
synthetic_mask (np.ndarray) – Synthetic RGB labeled mask (H x W x 3).
cell_ages (Dict) – Dictionary mapping cell identifier to age (in iterations).
cell_colors (Dict) – Dictionary mapping cell identifier to RGB color tuple.
brightness_range (tuple) – (young_brightness, old_brightness).
max_age (int) – Maximum age for normalization. If None, uses max observed age.
noise_strength (float) – Strength of Perlin-like noise variation (0 = no noise).
seed (int) – Random seed for reproducibility. If None, uses random state.
- Returns:
Additive brightness adjustment map (same shape as synthetic_image).
- Return type:
np.ndarray