1 /**
2  * @file LaplaceDemo.java
3  * @brief Sample code showing how to detect edges using the Laplace operator
4  */
5 
6 import org.opencv.core.*;
7 import org.opencv.highgui.HighGui;
8 import org.opencv.imgcodecs.Imgcodecs;
9 import org.opencv.imgproc.Imgproc;
10 
11 class LaplaceDemoRun {
12 
run(String[] args)13     public void run(String[] args) {
14         //! [variables]
15         // Declare the variables we are going to use
16         Mat src, src_gray = new Mat(), dst = new Mat();
17         int kernel_size = 3;
18         int scale = 1;
19         int delta = 0;
20         int ddepth = CvType.CV_16S;
21         String window_name = "Laplace Demo";
22         //! [variables]
23 
24         //! [load]
25         String imageName = ((args.length > 0) ? args[0] : "../data/lena.jpg");
26 
27         src = Imgcodecs.imread(imageName, Imgcodecs.IMREAD_COLOR); // Load an image
28 
29         // Check if image is loaded fine
30         if( src.empty() ) {
31             System.out.println("Error opening image");
32             System.out.println("Program Arguments: [image_name -- default ../data/lena.jpg] \n");
33             System.exit(-1);
34         }
35         //! [load]
36 
37         //! [reduce_noise]
38         // Reduce noise by blurring with a Gaussian filter ( kernel size = 3 )
39         Imgproc.GaussianBlur( src, src, new Size(3, 3), 0, 0, Core.BORDER_DEFAULT );
40         //! [reduce_noise]
41 
42         //! [convert_to_gray]
43         // Convert the image to grayscale
44         Imgproc.cvtColor( src, src_gray, Imgproc.COLOR_RGB2GRAY );
45         //! [convert_to_gray]
46 
47         /// Apply Laplace function
48         Mat abs_dst = new Mat();
49         //! [laplacian]
50         Imgproc.Laplacian( src_gray, dst, ddepth, kernel_size, scale, delta, Core.BORDER_DEFAULT );
51         //! [laplacian]
52 
53         //! [convert]
54         // converting back to CV_8U
55         Core.convertScaleAbs( dst, abs_dst );
56         //! [convert]
57 
58         //! [display]
59         HighGui.imshow( window_name, abs_dst );
60         HighGui.waitKey(0);
61         //! [display]
62 
63         System.exit(0);
64     }
65 }
66 
67 public class LaplaceDemo {
main(String[] args)68     public static void main(String[] args) {
69         // Load the native library.
70         System.loadLibrary(Core.NATIVE_LIBRARY_NAME);
71         new LaplaceDemoRun().run(args);
72     }
73 }
74