annotate points2label.py @ 3:2ae122d5d85a draft

planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/points2labelimage/ commit 78614a9010c2ca0e1fa5973639c05ab74bcdb148
author imgteam
date Wed, 23 Apr 2025 14:37:31 +0000
parents 714a57d6f3a1
children 64c155acb864
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:
3
2ae122d5d85a planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/points2labelimage/ commit 78614a9010c2ca0e1fa5973639c05ab74bcdb148
imgteam
parents: 2
diff changeset
36 width_column = giatools.pandas.find_column(df, ['width', 'WIDTH'])
2ae122d5d85a planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/points2labelimage/ commit 78614a9010c2ca0e1fa5973639c05ab74bcdb148
imgteam
parents: 2
diff changeset
37 height_column = giatools.pandas.find_column(df, ['height', 'HEIGHT'])
2ae122d5d85a planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/points2labelimage/ commit 78614a9010c2ca0e1fa5973639c05ab74bcdb148
imgteam
parents: 2
diff changeset
38 width_list = df[width_column]
2ae122d5d85a planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/points2labelimage/ commit 78614a9010c2ca0e1fa5973639c05ab74bcdb148
imgteam
parents: 2
diff changeset
39 height_list = df[height_column]
2ae122d5d85a planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/points2labelimage/ commit 78614a9010c2ca0e1fa5973639c05ab74bcdb148
imgteam
parents: 2
diff changeset
40 assert len(pos_x_list) == len(width_list)
2ae122d5d85a planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/points2labelimage/ commit 78614a9010c2ca0e1fa5973639c05ab74bcdb148
imgteam
parents: 2
diff changeset
41 assert len(pos_x_list) == len(height_list)
2ae122d5d85a planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/points2labelimage/ commit 78614a9010c2ca0e1fa5973639c05ab74bcdb148
imgteam
parents: 2
diff changeset
42 except KeyError:
2ae122d5d85a planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/points2labelimage/ commit 78614a9010c2ca0e1fa5973639c05ab74bcdb148
imgteam
parents: 2
diff changeset
43 width_list = [0] * len(pos_x_list)
2ae122d5d85a planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/points2labelimage/ commit 78614a9010c2ca0e1fa5973639c05ab74bcdb148
imgteam
parents: 2
diff changeset
44 height_list = [0] * len(pos_x_list)
2ae122d5d85a planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/points2labelimage/ commit 78614a9010c2ca0e1fa5973639c05ab74bcdb148
imgteam
parents: 2
diff changeset
45
2ae122d5d85a planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/points2labelimage/ commit 78614a9010c2ca0e1fa5973639c05ab74bcdb148
imgteam
parents: 2
diff changeset
46 try:
2
714a57d6f3a1 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/points2labelimage/ commit 6fc9ab8db9ef72ac7ded30d7373768feeae9390d
imgteam
parents: 1
diff changeset
47 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
48 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
49 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
50 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
51 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
52
714a57d6f3a1 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/points2labelimage/ commit 6fc9ab8db9ef72ac7ded30d7373768feeae9390d
imgteam
parents: 1
diff changeset
53 # 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
54 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
55 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
56 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
57 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
58 assert len(pos_x_list) == len(pos_y_list)
3
2ae122d5d85a planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/points2labelimage/ commit 78614a9010c2ca0e1fa5973639c05ab74bcdb148
imgteam
parents: 2
diff changeset
59 radius_list, width_list, height_list = [[0] * len(pos_x_list)] * 3
2
714a57d6f3a1 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/points2labelimage/ commit 6fc9ab8db9ef72ac7ded30d7373768feeae9390d
imgteam
parents: 1
diff changeset
60 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
61
714a57d6f3a1 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/points2labelimage/ commit 6fc9ab8db9ef72ac7ded30d7373768feeae9390d
imgteam
parents: 1
diff changeset
62 # 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
63 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
64 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
65
2
714a57d6f3a1 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/points2labelimage/ commit 6fc9ab8db9ef72ac7ded30d7373768feeae9390d
imgteam
parents: 1
diff changeset
66 # Perform the rasterization
3
2ae122d5d85a planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/points2labelimage/ commit 78614a9010c2ca0e1fa5973639c05ab74bcdb148
imgteam
parents: 2
diff changeset
67 for y, x, radius, width, height, label in zip(
2ae122d5d85a planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/points2labelimage/ commit 78614a9010c2ca0e1fa5973639c05ab74bcdb148
imgteam
parents: 2
diff changeset
68 pos_y_list, pos_x_list, radius_list, width_list, height_list, label_list,
2ae122d5d85a planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/points2labelimage/ commit 78614a9010c2ca0e1fa5973639c05ab74bcdb148
imgteam
parents: 2
diff changeset
69 ):
2
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 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
71 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
72
714a57d6f3a1 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/points2labelimage/ commit 6fc9ab8db9ef72ac7ded30d7373768feeae9390d
imgteam
parents: 1
diff changeset
73 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
74 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
75
714a57d6f3a1 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/points2labelimage/ commit 6fc9ab8db9ef72ac7ded30d7373768feeae9390d
imgteam
parents: 1
diff changeset
76 # Rasterize circle and distribute overlapping image area
3
2ae122d5d85a planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/points2labelimage/ commit 78614a9010c2ca0e1fa5973639c05ab74bcdb148
imgteam
parents: 2
diff changeset
77 # Rasterize primitive geometry
2ae122d5d85a planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/points2labelimage/ commit 78614a9010c2ca0e1fa5973639c05ab74bcdb148
imgteam
parents: 2
diff changeset
78 if radius > 0 or (width > 0 and height > 0):
2ae122d5d85a planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/points2labelimage/ commit 78614a9010c2ca0e1fa5973639c05ab74bcdb148
imgteam
parents: 2
diff changeset
79
2ae122d5d85a planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/points2labelimage/ commit 78614a9010c2ca0e1fa5973639c05ab74bcdb148
imgteam
parents: 2
diff changeset
80 # Rasterize circle
2ae122d5d85a planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/points2labelimage/ commit 78614a9010c2ca0e1fa5973639c05ab74bcdb148
imgteam
parents: 2
diff changeset
81 if radius > 0:
2ae122d5d85a planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/points2labelimage/ commit 78614a9010c2ca0e1fa5973639c05ab74bcdb148
imgteam
parents: 2
diff changeset
82 mask = np.ones(shape, dtype=bool)
2ae122d5d85a planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/points2labelimage/ commit 78614a9010c2ca0e1fa5973639c05ab74bcdb148
imgteam
parents: 2
diff changeset
83 mask[y, x] = False
2ae122d5d85a planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/points2labelimage/ commit 78614a9010c2ca0e1fa5973639c05ab74bcdb148
imgteam
parents: 2
diff changeset
84 mask = (ndi.distance_transform_edt(mask) <= radius)
2ae122d5d85a planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/points2labelimage/ commit 78614a9010c2ca0e1fa5973639c05ab74bcdb148
imgteam
parents: 2
diff changeset
85 else:
2ae122d5d85a planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/points2labelimage/ commit 78614a9010c2ca0e1fa5973639c05ab74bcdb148
imgteam
parents: 2
diff changeset
86 mask = np.zeros(shape, dtype=bool)
2ae122d5d85a planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/points2labelimage/ commit 78614a9010c2ca0e1fa5973639c05ab74bcdb148
imgteam
parents: 2
diff changeset
87
2ae122d5d85a planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/points2labelimage/ commit 78614a9010c2ca0e1fa5973639c05ab74bcdb148
imgteam
parents: 2
diff changeset
88 # Rasterize rectangle
2ae122d5d85a planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/points2labelimage/ commit 78614a9010c2ca0e1fa5973639c05ab74bcdb148
imgteam
parents: 2
diff changeset
89 if width > 0 and height > 0:
2ae122d5d85a planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/points2labelimage/ commit 78614a9010c2ca0e1fa5973639c05ab74bcdb148
imgteam
parents: 2
diff changeset
90 mask[
2ae122d5d85a planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/points2labelimage/ commit 78614a9010c2ca0e1fa5973639c05ab74bcdb148
imgteam
parents: 2
diff changeset
91 y:min(shape[0], y + width),
2ae122d5d85a planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/points2labelimage/ commit 78614a9010c2ca0e1fa5973639c05ab74bcdb148
imgteam
parents: 2
diff changeset
92 x:min(shape[1], x + height)
2ae122d5d85a planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/points2labelimage/ commit 78614a9010c2ca0e1fa5973639c05ab74bcdb148
imgteam
parents: 2
diff changeset
93 ] = True
2
714a57d6f3a1 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/points2labelimage/ commit 6fc9ab8db9ef72ac7ded30d7373768feeae9390d
imgteam
parents: 1
diff changeset
94
714a57d6f3a1 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/points2labelimage/ commit 6fc9ab8db9ef72ac7ded30d7373768feeae9390d
imgteam
parents: 1
diff changeset
95 # 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
96 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
97 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
98 else:
714a57d6f3a1 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/points2labelimage/ commit 6fc9ab8db9ef72ac7ded30d7373768feeae9390d
imgteam
parents: 1
diff changeset
99 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
100
2
714a57d6f3a1 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/points2labelimage/ commit 6fc9ab8db9ef72ac7ded30d7373768feeae9390d
imgteam
parents: 1
diff changeset
101 # 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
102 #
714a57d6f3a1 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/points2labelimage/ commit 6fc9ab8db9ef72ac7ded30d7373768feeae9390d
imgteam
parents: 1
diff changeset
103 # 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
104 # 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
105 # 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
106 # 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
107 # 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
108 #
714a57d6f3a1 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/points2labelimage/ commit 6fc9ab8db9ef72ac7ded30d7373768feeae9390d
imgteam
parents: 1
diff changeset
109 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
110 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
111 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
112
714a57d6f3a1 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/points2labelimage/ commit 6fc9ab8db9ef72ac7ded30d7373768feeae9390d
imgteam
parents: 1
diff changeset
113 # 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
114 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
115 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
116 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
117 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
118 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
119
714a57d6f3a1 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/points2labelimage/ commit 6fc9ab8db9ef72ac7ded30d7373768feeae9390d
imgteam
parents: 1
diff changeset
120 # 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
121 else:
714a57d6f3a1 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/points2labelimage/ commit 6fc9ab8db9ef72ac7ded30d7373768feeae9390d
imgteam
parents: 1
diff changeset
122 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
123
0
07d362a8af11 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/points2labelimage/ commit c3f4b766f03770f094fda6bda0a5882c0ebd4581
imgteam
parents:
diff changeset
124 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
125 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
126
714a57d6f3a1 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/points2labelimage/ commit 6fc9ab8db9ef72ac7ded30d7373768feeae9390d
imgteam
parents: 1
diff changeset
127 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
128 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
129 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
130
0
07d362a8af11 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/points2labelimage/ commit c3f4b766f03770f094fda6bda0a5882c0ebd4581
imgteam
parents:
diff changeset
131
07d362a8af11 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/points2labelimage/ commit c3f4b766f03770f094fda6bda0a5882c0ebd4581
imgteam
parents:
diff changeset
132 if __name__ == "__main__":
07d362a8af11 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/points2labelimage/ commit c3f4b766f03770f094fda6bda0a5882c0ebd4581
imgteam
parents:
diff changeset
133 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
134 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
135 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
136 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
137 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
138 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
139 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
140 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
141
0
07d362a8af11 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/points2labelimage/ commit c3f4b766f03770f094fda6bda0a5882c0ebd4581
imgteam
parents:
diff changeset
142 args = parser.parse_args()
07d362a8af11 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/points2labelimage/ commit c3f4b766f03770f094fda6bda0a5882c0ebd4581
imgteam
parents:
diff changeset
143
2
714a57d6f3a1 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/points2labelimage/ commit 6fc9ab8db9ef72ac7ded30d7373768feeae9390d
imgteam
parents: 1
diff changeset
144 rasterize(
714a57d6f3a1 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/points2labelimage/ commit 6fc9ab8db9ef72ac7ded30d7373768feeae9390d
imgteam
parents: 1
diff changeset
145 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
146 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
147 (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
148 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
149 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
150 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
151 )