Class MorphologyOp

java.lang.Object
cloud.opencode.base.image.morphology.MorphologyOp

public final class MorphologyOp extends Object
Morphological Image Operations 形态学图像操作工具类

Provides standard morphological operations on grayscale images: erosion, dilation, opening, closing, gradient, top-hat, and black-hat transforms.

提供灰度图像上的标准形态学操作:腐蚀、膨胀、开运算、闭运算、梯度、顶帽和黑帽变换。

Features | 主要功能:

  • Erosion — minimum filter over structuring element - 腐蚀 — 结构元素上的最小值滤波
  • Dilation — maximum filter over structuring element - 膨胀 — 结构元素上的最大值滤波
  • Opening — erosion followed by dilation - 开运算 — 先腐蚀后膨胀
  • Closing — dilation followed by erosion - 闭运算 — 先膨胀后腐蚀
  • Gradient — dilation minus erosion - 梯度 — 膨胀减去腐蚀
  • Top-hat — original minus opening - 顶帽 — 原图减去开运算
  • Black-hat — closing minus original - 黑帽 — 闭运算减去原图

Usage Examples | 使用示例:

StructuringElement se = StructuringElement.rect(3, 3);
BufferedImage eroded = MorphologyOp.erode(image, se);
BufferedImage dilated = MorphologyOp.dilate(image, se);
BufferedImage opened = MorphologyOp.open(image, se);
BufferedImage edges = MorphologyOp.gradient(image, se);

Performance | 性能特性:

  • Time complexity: O(w * h * kw * kh) per operation - 时间复杂度: 每次操作 O(w * h * kw * kh)
  • Space complexity: O(w * h) for output image - 空间复杂度: O(w * h) 输出图像

Security | 安全性:

  • Thread-safe: Yes (stateless) - 线程安全: 是(无状态)
  • Null-safe: No - 空值安全: 否
Since:
JDK 25, opencode-base-image V2.0.0
Author:
Leon Soo www.LeonSoo.com
See Also:
  • Method Details

    • erode

      public static BufferedImage erode(BufferedImage image, StructuringElement element)
      Erode an image using the given structuring element. 使用给定的结构元素对图像进行腐蚀。

      For each pixel, computes the minimum grayscale value over the structuring element mask. The image is first converted to grayscale via ChannelOp.toGray(int[]).

      对于每个像素,计算结构元素掩码范围内的最小灰度值。 图像首先通过 ChannelOp.toGray(int[]) 转换为灰度图像。

      Parameters:
      image - the source image | 源图像
      element - the structuring element | 结构元素
      Returns:
      the eroded grayscale image | 腐蚀后的灰度图像
      Throws:
      NullPointerException - if any argument is null | 当任一参数为 null 时抛出
    • dilate

      public static BufferedImage dilate(BufferedImage image, StructuringElement element)
      Dilate an image using the given structuring element. 使用给定的结构元素对图像进行膨胀。

      For each pixel, computes the maximum grayscale value over the structuring element mask. The image is first converted to grayscale via ChannelOp.toGray(int[]).

      对于每个像素,计算结构元素掩码范围内的最大灰度值。 图像首先通过 ChannelOp.toGray(int[]) 转换为灰度图像。

      Parameters:
      image - the source image | 源图像
      element - the structuring element | 结构元素
      Returns:
      the dilated grayscale image | 膨胀后的灰度图像
      Throws:
      NullPointerException - if any argument is null | 当任一参数为 null 时抛出
    • open

      public static BufferedImage open(BufferedImage image, StructuringElement element)
      Open an image (erode then dilate). 对图像进行开运算(先腐蚀后膨胀)。

      Opening removes small bright regions (noise) while preserving larger structures.

      开运算去除小的亮区域(噪声),同时保留较大结构。

      Parameters:
      image - the source image | 源图像
      element - the structuring element | 结构元素
      Returns:
      the opened grayscale image | 开运算后的灰度图像
      Throws:
      NullPointerException - if any argument is null | 当任一参数为 null 时抛出
    • close

      public static BufferedImage close(BufferedImage image, StructuringElement element)
      Close an image (dilate then erode). 对图像进行闭运算(先膨胀后腐蚀)。

      Closing fills small dark regions (gaps) while preserving larger structures.

      闭运算填充小的暗区域(空隙),同时保留较大结构。

      Parameters:
      image - the source image | 源图像
      element - the structuring element | 结构元素
      Returns:
      the closed grayscale image | 闭运算后的灰度图像
      Throws:
      NullPointerException - if any argument is null | 当任一参数为 null 时抛出
    • gradient

      public static BufferedImage gradient(BufferedImage image, StructuringElement element)
      Compute the morphological gradient (dilate minus erode). 计算形态学梯度(膨胀减去腐蚀)。

      The gradient highlights edges and boundaries in the image.

      梯度突出图像中的边缘和边界。

      Parameters:
      image - the source image | 源图像
      element - the structuring element | 结构元素
      Returns:
      the gradient grayscale image | 梯度灰度图像
      Throws:
      NullPointerException - if any argument is null | 当任一参数为 null 时抛出
    • topHat

      public static BufferedImage topHat(BufferedImage image, StructuringElement element)
      Compute the top-hat transform (original minus opening). 计算顶帽变换(原图减去开运算)。

      Top-hat extracts small bright elements from a dark background.

      顶帽变换从暗背景中提取小的亮元素。

      Parameters:
      image - the source image | 源图像
      element - the structuring element | 结构元素
      Returns:
      the top-hat grayscale image | 顶帽变换灰度图像
      Throws:
      NullPointerException - if any argument is null | 当任一参数为 null 时抛出
    • blackHat

      public static BufferedImage blackHat(BufferedImage image, StructuringElement element)
      Compute the black-hat transform (closing minus original). 计算黑帽变换(闭运算减去原图)。

      Black-hat extracts small dark elements from a bright background.

      黑帽变换从亮背景中提取小的暗元素。

      Parameters:
      image - the source image | 源图像
      element - the structuring element | 结构元素
      Returns:
      the black-hat grayscale image | 黑帽变换灰度图像
      Throws:
      NullPointerException - if any argument is null | 当任一参数为 null 时抛出