Insight Toolkit for N-dimensional image processing, segmentation, and registration in medical and scientific applications
—
Watershed-based segmentation methods for separating touching objects and hierarchical segmentation.
def watershed_image_filter(input, level=0.0, threshold=0.0):
"""
Classic watershed segmentation.
Parameters:
- input: itk.Image - Input image (typically gradient magnitude)
- level: float - Flood level
- threshold: float - Threshold for minima
Returns:
itk.Image - Labeled watershed segmentation
"""
class WatershedImageFilter:
"""Classic watershed segmentation algorithm."""
def SetLevel(self, level):
"""Set the flood level."""
def SetThreshold(self, threshold):
"""Set the threshold for minima."""
def morphological_watershed_image_filter(input, level=0.0, mark_watershed_line=True, fully_connected=False):
"""
Morphological watershed segmentation.
Parameters:
- input: itk.Image - Input image
- level: float - Flood level
- mark_watershed_line: bool - Mark watershed lines
- fully_connected: bool - Use full connectivity
Returns:
itk.Image - Labeled segmentation
"""
class MorphologicalWatershedImageFilter:
"""Morphological watershed segmentation."""
def morphological_watershed_from_markers_image_filter(input, markers, mark_watershed_line=True, fully_connected=False):
"""
Watershed from marker image.
Parameters:
- input: itk.Image - Input image
- markers: itk.Image - Marker image with labeled regions
- mark_watershed_line: bool - Mark watershed lines
- fully_connected: bool - Use full connectivity
Returns:
itk.Image - Labeled segmentation
"""
def isolated_watershed_image_filter(input, seed1, seed2, threshold=0.0, isolation_value_tolerance=0.001):
"""
Watershed that isolates two regions.
Parameters:
- input: itk.Image - Input image
- seed1: itk.Index - First seed point
- seed2: itk.Index - Second seed point
- threshold: float - Threshold value
- isolation_value_tolerance: float - Tolerance for isolation
Returns:
itk.Image - Binary segmentation
"""import itk
# Load image and compute gradient
image = itk.imread('input.png', itk.F)
gradient = itk.gradient_magnitude_image_filter(image)
# Watershed segmentation
watershed = itk.watershed_image_filter(gradient, level=0.0)
itk.imwrite(watershed, 'watershed.png')import itk
# Load image
image = itk.imread('input.png', itk.F)
# Create markers (e.g., from seeds or other segmentation)
markers = itk.imread('markers.png', itk.UL)
# Compute gradient
gradient = itk.gradient_magnitude_image_filter(image)
# Watershed from markers
segmented = itk.morphological_watershed_from_markers_image_filter(
gradient,
markers,
mark_watershed_line=True
)
itk.imwrite(segmented, 'marker_watershed.png')Install with Tessl CLI
npx tessl i tessl/pypi-itkdocs
guides
reference