annotate points2label.py @ 5:4a49f74a3c14 draft default tip

planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/points2labelimage/ commit 9a40a5d1e1008c26cc327c6d163df2a1af22a1a0
author imgteam
date Mon, 12 May 2025 14:01:26 +0000
parents aef9911c0d5c
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
dcc8c1d6af48 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/points2labelimage/ commit c3f4b766f03770f094fda6bda0a5882c0ebd4581
imgteam
parents:
diff changeset
1 import argparse
5
4a49f74a3c14 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/points2labelimage/ commit 9a40a5d1e1008c26cc327c6d163df2a1af22a1a0
imgteam
parents: 4
diff changeset
2 import json
3
de611b3b5ae8 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/points2labelimage/ commit 6fc9ab8db9ef72ac7ded30d7373768feeae9390d
imgteam
parents: 2
diff changeset
3 import os
2
30ca5d5d03ec planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/points2labelimage/ commit 2286a6c9da88596349ed9d967c51541409c0a7bf
imgteam
parents: 1
diff changeset
4 import warnings
5
4a49f74a3c14 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/points2labelimage/ commit 9a40a5d1e1008c26cc327c6d163df2a1af22a1a0
imgteam
parents: 4
diff changeset
5 from typing import (
4a49f74a3c14 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/points2labelimage/ commit 9a40a5d1e1008c26cc327c6d163df2a1af22a1a0
imgteam
parents: 4
diff changeset
6 Dict,
4a49f74a3c14 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/points2labelimage/ commit 9a40a5d1e1008c26cc327c6d163df2a1af22a1a0
imgteam
parents: 4
diff changeset
7 List,
4a49f74a3c14 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/points2labelimage/ commit 9a40a5d1e1008c26cc327c6d163df2a1af22a1a0
imgteam
parents: 4
diff changeset
8 Tuple,
4a49f74a3c14 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/points2labelimage/ commit 9a40a5d1e1008c26cc327c6d163df2a1af22a1a0
imgteam
parents: 4
diff changeset
9 Union,
4a49f74a3c14 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/points2labelimage/ commit 9a40a5d1e1008c26cc327c6d163df2a1af22a1a0
imgteam
parents: 4
diff changeset
10 )
2
30ca5d5d03ec planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/points2labelimage/ commit 2286a6c9da88596349ed9d967c51541409c0a7bf
imgteam
parents: 1
diff changeset
11
3
de611b3b5ae8 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/points2labelimage/ commit 6fc9ab8db9ef72ac7ded30d7373768feeae9390d
imgteam
parents: 2
diff changeset
12 import giatools.pandas
0
dcc8c1d6af48 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/points2labelimage/ commit c3f4b766f03770f094fda6bda0a5882c0ebd4581
imgteam
parents:
diff changeset
13 import numpy as np
5
4a49f74a3c14 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/points2labelimage/ commit 9a40a5d1e1008c26cc327c6d163df2a1af22a1a0
imgteam
parents: 4
diff changeset
14 import numpy.typing as npt
2
30ca5d5d03ec planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/points2labelimage/ commit 2286a6c9da88596349ed9d967c51541409c0a7bf
imgteam
parents: 1
diff changeset
15 import pandas as pd
3
de611b3b5ae8 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/points2labelimage/ commit 6fc9ab8db9ef72ac7ded30d7373768feeae9390d
imgteam
parents: 2
diff changeset
16 import scipy.ndimage as ndi
0
dcc8c1d6af48 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/points2labelimage/ commit c3f4b766f03770f094fda6bda0a5882c0ebd4581
imgteam
parents:
diff changeset
17 import skimage.io
3
de611b3b5ae8 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/points2labelimage/ commit 6fc9ab8db9ef72ac7ded30d7373768feeae9390d
imgteam
parents: 2
diff changeset
18 import skimage.segmentation
2
30ca5d5d03ec planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/points2labelimage/ commit 2286a6c9da88596349ed9d967c51541409c0a7bf
imgteam
parents: 1
diff changeset
19
0
dcc8c1d6af48 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/points2labelimage/ commit c3f4b766f03770f094fda6bda0a5882c0ebd4581
imgteam
parents:
diff changeset
20
5
4a49f74a3c14 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/points2labelimage/ commit 9a40a5d1e1008c26cc327c6d163df2a1af22a1a0
imgteam
parents: 4
diff changeset
21 def is_rectangular(points: Union[List[Tuple[float, float]], npt.NDArray]) -> bool:
4a49f74a3c14 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/points2labelimage/ commit 9a40a5d1e1008c26cc327c6d163df2a1af22a1a0
imgteam
parents: 4
diff changeset
22 points = np.asarray(points)
4a49f74a3c14 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/points2labelimage/ commit 9a40a5d1e1008c26cc327c6d163df2a1af22a1a0
imgteam
parents: 4
diff changeset
23
4a49f74a3c14 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/points2labelimage/ commit 9a40a5d1e1008c26cc327c6d163df2a1af22a1a0
imgteam
parents: 4
diff changeset
24 # Rectangle must have 5 points, where first and last are identical
4a49f74a3c14 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/points2labelimage/ commit 9a40a5d1e1008c26cc327c6d163df2a1af22a1a0
imgteam
parents: 4
diff changeset
25 if len(points) != 5 or not (points[0] == points[-1]).all():
4a49f74a3c14 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/points2labelimage/ commit 9a40a5d1e1008c26cc327c6d163df2a1af22a1a0
imgteam
parents: 4
diff changeset
26 return False
4a49f74a3c14 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/points2labelimage/ commit 9a40a5d1e1008c26cc327c6d163df2a1af22a1a0
imgteam
parents: 4
diff changeset
27
4a49f74a3c14 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/points2labelimage/ commit 9a40a5d1e1008c26cc327c6d163df2a1af22a1a0
imgteam
parents: 4
diff changeset
28 # Check that all edges align with the axes
4a49f74a3c14 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/points2labelimage/ commit 9a40a5d1e1008c26cc327c6d163df2a1af22a1a0
imgteam
parents: 4
diff changeset
29 edges = points[1:] - points[:-1]
4a49f74a3c14 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/points2labelimage/ commit 9a40a5d1e1008c26cc327c6d163df2a1af22a1a0
imgteam
parents: 4
diff changeset
30 if any((edge == 0).sum() != 1 for edge in edges):
4a49f74a3c14 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/points2labelimage/ commit 9a40a5d1e1008c26cc327c6d163df2a1af22a1a0
imgteam
parents: 4
diff changeset
31 return False
4a49f74a3c14 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/points2labelimage/ commit 9a40a5d1e1008c26cc327c6d163df2a1af22a1a0
imgteam
parents: 4
diff changeset
32
4a49f74a3c14 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/points2labelimage/ commit 9a40a5d1e1008c26cc327c6d163df2a1af22a1a0
imgteam
parents: 4
diff changeset
33 # All checks have passed, the geometry is rectangular
4a49f74a3c14 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/points2labelimage/ commit 9a40a5d1e1008c26cc327c6d163df2a1af22a1a0
imgteam
parents: 4
diff changeset
34 return True
4a49f74a3c14 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/points2labelimage/ commit 9a40a5d1e1008c26cc327c6d163df2a1af22a1a0
imgteam
parents: 4
diff changeset
35
4a49f74a3c14 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/points2labelimage/ commit 9a40a5d1e1008c26cc327c6d163df2a1af22a1a0
imgteam
parents: 4
diff changeset
36
4a49f74a3c14 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/points2labelimage/ commit 9a40a5d1e1008c26cc327c6d163df2a1af22a1a0
imgteam
parents: 4
diff changeset
37 def geojson_to_tabular(geojson: Dict):
4a49f74a3c14 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/points2labelimage/ commit 9a40a5d1e1008c26cc327c6d163df2a1af22a1a0
imgteam
parents: 4
diff changeset
38 rows = []
4a49f74a3c14 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/points2labelimage/ commit 9a40a5d1e1008c26cc327c6d163df2a1af22a1a0
imgteam
parents: 4
diff changeset
39 labels = []
4a49f74a3c14 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/points2labelimage/ commit 9a40a5d1e1008c26cc327c6d163df2a1af22a1a0
imgteam
parents: 4
diff changeset
40 for feature in geojson['features']:
4a49f74a3c14 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/points2labelimage/ commit 9a40a5d1e1008c26cc327c6d163df2a1af22a1a0
imgteam
parents: 4
diff changeset
41 assert feature['geometry']['type'].lower() == 'polygon', (
4a49f74a3c14 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/points2labelimage/ commit 9a40a5d1e1008c26cc327c6d163df2a1af22a1a0
imgteam
parents: 4
diff changeset
42 f'Unsupported geometry type: "{feature["geometry"]["type"]}"'
4a49f74a3c14 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/points2labelimage/ commit 9a40a5d1e1008c26cc327c6d163df2a1af22a1a0
imgteam
parents: 4
diff changeset
43 )
4a49f74a3c14 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/points2labelimage/ commit 9a40a5d1e1008c26cc327c6d163df2a1af22a1a0
imgteam
parents: 4
diff changeset
44 coords = feature['geometry']['coordinates'][0]
4a49f74a3c14 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/points2labelimage/ commit 9a40a5d1e1008c26cc327c6d163df2a1af22a1a0
imgteam
parents: 4
diff changeset
45
4a49f74a3c14 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/points2labelimage/ commit 9a40a5d1e1008c26cc327c6d163df2a1af22a1a0
imgteam
parents: 4
diff changeset
46 # Properties and name (label) are optional
4a49f74a3c14 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/points2labelimage/ commit 9a40a5d1e1008c26cc327c6d163df2a1af22a1a0
imgteam
parents: 4
diff changeset
47 try:
4a49f74a3c14 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/points2labelimage/ commit 9a40a5d1e1008c26cc327c6d163df2a1af22a1a0
imgteam
parents: 4
diff changeset
48 label = feature['properties']['name']
4a49f74a3c14 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/points2labelimage/ commit 9a40a5d1e1008c26cc327c6d163df2a1af22a1a0
imgteam
parents: 4
diff changeset
49 except KeyError:
4a49f74a3c14 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/points2labelimage/ commit 9a40a5d1e1008c26cc327c6d163df2a1af22a1a0
imgteam
parents: 4
diff changeset
50 label = max(labels, default=0) + 1
4a49f74a3c14 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/points2labelimage/ commit 9a40a5d1e1008c26cc327c6d163df2a1af22a1a0
imgteam
parents: 4
diff changeset
51 labels.append(label)
4a49f74a3c14 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/points2labelimage/ commit 9a40a5d1e1008c26cc327c6d163df2a1af22a1a0
imgteam
parents: 4
diff changeset
52
4a49f74a3c14 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/points2labelimage/ commit 9a40a5d1e1008c26cc327c6d163df2a1af22a1a0
imgteam
parents: 4
diff changeset
53 # Read geometry
4a49f74a3c14 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/points2labelimage/ commit 9a40a5d1e1008c26cc327c6d163df2a1af22a1a0
imgteam
parents: 4
diff changeset
54 xs = [pt[0] for pt in coords]
4a49f74a3c14 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/points2labelimage/ commit 9a40a5d1e1008c26cc327c6d163df2a1af22a1a0
imgteam
parents: 4
diff changeset
55 ys = [pt[1] for pt in coords]
4a49f74a3c14 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/points2labelimage/ commit 9a40a5d1e1008c26cc327c6d163df2a1af22a1a0
imgteam
parents: 4
diff changeset
56
4a49f74a3c14 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/points2labelimage/ commit 9a40a5d1e1008c26cc327c6d163df2a1af22a1a0
imgteam
parents: 4
diff changeset
57 x = min(xs)
4a49f74a3c14 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/points2labelimage/ commit 9a40a5d1e1008c26cc327c6d163df2a1af22a1a0
imgteam
parents: 4
diff changeset
58 y = min(ys)
4a49f74a3c14 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/points2labelimage/ commit 9a40a5d1e1008c26cc327c6d163df2a1af22a1a0
imgteam
parents: 4
diff changeset
59
4a49f74a3c14 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/points2labelimage/ commit 9a40a5d1e1008c26cc327c6d163df2a1af22a1a0
imgteam
parents: 4
diff changeset
60 width = max(xs) + 1 - x
4a49f74a3c14 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/points2labelimage/ commit 9a40a5d1e1008c26cc327c6d163df2a1af22a1a0
imgteam
parents: 4
diff changeset
61 height = max(ys) + 1 - y
4a49f74a3c14 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/points2labelimage/ commit 9a40a5d1e1008c26cc327c6d163df2a1af22a1a0
imgteam
parents: 4
diff changeset
62
4a49f74a3c14 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/points2labelimage/ commit 9a40a5d1e1008c26cc327c6d163df2a1af22a1a0
imgteam
parents: 4
diff changeset
63 # Validate geometry (must be rectangular)
4a49f74a3c14 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/points2labelimage/ commit 9a40a5d1e1008c26cc327c6d163df2a1af22a1a0
imgteam
parents: 4
diff changeset
64 assert is_rectangular(list(zip(xs, ys)))
4a49f74a3c14 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/points2labelimage/ commit 9a40a5d1e1008c26cc327c6d163df2a1af22a1a0
imgteam
parents: 4
diff changeset
65
4a49f74a3c14 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/points2labelimage/ commit 9a40a5d1e1008c26cc327c6d163df2a1af22a1a0
imgteam
parents: 4
diff changeset
66 # Append the rectangle
4a49f74a3c14 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/points2labelimage/ commit 9a40a5d1e1008c26cc327c6d163df2a1af22a1a0
imgteam
parents: 4
diff changeset
67 rows.append({
4a49f74a3c14 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/points2labelimage/ commit 9a40a5d1e1008c26cc327c6d163df2a1af22a1a0
imgteam
parents: 4
diff changeset
68 'pos_x': x,
4a49f74a3c14 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/points2labelimage/ commit 9a40a5d1e1008c26cc327c6d163df2a1af22a1a0
imgteam
parents: 4
diff changeset
69 'pos_y': y,
4a49f74a3c14 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/points2labelimage/ commit 9a40a5d1e1008c26cc327c6d163df2a1af22a1a0
imgteam
parents: 4
diff changeset
70 'width': width,
4a49f74a3c14 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/points2labelimage/ commit 9a40a5d1e1008c26cc327c6d163df2a1af22a1a0
imgteam
parents: 4
diff changeset
71 'height': height,
4a49f74a3c14 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/points2labelimage/ commit 9a40a5d1e1008c26cc327c6d163df2a1af22a1a0
imgteam
parents: 4
diff changeset
72 'label': label,
4a49f74a3c14 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/points2labelimage/ commit 9a40a5d1e1008c26cc327c6d163df2a1af22a1a0
imgteam
parents: 4
diff changeset
73 })
4a49f74a3c14 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/points2labelimage/ commit 9a40a5d1e1008c26cc327c6d163df2a1af22a1a0
imgteam
parents: 4
diff changeset
74 df = pd.DataFrame(rows)
4a49f74a3c14 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/points2labelimage/ commit 9a40a5d1e1008c26cc327c6d163df2a1af22a1a0
imgteam
parents: 4
diff changeset
75 point_file = './point_file.tabular'
4a49f74a3c14 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/points2labelimage/ commit 9a40a5d1e1008c26cc327c6d163df2a1af22a1a0
imgteam
parents: 4
diff changeset
76 df.to_csv(point_file, sep='\t', index=False)
4a49f74a3c14 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/points2labelimage/ commit 9a40a5d1e1008c26cc327c6d163df2a1af22a1a0
imgteam
parents: 4
diff changeset
77 return point_file
4a49f74a3c14 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/points2labelimage/ commit 9a40a5d1e1008c26cc327c6d163df2a1af22a1a0
imgteam
parents: 4
diff changeset
78
4a49f74a3c14 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/points2labelimage/ commit 9a40a5d1e1008c26cc327c6d163df2a1af22a1a0
imgteam
parents: 4
diff changeset
79
3
de611b3b5ae8 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/points2labelimage/ commit 6fc9ab8db9ef72ac7ded30d7373768feeae9390d
imgteam
parents: 2
diff changeset
80 def rasterize(point_file, out_file, shape, has_header=False, swap_xy=False, bg_value=0, fg_value=None):
0
dcc8c1d6af48 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/points2labelimage/ commit c3f4b766f03770f094fda6bda0a5882c0ebd4581
imgteam
parents:
diff changeset
81
3
de611b3b5ae8 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/points2labelimage/ commit 6fc9ab8db9ef72ac7ded30d7373768feeae9390d
imgteam
parents: 2
diff changeset
82 img = np.full(shape, dtype=np.uint16, fill_value=bg_value)
de611b3b5ae8 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/points2labelimage/ commit 6fc9ab8db9ef72ac7ded30d7373768feeae9390d
imgteam
parents: 2
diff changeset
83 if os.path.exists(point_file) and os.path.getsize(point_file) > 0:
de611b3b5ae8 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/points2labelimage/ commit 6fc9ab8db9ef72ac7ded30d7373768feeae9390d
imgteam
parents: 2
diff changeset
84
de611b3b5ae8 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/points2labelimage/ commit 6fc9ab8db9ef72ac7ded30d7373768feeae9390d
imgteam
parents: 2
diff changeset
85 # Read the tabular file with information from the header
0
dcc8c1d6af48 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/points2labelimage/ commit c3f4b766f03770f094fda6bda0a5882c0ebd4581
imgteam
parents:
diff changeset
86 if has_header:
3
de611b3b5ae8 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/points2labelimage/ commit 6fc9ab8db9ef72ac7ded30d7373768feeae9390d
imgteam
parents: 2
diff changeset
87 df = pd.read_csv(point_file, delimiter='\t')
de611b3b5ae8 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/points2labelimage/ commit 6fc9ab8db9ef72ac7ded30d7373768feeae9390d
imgteam
parents: 2
diff changeset
88
de611b3b5ae8 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/points2labelimage/ commit 6fc9ab8db9ef72ac7ded30d7373768feeae9390d
imgteam
parents: 2
diff changeset
89 pos_x_column = giatools.pandas.find_column(df, ['pos_x', 'POS_X'])
de611b3b5ae8 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/points2labelimage/ commit 6fc9ab8db9ef72ac7ded30d7373768feeae9390d
imgteam
parents: 2
diff changeset
90 pos_y_column = giatools.pandas.find_column(df, ['pos_y', 'POS_Y'])
de611b3b5ae8 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/points2labelimage/ commit 6fc9ab8db9ef72ac7ded30d7373768feeae9390d
imgteam
parents: 2
diff changeset
91 pos_x_list = df[pos_x_column].round().astype(int)
de611b3b5ae8 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/points2labelimage/ commit 6fc9ab8db9ef72ac7ded30d7373768feeae9390d
imgteam
parents: 2
diff changeset
92 pos_y_list = df[pos_y_column].round().astype(int)
de611b3b5ae8 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/points2labelimage/ commit 6fc9ab8db9ef72ac7ded30d7373768feeae9390d
imgteam
parents: 2
diff changeset
93 assert len(pos_x_list) == len(pos_y_list)
de611b3b5ae8 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/points2labelimage/ commit 6fc9ab8db9ef72ac7ded30d7373768feeae9390d
imgteam
parents: 2
diff changeset
94
de611b3b5ae8 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/points2labelimage/ commit 6fc9ab8db9ef72ac7ded30d7373768feeae9390d
imgteam
parents: 2
diff changeset
95 try:
de611b3b5ae8 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/points2labelimage/ commit 6fc9ab8db9ef72ac7ded30d7373768feeae9390d
imgteam
parents: 2
diff changeset
96 radius_column = giatools.pandas.find_column(df, ['radius', 'RADIUS'])
de611b3b5ae8 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/points2labelimage/ commit 6fc9ab8db9ef72ac7ded30d7373768feeae9390d
imgteam
parents: 2
diff changeset
97 radius_list = df[radius_column]
de611b3b5ae8 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/points2labelimage/ commit 6fc9ab8db9ef72ac7ded30d7373768feeae9390d
imgteam
parents: 2
diff changeset
98 assert len(pos_x_list) == len(radius_list)
de611b3b5ae8 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/points2labelimage/ commit 6fc9ab8db9ef72ac7ded30d7373768feeae9390d
imgteam
parents: 2
diff changeset
99 except KeyError:
de611b3b5ae8 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/points2labelimage/ commit 6fc9ab8db9ef72ac7ded30d7373768feeae9390d
imgteam
parents: 2
diff changeset
100 radius_list = [0] * len(pos_x_list)
de611b3b5ae8 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/points2labelimage/ commit 6fc9ab8db9ef72ac7ded30d7373768feeae9390d
imgteam
parents: 2
diff changeset
101
de611b3b5ae8 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/points2labelimage/ commit 6fc9ab8db9ef72ac7ded30d7373768feeae9390d
imgteam
parents: 2
diff changeset
102 try:
4
aef9911c0d5c planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/points2labelimage/ commit 78614a9010c2ca0e1fa5973639c05ab74bcdb148
imgteam
parents: 3
diff changeset
103 width_column = giatools.pandas.find_column(df, ['width', 'WIDTH'])
aef9911c0d5c planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/points2labelimage/ commit 78614a9010c2ca0e1fa5973639c05ab74bcdb148
imgteam
parents: 3
diff changeset
104 height_column = giatools.pandas.find_column(df, ['height', 'HEIGHT'])
aef9911c0d5c planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/points2labelimage/ commit 78614a9010c2ca0e1fa5973639c05ab74bcdb148
imgteam
parents: 3
diff changeset
105 width_list = df[width_column]
aef9911c0d5c planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/points2labelimage/ commit 78614a9010c2ca0e1fa5973639c05ab74bcdb148
imgteam
parents: 3
diff changeset
106 height_list = df[height_column]
aef9911c0d5c planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/points2labelimage/ commit 78614a9010c2ca0e1fa5973639c05ab74bcdb148
imgteam
parents: 3
diff changeset
107 assert len(pos_x_list) == len(width_list)
aef9911c0d5c planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/points2labelimage/ commit 78614a9010c2ca0e1fa5973639c05ab74bcdb148
imgteam
parents: 3
diff changeset
108 assert len(pos_x_list) == len(height_list)
aef9911c0d5c planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/points2labelimage/ commit 78614a9010c2ca0e1fa5973639c05ab74bcdb148
imgteam
parents: 3
diff changeset
109 except KeyError:
aef9911c0d5c planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/points2labelimage/ commit 78614a9010c2ca0e1fa5973639c05ab74bcdb148
imgteam
parents: 3
diff changeset
110 width_list = [0] * len(pos_x_list)
aef9911c0d5c planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/points2labelimage/ commit 78614a9010c2ca0e1fa5973639c05ab74bcdb148
imgteam
parents: 3
diff changeset
111 height_list = [0] * len(pos_x_list)
aef9911c0d5c planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/points2labelimage/ commit 78614a9010c2ca0e1fa5973639c05ab74bcdb148
imgteam
parents: 3
diff changeset
112
aef9911c0d5c planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/points2labelimage/ commit 78614a9010c2ca0e1fa5973639c05ab74bcdb148
imgteam
parents: 3
diff changeset
113 try:
3
de611b3b5ae8 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/points2labelimage/ commit 6fc9ab8db9ef72ac7ded30d7373768feeae9390d
imgteam
parents: 2
diff changeset
114 label_column = giatools.pandas.find_column(df, ['label', 'LABEL'])
de611b3b5ae8 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/points2labelimage/ commit 6fc9ab8db9ef72ac7ded30d7373768feeae9390d
imgteam
parents: 2
diff changeset
115 label_list = df[label_column]
de611b3b5ae8 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/points2labelimage/ commit 6fc9ab8db9ef72ac7ded30d7373768feeae9390d
imgteam
parents: 2
diff changeset
116 assert len(pos_x_list) == len(label_list)
de611b3b5ae8 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/points2labelimage/ commit 6fc9ab8db9ef72ac7ded30d7373768feeae9390d
imgteam
parents: 2
diff changeset
117 except KeyError:
de611b3b5ae8 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/points2labelimage/ commit 6fc9ab8db9ef72ac7ded30d7373768feeae9390d
imgteam
parents: 2
diff changeset
118 label_list = list(range(1, len(pos_x_list) + 1))
de611b3b5ae8 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/points2labelimage/ commit 6fc9ab8db9ef72ac7ded30d7373768feeae9390d
imgteam
parents: 2
diff changeset
119
de611b3b5ae8 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/points2labelimage/ commit 6fc9ab8db9ef72ac7ded30d7373768feeae9390d
imgteam
parents: 2
diff changeset
120 # Read the tabular file without header
0
dcc8c1d6af48 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/points2labelimage/ commit c3f4b766f03770f094fda6bda0a5882c0ebd4581
imgteam
parents:
diff changeset
121 else:
3
de611b3b5ae8 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/points2labelimage/ commit 6fc9ab8db9ef72ac7ded30d7373768feeae9390d
imgteam
parents: 2
diff changeset
122 df = pd.read_csv(point_file, header=None, delimiter='\t')
de611b3b5ae8 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/points2labelimage/ commit 6fc9ab8db9ef72ac7ded30d7373768feeae9390d
imgteam
parents: 2
diff changeset
123 pos_x_list = df[0].round().astype(int)
de611b3b5ae8 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/points2labelimage/ commit 6fc9ab8db9ef72ac7ded30d7373768feeae9390d
imgteam
parents: 2
diff changeset
124 pos_y_list = df[1].round().astype(int)
de611b3b5ae8 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/points2labelimage/ commit 6fc9ab8db9ef72ac7ded30d7373768feeae9390d
imgteam
parents: 2
diff changeset
125 assert len(pos_x_list) == len(pos_y_list)
4
aef9911c0d5c planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/points2labelimage/ commit 78614a9010c2ca0e1fa5973639c05ab74bcdb148
imgteam
parents: 3
diff changeset
126 radius_list, width_list, height_list = [[0] * len(pos_x_list)] * 3
3
de611b3b5ae8 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/points2labelimage/ commit 6fc9ab8db9ef72ac7ded30d7373768feeae9390d
imgteam
parents: 2
diff changeset
127 label_list = list(range(1, len(pos_x_list) + 1))
de611b3b5ae8 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/points2labelimage/ commit 6fc9ab8db9ef72ac7ded30d7373768feeae9390d
imgteam
parents: 2
diff changeset
128
de611b3b5ae8 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/points2labelimage/ commit 6fc9ab8db9ef72ac7ded30d7373768feeae9390d
imgteam
parents: 2
diff changeset
129 # Optionally swap the coordinates
de611b3b5ae8 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/points2labelimage/ commit 6fc9ab8db9ef72ac7ded30d7373768feeae9390d
imgteam
parents: 2
diff changeset
130 if swap_xy:
de611b3b5ae8 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/points2labelimage/ commit 6fc9ab8db9ef72ac7ded30d7373768feeae9390d
imgteam
parents: 2
diff changeset
131 pos_x_list, pos_y_list = pos_y_list, pos_x_list
0
dcc8c1d6af48 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/points2labelimage/ commit c3f4b766f03770f094fda6bda0a5882c0ebd4581
imgteam
parents:
diff changeset
132
3
de611b3b5ae8 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/points2labelimage/ commit 6fc9ab8db9ef72ac7ded30d7373768feeae9390d
imgteam
parents: 2
diff changeset
133 # Perform the rasterization
4
aef9911c0d5c planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/points2labelimage/ commit 78614a9010c2ca0e1fa5973639c05ab74bcdb148
imgteam
parents: 3
diff changeset
134 for y, x, radius, width, height, label in zip(
aef9911c0d5c planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/points2labelimage/ commit 78614a9010c2ca0e1fa5973639c05ab74bcdb148
imgteam
parents: 3
diff changeset
135 pos_y_list, pos_x_list, radius_list, width_list, height_list, label_list,
aef9911c0d5c planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/points2labelimage/ commit 78614a9010c2ca0e1fa5973639c05ab74bcdb148
imgteam
parents: 3
diff changeset
136 ):
3
de611b3b5ae8 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/points2labelimage/ commit 6fc9ab8db9ef72ac7ded30d7373768feeae9390d
imgteam
parents: 2
diff changeset
137 if fg_value is not None:
de611b3b5ae8 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/points2labelimage/ commit 6fc9ab8db9ef72ac7ded30d7373768feeae9390d
imgteam
parents: 2
diff changeset
138 label = fg_value
de611b3b5ae8 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/points2labelimage/ commit 6fc9ab8db9ef72ac7ded30d7373768feeae9390d
imgteam
parents: 2
diff changeset
139
de611b3b5ae8 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/points2labelimage/ commit 6fc9ab8db9ef72ac7ded30d7373768feeae9390d
imgteam
parents: 2
diff changeset
140 if y < 0 or x < 0 or y >= shape[0] or x >= shape[1]:
de611b3b5ae8 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/points2labelimage/ commit 6fc9ab8db9ef72ac7ded30d7373768feeae9390d
imgteam
parents: 2
diff changeset
141 raise IndexError(f'The point x={x}, y={y} exceeds the bounds of the image (width: {shape[1]}, height: {shape[0]})')
de611b3b5ae8 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/points2labelimage/ commit 6fc9ab8db9ef72ac7ded30d7373768feeae9390d
imgteam
parents: 2
diff changeset
142
de611b3b5ae8 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/points2labelimage/ commit 6fc9ab8db9ef72ac7ded30d7373768feeae9390d
imgteam
parents: 2
diff changeset
143 # Rasterize circle and distribute overlapping image area
4
aef9911c0d5c planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/points2labelimage/ commit 78614a9010c2ca0e1fa5973639c05ab74bcdb148
imgteam
parents: 3
diff changeset
144 # Rasterize primitive geometry
aef9911c0d5c planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/points2labelimage/ commit 78614a9010c2ca0e1fa5973639c05ab74bcdb148
imgteam
parents: 3
diff changeset
145 if radius > 0 or (width > 0 and height > 0):
aef9911c0d5c planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/points2labelimage/ commit 78614a9010c2ca0e1fa5973639c05ab74bcdb148
imgteam
parents: 3
diff changeset
146
aef9911c0d5c planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/points2labelimage/ commit 78614a9010c2ca0e1fa5973639c05ab74bcdb148
imgteam
parents: 3
diff changeset
147 # Rasterize circle
aef9911c0d5c planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/points2labelimage/ commit 78614a9010c2ca0e1fa5973639c05ab74bcdb148
imgteam
parents: 3
diff changeset
148 if radius > 0:
aef9911c0d5c planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/points2labelimage/ commit 78614a9010c2ca0e1fa5973639c05ab74bcdb148
imgteam
parents: 3
diff changeset
149 mask = np.ones(shape, dtype=bool)
aef9911c0d5c planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/points2labelimage/ commit 78614a9010c2ca0e1fa5973639c05ab74bcdb148
imgteam
parents: 3
diff changeset
150 mask[y, x] = False
aef9911c0d5c planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/points2labelimage/ commit 78614a9010c2ca0e1fa5973639c05ab74bcdb148
imgteam
parents: 3
diff changeset
151 mask = (ndi.distance_transform_edt(mask) <= radius)
aef9911c0d5c planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/points2labelimage/ commit 78614a9010c2ca0e1fa5973639c05ab74bcdb148
imgteam
parents: 3
diff changeset
152 else:
aef9911c0d5c planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/points2labelimage/ commit 78614a9010c2ca0e1fa5973639c05ab74bcdb148
imgteam
parents: 3
diff changeset
153 mask = np.zeros(shape, dtype=bool)
aef9911c0d5c planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/points2labelimage/ commit 78614a9010c2ca0e1fa5973639c05ab74bcdb148
imgteam
parents: 3
diff changeset
154
aef9911c0d5c planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/points2labelimage/ commit 78614a9010c2ca0e1fa5973639c05ab74bcdb148
imgteam
parents: 3
diff changeset
155 # Rasterize rectangle
aef9911c0d5c planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/points2labelimage/ commit 78614a9010c2ca0e1fa5973639c05ab74bcdb148
imgteam
parents: 3
diff changeset
156 if width > 0 and height > 0:
aef9911c0d5c planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/points2labelimage/ commit 78614a9010c2ca0e1fa5973639c05ab74bcdb148
imgteam
parents: 3
diff changeset
157 mask[
aef9911c0d5c planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/points2labelimage/ commit 78614a9010c2ca0e1fa5973639c05ab74bcdb148
imgteam
parents: 3
diff changeset
158 y:min(shape[0], y + width),
aef9911c0d5c planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/points2labelimage/ commit 78614a9010c2ca0e1fa5973639c05ab74bcdb148
imgteam
parents: 3
diff changeset
159 x:min(shape[1], x + height)
aef9911c0d5c planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/points2labelimage/ commit 78614a9010c2ca0e1fa5973639c05ab74bcdb148
imgteam
parents: 3
diff changeset
160 ] = True
0
dcc8c1d6af48 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/points2labelimage/ commit c3f4b766f03770f094fda6bda0a5882c0ebd4581
imgteam
parents:
diff changeset
161
3
de611b3b5ae8 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/points2labelimage/ commit 6fc9ab8db9ef72ac7ded30d7373768feeae9390d
imgteam
parents: 2
diff changeset
162 # Compute the overlap (pretend there is none if the rasterization is binary)
de611b3b5ae8 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/points2labelimage/ commit 6fc9ab8db9ef72ac7ded30d7373768feeae9390d
imgteam
parents: 2
diff changeset
163 if fg_value is None:
de611b3b5ae8 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/points2labelimage/ commit 6fc9ab8db9ef72ac7ded30d7373768feeae9390d
imgteam
parents: 2
diff changeset
164 overlap = np.logical_and(img > 0, mask)
de611b3b5ae8 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/points2labelimage/ commit 6fc9ab8db9ef72ac7ded30d7373768feeae9390d
imgteam
parents: 2
diff changeset
165 else:
de611b3b5ae8 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/points2labelimage/ commit 6fc9ab8db9ef72ac7ded30d7373768feeae9390d
imgteam
parents: 2
diff changeset
166 overlap = np.zeros(shape, dtype=bool)
de611b3b5ae8 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/points2labelimage/ commit 6fc9ab8db9ef72ac7ded30d7373768feeae9390d
imgteam
parents: 2
diff changeset
167
de611b3b5ae8 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/points2labelimage/ commit 6fc9ab8db9ef72ac7ded30d7373768feeae9390d
imgteam
parents: 2
diff changeset
168 # Rasterize the part of the circle which is disjoint from other foreground.
de611b3b5ae8 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/points2labelimage/ commit 6fc9ab8db9ef72ac7ded30d7373768feeae9390d
imgteam
parents: 2
diff changeset
169 #
de611b3b5ae8 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/points2labelimage/ commit 6fc9ab8db9ef72ac7ded30d7373768feeae9390d
imgteam
parents: 2
diff changeset
170 # In the current implementation, the result depends on the order of the rasterized circles if somewhere
de611b3b5ae8 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/points2labelimage/ commit 6fc9ab8db9ef72ac7ded30d7373768feeae9390d
imgteam
parents: 2
diff changeset
171 # more than two circles overlap. This is probably negligable for most applications. To achieve results
de611b3b5ae8 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/points2labelimage/ commit 6fc9ab8db9ef72ac7ded30d7373768feeae9390d
imgteam
parents: 2
diff changeset
172 # that are invariant to the order, first all circles would need to be rasterized independently, and
de611b3b5ae8 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/points2labelimage/ commit 6fc9ab8db9ef72ac7ded30d7373768feeae9390d
imgteam
parents: 2
diff changeset
173 # then blended together. This, however, would either strongly increase the memory consumption, or
de611b3b5ae8 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/points2labelimage/ commit 6fc9ab8db9ef72ac7ded30d7373768feeae9390d
imgteam
parents: 2
diff changeset
174 # require a more complex implementation which exploits the sparsity of the rasterized masks.
de611b3b5ae8 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/points2labelimage/ commit 6fc9ab8db9ef72ac7ded30d7373768feeae9390d
imgteam
parents: 2
diff changeset
175 #
de611b3b5ae8 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/points2labelimage/ commit 6fc9ab8db9ef72ac7ded30d7373768feeae9390d
imgteam
parents: 2
diff changeset
176 disjoint_mask = np.logical_xor(mask, overlap)
de611b3b5ae8 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/points2labelimage/ commit 6fc9ab8db9ef72ac7ded30d7373768feeae9390d
imgteam
parents: 2
diff changeset
177 if disjoint_mask.any():
de611b3b5ae8 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/points2labelimage/ commit 6fc9ab8db9ef72ac7ded30d7373768feeae9390d
imgteam
parents: 2
diff changeset
178 img[disjoint_mask] = label
de611b3b5ae8 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/points2labelimage/ commit 6fc9ab8db9ef72ac7ded30d7373768feeae9390d
imgteam
parents: 2
diff changeset
179
de611b3b5ae8 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/points2labelimage/ commit 6fc9ab8db9ef72ac7ded30d7373768feeae9390d
imgteam
parents: 2
diff changeset
180 # Distribute the remaining part of the circle
de611b3b5ae8 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/points2labelimage/ commit 6fc9ab8db9ef72ac7ded30d7373768feeae9390d
imgteam
parents: 2
diff changeset
181 if overlap.any():
de611b3b5ae8 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/points2labelimage/ commit 6fc9ab8db9ef72ac7ded30d7373768feeae9390d
imgteam
parents: 2
diff changeset
182 dist = ndi.distance_transform_edt(overlap)
de611b3b5ae8 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/points2labelimage/ commit 6fc9ab8db9ef72ac7ded30d7373768feeae9390d
imgteam
parents: 2
diff changeset
183 foreground = (img > 0)
de611b3b5ae8 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/points2labelimage/ commit 6fc9ab8db9ef72ac7ded30d7373768feeae9390d
imgteam
parents: 2
diff changeset
184 img[overlap] = 0
de611b3b5ae8 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/points2labelimage/ commit 6fc9ab8db9ef72ac7ded30d7373768feeae9390d
imgteam
parents: 2
diff changeset
185 img = skimage.segmentation.watershed(dist, img, mask=foreground)
de611b3b5ae8 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/points2labelimage/ commit 6fc9ab8db9ef72ac7ded30d7373768feeae9390d
imgteam
parents: 2
diff changeset
186
de611b3b5ae8 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/points2labelimage/ commit 6fc9ab8db9ef72ac7ded30d7373768feeae9390d
imgteam
parents: 2
diff changeset
187 # Rasterize point (there is no overlapping area to be distributed)
de611b3b5ae8 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/points2labelimage/ commit 6fc9ab8db9ef72ac7ded30d7373768feeae9390d
imgteam
parents: 2
diff changeset
188 else:
de611b3b5ae8 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/points2labelimage/ commit 6fc9ab8db9ef72ac7ded30d7373768feeae9390d
imgteam
parents: 2
diff changeset
189 img[y, x] = label
de611b3b5ae8 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/points2labelimage/ commit 6fc9ab8db9ef72ac7ded30d7373768feeae9390d
imgteam
parents: 2
diff changeset
190
0
dcc8c1d6af48 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/points2labelimage/ commit c3f4b766f03770f094fda6bda0a5882c0ebd4581
imgteam
parents:
diff changeset
191 else:
5
4a49f74a3c14 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/points2labelimage/ commit 9a40a5d1e1008c26cc327c6d163df2a1af22a1a0
imgteam
parents: 4
diff changeset
192 raise Exception('{} is empty or does not exist.'.format(point_file)) # appropriate built-in error?
3
de611b3b5ae8 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/points2labelimage/ commit 6fc9ab8db9ef72ac7ded30d7373768feeae9390d
imgteam
parents: 2
diff changeset
193
de611b3b5ae8 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/points2labelimage/ commit 6fc9ab8db9ef72ac7ded30d7373768feeae9390d
imgteam
parents: 2
diff changeset
194 with warnings.catch_warnings():
de611b3b5ae8 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/points2labelimage/ commit 6fc9ab8db9ef72ac7ded30d7373768feeae9390d
imgteam
parents: 2
diff changeset
195 warnings.simplefilter("ignore")
de611b3b5ae8 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/points2labelimage/ commit 6fc9ab8db9ef72ac7ded30d7373768feeae9390d
imgteam
parents: 2
diff changeset
196 skimage.io.imsave(out_file, img, plugin='tifffile') # otherwise we get problems with the .dat extension
0
dcc8c1d6af48 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/points2labelimage/ commit c3f4b766f03770f094fda6bda0a5882c0ebd4581
imgteam
parents:
diff changeset
197
2
30ca5d5d03ec planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/points2labelimage/ commit 2286a6c9da88596349ed9d967c51541409c0a7bf
imgteam
parents: 1
diff changeset
198
5
4a49f74a3c14 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/points2labelimage/ commit 9a40a5d1e1008c26cc327c6d163df2a1af22a1a0
imgteam
parents: 4
diff changeset
199 if __name__ == '__main__':
0
dcc8c1d6af48 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/points2labelimage/ commit c3f4b766f03770f094fda6bda0a5882c0ebd4581
imgteam
parents:
diff changeset
200 parser = argparse.ArgumentParser()
5
4a49f74a3c14 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/points2labelimage/ commit 9a40a5d1e1008c26cc327c6d163df2a1af22a1a0
imgteam
parents: 4
diff changeset
201 parser.add_argument('in_file', type=argparse.FileType('r'), help='Input point file or GeoJSON file')
3
de611b3b5ae8 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/points2labelimage/ commit 6fc9ab8db9ef72ac7ded30d7373768feeae9390d
imgteam
parents: 2
diff changeset
202 parser.add_argument('out_file', type=str, help='out file (TIFF)')
de611b3b5ae8 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/points2labelimage/ commit 6fc9ab8db9ef72ac7ded30d7373768feeae9390d
imgteam
parents: 2
diff changeset
203 parser.add_argument('shapex', type=int, help='shapex')
de611b3b5ae8 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/points2labelimage/ commit 6fc9ab8db9ef72ac7ded30d7373768feeae9390d
imgteam
parents: 2
diff changeset
204 parser.add_argument('shapey', type=int, help='shapey')
de611b3b5ae8 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/points2labelimage/ commit 6fc9ab8db9ef72ac7ded30d7373768feeae9390d
imgteam
parents: 2
diff changeset
205 parser.add_argument('--has_header', dest='has_header', default=False, help='set True if point file has header')
de611b3b5ae8 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/points2labelimage/ commit 6fc9ab8db9ef72ac7ded30d7373768feeae9390d
imgteam
parents: 2
diff changeset
206 parser.add_argument('--swap_xy', dest='swap_xy', default=False, help='Swap X and Y coordinates')
de611b3b5ae8 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/points2labelimage/ commit 6fc9ab8db9ef72ac7ded30d7373768feeae9390d
imgteam
parents: 2
diff changeset
207 parser.add_argument('--binary', dest='binary', default=False, help='Produce binary image')
de611b3b5ae8 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/points2labelimage/ commit 6fc9ab8db9ef72ac7ded30d7373768feeae9390d
imgteam
parents: 2
diff changeset
208
0
dcc8c1d6af48 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/points2labelimage/ commit c3f4b766f03770f094fda6bda0a5882c0ebd4581
imgteam
parents:
diff changeset
209 args = parser.parse_args()
dcc8c1d6af48 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/points2labelimage/ commit c3f4b766f03770f094fda6bda0a5882c0ebd4581
imgteam
parents:
diff changeset
210
5
4a49f74a3c14 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/points2labelimage/ commit 9a40a5d1e1008c26cc327c6d163df2a1af22a1a0
imgteam
parents: 4
diff changeset
211 point_file = args.in_file.name
4a49f74a3c14 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/points2labelimage/ commit 9a40a5d1e1008c26cc327c6d163df2a1af22a1a0
imgteam
parents: 4
diff changeset
212 has_header = args.has_header
4a49f74a3c14 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/points2labelimage/ commit 9a40a5d1e1008c26cc327c6d163df2a1af22a1a0
imgteam
parents: 4
diff changeset
213
4a49f74a3c14 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/points2labelimage/ commit 9a40a5d1e1008c26cc327c6d163df2a1af22a1a0
imgteam
parents: 4
diff changeset
214 try:
4a49f74a3c14 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/points2labelimage/ commit 9a40a5d1e1008c26cc327c6d163df2a1af22a1a0
imgteam
parents: 4
diff changeset
215 with open(args.in_file.name, 'r') as f:
4a49f74a3c14 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/points2labelimage/ commit 9a40a5d1e1008c26cc327c6d163df2a1af22a1a0
imgteam
parents: 4
diff changeset
216 content = json.load(f)
4a49f74a3c14 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/points2labelimage/ commit 9a40a5d1e1008c26cc327c6d163df2a1af22a1a0
imgteam
parents: 4
diff changeset
217 if isinstance(content, dict) and content.get('type') == 'FeatureCollection' and isinstance(content.get('features'), list):
4a49f74a3c14 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/points2labelimage/ commit 9a40a5d1e1008c26cc327c6d163df2a1af22a1a0
imgteam
parents: 4
diff changeset
218 point_file = geojson_to_tabular(content)
4a49f74a3c14 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/points2labelimage/ commit 9a40a5d1e1008c26cc327c6d163df2a1af22a1a0
imgteam
parents: 4
diff changeset
219 has_header = True # header included in the converted file
4a49f74a3c14 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/points2labelimage/ commit 9a40a5d1e1008c26cc327c6d163df2a1af22a1a0
imgteam
parents: 4
diff changeset
220 else:
4a49f74a3c14 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/points2labelimage/ commit 9a40a5d1e1008c26cc327c6d163df2a1af22a1a0
imgteam
parents: 4
diff changeset
221 raise ValueError('Input is a JSON file but not a valid GeoJSON file')
4a49f74a3c14 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/points2labelimage/ commit 9a40a5d1e1008c26cc327c6d163df2a1af22a1a0
imgteam
parents: 4
diff changeset
222 except json.JSONDecodeError:
4a49f74a3c14 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/points2labelimage/ commit 9a40a5d1e1008c26cc327c6d163df2a1af22a1a0
imgteam
parents: 4
diff changeset
223 print('Input is not a valid JSON file. Assuming it a tabular file.')
4a49f74a3c14 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/points2labelimage/ commit 9a40a5d1e1008c26cc327c6d163df2a1af22a1a0
imgteam
parents: 4
diff changeset
224
3
de611b3b5ae8 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/points2labelimage/ commit 6fc9ab8db9ef72ac7ded30d7373768feeae9390d
imgteam
parents: 2
diff changeset
225 rasterize(
5
4a49f74a3c14 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/points2labelimage/ commit 9a40a5d1e1008c26cc327c6d163df2a1af22a1a0
imgteam
parents: 4
diff changeset
226 point_file,
3
de611b3b5ae8 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/points2labelimage/ commit 6fc9ab8db9ef72ac7ded30d7373768feeae9390d
imgteam
parents: 2
diff changeset
227 args.out_file,
de611b3b5ae8 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/points2labelimage/ commit 6fc9ab8db9ef72ac7ded30d7373768feeae9390d
imgteam
parents: 2
diff changeset
228 (args.shapey, args.shapex),
5
4a49f74a3c14 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/points2labelimage/ commit 9a40a5d1e1008c26cc327c6d163df2a1af22a1a0
imgteam
parents: 4
diff changeset
229 has_header=has_header,
3
de611b3b5ae8 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/points2labelimage/ commit 6fc9ab8db9ef72ac7ded30d7373768feeae9390d
imgteam
parents: 2
diff changeset
230 swap_xy=args.swap_xy,
de611b3b5ae8 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/points2labelimage/ commit 6fc9ab8db9ef72ac7ded30d7373768feeae9390d
imgteam
parents: 2
diff changeset
231 fg_value=0xffff if args.binary else None,
de611b3b5ae8 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/points2labelimage/ commit 6fc9ab8db9ef72ac7ded30d7373768feeae9390d
imgteam
parents: 2
diff changeset
232 )