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 | 使用示例:

Raster 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(Raster raster)
      Find all contours in a binary Raster. 在二值 Raster 中查找所有轮廓。

      Foreground pixels are those with non-zero BT.601 grayscale value.

      非零 BT.601 灰度值的像素为前景像素。

      Parameters:
      raster - the input raster | 输入 raster
      Returns:
      the list of detected contours | 检测到的轮廓列表
      Throws:
      NullPointerException - if raster is null | 当 raster 为 null 时抛出
      ImageOperationException - if dimensions are invalid | 当尺寸无效时抛出
      Since:
      opencode-base-image V1.0.4