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).

1  
// A is the user object associated with each mask (aka a "label").
2  
persistable sclass G22HashedMasks<A> is IG22SimpleMasksHolder<A> {
3  
  !include early #1035854 // Masks holder Include
4  
    
5  
  record noeq Mask(Image2B image) is IG22Mask<A> {
6  
    settable A label;
7  
    int hash;
8  
    
9  
    public bool equals(O o) {
10  
      if (o cast G22HashedMasks.Mask)
11  
        ret hashCode() == o.hashCode()
12  
          && binaryImagesIdentical(image, o.image);
13  
      false;
14  
    }
15  
    
16  
    public int hashCode() {
17  
      if (hash == 0)
18  
        hash = (int) hashImage2B(image);
19  
      ret hash;
20  
    }
21  
    
22  
    public Image2B image() { ret image; }
23  
  }
24  
25  
  new CompactHashSet<Mask> masks;
26  
27  
  // creating a sub-holder
28  
  *(L<IG22Mask<A>> masks) {
29  
    assertNempty(masks);
30  
    maskSize(imageSize(first(masks).image()));
31  
    fOr (mask : masks)
32  
      this.masks.add(mask);
33  
  }
34  
  
35  
  // same with pre-calculated ghost
36  
  *(L<IG22Mask<A>> masks, G22GhostImage ghost) {
37  
    this(masks);
38  
    ghost_cache = ghost;
39  
  }
40  
41  
  void addRegion(IImageRegion region, A label) {
42  
    addMask(regionToMaskImage(region), label);
43  
  }
44  
    
45  
  void addMask(IBinaryImage maskImage, A label default null) {
46  
    var mask = new Mask(toImage2B(maskImage));
47  
    var existingMask = masks.find(mask);
48  
    if (existingMask != null) {
49  
      mask = existingMask;
50  
      mask.label = combineLabels(mask.label, label);
51  
    } else {
52  
      mask.label = label;
53  
      masks.add(mask);
54  
    }
55  
    ghost_cache = null;
56  
    certainty_cache = null;
57  
    //this.labels.addAll(labels);
58  
  }
59  
  
60  
  public L<Mask> masks() { ret asList(masks); }
61  
  
62  
  /*public LPair<Image2B, A> maskImagesWithLabels() {
63  
    ret map(masks(), mask -> pair(mask.image, mask.labels));
64  
  }*/
65  
  
66  
  A combineLabels(A label1, A label2) {
67  
    if (eq(label1, label2)) ret label1;
68  
    if (label1 == null) ret label2;
69  
    if (label2 == null) ret label1;
70  
    if (label1 cast MultiSet) {
71  
      if (label2 cast MultiSet) {
72  
        label1.addAll(label2);
73  
        ret (A) label1;
74  
      }
75  
    }
76  
      
77  
    ret specialCombineLabels(label1, label2);
78  
  }
79  
  
80  
  swappable A specialCombineLabels(A label1, A label2) {
81  
    fail("Can't combine labels: " + label1 + " + " + label2);
82  
  }
83  
  
84  
  public <B> IG22MasksHolder<B> cloneTreeWithLabelTransform(IF1<A, B> f) {
85  
    new G22HashedMasks<B> clone;
86  
    for (mask : masks) {
87  
      G22HashedMasks<B>.Mask clonedMask = clone.new Mask;
88  
      clonedMask.image = mask.image;
89  
      clonedMask.hash = mask.hash;
90  
      clonedMask.label = f.get(mask.label);
91  
      clone.masks.add(clonedMask);
92  
    }
93  
    ret clone;
94  
  }
95  
}

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: 248 / 510
Version history: 36 change(s)
Referenced in: [show references]