module

CrImage::Util::Morphology

Morphological operations for image processing. Includes erosion, dilation, opening, closing, and morphological gradient.

Class methods

close(src : Image, kernel_size : Int32 = 3, shape : StructuringElement = StructuringElement::Rectangle) : Image

Applies morphological closing (dilation followed by erosion).

Closing fills small holes and gaps while preserving larger structures. Useful for connecting nearby objects and filling gaps.

Parameters:

  • src : The source image
  • kernel_size : Size of structuring element (must be odd)
  • shape : Shape of structuring element (default: Rectangle)

Returns: A new closed Image

Example:

img = CrImage::PNG.read("gaps.png")
closed = CrImage::Util::Morphology.close(img, 5)
Source
dilate(src : Image, kernel_size : Int32 = 3, shape : StructuringElement = StructuringElement::Rectangle) : Image

Applies dilation to an image.

Dilation expands bright regions and shrinks dark regions. Useful for filling small holes and connecting nearby objects.

Parameters:

  • src : The source image
  • kernel_size : Size of structuring element (must be odd)
  • shape : Shape of structuring element (default: Rectangle)

Returns: A new dilated Image

Example:

img = CrImage::PNG.read("binary.png")
dilated = CrImage::Util::Morphology.dilate(img, 3)
Source
erode(src : Image, kernel_size : Int32 = 3, shape : StructuringElement = StructuringElement::Rectangle) : Image

Applies erosion to an image.

Erosion shrinks bright regions and enlarges dark regions. Useful for removing small bright spots and separating objects.

Parameters:

  • src : The source image
  • kernel_size : Size of structuring element (must be odd)
  • shape : Shape of structuring element (default: Rectangle)

Returns: A new eroded Image

Example:

img = CrImage::PNG.read("noisy.png")
eroded = CrImage::Util::Morphology.erode(img, 3)
Source
gradient(src : Image, kernel_size : Int32 = 3, shape : StructuringElement = StructuringElement::Rectangle) : Image

Applies morphological gradient (dilation - erosion).

Gradient highlights edges and boundaries of objects. Useful for edge detection and boundary extraction.

Parameters:

  • src : The source image
  • kernel_size : Size of structuring element (must be odd)
  • shape : Shape of structuring element (default: Rectangle)

Returns: A new gradient Image

Example:

img = CrImage::PNG.read("objects.png")
gradient = CrImage::Util::Morphology.gradient(img, 3)
Source
open(src : Image, kernel_size : Int32 = 3, shape : StructuringElement = StructuringElement::Rectangle) : Image

Applies morphological opening (erosion followed by dilation).

Opening removes small bright spots while preserving larger structures. Useful for noise removal and smoothing object boundaries.

Parameters:

  • src : The source image
  • kernel_size : Size of structuring element (must be odd)
  • shape : Shape of structuring element (default: Rectangle)

Returns: A new opened Image

Example:

img = CrImage::PNG.read("noisy.png")
opened = CrImage::Util::Morphology.open(img, 5)
Source