Insight Toolkit for N-dimensional image processing, segmentation, and registration in medical and scientific applications
—
Threshold images using various methods including manual thresholding and automatic threshold selection algorithms.
def binary_threshold_image_filter(input, lower_threshold=0, upper_threshold=255, inside_value=1, outside_value=0):
"""
Binary threshold with specified range.
Parameters:
- input: itk.Image - Input image
- lower_threshold: pixel_type - Lower bound of threshold range
- upper_threshold: pixel_type - Upper bound of threshold range
- inside_value: pixel_type - Output value for pixels in range
- outside_value: pixel_type - Output value for pixels outside range
Returns:
itk.Image - Binary thresholded image
"""
def threshold_image_filter(input, lower=0, upper=255, outside_value=0):
"""
Threshold image in-place (pixels outside range set to outside_value).
Parameters:
- input: itk.Image - Input image
- lower: pixel_type - Lower threshold
- upper: pixel_type - Upper threshold
- outside_value: pixel_type - Value for pixels outside range
Returns:
itk.Image - Thresholded image
"""def otsu_threshold_image_filter(input, inside_value=1, outside_value=0, number_of_histogram_bins=128):
"""
Otsu's automatic threshold selection method.
Parameters:
- input: itk.Image - Input image
- inside_value: pixel_type - Output value for foreground
- outside_value: pixel_type - Output value for background
- number_of_histogram_bins: int - Number of histogram bins
Returns:
itk.Image - Binary thresholded image
"""
def otsu_multiple_thresholds_image_filter(input, number_of_thresholds=1, number_of_histogram_bins=128):
"""
Multi-level Otsu thresholding.
Parameters:
- input: itk.Image - Input image
- number_of_thresholds: int - Number of thresholds to compute
- number_of_histogram_bins: int - Number of histogram bins
Returns:
itk.Image - Multi-level labeled image
"""
def huang_threshold_image_filter(input, inside_value=1, outside_value=0):
"""
Huang's fuzzy thresholding method.
Parameters:
- input: itk.Image - Input image
- inside_value: pixel_type - Output value for foreground
- outside_value: pixel_type - Output value for background
Returns:
itk.Image - Binary thresholded image
"""
def li_threshold_image_filter(input, inside_value=1, outside_value=0):
"""
Li's iterative minimum cross entropy method.
Parameters:
- input: itk.Image - Input image
- inside_value: pixel_type - Output value for foreground
- outside_value: pixel_type - Output value for background
Returns:
itk.Image - Binary thresholded image
"""
def yen_threshold_image_filter(input, inside_value=1, outside_value=0):
"""
Yen's threshold method.
Parameters:
- input: itk.Image - Input image
- inside_value: pixel_type - Output value for foreground
- outside_value: pixel_type - Output value for background
Returns:
itk.Image - Binary thresholded image
"""
def renyi_entropy_threshold_image_filter(input, inside_value=1, outside_value=0):
"""
Renyi entropy threshold method.
Parameters:
- input: itk.Image - Input image
- inside_value: pixel_type - Output value for foreground
- outside_value: pixel_type - Output value for background
Returns:
itk.Image - Binary thresholded image
"""
def triangle_threshold_image_filter(input, inside_value=1, outside_value=0):
"""
Triangle threshold method.
Parameters:
- input: itk.Image - Input image
- inside_value: pixel_type - Output value for foreground
- outside_value: pixel_type - Output value for background
Returns:
itk.Image - Binary thresholded image
"""
def moments_threshold_image_filter(input, inside_value=1, outside_value=0):
"""
Moments-based threshold method.
Parameters:
- input: itk.Image - Input image
- inside_value: pixel_type - Output value for foreground
- outside_value: pixel_type - Output value for background
Returns:
itk.Image - Binary thresholded image
"""
def kittler_illingworth_threshold_image_filter(input, inside_value=1, outside_value=0):
"""
Kittler-Illingworth threshold method.
Parameters:
- input: itk.Image - Input image
- inside_value: pixel_type - Output value for foreground
- outside_value: pixel_type - Output value for background
Returns:
itk.Image - Binary thresholded image
"""import itk
image = itk.imread('input.png', itk.UC)
# Threshold to binary
binary = itk.binary_threshold_image_filter(
image,
lower_threshold=100,
upper_threshold=255,
inside_value=255,
outside_value=0
)
itk.imwrite(binary, 'binary.png')import itk
image = itk.imread('input.png', itk.UC)
# Automatic threshold using Otsu's method
binary = itk.otsu_threshold_image_filter(
image,
inside_value=255,
outside_value=0
)
itk.imwrite(binary, 'otsu.png')
# Multi-level Otsu
multilevel = itk.otsu_multiple_thresholds_image_filter(
image,
number_of_thresholds=2 # Creates 3 classes
)
itk.imwrite(multilevel, 'multilevel.png')import itk
image = itk.imread('input.png', itk.UC)
methods = {
'otsu': itk.otsu_threshold_image_filter,
'huang': itk.huang_threshold_image_filter,
'li': itk.li_threshold_image_filter,
'yen': itk.yen_threshold_image_filter,
'triangle': itk.triangle_threshold_image_filter,
'moments': itk.moments_threshold_image_filter,
}
for name, method in methods.items():
result = method(image, inside_value=255, outside_value=0)
itk.imwrite(result, f'{name}_threshold.png')Install with Tessl CLI
npx tessl i tessl/pypi-itkdocs
guides
reference