Class OtsuOp

java.lang.Object
cloud.opencode.base.image.threshold.OtsuOp

public final class OtsuOp extends Object
Otsu's Automatic Thresholding 大津法自动阈值选取

Implements Otsu's method for computing the optimal global threshold that maximizes inter-class variance between foreground and background pixels.

实现大津法,计算使前景和背景像素之间类间方差最大化的最优全局阈值。

Features | 主要功能:

  • Automatic optimal threshold computation - 自动最优阈值计算
  • Histogram-based inter-class variance maximization - 基于直方图的类间方差最大化
  • Combined compute + apply convenience method - 计算与应用一体化的便捷方法

Usage Examples | 使用示例:

// Automatic thresholding
Raster binary = OtsuOp.apply(raster);

// Compute threshold from histogram
int[] histogram = new int[256];
// ... populate histogram ...
int threshold = OtsuOp.computeThreshold(histogram);

Performance | 性能特性:

  • Time: O(n + 256) where n = pixel count - 时间: O(n + 256),n 为像素数量
  • Space: O(n) for output image - 空间: O(n) 用于输出图像

Security | 安全性:

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

    • computeThreshold

      public static int computeThreshold(int[] histogram)
      Compute the optimal Otsu threshold from a 256-bin histogram. 从 256 个 bin 的直方图中计算最优大津阈值。

      Iterates over all possible thresholds [0, 255] and selects the one that maximizes the inter-class variance: sigma_b^2 = w0 * w1 * (mu0 - mu1)^2.

      遍历所有可能的阈值 [0, 255],选取使类间方差最大化的阈值: sigma_b^2 = w0 * w1 * (mu0 - mu1)^2。

      Parameters:
      histogram - the 256-bin histogram (must have length 256) | 256 个 bin 的直方图(长度必须为 256)
      Returns:
      the optimal threshold [0, 255] | 最优阈值 [0, 255]
      Throws:
      ImageOperationException - if histogram is null or length is not 256 | 当直方图为 null 或长度不为 256 时抛出
    • apply

      public static Raster apply(Raster raster)
      Apply Otsu's automatic thresholding to a Raster. 对 Raster 应用大津法自动阈值处理。

      Builds a grayscale histogram, finds the optimal Otsu threshold, and writes a PixelFormat.GRAY_8 binary output.

      构建灰度直方图,求出最优大津阈值,输出 PixelFormat.GRAY_8 二值结果。

      Parameters:
      raster - the source raster | 源 raster
      Returns:
      the GRAY_8 binary thresholded raster | GRAY_8 二值化后的 raster
      Throws:
      ImageOperationException - if raster is null | 当 raster 为 null 时抛出
      Since:
      opencode-base-image V1.0.4
    • apply

      public static Raster apply(Raster raster, Raster dst)
      Apply Otsu's automatic thresholding to a Raster with optional caller-provided output buffer. dst is reused when its (w, h, GRAY_8) match; otherwise a fresh raster is allocated. 对 Raster 应用大津法自动阈值处理,可指定输出缓冲。 当 dst 的 (w, h, GRAY_8) 匹配时复用,否则分配新 raster。
      Parameters:
      raster - the source raster | 源 raster
      dst - optional output raster (GRAY_8); may be null | 可选输出 raster(GRAY_8),可为 null
      Returns:
      the GRAY_8 binary thresholded raster | GRAY_8 二值化后的 raster
      Throws:
      ImageOperationException - if raster is null | 当 raster 为 null 时抛出
      Since:
      opencode-base-image V1.0.4