Not logged in.  Login/Logout/Register | List snippets | | Create snippet | Upload image | Upload data

80
LINES

< > BotCompany Repo | #1027278 // CheesyRecognizer - cheaty image recognition by scanning for prototype images [dev.]

JavaX fragment (include) [tags: use-pretranspiled]

Libraryless. Click here for Pure Java version (5559L/36K).

sclass CheesyRecognizer {
  BWIntegralImage inputImage;
  
  new L<Prototype> prototypes;
  new Map<Int, ScaledInputImage> scaledInputImages; // key = width
  
  class Prototype {
    BWIntegralImage image;
    new Map<Int, ScaledPrototype> 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<Rect, Double> onFound) {
    bwImageSearch_onFound_allCandidates(img.image, proto.image, minSimilarity, onFound);
  }
  
  LPair<Rect, Double> fullScanToNBest(ScaledInputImage img, ScaledPrototype proto, float minSimilarity, int n) {
    new LPair<Rect, Double> 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<Rect, Double> 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<Rect, Double> 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(); }
}

download  show line numbers  debug dex  old transpilations   

Travelled to 7 computer(s): bhatertpkbcr, mqqgnosmbjvj, pyentgdyhuwx, pzhvpgtvlbxg, tvejysmllsmz, vouqrxazstgt, xrpafgyirdlv

No comments. add comment

Snippet ID: #1027278
Snippet name: CheesyRecognizer - cheaty image recognition by scanning for prototype images [dev.]
Eternal ID of this version: #1027278/33
Text MD5: 9d7eeddb86f3dfa5d43783f0a5bb7660
Transpilation MD5: 465da5a721e314a71e8195211012e8d1
Author: stefan
Category: javax / image recognition
Type: JavaX fragment (include)
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2020-02-29 17:21:55
Source code size: 2878 bytes / 80 lines
Pitched / IR pitched: No / No
Views / Downloads: 302 / 749
Version history: 32 change(s)
Referenced in: [show references]