Toán tử Scharr cho phép tìm cạnh theo cả hướng ngang và dọc.
Phương thức Scharr() của lớp Imgproc áp dụng thuật toán Scharr. Tham số:
- Hai Mat: ảnh nguồn, đích.
- Int: độ sâu ảnh (CV_SCHARR).
- Hai double: đạo hàm x và y (1 hoặc 0).
Ví dụ minh họa
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 ScharrEdgeDetection {
public static void main(String[] args) {
System.loadLibrary(Core.NATIVE_LIBRARY_NAME);
String file = "D:\\Images\\win2.jpg";
Mat src = Imgcodecs.imread(file);
Mat dst = new Mat();
// Scharr theo trục Y (x=0, y=1) - cạnh ngang
Imgproc.Scharr(src, dst, Imgproc.CV_SCHARR, 0, 1);
HighGui.imshow("Scharr - x:0 & y:1", dst);
// Scharr theo trục X (x=1, y=0) - cạnh dọc
Imgproc.Scharr(src, dst, Imgproc.CV_SCHARR, 1, 0);
HighGui.imshow("Scharr - x:1 & y:0", dst);
HighGui.waitKey();
}
}
Hình ảnh đầu vào và kết quả
Input: 
Scharr x:0 y:1: 
Scharr x:1 y:0: 