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

95
LINES

< > BotCompany Repo | #1035850 // G22HashedMasks - G22MasksHolder plus a hashmap to find identical masks

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

Libraryless. Click here for Pure Java version (30452L/187K).

// A is the user object associated with each mask (aka a "label").
persistable sclass G22HashedMasks<A> is IG22SimpleMasksHolder<A> {
  !include early #1035854 // Masks holder Include
    
  record noeq Mask(Image2B image) is IG22Mask<A> {
    settable A label;
    int hash;
    
    public bool equals(O o) {
      if (o cast G22HashedMasks.Mask)
        ret hashCode() == o.hashCode()
          && binaryImagesIdentical(image, o.image);
      false;
    }
    
    public int hashCode() {
      if (hash == 0)
        hash = (int) hashImage2B(image);
      ret hash;
    }
    
    public Image2B image() { ret image; }
  }

  new CompactHashSet<Mask> masks;

  // creating a sub-holder
  *(L<IG22Mask<A>> masks) {
    assertNempty(masks);
    maskSize(imageSize(first(masks).image()));
    fOr (mask : masks)
      this.masks.add(mask);
  }
  
  // same with pre-calculated ghost
  *(L<IG22Mask<A>> masks, G22GhostImage ghost) {
    this(masks);
    ghost_cache = ghost;
  }

  void addRegion(IImageRegion region, A label) {
    addMask(regionToMaskImage(region), label);
  }
    
  void addMask(IBinaryImage maskImage, A label default null) {
    var mask = new Mask(toImage2B(maskImage));
    var existingMask = masks.find(mask);
    if (existingMask != null) {
      mask = existingMask;
      mask.label = combineLabels(mask.label, label);
    } else {
      mask.label = label;
      masks.add(mask);
    }
    ghost_cache = null;
    certainty_cache = null;
    //this.labels.addAll(labels);
  }
  
  public L<Mask> masks() { ret asList(masks); }
  
  /*public LPair<Image2B, A> maskImagesWithLabels() {
    ret map(masks(), mask -> pair(mask.image, mask.labels));
  }*/
  
  A combineLabels(A label1, A label2) {
    if (eq(label1, label2)) ret label1;
    if (label1 == null) ret label2;
    if (label2 == null) ret label1;
    if (label1 cast MultiSet) {
      if (label2 cast MultiSet) {
        label1.addAll(label2);
        ret (A) label1;
      }
    }
      
    ret specialCombineLabels(label1, label2);
  }
  
  swappable A specialCombineLabels(A label1, A label2) {
    fail("Can't combine labels: " + label1 + " + " + label2);
  }
  
  public <B> IG22MasksHolder<B> cloneTreeWithLabelTransform(IF1<A, B> f) {
    new G22HashedMasks<B> clone;
    for (mask : masks) {
      G22HashedMasks<B>.Mask clonedMask = clone.new Mask;
      clonedMask.image = mask.image;
      clonedMask.hash = mask.hash;
      clonedMask.label = f.get(mask.label);
      clone.masks.add(clonedMask);
    }
    ret clone;
  }
}

Author comment

Began life as a copy of #1035842

download  show line numbers  debug dex  old transpilations   

Travelled to 2 computer(s): elmgxqgtpvxh, mqqgnosmbjvj

No comments. add comment

Snippet ID: #1035850
Snippet name: G22HashedMasks - G22MasksHolder plus a hashmap to find identical masks
Eternal ID of this version: #1035850/37
Text MD5: 0208900d50963d9b75a98c814882e48b
Transpilation MD5: f012d3b06989d300c1a9ce9e35ecda5e
Author: stefan
Category: javax
Type: JavaX fragment (include)
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2023-02-12 19:41:13
Source code size: 2619 bytes / 95 lines
Pitched / IR pitched: No / No
Views / Downloads: 174 / 416
Version history: 36 change(s)
Referenced in: #1003674 - Standard Classes + Interfaces (LIVE continued in #1034167)
#1035854 - Masks holder Include
#1035855 - G22HashedMasks backup
#1035861 - G22PixelSplitMasks - masks holder split by brightness of certain pixel