Insight Toolkit for N-dimensional image processing, segmentation, and registration in medical and scientific applications
—
Compute image gradients, gradient magnitude, and detect edges using various operators and methods.
def gradient_image_filter(input, use_image_spacing=True):
"""
Compute gradient at each pixel using finite differences.
Parameters:
- input: itk.Image - Input scalar image
- use_image_spacing: bool - Account for image spacing in gradient computation
Returns:
itk.Image - Gradient image with CovariantVector pixels
"""
class GradientImageFilter:
"""Computes the gradient of an image using finite differences."""
def SetUseImageSpacing(self, use_spacing):
"""Enable/disable using image spacing."""
def gradient_magnitude_image_filter(input, use_image_spacing=True):
"""
Compute gradient magnitude at each pixel.
Parameters:
- input: itk.Image - Input scalar image
- use_image_spacing: bool - Account for image spacing
Returns:
itk.Image - Gradient magnitude image
"""
class GradientMagnitudeImageFilter:
"""Computes the magnitude of the image gradient."""
def SetUseImageSpacing(self, use_spacing):
"""Enable/disable using image spacing."""
def gradient_magnitude_recursive_gaussian_image_filter(input, sigma=1.0, normalize_across_scale=False):
"""
Compute gradient magnitude with Gaussian smoothing.
Parameters:
- input: itk.Image - Input image
- sigma: float - Gaussian sigma for smoothing
- normalize_across_scale: bool - Normalize derivatives across scale
Returns:
itk.Image - Smoothed gradient magnitude
"""
class GradientMagnitudeRecursiveGaussianImageFilter:
"""Computes gradient magnitude with recursive Gaussian smoothing."""
def SetSigma(self, sigma):
"""Set the Gaussian sigma."""
def SetNormalizeAcrossScale(self, normalize):
"""Enable scale normalization."""
def gradient_recursive_gaussian_image_filter(input, sigma=1.0):
"""
Compute gradient vector with Gaussian smoothing.
Parameters:
- input: itk.Image - Input image
- sigma: float - Gaussian sigma
Returns:
itk.Image - Gradient vector image
"""def canny_edge_detection_image_filter(input, variance=1.0, upper_threshold=0.0, lower_threshold=0.0, maximum_error=0.01):
"""
Canny edge detector with hysteresis thresholding.
Parameters:
- input: itk.Image - Input image
- variance: float - Variance of the Gaussian smoothing
- upper_threshold: float - Upper threshold for hysteresis
- lower_threshold: float - Lower threshold for hysteresis
- maximum_error: float - Maximum error in Gaussian approximation
Returns:
itk.Image - Binary edge map
"""
class CannyEdgeDetectionImageFilter:
"""Multi-stage Canny edge detector."""
def SetVariance(self, variance):
"""Set Gaussian variance for smoothing."""
def SetUpperThreshold(self, threshold):
"""Set upper threshold for strong edges."""
def SetLowerThreshold(self, threshold):
"""Set lower threshold for weak edges."""
def SetMaximumError(self, error):
"""Set maximum Gaussian approximation error."""
def sobel_edge_detection_image_filter(input):
"""
Sobel edge detection operator.
Parameters:
- input: itk.Image - Input image
Returns:
itk.Image - Sobel edge magnitude
"""
class SobelEdgeDetectionImageFilter:
"""Sobel operator for edge detection."""
def laplacian_image_filter(input, use_image_spacing=True):
"""
Compute Laplacian (second derivative).
Parameters:
- input: itk.Image - Input image
- use_image_spacing: bool - Account for image spacing
Returns:
itk.Image - Laplacian image
"""
class LaplacianImageFilter:
"""Computes the Laplacian of an image."""
def SetUseImageSpacing(self, use_spacing):
"""Enable/disable using image spacing."""
def zero_crossing_image_filter(input, foreground_value=1, background_value=0):
"""
Detect zero crossings in an image (useful after Laplacian).
Parameters:
- input: itk.Image - Input image (typically Laplacian)
- foreground_value: pixel_type - Value for detected edges
- background_value: pixel_type - Value for non-edges
Returns:
itk.Image - Binary edge map
"""def hessian_recursive_gaussian_image_filter(input, sigma=1.0):
"""
Compute Hessian matrix (second derivatives) with Gaussian smoothing.
Parameters:
- input: itk.Image - Input image
- sigma: float - Gaussian sigma
Returns:
itk.Image - Hessian matrix image (SymmetricSecondRankTensor pixels)
"""
class HessianRecursiveGaussianImageFilter:
"""Computes the Hessian matrix using recursive Gaussian filters."""
def SetSigma(self, sigma):
"""Set the Gaussian sigma."""
def SetNormalizeAcrossScale(self, normalize):
"""Enable scale normalization."""import itk
image = itk.imread('input.png', itk.F)
# Compute gradient vectors
gradient = itk.gradient_image_filter(image)
# Compute gradient magnitude
magnitude = itk.gradient_magnitude_image_filter(image)
itk.imwrite(magnitude, 'gradient_magnitude.png')
# With Gaussian smoothing
smoothed_magnitude = itk.gradient_magnitude_recursive_gaussian_image_filter(
image, sigma=2.0
)
itk.imwrite(smoothed_magnitude, 'smooth_gradient.png')import itk
image = itk.imread('input.png', itk.F)
# Canny edge detection
edges = itk.canny_edge_detection_image_filter(
image,
variance=2.0, # Gaussian smoothing
upper_threshold=0.1, # Strong edges
lower_threshold=0.05 # Weak edges
)
itk.imwrite(edges, 'canny_edges.png')import itk
image = itk.imread('input.png', itk.F)
# Sobel edges
sobel_edges = itk.sobel_edge_detection_image_filter(image)
itk.imwrite(sobel_edges, 'sobel_edges.png')import itk
image = itk.imread('input.png', itk.F)
# Smooth first
smoothed = itk.discrete_gaussian_image_filter(image, variance=2.0)
# Compute Laplacian
laplacian = itk.laplacian_image_filter(smoothed)
# Detect zero crossings
edges = itk.zero_crossing_image_filter(laplacian)
itk.imwrite(edges, 'zero_crossing_edges.png')Install with Tessl CLI
npx tessl i tessl/pypi-itkdocs
guides
reference