Computer >> Hướng Dẫn Máy Tính >  >> Lập Trình >> Java

Phát hiện cạnh Sobel trong OpenCV Java

Toán tử Sobel tìm cạnh theo hướng ngang và dọc.

Phương thức Sobel() của Imgproc áp dụng thuật toán Sobel. Tham số:

  • Hai Mat: ảnh nguồn, đích.
  • Int: độ sâu ảnh (-1 = cùng độ sâu nguồn).
  • Hai double: đạo hàm x và y.

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 SobelEdgeDetection {
    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();
        
        // Sobel x=0, y=1 (cạnh ngang)
        Imgproc.Sobel(src, dst, -1, 0, 1);
        HighGui.imshow("Sobel - x:0 & y:1", dst);
        HighGui.waitKey();
        
        // Sobel x=1, y=0 (cạnh dọc)
        Imgproc.Sobel(src, dst, -1, 1, 0);
        HighGui.imshow("Sobel - x:1 & y:0", dst);
        HighGui.waitKey();
        
        // Sobel x=1, y=1 (cả hai hướng)
        Imgproc.Sobel(src, dst, -1, 1, 1);
        HighGui.imshow("Sobel - x:1 & y:1", dst);
        HighGui.waitKey();
    }
}

Kết quả

Sobel x:0 y:1: Phát hiện cạnh Sobel trong OpenCV Java

Sobel x:1 y:0: Phát hiện cạnh Sobel trong OpenCV Java

Sobel x:1 y:1: Phát hiện cạnh Sobel trong OpenCV Java