LaplaceDemo.java 2.1 KB

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