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

85
LINES

< > BotCompany Repo | #1033587 - FloodFillBWImage_v2 [with regions]

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

Transpiled version (6238L) is out of date.

sclass FloodFillBWImage_v2 extends FloodFillBWImage {
  Matrix<Region> matrix;
  bool verbose;
  new LinkedHashSet<Region> regions;
  
  class Region {
    new PointSetBitMatrix points;
    Rect boundingBox;
    float visualization = randomFloat(0.8f);

    void add aka addPoint(Pt p) {
      points.set(p, true);
      boundingBox = rectUnionWithPt(boundingBox, p);
    }
    
    int size() { ret points.nSetBits(); }
    
    Iterable<Pt> points() { ret points.points; }
    
    toString { ret "Region with " + nPixels(size()) + " within " + boundingBox; }
    
    IBWImage asImage() {
      ret iBWImageFromFunction((x, y) -> points.get(pt(x, y)) ? 0 : 1, w, h);
    }
  }
  
  void addedPoint(Pt p) {
    new Region r;
    r.add(p);
    matrix.put(p, r);
    print("addedPoint " + p + ": " + r);
    regions.add(r);
  }
  
  void visited(Pt p) {
    if (verbose) print("Visited point: " + p);
  }

  *(IBWImage *img) {
    super(img); 
    matrix = new ArrayMatrix(w, h);
  }
  
  bool isConnectable(Pt a, Pt b) {
    float brightnessA = img.getFloatPixel(a);
    float brightnessB = img.getFloatPixel(b);
    ret abs(brightnessA-brightnessB) <= tolerance;
  }
  
  void createBridge(Pt a, Pt b) {
    var regionA = matrix.get(a);
    var regionB = matrix.get(b);
    if (regionA == regionB) ret;
    
    if (regionB == null) fail("regionB = null: " + b);
    
    for (p : regionB.points()) {
      matrix.set(p, regionA);
      regionA.addPoint(p);
    }
    
    regions.remove(regionB);
  }
  
  void dropOnePixelRegions {
    filterCollectionInPlace(regions, r -> {
      if (r.size() <= 1) {
        removeFromMatrix(r);
        false;
      }
      true;
    });
  }
  
  void removeFromMatrix(Region r) {
    for (Pt p : r.points.points) matrix.set(p, null);
  }
  
  IBWImage regionsImage() {
    ret iBWImageFromFunction((x, y) -> {
      var region = matrix.get(x, y);
      ret region == null ? 1 : region.visualization;
    }, w, h);
  }
}

Author comment

Began life as a copy of #1033583

download  show line numbers  debug dex  old transpilations   

Travelled to 4 computer(s): bhatertpkbcr, ekrmjmnbrukm, mowyntqkapby, mqqgnosmbjvj

No comments. add comment

Snippet ID: #1033587
Snippet name: FloodFillBWImage_v2 [with regions]
Eternal ID of this version: #1033587/17
Text MD5: 1d9a5d10853f41112ea585a3803e51a2
Author: stefan
Category: javax / imaging
Type: JavaX fragment (include)
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2021-11-04 06:43:52
Source code size: 2045 bytes / 85 lines
Pitched / IR pitched: No / No
Views / Downloads: 38 / 112
Version history: 16 change(s)
Referenced in: [show references]