SobelDemo.java 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. /**
  2. * @file SobelDemo.java
  3. * @brief Sample code using Sobel and/or Scharr OpenCV functions to make a simple Edge Detector
  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 SobelDemoRun {
  10. public void run(String[] args) {
  11. //! [declare_variables]
  12. // First we declare the variables we are going to use
  13. Mat src, src_gray = new Mat();
  14. Mat grad = new Mat();
  15. String window_name = "Sobel Demo - Simple Edge Detector";
  16. int scale = 1;
  17. int delta = 0;
  18. int ddepth = CvType.CV_16S;
  19. //! [declare_variables]
  20. //! [load]
  21. // As usual we load our source image (src)
  22. // Check number of arguments
  23. if (args.length == 0){
  24. System.out.println("Not enough parameters!");
  25. System.out.println("Program Arguments: [image_path]");
  26. System.exit(-1);
  27. }
  28. // Load the image
  29. src = Imgcodecs.imread(args[0]);
  30. // Check if image is loaded fine
  31. if( src.empty() ) {
  32. System.out.println("Error opening image: " + args[0]);
  33. System.exit(-1);
  34. }
  35. //! [load]
  36. //! [reduce_noise]
  37. // Remove noise by blurring with a Gaussian filter ( kernel size = 3 )
  38. Imgproc.GaussianBlur( src, src, new Size(3, 3), 0, 0, Core.BORDER_DEFAULT );
  39. //! [reduce_noise]
  40. //! [convert_to_gray]
  41. // Convert the image to grayscale
  42. Imgproc.cvtColor( src, src_gray, Imgproc.COLOR_RGB2GRAY );
  43. //! [convert_to_gray]
  44. //! [sobel]
  45. /// Generate grad_x and grad_y
  46. Mat grad_x = new Mat(), grad_y = new Mat();
  47. Mat abs_grad_x = new Mat(), abs_grad_y = new Mat();
  48. /// Gradient X
  49. //Imgproc.Scharr( src_gray, grad_x, ddepth, 1, 0, scale, delta, Core.BORDER_DEFAULT );
  50. Imgproc.Sobel( src_gray, grad_x, ddepth, 1, 0, 3, scale, delta, Core.BORDER_DEFAULT );
  51. /// Gradient Y
  52. //Imgproc.Scharr( src_gray, grad_y, ddepth, 0, 1, scale, delta, Core.BORDER_DEFAULT );
  53. Imgproc.Sobel( src_gray, grad_y, ddepth, 0, 1, 3, scale, delta, Core.BORDER_DEFAULT );
  54. //! [sobel]
  55. //![convert]
  56. // converting back to CV_8U
  57. Core.convertScaleAbs( grad_x, abs_grad_x );
  58. Core.convertScaleAbs( grad_y, abs_grad_y );
  59. //![convert]
  60. //! [add_weighted]
  61. /// Total Gradient (approximate)
  62. Core.addWeighted( abs_grad_x, 0.5, abs_grad_y, 0.5, 0, grad );
  63. //! [add_weighted]
  64. //! [display]
  65. HighGui.imshow( window_name, grad );
  66. HighGui.waitKey(0);
  67. //! [display]
  68. System.exit(0);
  69. }
  70. }
  71. public class SobelDemo {
  72. public static void main(String[] args) {
  73. // Load the native library.
  74. System.loadLibrary(Core.NATIVE_LIBRARY_NAME);
  75. new SobelDemoRun().run(args);
  76. }
  77. }