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

113
LINES

< > BotCompany Repo | #1027229 // MultiLevelRecognizer1 (backup 1, single size)

JavaX fragment (include)

sclass MultiLevelRecognizer1 {
  // how big do we think the prototype is in the whole image
  // (percentage by height)
  transient double assumedPrototypeHeightPercentage = 70;
  
  transient JTabbedPane tabs; // initialize if you want visualization
  
  transient BWIntegralImage baseImage;
  transient BWImage prototypeImage;
  transient L<Int> widths = ll(32, 64, 128);
  transient new L<IBWIntegralImage> integralImages;
  transient new L<OneLevel> levels; // recognizers for each granularity

  class OneLevel extends SteppableAndBest<Rect> {
    IBWIntegralImage ii; // scaled integral image
    BWImage image;       // scaled image
    BWImage prototype;   // scaled prototype
    float minSimilarity = 0.5f;
    ImageSurface is;

    // candidates are top-left corner of rect to try in our coordinates
    L<Pt> candidatesQueue = syncLinkedList();
    new Set<Pt> candidatesTried;
    Iterator<Pt> candidatesStream;

    *(IBWIntegralImage *ii) {
      image = iBWIntegralImageToBWImage(ii);
      // get assumed height of prototype in scaled-down image
      int ph = iround(ii.getHeight()*assumedPrototypeHeightPercentage/100.0);
      // resize prototype
      prototype = bwResizeToHeightSmooth(prototypeImage, ph);
      addTab(tabs, "proto " + ii.getWidth(),
        jFullCenterScroll(jPixelatedZoomedImageSurface(4.0, prototype)));
      candidatesStream = mapI rectTopLeftCorner(allSubRectsOfSizeIterator(prototype.getWidth(), prototype.getHeight(),
        imageRect(image)));
    }
    
    public bool step() {
      Pt p = nextCandidate();
      if (p != null) ret true with tryCandidate(p);
      false;
    }

    Pt nextCandidate() {
      try object Pt p = popFirst(candidatesQueue);
      ret nextFromIterator(candidatesStream);
    }

    void tryCandidate(Pt p) {
      if (!candidatesTried.add(p)) ret;

      int x = p.x, y = p.y, wp = prototype.getWidth(), hp = prototype.getHeight();

      float maxError = (1f-minSimilarity)*wp*hp;
      float diff = bwImageSectionsSimilarity(image, prototype, x, y, maxError);
      if (diff <= maxError)
        best.put(new Rect(x, y, wp, hp), 1-diff/(wp*hp));
    }

    void showBest() {
      setImageSurfaceSelection(is, best!);
    }
    
    // best rect in original image coordinates
    Rect bestRescaled() {
      ret rescaleRect(best!, ii.getWidth(), ii.getHeight(),
        baseImage.getWidth(), baseImage.getHeight());
    }
    
    Scored<Rect> scoredBestRescaled() {
      ret scored(bestRescaled(), best.score);
    }
  }
  
  *() {}
  *(File prototypeImage, File imgFile) {
    this.prototypeImage = loadBWImage(prototypeImage);
    this.baseImage = loadBWIntegralImage(imgFile);
  }

  Scored<Rect> go() {
    assertNotNull(+baseImage);
    assertNotNull(+prototypeImage);
    
    for (int w : widths) {
      IBWIntegralImage ii = scaledIBWIntegralImage(baseImage, w);
      integralImages.add(ii);
      OneLevel lvl = new(ii);
      levels.add(lvl);
      if (tabs != null) {
        ImageSurface is = jPixelatedZoomedImageSurface(
          doubleRatio(baseImage.getWidth(), w), iBWIntegralImageToBWImage(ii));
        addTab(tabs, "w=" + w, jFullCenterScroll(is));
        lvl.is = is;
      }
    }
    if (tabs != null)
      addTab(tabs, "Prototype", jFullCenterScroll(jPixelatedZoomedImageSurface(4, prototypeImage)));

    time "Process" {
      print("Steps: " + stepAll_roundRobin(levels));
    }

    if (tabs != null)
      for (OneLevel l : levels) l.showBest();
      
    ret bestResult();
  }
  
  Scored<Rect> bestResult() {
    ret last(levels).scoredBestRescaled();
  }
}

Author comment

Began life as a copy of #1027225

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: #1027229
Snippet name: MultiLevelRecognizer1 (backup 1, single size)
Eternal ID of this version: #1027229/1
Text MD5: 119c1753957400d30d10ec621d81ac42
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-26 14:12:29
Source code size: 3694 bytes / 113 lines
Pitched / IR pitched: No / No
Views / Downloads: 122 / 153
Referenced in: [show references]