sclass CheesyRecognizer { BWIntegralImage inputImage; new L prototypes; new Map scaledInputImages; // key = width class Prototype { BWIntegralImage image; new Map scaledPrototypes; // key = width *(BWImage image) { this.image = BWIntegralImage(image); } ScaledPrototype scaledPrototype(int width) { ret mapGetOrCreate(scaledPrototypes, width, () -> new ScaledPrototype(this, scaleDownUsingIntegralImageBW(image, width))); } } class ScaledInputImage { BWImage image; *(BWImage *image) {} Rect scaleToInputImage(Rect r) { ret scaleRect(doubleRatio(inputImage.getWidth(), image.getWidth()), r); } int getHeight() { ret image.getHeight(); } } class ScaledPrototype { Prototype prototype; BWImage image; *(Prototype *prototype, BWImage *image) {} int getHeight() { ret image.getHeight(); } } *(BWImage inputImage) { this.inputImage = bwIntegralImage(inputImage); } *(BufferedImage inputImage) { this(BWImage(inputImage)); } void fullScan(ScaledInputImage img, ScaledPrototype proto, float minSimilarity, IVF2 onFound) { bwImageSearch_onFound_allCandidates(img.image, proto.image, minSimilarity, onFound); } LPair fullScanToNBest(ScaledInputImage img, ScaledPrototype proto, float minSimilarity, int n) { new LPair list; fullScan(img, proto, minSimilarity, (r, score) -> list.add(pair(r, score))); sortBySecondOfPairsDesc_inPlace(list); ret takeFirst(n, list); } // compare scaled prototype to scaled image at some location Pair singleCheck(ScaledInputImage img, ScaledPrototype prototype, int x, int y, float minSimilarity) { BWImage proto = prototype.image, image = img.image; int wp = proto.getWidth(), hp = proto.getHeight(); float sim = bwImageSectionsSimilarity2(image, proto, x, y, minSimilarity); ret sim < minSimilarity ? null : pair(new Rect(x, y, wp, hp), (double) sim); } ScaledInputImage scaledInputImage(int width) { ret mapGetOrCreate(scaledInputImages, width, () -> new ScaledInputImage(scaleDownUsingIntegralImageBW(inputImage, width))); } Prototype addPrototype(BWImage img) { ret addAndReturn(prototypes, new Prototype(img)); } LPair fullScanToNBest(Prototype proto, int imageWidth, int protoWidth, float minSimilarity, int n) { ScaledInputImage scaled = scaledInputImage(imageWidth); ret mapPairsA(r -> scaled.scaleToInputImage(r), fullScanToNBest(scaled, proto.scaledPrototype(protoWidth), 0f, n)); } int getWidth() { ret inputImage.getWidth(); } int getHeight() { ret inputImage.getHeight(); } }