Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public static void lakeRemove() {
- try {
- System.loadLibrary(Core.NATIVE_LIBRARY_NAME);
- Mat source = Imgcodecs.imread("lake.png", Imgcodecs.CV_LOAD_IMAGE_COLOR);
- Mat sourceClone = source.clone();
- Imgproc.cvtColor(source, source, Imgproc.COLOR_BGR2GRAY, 0);
- // Imgcodecs.imwrite("noLake1.png", source);
- Mat morph = Imgproc.getStructuringElement(Imgproc.MORPH_RECT, new Size(9, 3));
- Imgproc.morphologyEx(source, source, Imgproc.MORPH_TOPHAT, morph);
- // Imgcodecs.imwrite("noLake2.png", source);
- Imgproc.threshold(source, source, 0, 255, Imgproc.THRESH_OTSU);
- // Imgcodecs.imwrite("noLake3.png", source);
- Imgproc.adaptiveThreshold(source, source, 255, Imgproc.ADAPTIVE_THRESH_GAUSSIAN_C,
- Imgproc.THRESH_BINARY_INV, 13, 2);
- // Imgcodecs.imwrite("noLake4.png", source);
- // Imgcodecs.imwrite("cc.png", sourceClone);
- for (int i = 0; i < source.rows(); i++) {
- for (int j = 0; j < source.cols(); j++) {
- double[] d = source.get(i, j);
- // System.out.println(d.length);
- // System.out.println(d[0]);
- if (d[0] == 255) {
- source.put(i, j + 1, d);
- source.put(i, j + 2, d);
- source.put(i, j + 3, d);
- source.put(i, j + 4, d);
- source.put(i, j + 5, d);
- source.put(i, j + 6, d);
- source.put(i, j + 7, d);
- j += 7;
- }
- }
- }
- // Imgcodecs.imwrite("noLake5.png", source);
- Mat destination = new Mat(source.rows(), source.cols(), source.type());
- for (int i = 0; i < source.rows(); i++) {
- for (int j = 0; j < source.cols(); j++) {
- double[] d = source.get(i, j);
- // System.out.println(d.length);
- if (d[0] == 255)
- destination.put(i, j, sourceClone.get(i, j));
- else
- destination.put(i, j, d);
- }
- Imgcodecs.imwrite("noLake.png", destination);
- }
- } catch (Exception e) {
- System.out.println("error: " + e.getMessage());
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement