Java 示例演示 OpenCV 中的 Sobel 边缘检测。

javaobject oriented programmingprogramming更新于 2025/10/9 12:28:17

用于边缘检测的 Sobel 运算符允许您在水平和垂直方向上找到给定图像中的边缘。

Imgproc 类的 Sobel() 方法将 Sobel 边缘检测算法应用于给定图像。此方法接受 −

  • 两个 Mat 对象,分别表示源图像和目标图像。

  • 一个表示图像深度的整数变量。

  • 两个双精度变量,用于保存 x 和 y 导数。

示例

import org.opencv.core.Core;
import org.opencv.core.Mat;
import org.opencv.highgui.HighGui;
import org.opencv.imgcodecs.Imgcodecs;
import org.opencv.imgproc.Imgproc;
public class SobelEdgeDetection {
   public static void main(String args[]) {
      //加载 OpenCV 核心库
      System.loadLibrary( Core.NATIVE_LIBRARY_NAME );
      String file ="D:\Images\win2.jpg";
      Mat src = Imgcodecs.imread(file);
      //为目标图像创建一个空矩阵
      Mat dst = new Mat();
      //应用值为 x:0 y:1 的 sobel 导数
      Imgproc.Sobel(src, dst, -1, 0, 1);
      HighGui.imshow("Sobel - x:0 & y:1 ", dst);
      HighGui.waitKey();
      //应用值为 x:1 y:0 的 sobel 导数
      Imgproc.Sobel(src, dst, -1, 1, 0);
      HighGui.imshow("Sobel - x:1 & y:0 ", dst);
      HighGui.waitKey();
      //应用值为 x:1 y:1 的 sobel 导数
      Imgproc.Sobel(src, dst, -1, 1, 1);
      HighGui.imshow("Sobel - x:1 & y:1 ", dst);
      HighGui.waitKey();
   }
}

输出

执行时,上述程序生成以下窗口 −

Sobel - x:0 & y:1 − 

Sobel - x:1 & y:0 − 

Sobel - x:1 & y:1 减号; 


相关文章


有用资源