annotate points2label.py @ 2:714a57d6f3a1 draft

planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/points2labelimage/ commit 6fc9ab8db9ef72ac7ded30d7373768feeae9390d
author imgteam
date Fri, 27 Sep 2024 17:41:06 +0000
parents 3fa3c8e41364
children 2ae122d5d85a
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
07d362a8af11 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/points2labelimage/ commit c3f4b766f03770f094fda6bda0a5882c0ebd4581
imgteam
parents:
diff changeset
1 import argparse
2
714a57d6f3a1 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/points2labelimage/ commit 6fc9ab8db9ef72ac7ded30d7373768feeae9390d
imgteam
parents: 1
diff changeset
2 import os
0
07d362a8af11 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/points2labelimage/ commit c3f4b766f03770f094fda6bda0a5882c0ebd4581
imgteam
parents:
diff changeset
3 import warnings
07d362a8af11 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/points2labelimage/ commit c3f4b766f03770f094fda6bda0a5882c0ebd4581
imgteam
parents:
diff changeset
4
2
714a57d6f3a1 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/points2labelimage/ commit 6fc9ab8db9ef72ac7ded30d7373768feeae9390d
imgteam
parents: 1
diff changeset
5 import giatools.pandas
714a57d6f3a1 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/points2labelimage/ commit 6fc9ab8db9ef72ac7ded30d7373768feeae9390d
imgteam
parents: 1
diff changeset
6 import numpy as np
714a57d6f3a1 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/points2labelimage/ commit 6fc9ab8db9ef72ac7ded30d7373768feeae9390d
imgteam
parents: 1
diff changeset
7 import pandas as pd
714a57d6f3a1 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/points2labelimage/ commit 6fc9ab8db9ef72ac7ded30d7373768feeae9390d
imgteam
parents: 1
diff changeset
8 import scipy.ndimage as ndi
714a57d6f3a1 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/points2labelimage/ commit 6fc9ab8db9ef72ac7ded30d7373768feeae9390d
imgteam
parents: 1
diff changeset
9 import skimage.io
714a57d6f3a1 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/points2labelimage/ commit 6fc9ab8db9ef72ac7ded30d7373768feeae9390d
imgteam
parents: 1
diff changeset
10 import skimage.segmentation
0
07d362a8af11 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/points2labelimage/ commit c3f4b766f03770f094fda6bda0a5882c0ebd4581
imgteam
parents:
diff changeset
11
2
714a57d6f3a1 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/points2labelimage/ commit 6fc9ab8db9ef72ac7ded30d7373768feeae9390d
imgteam
parents: 1
diff changeset
12
714a57d6f3a1 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/points2labelimage/ commit 6fc9ab8db9ef72ac7ded30d7373768feeae9390d
imgteam
parents: 1
diff changeset
13 def rasterize(point_file, out_file, shape, has_header=False, swap_xy=False, bg_value=0, fg_value=None):
714a57d6f3a1 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/points2labelimage/ commit 6fc9ab8db9ef72ac7ded30d7373768feeae9390d
imgteam
parents: 1
diff changeset
14
714a57d6f3a1 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/points2labelimage/ commit 6fc9ab8db9ef72ac7ded30d7373768feeae9390d
imgteam
parents: 1
diff changeset
15 img = np.full(shape, dtype=np.uint16, fill_value=bg_value)
714a57d6f3a1 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/points2labelimage/ commit 6fc9ab8db9ef72ac7ded30d7373768feeae9390d
imgteam
parents: 1
diff changeset
16 if os.path.exists(point_file) and os.path.getsize(point_file) > 0:
714a57d6f3a1 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/points2labelimage/ commit 6fc9ab8db9ef72ac7ded30d7373768feeae9390d
imgteam
parents: 1
diff changeset
17
714a57d6f3a1 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/points2labelimage/ commit 6fc9ab8db9ef72ac7ded30d7373768feeae9390d
imgteam
parents: 1
diff changeset
18 # Read the tabular file with information from the header
0
07d362a8af11 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/points2labelimage/ commit c3f4b766f03770f094fda6bda0a5882c0ebd4581
imgteam
parents:
diff changeset
19 if has_header:
2
714a57d6f3a1 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/points2labelimage/ commit 6fc9ab8db9ef72ac7ded30d7373768feeae9390d
imgteam
parents: 1
diff changeset
20 df = pd.read_csv(point_file, delimiter='\t')
714a57d6f3a1 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/points2labelimage/ commit 6fc9ab8db9ef72ac7ded30d7373768feeae9390d
imgteam
parents: 1
diff changeset
21
714a57d6f3a1 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/points2labelimage/ commit 6fc9ab8db9ef72ac7ded30d7373768feeae9390d
imgteam
parents: 1
diff changeset
22 pos_x_column = giatools.pandas.find_column(df, ['pos_x', 'POS_X'])
714a57d6f3a1 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/points2labelimage/ commit 6fc9ab8db9ef72ac7ded30d7373768feeae9390d
imgteam
parents: 1
diff changeset
23 pos_y_column = giatools.pandas.find_column(df, ['pos_y', 'POS_Y'])
714a57d6f3a1 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/points2labelimage/ commit 6fc9ab8db9ef72ac7ded30d7373768feeae9390d
imgteam
parents: 1
diff changeset
24 pos_x_list = df[pos_x_column].round().astype(int)
714a57d6f3a1 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/points2labelimage/ commit 6fc9ab8db9ef72ac7ded30d7373768feeae9390d
imgteam
parents: 1
diff changeset
25 pos_y_list = df[pos_y_column].round().astype(int)
714a57d6f3a1 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/points2labelimage/ commit 6fc9ab8db9ef72ac7ded30d7373768feeae9390d
imgteam
parents: 1
diff changeset
26 assert len(pos_x_list) == len(pos_y_list)
714a57d6f3a1 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/points2labelimage/ commit 6fc9ab8db9ef72ac7ded30d7373768feeae9390d
imgteam
parents: 1
diff changeset
27
714a57d6f3a1 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/points2labelimage/ commit 6fc9ab8db9ef72ac7ded30d7373768feeae9390d
imgteam
parents: 1
diff changeset
28 try:
714a57d6f3a1 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/points2labelimage/ commit 6fc9ab8db9ef72ac7ded30d7373768feeae9390d
imgteam
parents: 1
diff changeset
29 radius_column = giatools.pandas.find_column(df, ['radius', 'RADIUS'])
714a57d6f3a1 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/points2labelimage/ commit 6fc9ab8db9ef72ac7ded30d7373768feeae9390d
imgteam
parents: 1
diff changeset
30 radius_list = df[radius_column]
714a57d6f3a1 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/points2labelimage/ commit 6fc9ab8db9ef72ac7ded30d7373768feeae9390d
imgteam
parents: 1
diff changeset
31 assert len(pos_x_list) == len(radius_list)
714a57d6f3a1 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/points2labelimage/ commit 6fc9ab8db9ef72ac7ded30d7373768feeae9390d
imgteam
parents: 1
diff changeset
32 except KeyError:
714a57d6f3a1 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/points2labelimage/ commit 6fc9ab8db9ef72ac7ded30d7373768feeae9390d
imgteam
parents: 1
diff changeset
33 radius_list = [0] * len(pos_x_list)
714a57d6f3a1 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/points2labelimage/ commit 6fc9ab8db9ef72ac7ded30d7373768feeae9390d
imgteam
parents: 1
diff changeset
34
714a57d6f3a1 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/points2labelimage/ commit 6fc9ab8db9ef72ac7ded30d7373768feeae9390d
imgteam
parents: 1
diff changeset
35 try:
714a57d6f3a1 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/points2labelimage/ commit 6fc9ab8db9ef72ac7ded30d7373768feeae9390d
imgteam
parents: 1
diff changeset
36 label_column = giatools.pandas.find_column(df, ['label', 'LABEL'])
714a57d6f3a1 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/points2labelimage/ commit 6fc9ab8db9ef72ac7ded30d7373768feeae9390d
imgteam
parents: 1
diff changeset
37 label_list = df[label_column]
714a57d6f3a1 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/points2labelimage/ commit 6fc9ab8db9ef72ac7ded30d7373768feeae9390d
imgteam
parents: 1
diff changeset
38 assert len(pos_x_list) == len(label_list)
714a57d6f3a1 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/points2labelimage/ commit 6fc9ab8db9ef72ac7ded30d7373768feeae9390d
imgteam
parents: 1
diff changeset
39 except KeyError:
714a57d6f3a1 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/points2labelimage/ commit 6fc9ab8db9ef72ac7ded30d7373768feeae9390d
imgteam
parents: 1
diff changeset
40 label_list = list(range(1, len(pos_x_list) + 1))
714a57d6f3a1 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/points2labelimage/ commit 6fc9ab8db9ef72ac7ded30d7373768feeae9390d
imgteam
parents: 1
diff changeset
41
714a57d6f3a1 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/points2labelimage/ commit 6fc9ab8db9ef72ac7ded30d7373768feeae9390d
imgteam
parents: 1
diff changeset
42 # Read the tabular file without header
0
07d362a8af11 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/points2labelimage/ commit c3f4b766f03770f094fda6bda0a5882c0ebd4581
imgteam
parents:
diff changeset
43 else:
2
714a57d6f3a1 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/points2labelimage/ commit 6fc9ab8db9ef72ac7ded30d7373768feeae9390d
imgteam
parents: 1
diff changeset
44 df = pd.read_csv(point_file, header=None, delimiter='\t')
714a57d6f3a1 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/points2labelimage/ commit 6fc9ab8db9ef72ac7ded30d7373768feeae9390d
imgteam
parents: 1
diff changeset
45 pos_x_list = df[0].round().astype(int)
714a57d6f3a1 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/points2labelimage/ commit 6fc9ab8db9ef72ac7ded30d7373768feeae9390d
imgteam
parents: 1
diff changeset
46 pos_y_list = df[1].round().astype(int)
714a57d6f3a1 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/points2labelimage/ commit 6fc9ab8db9ef72ac7ded30d7373768feeae9390d
imgteam
parents: 1
diff changeset
47 assert len(pos_x_list) == len(pos_y_list)
714a57d6f3a1 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/points2labelimage/ commit 6fc9ab8db9ef72ac7ded30d7373768feeae9390d
imgteam
parents: 1
diff changeset
48 radius_list = [0] * len(pos_x_list)
714a57d6f3a1 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/points2labelimage/ commit 6fc9ab8db9ef72ac7ded30d7373768feeae9390d
imgteam
parents: 1
diff changeset
49 label_list = list(range(1, len(pos_x_list) + 1))
714a57d6f3a1 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/points2labelimage/ commit 6fc9ab8db9ef72ac7ded30d7373768feeae9390d
imgteam
parents: 1
diff changeset
50
714a57d6f3a1 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/points2labelimage/ commit 6fc9ab8db9ef72ac7ded30d7373768feeae9390d
imgteam
parents: 1
diff changeset
51 # Optionally swap the coordinates
714a57d6f3a1 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/points2labelimage/ commit 6fc9ab8db9ef72ac7ded30d7373768feeae9390d
imgteam
parents: 1
diff changeset
52 if swap_xy:
714a57d6f3a1 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/points2labelimage/ commit 6fc9ab8db9ef72ac7ded30d7373768feeae9390d
imgteam
parents: 1
diff changeset
53 pos_x_list, pos_y_list = pos_y_list, pos_x_list
0
07d362a8af11 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/points2labelimage/ commit c3f4b766f03770f094fda6bda0a5882c0ebd4581
imgteam
parents:
diff changeset
54
2
714a57d6f3a1 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/points2labelimage/ commit 6fc9ab8db9ef72ac7ded30d7373768feeae9390d
imgteam
parents: 1
diff changeset
55 # Perform the rasterization
714a57d6f3a1 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/points2labelimage/ commit 6fc9ab8db9ef72ac7ded30d7373768feeae9390d
imgteam
parents: 1
diff changeset
56 for y, x, radius, label in zip(pos_y_list, pos_x_list, radius_list, label_list):
714a57d6f3a1 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/points2labelimage/ commit 6fc9ab8db9ef72ac7ded30d7373768feeae9390d
imgteam
parents: 1
diff changeset
57 if fg_value is not None:
714a57d6f3a1 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/points2labelimage/ commit 6fc9ab8db9ef72ac7ded30d7373768feeae9390d
imgteam
parents: 1
diff changeset
58 label = fg_value
714a57d6f3a1 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/points2labelimage/ commit 6fc9ab8db9ef72ac7ded30d7373768feeae9390d
imgteam
parents: 1
diff changeset
59
714a57d6f3a1 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/points2labelimage/ commit 6fc9ab8db9ef72ac7ded30d7373768feeae9390d
imgteam
parents: 1
diff changeset
60 if y < 0 or x < 0 or y >= shape[0] or x >= shape[1]:
714a57d6f3a1 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/points2labelimage/ commit 6fc9ab8db9ef72ac7ded30d7373768feeae9390d
imgteam
parents: 1
diff changeset
61 raise IndexError(f'The point x={x}, y={y} exceeds the bounds of the image (width: {shape[1]}, height: {shape[0]})')
714a57d6f3a1 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/points2labelimage/ commit 6fc9ab8db9ef72ac7ded30d7373768feeae9390d
imgteam
parents: 1
diff changeset
62
714a57d6f3a1 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/points2labelimage/ commit 6fc9ab8db9ef72ac7ded30d7373768feeae9390d
imgteam
parents: 1
diff changeset
63 # Rasterize circle and distribute overlapping image area
714a57d6f3a1 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/points2labelimage/ commit 6fc9ab8db9ef72ac7ded30d7373768feeae9390d
imgteam
parents: 1
diff changeset
64 if radius > 0:
714a57d6f3a1 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/points2labelimage/ commit 6fc9ab8db9ef72ac7ded30d7373768feeae9390d
imgteam
parents: 1
diff changeset
65 mask = np.ones(shape, dtype=bool)
714a57d6f3a1 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/points2labelimage/ commit 6fc9ab8db9ef72ac7ded30d7373768feeae9390d
imgteam
parents: 1
diff changeset
66 mask[y, x] = False
714a57d6f3a1 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/points2labelimage/ commit 6fc9ab8db9ef72ac7ded30d7373768feeae9390d
imgteam
parents: 1
diff changeset
67 mask = (ndi.distance_transform_edt(mask) <= radius)
714a57d6f3a1 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/points2labelimage/ commit 6fc9ab8db9ef72ac7ded30d7373768feeae9390d
imgteam
parents: 1
diff changeset
68
714a57d6f3a1 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/points2labelimage/ commit 6fc9ab8db9ef72ac7ded30d7373768feeae9390d
imgteam
parents: 1
diff changeset
69 # Compute the overlap (pretend there is none if the rasterization is binary)
714a57d6f3a1 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/points2labelimage/ commit 6fc9ab8db9ef72ac7ded30d7373768feeae9390d
imgteam
parents: 1
diff changeset
70 if fg_value is None:
714a57d6f3a1 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/points2labelimage/ commit 6fc9ab8db9ef72ac7ded30d7373768feeae9390d
imgteam
parents: 1
diff changeset
71 overlap = np.logical_and(img > 0, mask)
714a57d6f3a1 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/points2labelimage/ commit 6fc9ab8db9ef72ac7ded30d7373768feeae9390d
imgteam
parents: 1
diff changeset
72 else:
714a57d6f3a1 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/points2labelimage/ commit 6fc9ab8db9ef72ac7ded30d7373768feeae9390d
imgteam
parents: 1
diff changeset
73 overlap = np.zeros(shape, dtype=bool)
0
07d362a8af11 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/points2labelimage/ commit c3f4b766f03770f094fda6bda0a5882c0ebd4581
imgteam
parents:
diff changeset
74
2
714a57d6f3a1 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/points2labelimage/ commit 6fc9ab8db9ef72ac7ded30d7373768feeae9390d
imgteam
parents: 1
diff changeset
75 # Rasterize the part of the circle which is disjoint from other foreground.
714a57d6f3a1 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/points2labelimage/ commit 6fc9ab8db9ef72ac7ded30d7373768feeae9390d
imgteam
parents: 1
diff changeset
76 #
714a57d6f3a1 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/points2labelimage/ commit 6fc9ab8db9ef72ac7ded30d7373768feeae9390d
imgteam
parents: 1
diff changeset
77 # In the current implementation, the result depends on the order of the rasterized circles if somewhere
714a57d6f3a1 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/points2labelimage/ commit 6fc9ab8db9ef72ac7ded30d7373768feeae9390d
imgteam
parents: 1
diff changeset
78 # more than two circles overlap. This is probably negligable for most applications. To achieve results
714a57d6f3a1 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/points2labelimage/ commit 6fc9ab8db9ef72ac7ded30d7373768feeae9390d
imgteam
parents: 1
diff changeset
79 # that are invariant to the order, first all circles would need to be rasterized independently, and
714a57d6f3a1 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/points2labelimage/ commit 6fc9ab8db9ef72ac7ded30d7373768feeae9390d
imgteam
parents: 1
diff changeset
80 # then blended together. This, however, would either strongly increase the memory consumption, or
714a57d6f3a1 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/points2labelimage/ commit 6fc9ab8db9ef72ac7ded30d7373768feeae9390d
imgteam
parents: 1
diff changeset
81 # require a more complex implementation which exploits the sparsity of the rasterized masks.
714a57d6f3a1 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/points2labelimage/ commit 6fc9ab8db9ef72ac7ded30d7373768feeae9390d
imgteam
parents: 1
diff changeset
82 #
714a57d6f3a1 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/points2labelimage/ commit 6fc9ab8db9ef72ac7ded30d7373768feeae9390d
imgteam
parents: 1
diff changeset
83 disjoint_mask = np.logical_xor(mask, overlap)
714a57d6f3a1 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/points2labelimage/ commit 6fc9ab8db9ef72ac7ded30d7373768feeae9390d
imgteam
parents: 1
diff changeset
84 if disjoint_mask.any():
714a57d6f3a1 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/points2labelimage/ commit 6fc9ab8db9ef72ac7ded30d7373768feeae9390d
imgteam
parents: 1
diff changeset
85 img[disjoint_mask] = label
714a57d6f3a1 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/points2labelimage/ commit 6fc9ab8db9ef72ac7ded30d7373768feeae9390d
imgteam
parents: 1
diff changeset
86
714a57d6f3a1 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/points2labelimage/ commit 6fc9ab8db9ef72ac7ded30d7373768feeae9390d
imgteam
parents: 1
diff changeset
87 # Distribute the remaining part of the circle
714a57d6f3a1 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/points2labelimage/ commit 6fc9ab8db9ef72ac7ded30d7373768feeae9390d
imgteam
parents: 1
diff changeset
88 if overlap.any():
714a57d6f3a1 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/points2labelimage/ commit 6fc9ab8db9ef72ac7ded30d7373768feeae9390d
imgteam
parents: 1
diff changeset
89 dist = ndi.distance_transform_edt(overlap)
714a57d6f3a1 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/points2labelimage/ commit 6fc9ab8db9ef72ac7ded30d7373768feeae9390d
imgteam
parents: 1
diff changeset
90 foreground = (img > 0)
714a57d6f3a1 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/points2labelimage/ commit 6fc9ab8db9ef72ac7ded30d7373768feeae9390d
imgteam
parents: 1
diff changeset
91 img[overlap] = 0
714a57d6f3a1 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/points2labelimage/ commit 6fc9ab8db9ef72ac7ded30d7373768feeae9390d
imgteam
parents: 1
diff changeset
92 img = skimage.segmentation.watershed(dist, img, mask=foreground)
714a57d6f3a1 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/points2labelimage/ commit 6fc9ab8db9ef72ac7ded30d7373768feeae9390d
imgteam
parents: 1
diff changeset
93
714a57d6f3a1 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/points2labelimage/ commit 6fc9ab8db9ef72ac7ded30d7373768feeae9390d
imgteam
parents: 1
diff changeset
94 # Rasterize point (there is no overlapping area to be distributed)
714a57d6f3a1 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/points2labelimage/ commit 6fc9ab8db9ef72ac7ded30d7373768feeae9390d
imgteam
parents: 1
diff changeset
95 else:
714a57d6f3a1 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/points2labelimage/ commit 6fc9ab8db9ef72ac7ded30d7373768feeae9390d
imgteam
parents: 1
diff changeset
96 img[y, x] = label
714a57d6f3a1 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/points2labelimage/ commit 6fc9ab8db9ef72ac7ded30d7373768feeae9390d
imgteam
parents: 1
diff changeset
97
0
07d362a8af11 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/points2labelimage/ commit c3f4b766f03770f094fda6bda0a5882c0ebd4581
imgteam
parents:
diff changeset
98 else:
2
714a57d6f3a1 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/points2labelimage/ commit 6fc9ab8db9ef72ac7ded30d7373768feeae9390d
imgteam
parents: 1
diff changeset
99 raise Exception("{} is empty or does not exist.".format(point_file)) # appropriate built-in error?
714a57d6f3a1 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/points2labelimage/ commit 6fc9ab8db9ef72ac7ded30d7373768feeae9390d
imgteam
parents: 1
diff changeset
100
714a57d6f3a1 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/points2labelimage/ commit 6fc9ab8db9ef72ac7ded30d7373768feeae9390d
imgteam
parents: 1
diff changeset
101 with warnings.catch_warnings():
714a57d6f3a1 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/points2labelimage/ commit 6fc9ab8db9ef72ac7ded30d7373768feeae9390d
imgteam
parents: 1
diff changeset
102 warnings.simplefilter("ignore")
714a57d6f3a1 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/points2labelimage/ commit 6fc9ab8db9ef72ac7ded30d7373768feeae9390d
imgteam
parents: 1
diff changeset
103 skimage.io.imsave(out_file, img, plugin='tifffile') # otherwise we get problems with the .dat extension
714a57d6f3a1 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/points2labelimage/ commit 6fc9ab8db9ef72ac7ded30d7373768feeae9390d
imgteam
parents: 1
diff changeset
104
0
07d362a8af11 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/points2labelimage/ commit c3f4b766f03770f094fda6bda0a5882c0ebd4581
imgteam
parents:
diff changeset
105
07d362a8af11 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/points2labelimage/ commit c3f4b766f03770f094fda6bda0a5882c0ebd4581
imgteam
parents:
diff changeset
106 if __name__ == "__main__":
07d362a8af11 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/points2labelimage/ commit c3f4b766f03770f094fda6bda0a5882c0ebd4581
imgteam
parents:
diff changeset
107 parser = argparse.ArgumentParser()
2
714a57d6f3a1 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/points2labelimage/ commit 6fc9ab8db9ef72ac7ded30d7373768feeae9390d
imgteam
parents: 1
diff changeset
108 parser.add_argument('point_file', type=argparse.FileType('r'), help='point file')
714a57d6f3a1 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/points2labelimage/ commit 6fc9ab8db9ef72ac7ded30d7373768feeae9390d
imgteam
parents: 1
diff changeset
109 parser.add_argument('out_file', type=str, help='out file (TIFF)')
714a57d6f3a1 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/points2labelimage/ commit 6fc9ab8db9ef72ac7ded30d7373768feeae9390d
imgteam
parents: 1
diff changeset
110 parser.add_argument('shapex', type=int, help='shapex')
714a57d6f3a1 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/points2labelimage/ commit 6fc9ab8db9ef72ac7ded30d7373768feeae9390d
imgteam
parents: 1
diff changeset
111 parser.add_argument('shapey', type=int, help='shapey')
714a57d6f3a1 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/points2labelimage/ commit 6fc9ab8db9ef72ac7ded30d7373768feeae9390d
imgteam
parents: 1
diff changeset
112 parser.add_argument('--has_header', dest='has_header', default=False, help='set True if point file has header')
714a57d6f3a1 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/points2labelimage/ commit 6fc9ab8db9ef72ac7ded30d7373768feeae9390d
imgteam
parents: 1
diff changeset
113 parser.add_argument('--swap_xy', dest='swap_xy', default=False, help='Swap X and Y coordinates')
714a57d6f3a1 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/points2labelimage/ commit 6fc9ab8db9ef72ac7ded30d7373768feeae9390d
imgteam
parents: 1
diff changeset
114 parser.add_argument('--binary', dest='binary', default=False, help='Produce binary image')
714a57d6f3a1 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/points2labelimage/ commit 6fc9ab8db9ef72ac7ded30d7373768feeae9390d
imgteam
parents: 1
diff changeset
115
0
07d362a8af11 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/points2labelimage/ commit c3f4b766f03770f094fda6bda0a5882c0ebd4581
imgteam
parents:
diff changeset
116 args = parser.parse_args()
07d362a8af11 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/points2labelimage/ commit c3f4b766f03770f094fda6bda0a5882c0ebd4581
imgteam
parents:
diff changeset
117
2
714a57d6f3a1 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/points2labelimage/ commit 6fc9ab8db9ef72ac7ded30d7373768feeae9390d
imgteam
parents: 1
diff changeset
118 rasterize(
714a57d6f3a1 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/points2labelimage/ commit 6fc9ab8db9ef72ac7ded30d7373768feeae9390d
imgteam
parents: 1
diff changeset
119 args.point_file.name,
714a57d6f3a1 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/points2labelimage/ commit 6fc9ab8db9ef72ac7ded30d7373768feeae9390d
imgteam
parents: 1
diff changeset
120 args.out_file,
714a57d6f3a1 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/points2labelimage/ commit 6fc9ab8db9ef72ac7ded30d7373768feeae9390d
imgteam
parents: 1
diff changeset
121 (args.shapey, args.shapex),
714a57d6f3a1 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/points2labelimage/ commit 6fc9ab8db9ef72ac7ded30d7373768feeae9390d
imgteam
parents: 1
diff changeset
122 has_header=args.has_header,
714a57d6f3a1 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/points2labelimage/ commit 6fc9ab8db9ef72ac7ded30d7373768feeae9390d
imgteam
parents: 1
diff changeset
123 swap_xy=args.swap_xy,
714a57d6f3a1 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/points2labelimage/ commit 6fc9ab8db9ef72ac7ded30d7373768feeae9390d
imgteam
parents: 1
diff changeset
124 fg_value=0xffff if args.binary else None,
714a57d6f3a1 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/points2labelimage/ commit 6fc9ab8db9ef72ac7ded30d7373768feeae9390d
imgteam
parents: 1
diff changeset
125 )