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
BufferedImage binary = OtsuOp.apply(image);

// 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 BufferedImage apply(BufferedImage image)
      Apply Otsu's automatic thresholding to an image. 对图像应用大津法自动阈值处理。

      Computes the grayscale histogram, finds the optimal Otsu threshold, and applies binary thresholding using ThresholdOp.

      计算灰度直方图,找到最优大津阈值,然后使用 ThresholdOp 应用二值化。

      Parameters:
      image - the source image | 源图像
      Returns:
      the binary thresholded image | 二值化后的图像
      Throws:
      ImageOperationException - if image is null | 当图像为 null 时抛出