Skip to content

Commit

Permalink
Add random colors generator for instance plotting (#244)
Browse files Browse the repository at this point in the history
  • Loading branch information
anwai98 committed Apr 28, 2024
1 parent 078cb73 commit 357fb44
Showing 1 changed file with 12 additions and 0 deletions.
12 changes: 12 additions & 0 deletions torch_em/util/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
import numpy as np
import torch
import torch_em
import matplotlib.pyplot as plt
from matplotlib import colors

# this is a fairly brittle way to check if a module is compiled.
# would be good to find a better solution, ideall something like
Expand Down Expand Up @@ -246,3 +248,13 @@ def model_is_equal(model1, model2):
if p1.data.ne(p2.data).sum() > 0:
return False
return True



def get_random_colors(labels):
"""Function to generate a random color map for a label image
"""
n_labels = len(np.unique(labels)) - 1
cmap = [[0, 0, 0]] + np.random.rand(n_labels, 3).tolist()
cmap = colors.ListedColormap(cmap)
return cmap

0 comments on commit 357fb44

Please sign in to comment.