Class SobelOp

java.lang.Object
cloud.opencode.base.image.edge.SobelOp

public final class SobelOp extends Object
Sobel Edge Detection Operator Sobel 边缘检测算子

Computes gradient magnitude using separable Sobel kernels. The horizontal kernel detects vertical edges and the vertical kernel detects horizontal edges. The combined gradient magnitude highlights all edges.

使用可分离 Sobel 卷积核计算梯度幅值。水平卷积核检测垂直边缘, 垂直卷积核检测水平边缘。合成梯度幅值突显所有边缘。

Features | 主要功能:

  • Full gradient magnitude (sqrt(gx^2 + gy^2)) edge detection - 完整梯度幅值边缘检测
  • Directional gradient (horizontal or vertical only) - 方向梯度(仅水平或垂直)
  • Grayscale gradient magnitude on raw pixel arrays - 灰度像素数组上的梯度幅值计算
  • Separable convolution for O(w*h*2k) performance - 可分离卷积实现 O(w*h*2k) 性能

Usage Examples | 使用示例:

// Full Sobel edge detection
Raster edges = SobelOp.apply(image);

// Horizontal gradient only (detects vertical edges)
Raster horizontal = SobelOp.apply(image, 1, 0);

// Vertical gradient only (detects horizontal edges)
Raster vertical = SobelOp.apply(image, 0, 1);

// Gradient magnitude on grayscale array
int[] magnitude = SobelOp.gradientMagnitude(grayPixels, width, height);

Performance | 性能特性:

  • Time complexity: O(w * h) using separable 3-tap kernels - 时间复杂度: 使用 3-tap 可分离卷积核 O(w * h)
  • Space complexity: O(w * h) - 空间复杂度: 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

    • gradientMagnitude

      public static int[] gradientMagnitude(int[] grayPixels, int width, int height)
      Compute gradient magnitude on a grayscale pixel array. 对灰度像素数组计算梯度幅值。

      Applies horizontal and vertical Sobel kernels and returns magnitude = sqrt(gx^2 + gy^2) clamped to [0, 255].

      应用水平和垂直 Sobel 卷积核,返回 magnitude = sqrt(gx^2 + gy^2) 裁剪到 [0, 255]。

      Parameters:
      grayPixels - the grayscale pixel array [0, 255] | 灰度像素数组 [0, 255]
      width - the image width | 图像宽度
      height - the image height | 图像高度
      Returns:
      the gradient magnitude array [0, 255] | 梯度幅值数组 [0, 255]
      Throws:
      NullPointerException - if grayPixels is null | 当 grayPixels 为 null 时抛出
      ImageOperationException - if dimensions are invalid | 当尺寸无效时抛出
    • apply

      public static Raster apply(Raster raster)
      Compute Sobel gradient magnitude on a Raster. Output is a GRAY_8 raster. 在 Raster 上计算 Sobel 梯度幅值。输出 GRAY_8 raster。
      Parameters:
      raster - the source raster | 源 raster
      Returns:
      the gradient magnitude as a GRAY_8 raster | 梯度幅值 GRAY_8 raster
      Throws:
      NullPointerException - if raster is null | 当 raster 为 null 时抛出
      Since:
      opencode-base-image V1.0.4
    • apply

      public static Raster apply(Raster raster, int dx, int dy)
      Compute directional Sobel gradient on a Raster. Use dx=1, dy=0 for the horizontal gradient (vertical edges); dx=0, dy=1 for the vertical gradient (horizontal edges); dx=1, dy=1 for the full magnitude. 在 Raster 上计算方向 Sobel 梯度。
      Parameters:
      raster - the source raster | 源 raster
      dx - horizontal gradient flag (1 or 0) | 水平梯度标志(1 或 0)
      dy - vertical gradient flag (1 or 0) | 垂直梯度标志(1 或 0)
      Returns:
      the directional gradient GRAY_8 raster | 方向梯度 GRAY_8 raster
      Throws:
      ImageOperationException - if both dx and dy are 0 | 当 dx 和 dy 均为 0 时抛出
      Since:
      opencode-base-image V1.0.4