Skip to content

Commit

Permalink
do not iterate multiple times over the full image
Browse files Browse the repository at this point in the history
  • Loading branch information
mkuehne707 committed Oct 7, 2023
1 parent fe8e0e3 commit 4ebdad4
Showing 1 changed file with 12 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -289,15 +289,17 @@ private List<Rectangle> populateRectangles() {
return emptyList();
}
groupRegions();
List<Rectangle> rectangles = new ArrayList<>();
while (counter <= regionCount) {
Rectangle rectangle = createRectangle();
if (!rectangle.equals(Rectangle.createDefault()) && rectangle.size() >= minimalRectangleSize) {
rectangles.add(rectangle);
}
counter++;
Map<Integer, Rectangle> regions = new LinkedHashMap<>();
System.out.println("RegionCount: " + regionCount);
for (int i = counter; i < regionCount; i++) {
regions.put(i, Rectangle.createDefault());
}
createRectangles(counter, regions);

List<Rectangle> rectangles = regions.values().stream()
.filter(rectangle -> !rectangle.equals(Rectangle.createDefault())
&& rectangle.size() >= minimalRectangleSize)
.collect(Collectors.toList());
return mergeRectangles(mergeRectangles(rectangles));
}

Expand All @@ -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<Integer, Rectangle> 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;
}

/**
Expand Down

0 comments on commit 4ebdad4

Please sign in to comment.