Skip to content

Instantly share code, notes, and snippets.

@AFutureD
Created January 16, 2020 03:37
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save AFutureD/31fa3ab742a4c9d97b8088eac3a3c13b to your computer and use it in GitHub Desktop.
Save AFutureD/31fa3ab742a4c9d97b8088eac3a3c13b to your computer and use it in GitHub Desktop.
Apple mask on a image.
def random_colors(self, N, bright=True):
"""
Generate random colors.
To get visually distinct colors, generate them in HSV space then
convert to RGB.
"""
brightness = 1.0 if bright else 0.7
hsv = [(i / N, 1, brightness) for i in range(N)]
colors = list(map(lambda c: colorsys.hsv_to_rgb(*c), hsv))
random.shuffle(colors)
return colors
def apply_mask(self, image, mask, color):
"""Apply the given mask to the image.
"""
for c in range(3):
mask_t = mask.numpy()
mask_t = mask_t[0]
image[:, :, c] = image[:, :, c] + mask_t * color[c] * 255
return image
def display(self, image,masks,scores):
N = masks.shape[0]
colors = self.random_colors(N)
height, width = image.shape[:2]
masked_image = np.zeros([height,width,3])
self.total_leaf_num = 0
for i in range(N):
if scores[i] < 0.82:
continue
self.total_leaf_num += 1
color = colors[i]
mask = masks[i,:, :,:]
masked_image = self.apply_mask(masked_image, mask, color)
masked_image = masked_image.astype(np.uint8)
masked_image = Image.fromarray(masked_image)
masked_image.save('total_mask.png')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment