Skip to content

Morphology Module Reference

Provides morphological image transformations using structuring elements.

Basic Operations

rust
impl<B: Backend> Image<B> {
    pub fn dilate(self, kernel_size: usize) -> Result<Self>;
    pub fn erode(self, kernel_size: usize) -> Result<Self>;
    pub fn morph_open(self, kernel_size: usize) -> Result<Self>;
    pub fn morph_close(self, kernel_size: usize) -> Result<Self>;
}

Advanced Operations

Morphology Ex

rust
impl<B: Backend> Image<B> {
    pub fn morphology_ex(&self, op: MorphOp, kernel_size: usize) -> Result<Self>;
}

Available MorphOp variants:

VariantDescription
MorphOp::OpeningErosion followed by dilation. Removes small bright noise.
MorphOp::ClosingDilation followed by erosion. Fills small holes.
MorphOp::GradientDifference between dilation and erosion. Highlights boundaries.
MorphOp::TopHatDifference between input and opening. Isolates bright features.
MorphOp::BlackHatDifference between closing and input. Isolates dark features.

Custom Kernel Operations

rust
impl<B: Backend> Image<B> {
    pub fn dilate_with_kernel(self, kernel: &[&[u8]]) -> Result<Self>;
    pub fn erode_with_kernel(self, kernel: &[&[u8]]) -> Result<Self>;
}

Structuring Elements

rust
pub struct Morphology;

impl Morphology {
    pub fn get_structuring_element(shape: MorphShape, size: Size<usize>) -> Vec<Vec<u8>>;
}

Available MorphShape variants:

ShapeDescription
MorphShape::RectRectangular (flat box) element.
MorphShape::CrossCross-shaped orthogonal lines.
MorphShape::EllipseElliptical kernel mask.

Example

rust
use iris::prelude::*;
use burn::backend::wgpu::Wgpu;

type Backend = Wgpu;
let device = Default::default();
let img = Image::<Backend>::open("input.jpg", &device)?;

let dilated = img.clone().dilate(3)?;
let eroded = img.clone().erode(3)?;
let opened = img.clone().morphology_ex(MorphOp::Opening, 3)?;

// Custom cross-shaped kernel
let kernel: Vec<&[u8]> = vec![&[0, 1, 0], &[1, 1, 1], &[0, 1, 0]];
let custom_dilated = img.dilate_with_kernel(&kernel)?;

Hit-or-Miss Transform

rust
impl<B: Backend> Image<B> {
    pub fn hit_or_miss(&self, pattern: &[&[u8]], bg_pattern: &[&[u8]]) -> Result<Self>;
}

Finds pixels matching a specific pattern of foreground (1) and background (0) neighbors. Useful for template matching within binary images.

ParameterDescription
patternKernel where 1 marks pixels that must be foreground.
bg_patternKernel where 1 marks pixels that must be background.

Example

rust
let fg: Vec<&[u8]> = vec![&[0, 1, 0], &[1, 1, 1], &[0, 1, 0]];
let bg: Vec<&[u8]> = vec![&[1, 0, 1], &[0, 0, 0], &[1, 0, 1]];
let hits = img.hit_or_miss(&fg, &bg)?;

Thinning

rust
impl<B: Backend> Image<B> {
    pub fn thin(&self) -> Result<Self>;
}

Applies the Zhang-Suen thinning algorithm to reduce foreground regions to single-pixel-wide strokes while preserving connectivity.

Example

rust
let thinned = img.thin()?;

Skeleton Extraction

rust
impl<B: Backend> Image<B> {
    pub fn skeleton(&self) -> Result<Self>;
}

Extracts the topological skeleton of a binary image using iterative morphological thinning. Returns a single-pixel-wide representation of the foreground structure.

Example

rust
let skel = img.skeleton()?;

Released under the MIT License.