Advertisement
Guest User

Untitled

a guest
Oct 20th, 2017
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.91 KB | None | 0 0
  1. public static void lakeRemove() {
  2. try {
  3. System.loadLibrary(Core.NATIVE_LIBRARY_NAME);
  4. Mat source = Imgcodecs.imread("lake.png", Imgcodecs.CV_LOAD_IMAGE_COLOR);
  5. Mat sourceClone = source.clone();
  6.  
  7. Imgproc.cvtColor(source, source, Imgproc.COLOR_BGR2GRAY, 0);
  8. // Imgcodecs.imwrite("noLake1.png", source);
  9.  
  10. Mat morph = Imgproc.getStructuringElement(Imgproc.MORPH_RECT, new Size(9, 3));
  11. Imgproc.morphologyEx(source, source, Imgproc.MORPH_TOPHAT, morph);
  12. // Imgcodecs.imwrite("noLake2.png", source);
  13.  
  14. Imgproc.threshold(source, source, 0, 255, Imgproc.THRESH_OTSU);
  15. // Imgcodecs.imwrite("noLake3.png", source);
  16.  
  17. Imgproc.adaptiveThreshold(source, source, 255, Imgproc.ADAPTIVE_THRESH_GAUSSIAN_C,
  18. Imgproc.THRESH_BINARY_INV, 13, 2);
  19. // Imgcodecs.imwrite("noLake4.png", source);
  20.  
  21. // Imgcodecs.imwrite("cc.png", sourceClone);
  22.  
  23. for (int i = 0; i < source.rows(); i++) {
  24. for (int j = 0; j < source.cols(); j++) {
  25.  
  26. double[] d = source.get(i, j);
  27. // System.out.println(d.length);
  28. // System.out.println(d[0]);
  29. if (d[0] == 255) {
  30. source.put(i, j + 1, d);
  31. source.put(i, j + 2, d);
  32. source.put(i, j + 3, d);
  33. source.put(i, j + 4, d);
  34. source.put(i, j + 5, d);
  35. source.put(i, j + 6, d);
  36. source.put(i, j + 7, d);
  37.  
  38.  
  39. j += 7;
  40. }
  41. }
  42. }
  43. // Imgcodecs.imwrite("noLake5.png", source);
  44. Mat destination = new Mat(source.rows(), source.cols(), source.type());
  45.  
  46. for (int i = 0; i < source.rows(); i++) {
  47. for (int j = 0; j < source.cols(); j++) {
  48.  
  49. double[] d = source.get(i, j);
  50. // System.out.println(d.length);
  51. if (d[0] == 255)
  52. destination.put(i, j, sourceClone.get(i, j));
  53. else
  54. destination.put(i, j, d);
  55. }
  56.  
  57. Imgcodecs.imwrite("noLake.png", destination);
  58.  
  59. }
  60.  
  61. } catch (Exception e) {
  62. System.out.println("error: " + e.getMessage());
  63. }
  64.  
  65. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement