在 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 −
廣告