annotate colorize_labels.py @ 3:2d1de6e7b113 draft

planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/colorize_labels commit c86a1b93cb7732f7331a981d13465653cc1a2790
author imgteam
date Wed, 24 Apr 2024 08:12:15 +0000
parents 43c80f3c3b60
children 5907be5a8d7c
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
3
2d1de6e7b113 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/colorize_labels commit c86a1b93cb7732f7331a981d13465653cc1a2790
imgteam
parents: 1
diff changeset
3 import giatools.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
4 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
5 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
6 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
7 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
8 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
9 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
10 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
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
0afb17e107ff planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/colorize_labels commit ffedf4e17ecbb226657ccf8472b0572532e9aa8a
imgteam
parents:
diff changeset
13 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
14 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
15 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
16 return (
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[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
18 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
19 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
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
0afb17e107ff planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/colorize_labels commit ffedf4e17ecbb226657ccf8472b0572532e9aa8a
imgteam
parents:
diff changeset
22
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
23 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
24 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
25 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
26 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
27
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 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
29 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
30
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 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
32
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 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
34 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
35 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
36
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 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
38
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 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
40 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
41
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(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
43 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
44
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 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
46
43c80f3c3b60 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/colorize_labels commit 98270c8f2805d406e0d88605fde778f8f182df9b
imgteam
parents: 0
diff changeset
47
0
0afb17e107ff planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/colorize_labels commit ffedf4e17ecbb226657ccf8472b0572532e9aa8a
imgteam
parents:
diff changeset
48 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
49
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 = 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
51 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
52 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
53 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
54 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
55 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
56 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
57
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
58 # Load image and normalize
3
2d1de6e7b113 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/colorize_labels commit c86a1b93cb7732f7331a981d13465653cc1a2790
imgteam
parents: 1
diff changeset
59 im = giatools.io.imread(args.input)
0
0afb17e107ff planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/colorize_labels commit ffedf4e17ecbb226657ccf8472b0572532e9aa8a
imgteam
parents:
diff changeset
60 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
61 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
62
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
63 # 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
64 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
65
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 # 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
67 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
68 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
69
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 # 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
71 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
72 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
73
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
74 # 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
75 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
76 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
77 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
78
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 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
80 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
81 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
82 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
83
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 # 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
85 skimage.io.imsave(args.output, result)