annotate colorize_labels.py @ 1:43c80f3c3b60 draft

planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/colorize_labels commit 98270c8f2805d406e0d88605fde778f8f182df9b
author imgteam
date Wed, 13 Mar 2024 19:08:34 +0000
parents 0afb17e107ff
children 2d1de6e7b113
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
0afb17e107ff planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/colorize_labels commit ffedf4e17ecbb226657ccf8472b0572532e9aa8a
imgteam
parents:
diff changeset
1 import argparse
0afb17e107ff planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/colorize_labels commit ffedf4e17ecbb226657ccf8472b0572532e9aa8a
imgteam
parents:
diff changeset
2
1
43c80f3c3b60 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/colorize_labels commit 98270c8f2805d406e0d88605fde778f8f182df9b
imgteam
parents: 0
diff changeset
3 import matplotlib.colors as mpl
43c80f3c3b60 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/colorize_labels commit 98270c8f2805d406e0d88605fde778f8f182df9b
imgteam
parents: 0
diff changeset
4 import networkx as nx
0
0afb17e107ff planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/colorize_labels commit ffedf4e17ecbb226657ccf8472b0572532e9aa8a
imgteam
parents:
diff changeset
5 import numpy as np
1
43c80f3c3b60 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/colorize_labels commit 98270c8f2805d406e0d88605fde778f8f182df9b
imgteam
parents: 0
diff changeset
6 import scipy.ndimage as ndi
0
0afb17e107ff planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/colorize_labels commit ffedf4e17ecbb226657ccf8472b0572532e9aa8a
imgteam
parents:
diff changeset
7 import skimage.io
1
43c80f3c3b60 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/colorize_labels commit 98270c8f2805d406e0d88605fde778f8f182df9b
imgteam
parents: 0
diff changeset
8 import skimage.morphology as morph
0
0afb17e107ff planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/colorize_labels commit ffedf4e17ecbb226657ccf8472b0572532e9aa8a
imgteam
parents:
diff changeset
9 import skimage.util
0afb17e107ff planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/colorize_labels commit ffedf4e17ecbb226657ccf8472b0572532e9aa8a
imgteam
parents:
diff changeset
10
0afb17e107ff planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/colorize_labels commit ffedf4e17ecbb226657ccf8472b0572532e9aa8a
imgteam
parents:
diff changeset
11
0afb17e107ff planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/colorize_labels commit ffedf4e17ecbb226657ccf8472b0572532e9aa8a
imgteam
parents:
diff changeset
12 def color_hex_to_rgb_tuple(hex):
0afb17e107ff planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/colorize_labels commit ffedf4e17ecbb226657ccf8472b0572532e9aa8a
imgteam
parents:
diff changeset
13 if hex.startswith('#'):
0afb17e107ff planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/colorize_labels commit ffedf4e17ecbb226657ccf8472b0572532e9aa8a
imgteam
parents:
diff changeset
14 hex = hex[1:]
0afb17e107ff planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/colorize_labels commit ffedf4e17ecbb226657ccf8472b0572532e9aa8a
imgteam
parents:
diff changeset
15 return (
0afb17e107ff planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/colorize_labels commit ffedf4e17ecbb226657ccf8472b0572532e9aa8a
imgteam
parents:
diff changeset
16 int(hex[0:2], 16),
0afb17e107ff planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/colorize_labels commit ffedf4e17ecbb226657ccf8472b0572532e9aa8a
imgteam
parents:
diff changeset
17 int(hex[2:4], 16),
0afb17e107ff planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/colorize_labels commit ffedf4e17ecbb226657ccf8472b0572532e9aa8a
imgteam
parents:
diff changeset
18 int(hex[4:6], 16),
0afb17e107ff planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/colorize_labels commit ffedf4e17ecbb226657ccf8472b0572532e9aa8a
imgteam
parents:
diff changeset
19 )
0afb17e107ff planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/colorize_labels commit ffedf4e17ecbb226657ccf8472b0572532e9aa8a
imgteam
parents:
diff changeset
20
0afb17e107ff planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/colorize_labels commit ffedf4e17ecbb226657ccf8472b0572532e9aa8a
imgteam
parents:
diff changeset
21
1
43c80f3c3b60 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/colorize_labels commit 98270c8f2805d406e0d88605fde778f8f182df9b
imgteam
parents: 0
diff changeset
22 def build_label_adjacency_graph(im, radius, bg_label):
43c80f3c3b60 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/colorize_labels commit 98270c8f2805d406e0d88605fde778f8f182df9b
imgteam
parents: 0
diff changeset
23 G = nx.Graph()
43c80f3c3b60 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/colorize_labels commit 98270c8f2805d406e0d88605fde778f8f182df9b
imgteam
parents: 0
diff changeset
24 selem = morph.disk(radius)
43c80f3c3b60 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/colorize_labels commit 98270c8f2805d406e0d88605fde778f8f182df9b
imgteam
parents: 0
diff changeset
25 for label in np.unique(im):
43c80f3c3b60 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/colorize_labels commit 98270c8f2805d406e0d88605fde778f8f182df9b
imgteam
parents: 0
diff changeset
26
43c80f3c3b60 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/colorize_labels commit 98270c8f2805d406e0d88605fde778f8f182df9b
imgteam
parents: 0
diff changeset
27 if label == bg_label:
43c80f3c3b60 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/colorize_labels commit 98270c8f2805d406e0d88605fde778f8f182df9b
imgteam
parents: 0
diff changeset
28 continue
43c80f3c3b60 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/colorize_labels commit 98270c8f2805d406e0d88605fde778f8f182df9b
imgteam
parents: 0
diff changeset
29
43c80f3c3b60 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/colorize_labels commit 98270c8f2805d406e0d88605fde778f8f182df9b
imgteam
parents: 0
diff changeset
30 G.add_node(label)
43c80f3c3b60 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/colorize_labels commit 98270c8f2805d406e0d88605fde778f8f182df9b
imgteam
parents: 0
diff changeset
31
43c80f3c3b60 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/colorize_labels commit 98270c8f2805d406e0d88605fde778f8f182df9b
imgteam
parents: 0
diff changeset
32 cc = (im == label)
43c80f3c3b60 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/colorize_labels commit 98270c8f2805d406e0d88605fde778f8f182df9b
imgteam
parents: 0
diff changeset
33 neighborhood = ndi.binary_dilation(cc, selem)
43c80f3c3b60 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/colorize_labels commit 98270c8f2805d406e0d88605fde778f8f182df9b
imgteam
parents: 0
diff changeset
34 adjacent_labels = np.unique(im[neighborhood])
43c80f3c3b60 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/colorize_labels commit 98270c8f2805d406e0d88605fde778f8f182df9b
imgteam
parents: 0
diff changeset
35
43c80f3c3b60 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/colorize_labels commit 98270c8f2805d406e0d88605fde778f8f182df9b
imgteam
parents: 0
diff changeset
36 for adjacent_label in adjacent_labels:
43c80f3c3b60 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/colorize_labels commit 98270c8f2805d406e0d88605fde778f8f182df9b
imgteam
parents: 0
diff changeset
37
43c80f3c3b60 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/colorize_labels commit 98270c8f2805d406e0d88605fde778f8f182df9b
imgteam
parents: 0
diff changeset
38 if adjacent_label == bg_label or adjacent_label <= label:
43c80f3c3b60 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/colorize_labels commit 98270c8f2805d406e0d88605fde778f8f182df9b
imgteam
parents: 0
diff changeset
39 continue
43c80f3c3b60 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/colorize_labels commit 98270c8f2805d406e0d88605fde778f8f182df9b
imgteam
parents: 0
diff changeset
40
43c80f3c3b60 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/colorize_labels commit 98270c8f2805d406e0d88605fde778f8f182df9b
imgteam
parents: 0
diff changeset
41 G.add_edge(label, adjacent_label)
43c80f3c3b60 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/colorize_labels commit 98270c8f2805d406e0d88605fde778f8f182df9b
imgteam
parents: 0
diff changeset
42 G.add_edge(adjacent_label, label)
43c80f3c3b60 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/colorize_labels commit 98270c8f2805d406e0d88605fde778f8f182df9b
imgteam
parents: 0
diff changeset
43
43c80f3c3b60 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/colorize_labels commit 98270c8f2805d406e0d88605fde778f8f182df9b
imgteam
parents: 0
diff changeset
44 return G
43c80f3c3b60 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/colorize_labels commit 98270c8f2805d406e0d88605fde778f8f182df9b
imgteam
parents: 0
diff changeset
45
43c80f3c3b60 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/colorize_labels commit 98270c8f2805d406e0d88605fde778f8f182df9b
imgteam
parents: 0
diff changeset
46
0
0afb17e107ff planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/colorize_labels commit ffedf4e17ecbb226657ccf8472b0572532e9aa8a
imgteam
parents:
diff changeset
47 if __name__ == '__main__':
0afb17e107ff planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/colorize_labels commit ffedf4e17ecbb226657ccf8472b0572532e9aa8a
imgteam
parents:
diff changeset
48
0afb17e107ff planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/colorize_labels commit ffedf4e17ecbb226657ccf8472b0572532e9aa8a
imgteam
parents:
diff changeset
49 parser = argparse.ArgumentParser()
0afb17e107ff planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/colorize_labels commit ffedf4e17ecbb226657ccf8472b0572532e9aa8a
imgteam
parents:
diff changeset
50 parser.add_argument('input', type=str)
0afb17e107ff planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/colorize_labels commit ffedf4e17ecbb226657ccf8472b0572532e9aa8a
imgteam
parents:
diff changeset
51 parser.add_argument('--bg_label', type=int)
0afb17e107ff planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/colorize_labels commit ffedf4e17ecbb226657ccf8472b0572532e9aa8a
imgteam
parents:
diff changeset
52 parser.add_argument('--bg_color', type=str)
1
43c80f3c3b60 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/colorize_labels commit 98270c8f2805d406e0d88605fde778f8f182df9b
imgteam
parents: 0
diff changeset
53 parser.add_argument('--radius', type=int)
0
0afb17e107ff planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/colorize_labels commit ffedf4e17ecbb226657ccf8472b0572532e9aa8a
imgteam
parents:
diff changeset
54 parser.add_argument('--output', type=str)
0afb17e107ff planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/colorize_labels commit ffedf4e17ecbb226657ccf8472b0572532e9aa8a
imgteam
parents:
diff changeset
55 args = parser.parse_args()
0afb17e107ff planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/colorize_labels commit ffedf4e17ecbb226657ccf8472b0572532e9aa8a
imgteam
parents:
diff changeset
56
1
43c80f3c3b60 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/colorize_labels commit 98270c8f2805d406e0d88605fde778f8f182df9b
imgteam
parents: 0
diff changeset
57 # Load image and normalize
0
0afb17e107ff planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/colorize_labels commit ffedf4e17ecbb226657ccf8472b0572532e9aa8a
imgteam
parents:
diff changeset
58 im = skimage.io.imread(args.input)
0afb17e107ff planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/colorize_labels commit ffedf4e17ecbb226657ccf8472b0572532e9aa8a
imgteam
parents:
diff changeset
59 im = np.squeeze(im)
0afb17e107ff planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/colorize_labels commit ffedf4e17ecbb226657ccf8472b0572532e9aa8a
imgteam
parents:
diff changeset
60 assert im.ndim == 2
0afb17e107ff planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/colorize_labels commit ffedf4e17ecbb226657ccf8472b0572532e9aa8a
imgteam
parents:
diff changeset
61
1
43c80f3c3b60 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/colorize_labels commit 98270c8f2805d406e0d88605fde778f8f182df9b
imgteam
parents: 0
diff changeset
62 # Build adjacency graph of the labels
43c80f3c3b60 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/colorize_labels commit 98270c8f2805d406e0d88605fde778f8f182df9b
imgteam
parents: 0
diff changeset
63 G = build_label_adjacency_graph(im, args.radius, args.bg_label)
43c80f3c3b60 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/colorize_labels commit 98270c8f2805d406e0d88605fde778f8f182df9b
imgteam
parents: 0
diff changeset
64
43c80f3c3b60 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/colorize_labels commit 98270c8f2805d406e0d88605fde778f8f182df9b
imgteam
parents: 0
diff changeset
65 # Apply greedy coloring
43c80f3c3b60 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/colorize_labels commit 98270c8f2805d406e0d88605fde778f8f182df9b
imgteam
parents: 0
diff changeset
66 graph_coloring = nx.greedy_color(G)
43c80f3c3b60 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/colorize_labels commit 98270c8f2805d406e0d88605fde778f8f182df9b
imgteam
parents: 0
diff changeset
67 unique_colors = frozenset(graph_coloring.values())
43c80f3c3b60 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/colorize_labels commit 98270c8f2805d406e0d88605fde778f8f182df9b
imgteam
parents: 0
diff changeset
68
43c80f3c3b60 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/colorize_labels commit 98270c8f2805d406e0d88605fde778f8f182df9b
imgteam
parents: 0
diff changeset
69 # Assign colors to nodes based on the greedy coloring
43c80f3c3b60 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/colorize_labels commit 98270c8f2805d406e0d88605fde778f8f182df9b
imgteam
parents: 0
diff changeset
70 graph_color_to_mpl_color = dict(zip(unique_colors, mpl.TABLEAU_COLORS.values()))
43c80f3c3b60 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/colorize_labels commit 98270c8f2805d406e0d88605fde778f8f182df9b
imgteam
parents: 0
diff changeset
71 node_colors = [graph_color_to_mpl_color[graph_coloring[n]] for n in G.nodes()]
0
0afb17e107ff planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/colorize_labels commit ffedf4e17ecbb226657ccf8472b0572532e9aa8a
imgteam
parents:
diff changeset
72
1
43c80f3c3b60 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/colorize_labels commit 98270c8f2805d406e0d88605fde778f8f182df9b
imgteam
parents: 0
diff changeset
73 # Render result
43c80f3c3b60 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/colorize_labels commit 98270c8f2805d406e0d88605fde778f8f182df9b
imgteam
parents: 0
diff changeset
74 bg_color_rgb = color_hex_to_rgb_tuple(args.bg_color)
43c80f3c3b60 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/colorize_labels commit 98270c8f2805d406e0d88605fde778f8f182df9b
imgteam
parents: 0
diff changeset
75 result = np.dstack([np.full(im.shape, bg_color_rgb[ch], np.uint8) for ch in range(3)])
43c80f3c3b60 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/colorize_labels commit 98270c8f2805d406e0d88605fde778f8f182df9b
imgteam
parents: 0
diff changeset
76 for label, label_color in zip(G.nodes(), node_colors):
43c80f3c3b60 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/colorize_labels commit 98270c8f2805d406e0d88605fde778f8f182df9b
imgteam
parents: 0
diff changeset
77
43c80f3c3b60 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/colorize_labels commit 98270c8f2805d406e0d88605fde778f8f182df9b
imgteam
parents: 0
diff changeset
78 cc = (im == label)
43c80f3c3b60 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/colorize_labels commit 98270c8f2805d406e0d88605fde778f8f182df9b
imgteam
parents: 0
diff changeset
79 label_color = color_hex_to_rgb_tuple(label_color)
43c80f3c3b60 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/colorize_labels commit 98270c8f2805d406e0d88605fde778f8f182df9b
imgteam
parents: 0
diff changeset
80 for ch in range(3):
43c80f3c3b60 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/colorize_labels commit 98270c8f2805d406e0d88605fde778f8f182df9b
imgteam
parents: 0
diff changeset
81 result[:, :, ch][cc] = label_color[ch]
43c80f3c3b60 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/colorize_labels commit 98270c8f2805d406e0d88605fde778f8f182df9b
imgteam
parents: 0
diff changeset
82
43c80f3c3b60 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/colorize_labels commit 98270c8f2805d406e0d88605fde778f8f182df9b
imgteam
parents: 0
diff changeset
83 # Write result image
43c80f3c3b60 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/colorize_labels commit 98270c8f2805d406e0d88605fde778f8f182df9b
imgteam
parents: 0
diff changeset
84 skimage.io.imsave(args.output, result)