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

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

// Vertical gradient only (detects horizontal edges)
BufferedImage 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

    • apply

      public static BufferedImage apply(BufferedImage image)
      Compute Sobel gradient magnitude image. 计算 Sobel 梯度幅值图像。

      Combines horizontal and vertical Sobel gradients using magnitude = sqrt(gx^2 + gy^2), clamped to [0, 255].

      使用 magnitude = sqrt(gx^2 + gy^2) 合成水平和垂直 Sobel 梯度, 结果裁剪到 [0, 255]。

      Parameters:
      image - the source image | 源图像
      Returns:
      the gradient magnitude image | 梯度幅值图像
      Throws:
      NullPointerException - if image is null | 当图像为 null 时抛出
    • apply

      public static BufferedImage apply(BufferedImage image, int dx, int dy)
      Compute directional Sobel gradient image. 计算方向 Sobel 梯度图像。

      Use dx=1, dy=0 for horizontal gradient (detects vertical edges), or dx=0, dy=1 for vertical gradient (detects horizontal edges).

      使用 dx=1, dy=0 计算水平梯度(检测垂直边缘), 或 dx=0, dy=1 计算垂直梯度(检测水平边缘)。

      Parameters:
      image - the source image | 源图像
      dx - horizontal gradient flag (1 or 0) | 水平梯度标志(1 或 0)
      dy - vertical gradient flag (1 or 0) | 垂直梯度标志(1 或 0)
      Returns:
      the directional gradient image | 方向梯度图像
      Throws:
      NullPointerException - if image is null | 当图像为 null 时抛出
      ImageOperationException - if both dx and dy are 0 | 当 dx 和 dy 均为 0 时抛出
    • 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 | 当尺寸无效时抛出