Mercurial > repos > imgteam > points2labelimage
annotate points2label.py @ 4:64c155acb864 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:09 +0000 |
parents | 2ae122d5d85a |
children |
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 |
4
64c155acb864
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/points2labelimage/ commit 9a40a5d1e1008c26cc327c6d163df2a1af22a1a0
imgteam
parents:
3
diff
changeset
|
2 import json |
2
714a57d6f3a1
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/points2labelimage/ commit 6fc9ab8db9ef72ac7ded30d7373768feeae9390d
imgteam
parents:
1
diff
changeset
|
3 import os |
0
07d362a8af11
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/points2labelimage/ commit c3f4b766f03770f094fda6bda0a5882c0ebd4581
imgteam
parents:
diff
changeset
|
4 import warnings |
4
64c155acb864
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/points2labelimage/ commit 9a40a5d1e1008c26cc327c6d163df2a1af22a1a0
imgteam
parents:
3
diff
changeset
|
5 from typing import ( |
64c155acb864
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/points2labelimage/ commit 9a40a5d1e1008c26cc327c6d163df2a1af22a1a0
imgteam
parents:
3
diff
changeset
|
6 Dict, |
64c155acb864
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/points2labelimage/ commit 9a40a5d1e1008c26cc327c6d163df2a1af22a1a0
imgteam
parents:
3
diff
changeset
|
7 List, |
64c155acb864
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/points2labelimage/ commit 9a40a5d1e1008c26cc327c6d163df2a1af22a1a0
imgteam
parents:
3
diff
changeset
|
8 Tuple, |
64c155acb864
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/points2labelimage/ commit 9a40a5d1e1008c26cc327c6d163df2a1af22a1a0
imgteam
parents:
3
diff
changeset
|
9 Union, |
64c155acb864
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/points2labelimage/ commit 9a40a5d1e1008c26cc327c6d163df2a1af22a1a0
imgteam
parents:
3
diff
changeset
|
10 ) |
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 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
|
13 import numpy as np |
4
64c155acb864
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/points2labelimage/ commit 9a40a5d1e1008c26cc327c6d163df2a1af22a1a0
imgteam
parents:
3
diff
changeset
|
14 import numpy.typing as npt |
2
714a57d6f3a1
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/points2labelimage/ commit 6fc9ab8db9ef72ac7ded30d7373768feeae9390d
imgteam
parents:
1
diff
changeset
|
15 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
|
16 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
|
17 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
|
18 import skimage.segmentation |
0
07d362a8af11
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/points2labelimage/ commit c3f4b766f03770f094fda6bda0a5882c0ebd4581
imgteam
parents:
diff
changeset
|
19 |
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 |
4
64c155acb864
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/points2labelimage/ commit 9a40a5d1e1008c26cc327c6d163df2a1af22a1a0
imgteam
parents:
3
diff
changeset
|
21 def is_rectangular(points: Union[List[Tuple[float, float]], npt.NDArray]) -> bool: |
64c155acb864
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/points2labelimage/ commit 9a40a5d1e1008c26cc327c6d163df2a1af22a1a0
imgteam
parents:
3
diff
changeset
|
22 points = np.asarray(points) |
64c155acb864
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/points2labelimage/ commit 9a40a5d1e1008c26cc327c6d163df2a1af22a1a0
imgteam
parents:
3
diff
changeset
|
23 |
64c155acb864
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/points2labelimage/ commit 9a40a5d1e1008c26cc327c6d163df2a1af22a1a0
imgteam
parents:
3
diff
changeset
|
24 # Rectangle must have 5 points, where first and last are identical |
64c155acb864
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/points2labelimage/ commit 9a40a5d1e1008c26cc327c6d163df2a1af22a1a0
imgteam
parents:
3
diff
changeset
|
25 if len(points) != 5 or not (points[0] == points[-1]).all(): |
64c155acb864
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/points2labelimage/ commit 9a40a5d1e1008c26cc327c6d163df2a1af22a1a0
imgteam
parents:
3
diff
changeset
|
26 return False |
64c155acb864
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/points2labelimage/ commit 9a40a5d1e1008c26cc327c6d163df2a1af22a1a0
imgteam
parents:
3
diff
changeset
|
27 |
64c155acb864
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/points2labelimage/ commit 9a40a5d1e1008c26cc327c6d163df2a1af22a1a0
imgteam
parents:
3
diff
changeset
|
28 # Check that all edges align with the axes |
64c155acb864
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/points2labelimage/ commit 9a40a5d1e1008c26cc327c6d163df2a1af22a1a0
imgteam
parents:
3
diff
changeset
|
29 edges = points[1:] - points[:-1] |
64c155acb864
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/points2labelimage/ commit 9a40a5d1e1008c26cc327c6d163df2a1af22a1a0
imgteam
parents:
3
diff
changeset
|
30 if any((edge == 0).sum() != 1 for edge in edges): |
64c155acb864
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/points2labelimage/ commit 9a40a5d1e1008c26cc327c6d163df2a1af22a1a0
imgteam
parents:
3
diff
changeset
|
31 return False |
64c155acb864
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/points2labelimage/ commit 9a40a5d1e1008c26cc327c6d163df2a1af22a1a0
imgteam
parents:
3
diff
changeset
|
32 |
64c155acb864
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/points2labelimage/ commit 9a40a5d1e1008c26cc327c6d163df2a1af22a1a0
imgteam
parents:
3
diff
changeset
|
33 # All checks have passed, the geometry is rectangular |
64c155acb864
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/points2labelimage/ commit 9a40a5d1e1008c26cc327c6d163df2a1af22a1a0
imgteam
parents:
3
diff
changeset
|
34 return True |
64c155acb864
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/points2labelimage/ commit 9a40a5d1e1008c26cc327c6d163df2a1af22a1a0
imgteam
parents:
3
diff
changeset
|
35 |
64c155acb864
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/points2labelimage/ commit 9a40a5d1e1008c26cc327c6d163df2a1af22a1a0
imgteam
parents:
3
diff
changeset
|
36 |
64c155acb864
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/points2labelimage/ commit 9a40a5d1e1008c26cc327c6d163df2a1af22a1a0
imgteam
parents:
3
diff
changeset
|
37 def geojson_to_tabular(geojson: Dict): |
64c155acb864
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/points2labelimage/ commit 9a40a5d1e1008c26cc327c6d163df2a1af22a1a0
imgteam
parents:
3
diff
changeset
|
38 rows = [] |
64c155acb864
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/points2labelimage/ commit 9a40a5d1e1008c26cc327c6d163df2a1af22a1a0
imgteam
parents:
3
diff
changeset
|
39 labels = [] |
64c155acb864
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/points2labelimage/ commit 9a40a5d1e1008c26cc327c6d163df2a1af22a1a0
imgteam
parents:
3
diff
changeset
|
40 for feature in geojson['features']: |
64c155acb864
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/points2labelimage/ commit 9a40a5d1e1008c26cc327c6d163df2a1af22a1a0
imgteam
parents:
3
diff
changeset
|
41 assert feature['geometry']['type'].lower() == 'polygon', ( |
64c155acb864
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/points2labelimage/ commit 9a40a5d1e1008c26cc327c6d163df2a1af22a1a0
imgteam
parents:
3
diff
changeset
|
42 f'Unsupported geometry type: "{feature["geometry"]["type"]}"' |
64c155acb864
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/points2labelimage/ commit 9a40a5d1e1008c26cc327c6d163df2a1af22a1a0
imgteam
parents:
3
diff
changeset
|
43 ) |
64c155acb864
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/points2labelimage/ commit 9a40a5d1e1008c26cc327c6d163df2a1af22a1a0
imgteam
parents:
3
diff
changeset
|
44 coords = feature['geometry']['coordinates'][0] |
64c155acb864
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/points2labelimage/ commit 9a40a5d1e1008c26cc327c6d163df2a1af22a1a0
imgteam
parents:
3
diff
changeset
|
45 |
64c155acb864
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/points2labelimage/ commit 9a40a5d1e1008c26cc327c6d163df2a1af22a1a0
imgteam
parents:
3
diff
changeset
|
46 # Properties and name (label) are optional |
64c155acb864
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/points2labelimage/ commit 9a40a5d1e1008c26cc327c6d163df2a1af22a1a0
imgteam
parents:
3
diff
changeset
|
47 try: |
64c155acb864
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/points2labelimage/ commit 9a40a5d1e1008c26cc327c6d163df2a1af22a1a0
imgteam
parents:
3
diff
changeset
|
48 label = feature['properties']['name'] |
64c155acb864
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/points2labelimage/ commit 9a40a5d1e1008c26cc327c6d163df2a1af22a1a0
imgteam
parents:
3
diff
changeset
|
49 except KeyError: |
64c155acb864
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/points2labelimage/ commit 9a40a5d1e1008c26cc327c6d163df2a1af22a1a0
imgteam
parents:
3
diff
changeset
|
50 label = max(labels, default=0) + 1 |
64c155acb864
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/points2labelimage/ commit 9a40a5d1e1008c26cc327c6d163df2a1af22a1a0
imgteam
parents:
3
diff
changeset
|
51 labels.append(label) |
64c155acb864
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/points2labelimage/ commit 9a40a5d1e1008c26cc327c6d163df2a1af22a1a0
imgteam
parents:
3
diff
changeset
|
52 |
64c155acb864
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/points2labelimage/ commit 9a40a5d1e1008c26cc327c6d163df2a1af22a1a0
imgteam
parents:
3
diff
changeset
|
53 # Read geometry |
64c155acb864
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/points2labelimage/ commit 9a40a5d1e1008c26cc327c6d163df2a1af22a1a0
imgteam
parents:
3
diff
changeset
|
54 xs = [pt[0] for pt in coords] |
64c155acb864
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/points2labelimage/ commit 9a40a5d1e1008c26cc327c6d163df2a1af22a1a0
imgteam
parents:
3
diff
changeset
|
55 ys = [pt[1] for pt in coords] |
64c155acb864
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/points2labelimage/ commit 9a40a5d1e1008c26cc327c6d163df2a1af22a1a0
imgteam
parents:
3
diff
changeset
|
56 |
64c155acb864
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/points2labelimage/ commit 9a40a5d1e1008c26cc327c6d163df2a1af22a1a0
imgteam
parents:
3
diff
changeset
|
57 x = min(xs) |
64c155acb864
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/points2labelimage/ commit 9a40a5d1e1008c26cc327c6d163df2a1af22a1a0
imgteam
parents:
3
diff
changeset
|
58 y = min(ys) |
64c155acb864
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/points2labelimage/ commit 9a40a5d1e1008c26cc327c6d163df2a1af22a1a0
imgteam
parents:
3
diff
changeset
|
59 |
64c155acb864
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/points2labelimage/ commit 9a40a5d1e1008c26cc327c6d163df2a1af22a1a0
imgteam
parents:
3
diff
changeset
|
60 width = max(xs) + 1 - x |
64c155acb864
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/points2labelimage/ commit 9a40a5d1e1008c26cc327c6d163df2a1af22a1a0
imgteam
parents:
3
diff
changeset
|
61 height = max(ys) + 1 - y |
64c155acb864
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/points2labelimage/ commit 9a40a5d1e1008c26cc327c6d163df2a1af22a1a0
imgteam
parents:
3
diff
changeset
|
62 |
64c155acb864
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/points2labelimage/ commit 9a40a5d1e1008c26cc327c6d163df2a1af22a1a0
imgteam
parents:
3
diff
changeset
|
63 # Validate geometry (must be rectangular) |
64c155acb864
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/points2labelimage/ commit 9a40a5d1e1008c26cc327c6d163df2a1af22a1a0
imgteam
parents:
3
diff
changeset
|
64 assert is_rectangular(list(zip(xs, ys))) |
64c155acb864
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/points2labelimage/ commit 9a40a5d1e1008c26cc327c6d163df2a1af22a1a0
imgteam
parents:
3
diff
changeset
|
65 |
64c155acb864
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/points2labelimage/ commit 9a40a5d1e1008c26cc327c6d163df2a1af22a1a0
imgteam
parents:
3
diff
changeset
|
66 # Append the rectangle |
64c155acb864
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/points2labelimage/ commit 9a40a5d1e1008c26cc327c6d163df2a1af22a1a0
imgteam
parents:
3
diff
changeset
|
67 rows.append({ |
64c155acb864
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/points2labelimage/ commit 9a40a5d1e1008c26cc327c6d163df2a1af22a1a0
imgteam
parents:
3
diff
changeset
|
68 'pos_x': x, |
64c155acb864
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/points2labelimage/ commit 9a40a5d1e1008c26cc327c6d163df2a1af22a1a0
imgteam
parents:
3
diff
changeset
|
69 'pos_y': y, |
64c155acb864
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/points2labelimage/ commit 9a40a5d1e1008c26cc327c6d163df2a1af22a1a0
imgteam
parents:
3
diff
changeset
|
70 'width': width, |
64c155acb864
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/points2labelimage/ commit 9a40a5d1e1008c26cc327c6d163df2a1af22a1a0
imgteam
parents:
3
diff
changeset
|
71 'height': height, |
64c155acb864
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/points2labelimage/ commit 9a40a5d1e1008c26cc327c6d163df2a1af22a1a0
imgteam
parents:
3
diff
changeset
|
72 'label': label, |
64c155acb864
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/points2labelimage/ commit 9a40a5d1e1008c26cc327c6d163df2a1af22a1a0
imgteam
parents:
3
diff
changeset
|
73 }) |
64c155acb864
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/points2labelimage/ commit 9a40a5d1e1008c26cc327c6d163df2a1af22a1a0
imgteam
parents:
3
diff
changeset
|
74 df = pd.DataFrame(rows) |
64c155acb864
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/points2labelimage/ commit 9a40a5d1e1008c26cc327c6d163df2a1af22a1a0
imgteam
parents:
3
diff
changeset
|
75 point_file = './point_file.tabular' |
64c155acb864
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/points2labelimage/ commit 9a40a5d1e1008c26cc327c6d163df2a1af22a1a0
imgteam
parents:
3
diff
changeset
|
76 df.to_csv(point_file, sep='\t', index=False) |
64c155acb864
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/points2labelimage/ commit 9a40a5d1e1008c26cc327c6d163df2a1af22a1a0
imgteam
parents:
3
diff
changeset
|
77 return point_file |
64c155acb864
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/points2labelimage/ commit 9a40a5d1e1008c26cc327c6d163df2a1af22a1a0
imgteam
parents:
3
diff
changeset
|
78 |
64c155acb864
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/points2labelimage/ commit 9a40a5d1e1008c26cc327c6d163df2a1af22a1a0
imgteam
parents:
3
diff
changeset
|
79 |
2
714a57d6f3a1
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/points2labelimage/ commit 6fc9ab8db9ef72ac7ded30d7373768feeae9390d
imgteam
parents:
1
diff
changeset
|
80 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
|
81 |
714a57d6f3a1
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/points2labelimage/ commit 6fc9ab8db9ef72ac7ded30d7373768feeae9390d
imgteam
parents:
1
diff
changeset
|
82 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
|
83 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
|
84 |
714a57d6f3a1
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/points2labelimage/ commit 6fc9ab8db9ef72ac7ded30d7373768feeae9390d
imgteam
parents:
1
diff
changeset
|
85 # 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
|
86 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
|
87 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
|
88 |
714a57d6f3a1
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/points2labelimage/ commit 6fc9ab8db9ef72ac7ded30d7373768feeae9390d
imgteam
parents:
1
diff
changeset
|
89 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
|
90 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
|
91 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
|
92 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
|
93 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
|
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 try: |
714a57d6f3a1
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/points2labelimage/ commit 6fc9ab8db9ef72ac7ded30d7373768feeae9390d
imgteam
parents:
1
diff
changeset
|
96 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
|
97 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
|
98 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
|
99 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
|
100 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
|
101 |
714a57d6f3a1
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/points2labelimage/ commit 6fc9ab8db9ef72ac7ded30d7373768feeae9390d
imgteam
parents:
1
diff
changeset
|
102 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
|
103 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
|
104 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
|
105 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
|
106 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
|
107 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
|
108 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
|
109 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
|
110 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
|
111 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
|
112 |
2ae122d5d85a
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/points2labelimage/ commit 78614a9010c2ca0e1fa5973639c05ab74bcdb148
imgteam
parents:
2
diff
changeset
|
113 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
|
114 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
|
115 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
|
116 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
|
117 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
|
118 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
|
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 # 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
|
121 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
|
122 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
|
123 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
|
124 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
|
125 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
|
126 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
|
127 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
|
128 |
714a57d6f3a1
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/points2labelimage/ commit 6fc9ab8db9ef72ac7ded30d7373768feeae9390d
imgteam
parents:
1
diff
changeset
|
129 # 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
|
130 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
|
131 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
|
132 |
2
714a57d6f3a1
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/points2labelimage/ commit 6fc9ab8db9ef72ac7ded30d7373768feeae9390d
imgteam
parents:
1
diff
changeset
|
133 # 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
|
134 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
|
135 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
|
136 ): |
2
714a57d6f3a1
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/points2labelimage/ commit 6fc9ab8db9ef72ac7ded30d7373768feeae9390d
imgteam
parents:
1
diff
changeset
|
137 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
|
138 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
|
139 |
714a57d6f3a1
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/points2labelimage/ commit 6fc9ab8db9ef72ac7ded30d7373768feeae9390d
imgteam
parents:
1
diff
changeset
|
140 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
|
141 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
|
142 |
714a57d6f3a1
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/points2labelimage/ commit 6fc9ab8db9ef72ac7ded30d7373768feeae9390d
imgteam
parents:
1
diff
changeset
|
143 # 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
|
144 # 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
|
145 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
|
146 |
2ae122d5d85a
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/points2labelimage/ commit 78614a9010c2ca0e1fa5973639c05ab74bcdb148
imgteam
parents:
2
diff
changeset
|
147 # 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
|
148 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
|
149 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
|
150 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
|
151 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
|
152 else: |
2ae122d5d85a
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/points2labelimage/ commit 78614a9010c2ca0e1fa5973639c05ab74bcdb148
imgteam
parents:
2
diff
changeset
|
153 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
|
154 |
2ae122d5d85a
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/points2labelimage/ commit 78614a9010c2ca0e1fa5973639c05ab74bcdb148
imgteam
parents:
2
diff
changeset
|
155 # 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
|
156 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
|
157 mask[ |
2ae122d5d85a
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/points2labelimage/ commit 78614a9010c2ca0e1fa5973639c05ab74bcdb148
imgteam
parents:
2
diff
changeset
|
158 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
|
159 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
|
160 ] = 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
|
161 |
714a57d6f3a1
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/points2labelimage/ commit 6fc9ab8db9ef72ac7ded30d7373768feeae9390d
imgteam
parents:
1
diff
changeset
|
162 # 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
|
163 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
|
164 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
|
165 else: |
714a57d6f3a1
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/points2labelimage/ commit 6fc9ab8db9ef72ac7ded30d7373768feeae9390d
imgteam
parents:
1
diff
changeset
|
166 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
|
167 |
2
714a57d6f3a1
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/points2labelimage/ commit 6fc9ab8db9ef72ac7ded30d7373768feeae9390d
imgteam
parents:
1
diff
changeset
|
168 # 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
|
169 # |
714a57d6f3a1
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/points2labelimage/ commit 6fc9ab8db9ef72ac7ded30d7373768feeae9390d
imgteam
parents:
1
diff
changeset
|
170 # 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
|
171 # 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
|
172 # 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
|
173 # 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
|
174 # 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
|
175 # |
714a57d6f3a1
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/points2labelimage/ commit 6fc9ab8db9ef72ac7ded30d7373768feeae9390d
imgteam
parents:
1
diff
changeset
|
176 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
|
177 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
|
178 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
|
179 |
714a57d6f3a1
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/points2labelimage/ commit 6fc9ab8db9ef72ac7ded30d7373768feeae9390d
imgteam
parents:
1
diff
changeset
|
180 # 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
|
181 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
|
182 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
|
183 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
|
184 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
|
185 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
|
186 |
714a57d6f3a1
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/points2labelimage/ commit 6fc9ab8db9ef72ac7ded30d7373768feeae9390d
imgteam
parents:
1
diff
changeset
|
187 # 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
|
188 else: |
714a57d6f3a1
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/points2labelimage/ commit 6fc9ab8db9ef72ac7ded30d7373768feeae9390d
imgteam
parents:
1
diff
changeset
|
189 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
|
190 |
0
07d362a8af11
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/points2labelimage/ commit c3f4b766f03770f094fda6bda0a5882c0ebd4581
imgteam
parents:
diff
changeset
|
191 else: |
4
64c155acb864
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/points2labelimage/ commit 9a40a5d1e1008c26cc327c6d163df2a1af22a1a0
imgteam
parents:
3
diff
changeset
|
192 raise Exception('{} is empty or does not exist.'.format(point_file)) # appropriate built-in error? |
2
714a57d6f3a1
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/points2labelimage/ commit 6fc9ab8db9ef72ac7ded30d7373768feeae9390d
imgteam
parents:
1
diff
changeset
|
193 |
714a57d6f3a1
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/points2labelimage/ commit 6fc9ab8db9ef72ac7ded30d7373768feeae9390d
imgteam
parents:
1
diff
changeset
|
194 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
|
195 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
|
196 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
|
197 |
0
07d362a8af11
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/points2labelimage/ commit c3f4b766f03770f094fda6bda0a5882c0ebd4581
imgteam
parents:
diff
changeset
|
198 |
4
64c155acb864
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/points2labelimage/ commit 9a40a5d1e1008c26cc327c6d163df2a1af22a1a0
imgteam
parents:
3
diff
changeset
|
199 if __name__ == '__main__': |
0
07d362a8af11
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/points2labelimage/ commit c3f4b766f03770f094fda6bda0a5882c0ebd4581
imgteam
parents:
diff
changeset
|
200 parser = argparse.ArgumentParser() |
4
64c155acb864
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/points2labelimage/ commit 9a40a5d1e1008c26cc327c6d163df2a1af22a1a0
imgteam
parents:
3
diff
changeset
|
201 parser.add_argument('in_file', type=argparse.FileType('r'), help='Input point file or GeoJSON file') |
2
714a57d6f3a1
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/points2labelimage/ commit 6fc9ab8db9ef72ac7ded30d7373768feeae9390d
imgteam
parents:
1
diff
changeset
|
202 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
|
203 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
|
204 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
|
205 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
|
206 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
|
207 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
|
208 |
0
07d362a8af11
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/points2labelimage/ commit c3f4b766f03770f094fda6bda0a5882c0ebd4581
imgteam
parents:
diff
changeset
|
209 args = parser.parse_args() |
07d362a8af11
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/points2labelimage/ commit c3f4b766f03770f094fda6bda0a5882c0ebd4581
imgteam
parents:
diff
changeset
|
210 |
4
64c155acb864
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/points2labelimage/ commit 9a40a5d1e1008c26cc327c6d163df2a1af22a1a0
imgteam
parents:
3
diff
changeset
|
211 point_file = args.in_file.name |
64c155acb864
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/points2labelimage/ commit 9a40a5d1e1008c26cc327c6d163df2a1af22a1a0
imgteam
parents:
3
diff
changeset
|
212 has_header = args.has_header |
64c155acb864
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/points2labelimage/ commit 9a40a5d1e1008c26cc327c6d163df2a1af22a1a0
imgteam
parents:
3
diff
changeset
|
213 |
64c155acb864
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/points2labelimage/ commit 9a40a5d1e1008c26cc327c6d163df2a1af22a1a0
imgteam
parents:
3
diff
changeset
|
214 try: |
64c155acb864
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/points2labelimage/ commit 9a40a5d1e1008c26cc327c6d163df2a1af22a1a0
imgteam
parents:
3
diff
changeset
|
215 with open(args.in_file.name, 'r') as f: |
64c155acb864
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/points2labelimage/ commit 9a40a5d1e1008c26cc327c6d163df2a1af22a1a0
imgteam
parents:
3
diff
changeset
|
216 content = json.load(f) |
64c155acb864
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/points2labelimage/ commit 9a40a5d1e1008c26cc327c6d163df2a1af22a1a0
imgteam
parents:
3
diff
changeset
|
217 if isinstance(content, dict) and content.get('type') == 'FeatureCollection' and isinstance(content.get('features'), list): |
64c155acb864
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/points2labelimage/ commit 9a40a5d1e1008c26cc327c6d163df2a1af22a1a0
imgteam
parents:
3
diff
changeset
|
218 point_file = geojson_to_tabular(content) |
64c155acb864
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/points2labelimage/ commit 9a40a5d1e1008c26cc327c6d163df2a1af22a1a0
imgteam
parents:
3
diff
changeset
|
219 has_header = True # header included in the converted file |
64c155acb864
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/points2labelimage/ commit 9a40a5d1e1008c26cc327c6d163df2a1af22a1a0
imgteam
parents:
3
diff
changeset
|
220 else: |
64c155acb864
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/points2labelimage/ commit 9a40a5d1e1008c26cc327c6d163df2a1af22a1a0
imgteam
parents:
3
diff
changeset
|
221 raise ValueError('Input is a JSON file but not a valid GeoJSON file') |
64c155acb864
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/points2labelimage/ commit 9a40a5d1e1008c26cc327c6d163df2a1af22a1a0
imgteam
parents:
3
diff
changeset
|
222 except json.JSONDecodeError: |
64c155acb864
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/points2labelimage/ commit 9a40a5d1e1008c26cc327c6d163df2a1af22a1a0
imgteam
parents:
3
diff
changeset
|
223 print('Input is not a valid JSON file. Assuming it a tabular file.') |
64c155acb864
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/points2labelimage/ commit 9a40a5d1e1008c26cc327c6d163df2a1af22a1a0
imgteam
parents:
3
diff
changeset
|
224 |
2
714a57d6f3a1
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/points2labelimage/ commit 6fc9ab8db9ef72ac7ded30d7373768feeae9390d
imgteam
parents:
1
diff
changeset
|
225 rasterize( |
4
64c155acb864
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/points2labelimage/ commit 9a40a5d1e1008c26cc327c6d163df2a1af22a1a0
imgteam
parents:
3
diff
changeset
|
226 point_file, |
2
714a57d6f3a1
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/points2labelimage/ commit 6fc9ab8db9ef72ac7ded30d7373768feeae9390d
imgteam
parents:
1
diff
changeset
|
227 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
|
228 (args.shapey, args.shapex), |
4
64c155acb864
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/points2labelimage/ commit 9a40a5d1e1008c26cc327c6d163df2a1af22a1a0
imgteam
parents:
3
diff
changeset
|
229 has_header=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
|
230 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
|
231 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
|
232 ) |