Sagar Bharadwaj commited on
Commit
13c2a66
·
1 Parent(s): 8ad61d2

Added padding to enable border detection at image boundaries

Browse files
colorbynumber/gen_islands.py CHANGED
@@ -10,9 +10,11 @@ class GenerateIslands:
10
  self.gradient_kernel = np.ones((gradient_kernel_size, gradient_kernel_size), np.uint8)
11
  self.color_index_island_list = []
12
 
13
- def _get_islands_for_one_color(self, color_index):
14
  # Get a binary image with just the selected color
15
  this_color = (self.indices_color_choices == color_index).astype(np.uint8)
 
 
16
 
17
  # Find connected components
18
  num_labels, labels_im = cv.connectedComponents(this_color)
@@ -29,9 +31,9 @@ class GenerateIslands:
29
 
30
  return this_color_index_island_list
31
 
32
- def get_islands(self):
33
  for color_index in np.unique(self.indices_color_choices):
34
- this_color_index_island_list = self._get_islands_for_one_color(color_index)
35
  self.color_index_island_list.extend(this_color_index_island_list)
36
 
37
  return self.color_index_island_list
 
10
  self.gradient_kernel = np.ones((gradient_kernel_size, gradient_kernel_size), np.uint8)
11
  self.color_index_island_list = []
12
 
13
+ def _get_islands_for_one_color(self, color_index, border_padding):
14
  # Get a binary image with just the selected color
15
  this_color = (self.indices_color_choices == color_index).astype(np.uint8)
16
+ # Pad the image to enable border detection on image boundaries
17
+ this_color = np.pad(this_color, border_padding, mode='constant', constant_values=0)
18
 
19
  # Find connected components
20
  num_labels, labels_im = cv.connectedComponents(this_color)
 
31
 
32
  return this_color_index_island_list
33
 
34
+ def get_islands(self, border_padding=2):
35
  for color_index in np.unique(self.indices_color_choices):
36
+ this_color_index_island_list = self._get_islands_for_one_color(color_index, border_padding)
37
  self.color_index_island_list.extend(this_color_index_island_list)
38
 
39
  return self.color_index_island_list
colorbynumber/numbered_islands.py CHANGED
@@ -34,7 +34,7 @@ def _add_text_to_image(image, text, position, font_size=1, font_color=(0, 0, 0))
34
  )
35
 
36
 
37
- def create_numbered_islands(islands, image_shape, border_color=[0, 0, 0]):
38
  """Create a new image with the islands numbered.
39
 
40
  Args:
@@ -45,7 +45,9 @@ def create_numbered_islands(islands, image_shape, border_color=[0, 0, 0]):
45
  np.array: A new image with the islands numbered.
46
  """
47
  # Create an all white image
48
- numbered_islands = np.ones(image_shape, dtype=np.uint8) * 255
 
 
49
 
50
  for color_id, island_coordinates in islands:
51
  numbered_islands[island_coordinates] = border_color
 
34
  )
35
 
36
 
37
+ def create_numbered_islands(islands, image_shape, border_color=[0, 0, 0], padding=2):
38
  """Create a new image with the islands numbered.
39
 
40
  Args:
 
45
  np.array: A new image with the islands numbered.
46
  """
47
  # Create an all white image
48
+ width, height, channels = image_shape
49
+ numbered_islands = np.ones((width + padding*2, height + padding*2, channels),
50
+ dtype=np.uint8) * 255
51
 
52
  for color_id, island_coordinates in islands:
53
  numbered_islands[island_coordinates] = border_color