From 4ebdad4993c50734908125e1c98d733976cc2d0e Mon Sep 17 00:00:00 2001 From: mkuehne707 Date: Sat, 7 Oct 2023 12:07:44 +0200 Subject: [PATCH 1/3] do not iterate multiple times over the full image --- .../image/comparison/ImageComparison.java | 26 +++++++++---------- 1 file changed, 12 insertions(+), 14 deletions(-) diff --git a/src/main/java/com/github/romankh3/image/comparison/ImageComparison.java b/src/main/java/com/github/romankh3/image/comparison/ImageComparison.java index 8067c0b..cc3d73d 100644 --- a/src/main/java/com/github/romankh3/image/comparison/ImageComparison.java +++ b/src/main/java/com/github/romankh3/image/comparison/ImageComparison.java @@ -289,15 +289,17 @@ private List populateRectangles() { return emptyList(); } groupRegions(); - List rectangles = new ArrayList<>(); - while (counter <= regionCount) { - Rectangle rectangle = createRectangle(); - if (!rectangle.equals(Rectangle.createDefault()) && rectangle.size() >= minimalRectangleSize) { - rectangles.add(rectangle); - } - counter++; + Map regions = new LinkedHashMap<>(); + System.out.println("RegionCount: " + regionCount); + for (int i = counter; i < regionCount; i++) { + regions.put(i, Rectangle.createDefault()); } + createRectangles(counter, regions); + List rectangles = regions.values().stream() + .filter(rectangle -> !rectangle.equals(Rectangle.createDefault()) + && rectangle.size() >= minimalRectangleSize) + .collect(Collectors.toList()); return mergeRectangles(mergeRectangles(rectangles)); } @@ -316,19 +318,15 @@ private boolean isAllowedPercentOfDifferentPixels(long countOfDifferentPixels) { /** * Create a {@link Rectangle} object. - * - * @return the {@link Rectangle} object. */ - private Rectangle createRectangle() { - Rectangle rectangle = Rectangle.createDefault(); + private void createRectangles(int counter, Map rectangles) { for (int y = 0; y < matrix.length; y++) { for (int x = 0; x < matrix[0].length; x++) { - if (matrix[y][x] == counter) { - updateRectangleCreation(rectangle, x, y); + if (matrix[y][x] >= counter) { + updateRectangleCreation(rectangles.get(matrix[y][x]), x, y); } } } - return rectangle; } /** From 4820d35a8686350f64b3ac65beab0075e80bb714 Mon Sep 17 00:00:00 2001 From: mkuehne707 Date: Sat, 7 Oct 2023 12:12:51 +0200 Subject: [PATCH 2/3] replaced the streaming api with foreach as it is significantly faster --- .../romankh3/image/comparison/model/ExcludedAreas.java | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/src/main/java/com/github/romankh3/image/comparison/model/ExcludedAreas.java b/src/main/java/com/github/romankh3/image/comparison/model/ExcludedAreas.java index d896d37..05b7386 100644 --- a/src/main/java/com/github/romankh3/image/comparison/model/ExcludedAreas.java +++ b/src/main/java/com/github/romankh3/image/comparison/model/ExcludedAreas.java @@ -39,7 +39,11 @@ public ExcludedAreas(List excluded) { * @return {@code true} if this {@link Point} contains in areas from {@link ExcludedAreas#excluded}. */ public boolean contains(Point point) { - return excluded.stream().anyMatch(rectangle -> rectangle.containsPoint(point)); + for (Rectangle rectangle : excluded) { + if (rectangle.containsPoint(point)) + return true; + } + return false; } /** From ef59fda182befa866c120b957f5676dc7d3ba9b9 Mon Sep 17 00:00:00 2001 From: mkuehne707 Date: Sat, 7 Oct 2023 16:06:21 +0200 Subject: [PATCH 3/3] ... remove print --- .../com/github/romankh3/image/comparison/ImageComparison.java | 1 - 1 file changed, 1 deletion(-) diff --git a/src/main/java/com/github/romankh3/image/comparison/ImageComparison.java b/src/main/java/com/github/romankh3/image/comparison/ImageComparison.java index cc3d73d..ff00dd0 100644 --- a/src/main/java/com/github/romankh3/image/comparison/ImageComparison.java +++ b/src/main/java/com/github/romankh3/image/comparison/ImageComparison.java @@ -290,7 +290,6 @@ private List populateRectangles() { } groupRegions(); Map regions = new LinkedHashMap<>(); - System.out.println("RegionCount: " + regionCount); for (int i = counter; i < regionCount; i++) { regions.put(i, Rectangle.createDefault()); }