Libraryless. Click here for Pure Java version (6913L/40K).
1 | sclass BWImage_FloodFillRegions extends FloodFillBWImage { |
2 | Matrix<Region> matrix; |
3 | bool verbose; |
4 | new LinkedHashSet<Region> regions; |
5 | |
6 | class Region { |
7 | new PointSetBitMatrix points; |
8 | Rect boundingBox; |
9 | float visualization = randomFloat(0.8f); |
10 | |
11 | void add aka addPoint(Pt p) { |
12 | points.set(p, true); |
13 | boundingBox = rectUnionWithPt(boundingBox, p); |
14 | } |
15 | |
16 | int size() { ret points.nSetBits(); } |
17 | |
18 | Iterable<Pt> points() { ret points.points; } |
19 | |
20 | toString { ret "Region with " + nPixels(size()) + " within " + boundingBox; } |
21 | |
22 | IBWImage asImage() { |
23 | ret iBWImageFromFunction((x, y) -> points.get(pt(x, y)) ? 0 : 1, w, h); |
24 | } |
25 | } |
26 | |
27 | void addedPoint(Pt p) { |
28 | new Region r; |
29 | r.add(p); |
30 | matrix.put(p, r); |
31 | print("addedPoint " + p + ": " + r); |
32 | regions.add(r); |
33 | } |
34 | |
35 | void visited(Pt p) { |
36 | if (verbose) print("Visited point: " + p); |
37 | } |
38 | |
39 | *(IBWImage *img) { |
40 | super(img); |
41 | matrix = new ArrayMatrix(w, h); |
42 | } |
43 | |
44 | bool isConnectable(Pt a, Pt b) { |
45 | float brightnessA = img.getFloatPixel(a); |
46 | float brightnessB = img.getFloatPixel(b); |
47 | ret abs(brightnessA-brightnessB) <= tolerance; |
48 | } |
49 | |
50 | void createBridge(Pt a, Pt b) { |
51 | var regionA = matrix.get(a); |
52 | var regionB = matrix.get(b); |
53 | if (regionA == regionB) ret; |
54 | |
55 | if (regionB == null) fail("regionB = null: " + b); |
56 | |
57 | for (p : regionB.points()) { |
58 | matrix.set(p, regionA); |
59 | regionA.addPoint(p); |
60 | } |
61 | |
62 | regions.remove(regionB); |
63 | } |
64 | |
65 | void dropOnePixelRegions { |
66 | filterCollectionInPlace(regions, r -> { |
67 | if (r.size() <= 1) { |
68 | removeFromMatrix(r); |
69 | false; |
70 | } |
71 | true; |
72 | }); |
73 | } |
74 | |
75 | void removeFromMatrix(Region r) { |
76 | for (Pt p : r.points.points) matrix.set(p, null); |
77 | } |
78 | |
79 | IBWImage regionsImage() { |
80 | ret iBWImageFromFunction((x, y) -> { |
81 | var region = matrix.get(x, y); |
82 | ret region == null ? 1 : region.visualization; |
83 | }, w, h); |
84 | } |
85 | |
86 | void run { doAllPoints(); } |
87 | } |
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: | 190 / 358 |
Version history: | 19 change(s) |
Referenced in: | [show references] |