使用索贝尔(sobel)操作,可以在水平和垂直方向上检测图像的边缘。可以使用sobel()方法在图像上应用sobel操作。以下是这种方法的语法 -
Sobel(src, dst, ddepth, dx, dy)
该方法接受以下参数 -
- src - 表示源(输入)图像的
Mat类的对象。 - dst - 表示目标(输出)图像的
Mat类的对象。 - ddepth - 表示图像深度的整数变量(
-1)。 - dx - 表示
x导数的整数变量(0或1)。 - dy - 表示
y导数的整数变量(0或1)。
示例
以下程序演示如何在给定图像上执行Sobel操作。
package com.zaixian.sobelderivatives;
import org.opencv.core.Core;
import org.opencv.core.Mat;
import org.opencv.imgcodecs.Imgcodecs;
import org.opencv.imgproc.Imgproc;
public class SobelTest {
public static void main(String args[]) {
// Loading the OpenCV core library
System.loadLibrary(Core.NATIVE_LIBRARY_NAME);
// Reading the Image from the file and storing it in to a Matrix object
String file ="F:/worksp/opencv/images/sample3.jpg";
Mat src = Imgcodecs.imread(file);
// Creating an empty matrix to store the result
Mat dst = new Mat();
// Applying sobel on the Image
Imgproc.Sobel(src, dst, -1, 1, 1);
// Writing the image
Imgcodecs.imwrite("F:/worksp/opencv/images/sample3sobel_output.jpg", dst);
System.out.println("Image processed");
}
}
假定以下是上述程序中指定的输入图像sample3.jpg。
执行上面示例代码,得到以下结果 -
索贝尔变体
将不同的值传递给最后一个参数(dx和dy),它的值在0和1之间,会得到不同的输出。
上一篇:
OpenCV添加边框
下一篇:
OpenCV Scharr操作
