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


用于边缘检测的 Sobel 算子让你能够在图像中找到水平和垂直方向上的边缘。

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

  • 表示源图像和目标图像的两个 Mat 对象。

  • 表示图像深度的 integer 变量。

  • 两个 double 变量,以存放 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[]) {
      //Loading the OpenCV core library
      System.loadLibrary( Core.NATIVE_LIBRARY_NAME );
      String file ="D:\Images\win2.jpg";
      Mat src = Imgcodecs.imread(file);
      //Creating an empty matrix for the destination image
      Mat dst = new Mat();
      //Applying sobel derivative with values x:0 y:1
      Imgproc.Sobel(src, dst, -1, 0, 1);
      HighGui.imshow("Sobel - x:0 & y:1 ", dst);
      HighGui.waitKey();
      //Applying sobel derivative with values x:1 y:0
      Imgproc.Sobel(src, dst, -1, 1, 0);
      HighGui.imshow("Sobel - x:1 & y:0 ", dst);
      HighGui.waitKey();
      //Applying sobel derivative with values x:1 y:1
      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 − 

更新时间:2020 年 4 月 13 日

700 次浏览

开启你的 职业生涯

通过完成课程获得认证

开始
广告
© . All rights reserved.