MBG0903 commited on
Commit
cffcde1
·
verified ·
1 Parent(s): 8c4133f

Update utils/fill_regions.py

Browse files
Files changed (1) hide show
  1. utils/fill_regions.py +14 -13
utils/fill_regions.py CHANGED
@@ -6,28 +6,24 @@ def fill_regions(binary):
6
 
7
  h, w = binary.shape
8
 
9
- canvas = np.ones(
10
- (h, w, 3),
11
- dtype=np.uint8
12
- ) * 255
13
-
14
- # Close tiny gaps in hand-drawn lines
15
  kernel = np.ones((3, 3), np.uint8)
16
 
17
- walls = cv2.dilate(
18
  binary,
 
19
  kernel,
20
- iterations=1
21
  )
22
 
23
- flood = walls.copy()
 
24
 
25
  mask = np.zeros(
26
  (h + 2, w + 2),
27
  np.uint8
28
  )
29
 
30
- # Fill from outside
31
  cv2.floodFill(
32
  flood,
33
  mask,
@@ -35,10 +31,16 @@ def fill_regions(binary):
35
  255
36
  )
37
 
38
- # Areas enclosed by lines
39
  internal = cv2.bitwise_not(flood)
40
 
41
- # Paint enclosed regions RED
 
 
 
 
 
 
42
  canvas[internal > 0] = [255, 0, 0]
43
 
44
  return canvas
@@ -48,5 +50,4 @@ def overlay_boundaries(
48
  filled,
49
  binary
50
  ):
51
- # No black outlines required
52
  return filled
 
6
 
7
  h, w = binary.shape
8
 
9
+ # Make boundaries thicker and close tiny gaps
 
 
 
 
 
10
  kernel = np.ones((3, 3), np.uint8)
11
 
12
+ closed = cv2.morphologyEx(
13
  binary,
14
+ cv2.MORPH_CLOSE,
15
  kernel,
16
+ iterations=2
17
  )
18
 
19
+ # Fill outside background
20
+ flood = closed.copy()
21
 
22
  mask = np.zeros(
23
  (h + 2, w + 2),
24
  np.uint8
25
  )
26
 
 
27
  cv2.floodFill(
28
  flood,
29
  mask,
 
31
  255
32
  )
33
 
34
+ # Internal regions only
35
  internal = cv2.bitwise_not(flood)
36
 
37
+ # Create white canvas
38
+ canvas = np.ones(
39
+ (h, w, 3),
40
+ dtype=np.uint8
41
+ ) * 255
42
+
43
+ # Fill internal regions with RED
44
  canvas[internal > 0] = [255, 0, 0]
45
 
46
  return canvas
 
50
  filled,
51
  binary
52
  ):
 
53
  return filled