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

88
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 (29726L/182K).

// A is the user object associated with each mask (aka a "label").
persistable sclass G22HashedMasks<A> is IG22SimpleMasksHolder<A> {
  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;
    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;
  }
  
  !include early #1035854 // Masks holder Include
}

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/34
Text MD5: 5bd3129144b5a75d6e9eb8a1363f9f2e
Transpilation MD5: 06c19d7d137fd41b6875591212a60a0a
Author: stefan
Category: javax
Type: JavaX fragment (include)
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2022-09-15 20:18:58
Source code size: 2468 bytes / 88 lines
Pitched / IR pitched: No / No
Views / Downloads: 102 / 311
Version history: 33 change(s)
Referenced in: [show references]