@Test public void testGetPosition() throws Exception { BufferedImage haystack = ImageIO.read(new File("C:\\Users\\flux\\Dropbox\\java\\VoxelShop\\Test Files\\SubImage Search\\haystack_small.png")); BufferedImage needle = ImageIO.read(new File("C:\\Users\\flux\\Dropbox\\java\\VoxelShop\\Test Files\\SubImage Search\\needle.png")); ImageComparator parent = new ImageComparator(haystack); ImageComparator child = new ImageComparator(needle); long time = System.currentTimeMillis(); int[] result = parent.getPosition(child, null); System.out.println("Time: " + (System.currentTimeMillis() - time)); if (result != null) { System.out.println("R " + result[0] + " " + result[1] + " @ " + result[2]); } assert getResult("needle.png", "o1.png", null)[0] == 0; assert getResult("needle.png", "o1.png", null)[1] == 0; assert getResult("needle.png", "o1.png", null)[2] == 0; assert getResult("needle.png", "r1.png", null)[0] == 0; assert getResult("needle.png", "r1.png", null)[1] == 0; assert getResult("needle.png", "r1.png", null)[2] == 1; assert getResult("needle.png", "r2.png", null)[0] == 0; assert getResult("needle.png", "r2.png", null)[1] == 0; assert getResult("needle.png", "r2.png", null)[2] == 2; assert getResult("needle.png", "r3.png", null)[0] == 0; assert getResult("needle.png", "r3.png", null)[1] == 0; assert getResult("needle.png", "r3.png", null)[2] == 3; assert getResult("needle.png", "f1.png", null)[0] == 0; assert getResult("needle.png", "f1.png", null)[1] == 0; assert getResult("needle.png", "f1.png", null)[2] == 4; assert getResult("needle.png", "fr1.png", null)[0] == 0; assert getResult("needle.png", "fr1.png", null)[1] == 0; assert getResult("needle.png", "fr1.png", null)[2] == 5; assert getResult("needle.png", "fr2.png", null)[0] == 0; assert getResult("needle.png", "fr2.png", null)[1] == 0; assert getResult("needle.png", "fr2.png", null)[2] == 6; assert getResult("needle.png", "fr3.png", null)[0] == 0; assert getResult("needle.png", "fr3.png", null)[1] == 0; assert getResult("needle.png", "fr3.png", null)[2] == 7; assert getResult("needle.png", "haystack_small.png", new int[] {0})[0] == 16; assert getResult("needle.png", "haystack_small.png", new int[] {0})[1] == 17; assert getResult("needle.png", "haystack_small.png", new int[] {0})[2] == 0; assert getResult("needle.png", "haystack_small.png", new int[] {1})[0] == 64; assert getResult("needle.png", "haystack_small.png", new int[] {1})[1] == 15; assert getResult("needle.png", "haystack_small.png", new int[] {1})[2] == 1; assert getResult("needle.png", "haystack_small.png", new int[] {2})[0] == 99; assert getResult("needle.png", "haystack_small.png", new int[] {2})[1] == 17; assert getResult("needle.png", "haystack_small.png", new int[] {2})[2] == 2; assert getResult("needle.png", "haystack_small.png", new int[] {3})[0] == 136; assert getResult("needle.png", "haystack_small.png", new int[] {3})[1] == 15; assert getResult("needle.png", "haystack_small.png", new int[] {3})[2] == 3; assert getResult("needle.png", "haystack_small.png", new int[] {4})[0] == 14; assert getResult("needle.png", "haystack_small.png", new int[] {4})[1] == 78; assert getResult("needle.png", "haystack_small.png", new int[] {4})[2] == 4; assert getResult("needle.png", "haystack_small.png", new int[] {5})[0] == 62; assert getResult("needle.png", "haystack_small.png", new int[] {5})[1] == 74; assert getResult("needle.png", "haystack_small.png", new int[] {5})[2] == 5; assert getResult("needle.png", "haystack_small.png", new int[] {6})[0] == 97; assert getResult("needle.png", "haystack_small.png", new int[] {6})[1] == 76; assert getResult("needle.png", "haystack_small.png", new int[] {6})[2] == 6; assert getResult("needle.png", "haystack_small.png", new int[] {7})[0] == 136; assert getResult("needle.png", "haystack_small.png", new int[] {7})[1] == 73; assert getResult("needle.png", "haystack_small.png", new int[] {7})[2] == 7; }
private int[] getPosition(ImageComparator child, ArrayList<Integer> one, ArrayList<Integer> two, int[] restriction, int type) { if (restriction == null || ArrayUtil.contains(restriction, type)) { for (int x : one) { for (int y : two) { boolean match = true; for (TIntIntIterator pixel = child.pixels.iterator(); pixel.hasNext(); ) { pixel.advance(); short[] childPos = IntegerTools.getShorts(pixel.key()); int color; switch (type) { case 0: color = this.pixels.get(IntegerTools.makeInt(x + childPos[0], y + childPos[1])); break; case 4: color = this.pixels.get(IntegerTools.makeInt(x + (child.widthM - childPos[0]), y + childPos[1])); break; case 2: color = this.pixels.get(IntegerTools.makeInt(x + (child.widthM - childPos[0]), y + (child.heightM - childPos[1]))); break; case 6: color = this.pixels.get(IntegerTools.makeInt(x + childPos[0], y + (child.heightM - childPos[1]))); break; case 7: color = this.pixels.get(IntegerTools.makeInt(x + childPos[1], y + childPos[0])); break; case 1: color = this.pixels.get(IntegerTools.makeInt(x + (child.heightM - childPos[1]), y + childPos[0])); break; case 3: color = this.pixels.get(IntegerTools.makeInt(x + childPos[1], y + (child.widthM - childPos[0]))); break; default: color = this.pixels.get(IntegerTools.makeInt(x + (child.heightM - childPos[1]), y + (child.widthM - childPos[0]))); break; } if (pixel.value() != color) { match = false; break; } } if (match) { return new int[]{x, y, type}; } } } } return null; }
ImageComparator { private int[] getPosition(ImageComparator child, ArrayList<Integer> one, ArrayList<Integer> two, int[] restriction, int type) { if (restriction == null || ArrayUtil.contains(restriction, type)) { for (int x : one) { for (int y : two) { boolean match = true; for (TIntIntIterator pixel = child.pixels.iterator(); pixel.hasNext(); ) { pixel.advance(); short[] childPos = IntegerTools.getShorts(pixel.key()); int color; switch (type) { case 0: color = this.pixels.get(IntegerTools.makeInt(x + childPos[0], y + childPos[1])); break; case 4: color = this.pixels.get(IntegerTools.makeInt(x + (child.widthM - childPos[0]), y + childPos[1])); break; case 2: color = this.pixels.get(IntegerTools.makeInt(x + (child.widthM - childPos[0]), y + (child.heightM - childPos[1]))); break; case 6: color = this.pixels.get(IntegerTools.makeInt(x + childPos[0], y + (child.heightM - childPos[1]))); break; case 7: color = this.pixels.get(IntegerTools.makeInt(x + childPos[1], y + childPos[0])); break; case 1: color = this.pixels.get(IntegerTools.makeInt(x + (child.heightM - childPos[1]), y + childPos[0])); break; case 3: color = this.pixels.get(IntegerTools.makeInt(x + childPos[1], y + (child.widthM - childPos[0]))); break; default: color = this.pixels.get(IntegerTools.makeInt(x + (child.heightM - childPos[1]), y + (child.widthM - childPos[0]))); break; } if (pixel.value() != color) { match = false; break; } } if (match) { return new int[]{x, y, type}; } } } } return null; } }
ImageComparator { private int[] getPosition(ImageComparator child, ArrayList<Integer> one, ArrayList<Integer> two, int[] restriction, int type) { if (restriction == null || ArrayUtil.contains(restriction, type)) { for (int x : one) { for (int y : two) { boolean match = true; for (TIntIntIterator pixel = child.pixels.iterator(); pixel.hasNext(); ) { pixel.advance(); short[] childPos = IntegerTools.getShorts(pixel.key()); int color; switch (type) { case 0: color = this.pixels.get(IntegerTools.makeInt(x + childPos[0], y + childPos[1])); break; case 4: color = this.pixels.get(IntegerTools.makeInt(x + (child.widthM - childPos[0]), y + childPos[1])); break; case 2: color = this.pixels.get(IntegerTools.makeInt(x + (child.widthM - childPos[0]), y + (child.heightM - childPos[1]))); break; case 6: color = this.pixels.get(IntegerTools.makeInt(x + childPos[0], y + (child.heightM - childPos[1]))); break; case 7: color = this.pixels.get(IntegerTools.makeInt(x + childPos[1], y + childPos[0])); break; case 1: color = this.pixels.get(IntegerTools.makeInt(x + (child.heightM - childPos[1]), y + childPos[0])); break; case 3: color = this.pixels.get(IntegerTools.makeInt(x + childPos[1], y + (child.widthM - childPos[0]))); break; default: color = this.pixels.get(IntegerTools.makeInt(x + (child.heightM - childPos[1]), y + (child.widthM - childPos[0]))); break; } if (pixel.value() != color) { match = false; break; } } if (match) { return new int[]{x, y, type}; } } } } return null; } ImageComparator(final BufferedImage image);  ImageComparator(Collection<int[]> pixels); }
ImageComparator { private int[] getPosition(ImageComparator child, ArrayList<Integer> one, ArrayList<Integer> two, int[] restriction, int type) { if (restriction == null || ArrayUtil.contains(restriction, type)) { for (int x : one) { for (int y : two) { boolean match = true; for (TIntIntIterator pixel = child.pixels.iterator(); pixel.hasNext(); ) { pixel.advance(); short[] childPos = IntegerTools.getShorts(pixel.key()); int color; switch (type) { case 0: color = this.pixels.get(IntegerTools.makeInt(x + childPos[0], y + childPos[1])); break; case 4: color = this.pixels.get(IntegerTools.makeInt(x + (child.widthM - childPos[0]), y + childPos[1])); break; case 2: color = this.pixels.get(IntegerTools.makeInt(x + (child.widthM - childPos[0]), y + (child.heightM - childPos[1]))); break; case 6: color = this.pixels.get(IntegerTools.makeInt(x + childPos[0], y + (child.heightM - childPos[1]))); break; case 7: color = this.pixels.get(IntegerTools.makeInt(x + childPos[1], y + childPos[0])); break; case 1: color = this.pixels.get(IntegerTools.makeInt(x + (child.heightM - childPos[1]), y + childPos[0])); break; case 3: color = this.pixels.get(IntegerTools.makeInt(x + childPos[1], y + (child.widthM - childPos[0]))); break; default: color = this.pixels.get(IntegerTools.makeInt(x + (child.heightM - childPos[1]), y + (child.widthM - childPos[0]))); break; } if (pixel.value() != color) { match = false; break; } } if (match) { return new int[]{x, y, type}; } } } } return null; } ImageComparator(final BufferedImage image);  ImageComparator(Collection<int[]> pixels); final float jaccard(ImageComparator other); static int[] getMergePoint(ImageComparator one, ImageComparator two); final int[] getPosition(ImageComparator child, int[] restriction); }
ImageComparator { private int[] getPosition(ImageComparator child, ArrayList<Integer> one, ArrayList<Integer> two, int[] restriction, int type) { if (restriction == null || ArrayUtil.contains(restriction, type)) { for (int x : one) { for (int y : two) { boolean match = true; for (TIntIntIterator pixel = child.pixels.iterator(); pixel.hasNext(); ) { pixel.advance(); short[] childPos = IntegerTools.getShorts(pixel.key()); int color; switch (type) { case 0: color = this.pixels.get(IntegerTools.makeInt(x + childPos[0], y + childPos[1])); break; case 4: color = this.pixels.get(IntegerTools.makeInt(x + (child.widthM - childPos[0]), y + childPos[1])); break; case 2: color = this.pixels.get(IntegerTools.makeInt(x + (child.widthM - childPos[0]), y + (child.heightM - childPos[1]))); break; case 6: color = this.pixels.get(IntegerTools.makeInt(x + childPos[0], y + (child.heightM - childPos[1]))); break; case 7: color = this.pixels.get(IntegerTools.makeInt(x + childPos[1], y + childPos[0])); break; case 1: color = this.pixels.get(IntegerTools.makeInt(x + (child.heightM - childPos[1]), y + childPos[0])); break; case 3: color = this.pixels.get(IntegerTools.makeInt(x + childPos[1], y + (child.widthM - childPos[0]))); break; default: color = this.pixels.get(IntegerTools.makeInt(x + (child.heightM - childPos[1]), y + (child.widthM - childPos[0]))); break; } if (pixel.value() != color) { match = false; break; } } if (match) { return new int[]{x, y, type}; } } } } return null; } ImageComparator(final BufferedImage image);  ImageComparator(Collection<int[]> pixels); final float jaccard(ImageComparator other); static int[] getMergePoint(ImageComparator one, ImageComparator two); final int[] getPosition(ImageComparator child, int[] restriction); final int pixelCount; final int colorCount; }