ImageUtils { public static void removeOverlappingRectangles(List<Rectangle> rectangles) { int smallDiff = 2; List<Rectangle> removeList = new ArrayList<Rectangle>(); for (int i = 0; i < rectangles.size(); i++) { Rectangle rect1 = rectangles.get(i); int xLeft1 = (int) rect1.getX(); int xRight1 = (int) (rect1.getX() + rect1.getWidth()); int yTop1 = (int) rect1.getY(); int yBottom1 = (int) (rect1.getY() + rect1.getHeight()); for (int j = i + 1; j < rectangles.size(); j++) { Rectangle rect2 = rectangles.get(j); int xLeft2 = (int) rect2.getX(); int xRight2 = (int) (rect2.getX() + rect2.getWidth()); int yTop2 = (int) rect2.getY(); int yBottom2 = (int) (rect2.getY() + rect2.getHeight()); if (xLeft1 - smallDiff <= xLeft2 && yTop1 - smallDiff <= yTop2 && xRight1 + smallDiff >= xRight2 && yBottom1 + smallDiff >= yBottom2) { removeList.add(rect2); } else if (xLeft2 - smallDiff <= xLeft1 && yTop2 - smallDiff <= yTop1 && xRight2 + smallDiff >= xRight1 && yBottom2 + smallDiff >= yBottom1) { removeList.add(rect1); } } } for (Rectangle removeRect : removeList) { rectangles.remove(removeRect); } } private  ImageUtils(); static ImageComparedResult compare(BufferedImage image1, Rectangle imageArea1, BufferedImage image2,
			Rectangle imageArea2, CompareOption[] options); static boolean isContained(BufferedImage entireImage, BufferedImage partImage); static double[][] calcIntegralImage(BufferedImage source); static boolean imageEquals(BufferedImage image1, BufferedImage image2); static BufferedImage getMaskedImage(BufferedImage image, List<Rectangle> maskAreas); static BufferedImage getMarkedImage(BufferedImage image, DiffPoints diffPoints); static BufferedImage getDiffImage(BufferedImage leftImage, BufferedImage rightImage, DiffPoints diffPoints); static BufferedImage trim(BufferedImage image, int trimTop, int trimLeft, int trimBottom, int trimRight); static BufferedImage verticalMerge(List<BufferedImage> images); static BufferedImage merge(List<List<BufferedImage>> images); static int[] getRGB(BufferedImage image, int width, int height); static int[] toARGB(int pixel); static int toPixel(int[] argb); static int toPixel(int r, int g, int b); static int toPixel(int a, int r, int g, int b); static int detectBackgroundColor(BufferedImage b); static boolean getObjectRectangle(BufferedImage image, Rectangle rectangle); static double norm(int pixel1, int pixel2); static double[] countSubpixel(BufferedImage bimage); static void removeRedundantRectangles(List<Rectangle> rectangles, int xLimit, int yLimit); static void removeOverlappingRectangles(List<Rectangle> rectangles); static void reshapeRect(Rectangle rectangle, int xLimit, int yLimit); static Rectangle getTightDiffArea(Rectangle rectangle, int xLimit, int yLimit); static List<Rectangle> convertObjectGroupsToAreas(List<ObjectGroup> objectGroups); static List<ObjectGroup> convertDiffPointsToObjectGroups(DiffPoints DP, int group_distance); static BufferedImage getSubImage(BufferedImage image, Rectangle rectangle); static Offset findDominantOffset(BufferedImage expectedImage, BufferedImage actualImage, double diffThreshold); static BufferedImage getDominantImage(BufferedImage expectedImage, BufferedImage actualImage, Offset offset); static int getSizeRelationType(int expectedWidth, int expectedHeight, int actualWidth, int actualHeight);  }
@Test public void testRemoveOverlappingRectangles() throws Exception { List<Rectangle> rectangles = new ArrayList<Rectangle>(); rectangles.add(new Rectangle(0, 0, 80, 80)); rectangles.add(new Rectangle(10, 10, 100, 100)); rectangles.add(new Rectangle(0, 0, 90, 90)); rectangles.add(new Rectangle(0, 0, 90, 90)); double[][] expected = { { 10.0, 10.0, 100.0, 100.0 }, { 0.0, 0.0, 90.0, 90.0 } }; ImageUtils.removeOverlappingRectangles(rectangles); assertThat(rectangles.size(), is(2)); for (int i = 0; i < rectangles.size(); i++) { Rectangle rectangle = rectangles.get(i); assertThat(rectangle.getX(), is(expected[i][0])); assertThat(rectangle.getY(), is(expected[i][1])); assertThat(rectangle.getWidth(), is(expected[i][2])); assertThat(rectangle.getHeight(), is(expected[i][3])); } }