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

87
LINES

< > BotCompany Repo | #1033587 // BWImage_FloodFillRegions

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

Libraryless. Click here for Pure Java version (6913L/40K).

sclass BWImage_FloodFillRegions 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);
  }
  
  void run { doAllPoints(); }
}

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: BWImage_FloodFillRegions
Eternal ID of this version: #1033587/20
Text MD5: 3749c97cabc295a21383088514de8729
Transpilation MD5: 92d97d9e3b6ac4ec78e50a348342af79
Author: stefan
Category: javax / imaging
Type: JavaX fragment (include)
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2021-12-28 19:06:50
Source code size: 2085 bytes / 87 lines
Pitched / IR pitched: No / No
Views / Downloads: 132 / 268
Version history: 19 change(s)
Referenced in: [show references]