How To: Combine Detectors with CompositeDetector#
When no single detector captures all colonies reliably, combine multiple detectors using CompositeDetector. It merges their results via union, intersection, or overlap.
[1]:
from phenotypic.data import load_yeast_plate
from phenotypic.enhance import GaussianBlur, CLAHE
from phenotypic.detect import OtsuDetector, HysteresisDetector, CompositeDetector
[2]:
plate = load_yeast_plate()
plate = GaussianBlur(sigma=2.0).apply(plate)
plate = CLAHE(clip_limit=0.01).apply(plate)
Union Mode#
Keeps any pixel detected by any detector. Maximizes recall at the cost of potential false positives.
[3]:
composite = CompositeDetector(
detectors=[OtsuDetector(), HysteresisDetector(low="mean", high="otsu")],
mode="union",
)
result = composite.apply(plate.copy())
print(f"Union: {result.num_objects} colonies")
result.dash(overlay=True)
Union: 7 colonies
Data type cannot be displayed: application/vnd.plotly.v1+json
Intersection Mode#
Keeps only pixels detected by all detectors. Maximizes precision.
[4]:
composite_intersect = CompositeDetector(
detectors=[OtsuDetector(), HysteresisDetector(low="mean", high="otsu")],
mode="intersection",
)
result2 = composite_intersect.apply(plate.copy())
print(f"Intersection: {result2.num_objects} colonies")
result2.dash(overlay=True)
Intersection: 9 colonies
Data type cannot be displayed: application/vnd.plotly.v1+json
Choose union to catch faint colonies, intersection for high-confidence detection, or overlap with a minimum overlap ratio for a middle ground.