Class ContourFinderOp

java.lang.Object
cloud.opencode.base.image.analysis.ContourFinderOp

public final class ContourFinderOp extends Object
Contour Finder using Moore Neighborhood Border Tracing Algorithm 基于 Moore 邻域边界追踪算法的轮廓检测

Finds contours (boundaries) of foreground regions in binary images. Each contour is a list of (x, y) border pixel coordinates forming the boundary of a connected foreground region.

在二值图像中查找前景区域的轮廓(边界)。每个轮廓是一组 (x, y) 边界像素坐标, 构成一个连通前景区域的边界。

Features | 主要功能:

  • Moore neighborhood border tracing for contour extraction - Moore 邻域边界追踪提取轮廓
  • Per-contour metrics: area (Shoelace formula), perimeter, bounding box - 每个轮廓的指标: 面积(鞋带公式)、周长、边界框
  • Handles multiple disjoint contours in a single image - 处理单张图像中的多个不相交轮廓
  • Visited-pixel tracking prevents duplicate detection - 已访问像素追踪防止重复检测

Usage Examples | 使用示例:

BufferedImage binary = ...; // binary image with white foreground on black background
List<ContourFinderOp.Contour> contours = ContourFinderOp.find(binary);
for (ContourFinderOp.Contour c : contours) {
    System.out.printf("Contour: %d points, area=%.1f, perimeter=%.1f%n",
        c.size(), c.area(), c.perimeter());
    int[] bbox = c.boundingBox();
    System.out.printf("  BBox: x=%d, y=%d, w=%d, h=%d%n",
        bbox[0], bbox[1], bbox[2], bbox[3]);
}

Performance | 性能特性:

  • Time complexity: O(n) where n = width * height (each pixel visited at most a constant number of times) - 时间复杂度: O(n),n = 宽 * 高(每个像素最多被访问常数次)
  • Space complexity: O(n) for the visited mask and foreground mask - 空间复杂度: O(n) 用于已访问掩码和前景掩码

Security | 安全性:

  • Thread-safe: Yes (stateless, all methods are pure functions) - 线程安全: 是(无状态,所有方法为纯函数)
  • Null-safe: No (null image throws NullPointerException) - 空值安全: 否(null 图像抛出 NullPointerException)
Since:
JDK 25, opencode-base-image V2.0.0
Author:
Leon Soo www.LeonSoo.com
See Also:
  • Method Details

    • find

      public static List<ContourFinderOp.Contour> find(BufferedImage image)
      Find all contours in a binary image. 在二值图像中查找所有轮廓。

      Foreground pixels are those with non-zero grayscale value after converting the image to grayscale using ITU-R BT.601 coefficients. A border pixel is a foreground pixel adjacent to at least one background pixel (or image edge).

      将图像转换为灰度(使用 ITU-R BT.601 系数)后,非零灰度值的像素为前景像素。 边界像素是至少与一个背景像素(或图像边缘)相邻的前景像素。

      The algorithm uses Moore neighborhood border tracing:

      1. Scan image for unvisited foreground pixel adjacent to background
      2. Follow border clockwise using Moore neighborhood tracing
      3. Mark visited border pixels to avoid re-detection
      4. Repeat until no new contour starts found

      算法使用 Moore 邻域边界追踪:

      1. 扫描图像中未访问的、与背景相邻的前景像素
      2. 使用 Moore 邻域追踪沿边界顺时针行走
      3. 标记已访问的边界像素以避免重复检测
      4. 重复直到找不到新的轮廓起点
      Parameters:
      image - the input image | 输入图像
      Returns:
      the list of detected contours | 检测到的轮廓列表
      Throws:
      NullPointerException - if image is null | 当图像为 null 时抛出
      ImageOperationException - if the image has invalid dimensions | 当图像尺寸无效时抛出