Smoothing.java 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. import org.opencv.core.*;
  2. import org.opencv.highgui.HighGui;
  3. import org.opencv.imgcodecs.Imgcodecs;
  4. import org.opencv.imgproc.Imgproc;
  5. class SmoothingRun {
  6. /// Global Variables
  7. int DELAY_CAPTION = 1500;
  8. int DELAY_BLUR = 100;
  9. int MAX_KERNEL_LENGTH = 31;
  10. Mat src = new Mat(), dst = new Mat();
  11. String windowName = "Filter Demo 1";
  12. public void run(String[] args) {
  13. String filename = ((args.length > 0) ? args[0] : "../data/lena.jpg");
  14. src = Imgcodecs.imread(filename, Imgcodecs.IMREAD_COLOR);
  15. if( src.empty() ) {
  16. System.out.println("Error opening image");
  17. System.out.println("Usage: ./Smoothing [image_name -- default ../data/lena.jpg] \n");
  18. System.exit(-1);
  19. }
  20. if( displayCaption( "Original Image" ) != 0 ) { System.exit(0); }
  21. dst = src.clone();
  22. if( displayDst( DELAY_CAPTION ) != 0 ) { System.exit(0); }
  23. /// Applying Homogeneous blur
  24. if( displayCaption( "Homogeneous Blur" ) != 0 ) { System.exit(0); }
  25. //! [blur]
  26. for (int i = 1; i < MAX_KERNEL_LENGTH; i = i + 2) {
  27. Imgproc.blur(src, dst, new Size(i, i), new Point(-1, -1));
  28. displayDst(DELAY_BLUR);
  29. }
  30. //! [blur]
  31. /// Applying Gaussian blur
  32. if( displayCaption( "Gaussian Blur" ) != 0 ) { System.exit(0); }
  33. //! [gaussianblur]
  34. for (int i = 1; i < MAX_KERNEL_LENGTH; i = i + 2) {
  35. Imgproc.GaussianBlur(src, dst, new Size(i, i), 0, 0);
  36. displayDst(DELAY_BLUR);
  37. }
  38. //! [gaussianblur]
  39. /// Applying Median blur
  40. if( displayCaption( "Median Blur" ) != 0 ) { System.exit(0); }
  41. //! [medianblur]
  42. for (int i = 1; i < MAX_KERNEL_LENGTH; i = i + 2) {
  43. Imgproc.medianBlur(src, dst, i);
  44. displayDst(DELAY_BLUR);
  45. }
  46. //! [medianblur]
  47. /// Applying Bilateral Filter
  48. if( displayCaption( "Bilateral Blur" ) != 0 ) { System.exit(0); }
  49. //![bilateralfilter]
  50. for (int i = 1; i < MAX_KERNEL_LENGTH; i = i + 2) {
  51. Imgproc.bilateralFilter(src, dst, i, i * 2, i / 2);
  52. displayDst(DELAY_BLUR);
  53. }
  54. //![bilateralfilter]
  55. /// Done
  56. displayCaption( "Done!" );
  57. System.exit(0);
  58. }
  59. int displayCaption(String caption) {
  60. dst = Mat.zeros(src.size(), src.type());
  61. Imgproc.putText(dst, caption,
  62. new Point(src.cols() / 4, src.rows() / 2),
  63. Imgproc.FONT_HERSHEY_COMPLEX, 1, new Scalar(255, 255, 255));
  64. return displayDst(DELAY_CAPTION);
  65. }
  66. int displayDst(int delay) {
  67. HighGui.imshow( windowName, dst );
  68. int c = HighGui.waitKey( delay );
  69. if (c >= 0) { return -1; }
  70. return 0;
  71. }
  72. }
  73. public class Smoothing {
  74. public static void main(String[] args) {
  75. // Load the native library.
  76. System.loadLibrary(Core.NATIVE_LIBRARY_NAME);
  77. new SmoothingRun().run(args);
  78. }
  79. }