annotate overlay_images.py @ 5:c1fd8b7c6bbc draft default tip

planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/overlay_images/ commit 8e99ddb62ceab1da15996906a0813826b62d38fe
author imgteam
date Wed, 17 Dec 2025 11:22:51 +0000
parents 61009b4361c1
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
45613da1bf23 "planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/overlay_images/ commit 736949b5fb325cc7880d0ff0bb13b78115b9f81c"
imgteam
parents:
diff changeset
1 """
2
f42d21fe65d8 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/overlay_images/ commit 71dae1df58f579b84d4f9d92fb0dd509c02dd48f
imgteam
parents: 0
diff changeset
2 Copyright 2022-2023 Biomedical Computer Vision Group, Heidelberg University.
0
45613da1bf23 "planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/overlay_images/ commit 736949b5fb325cc7880d0ff0bb13b78115b9f81c"
imgteam
parents:
diff changeset
3
45613da1bf23 "planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/overlay_images/ commit 736949b5fb325cc7880d0ff0bb13b78115b9f81c"
imgteam
parents:
diff changeset
4 Distributed under the MIT license.
45613da1bf23 "planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/overlay_images/ commit 736949b5fb325cc7880d0ff0bb13b78115b9f81c"
imgteam
parents:
diff changeset
5 See file LICENSE for detail or copy at https://opensource.org/licenses/MIT
45613da1bf23 "planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/overlay_images/ commit 736949b5fb325cc7880d0ff0bb13b78115b9f81c"
imgteam
parents:
diff changeset
6 """
45613da1bf23 "planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/overlay_images/ commit 736949b5fb325cc7880d0ff0bb13b78115b9f81c"
imgteam
parents:
diff changeset
7
45613da1bf23 "planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/overlay_images/ commit 736949b5fb325cc7880d0ff0bb13b78115b9f81c"
imgteam
parents:
diff changeset
8 import argparse
45613da1bf23 "planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/overlay_images/ commit 736949b5fb325cc7880d0ff0bb13b78115b9f81c"
imgteam
parents:
diff changeset
9
3
eb173a1fabc4 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/overlay_images/ commit 574caf027453f080a7f86f80eae9775ed1c8afa0
imgteam
parents: 2
diff changeset
10 import giatools.io
2
f42d21fe65d8 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/overlay_images/ commit 71dae1df58f579b84d4f9d92fb0dd509c02dd48f
imgteam
parents: 0
diff changeset
11 import matplotlib.colors
0
45613da1bf23 "planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/overlay_images/ commit 736949b5fb325cc7880d0ff0bb13b78115b9f81c"
imgteam
parents:
diff changeset
12 import matplotlib.pyplot as plt
45613da1bf23 "planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/overlay_images/ commit 736949b5fb325cc7880d0ff0bb13b78115b9f81c"
imgteam
parents:
diff changeset
13 import numpy as np
45613da1bf23 "planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/overlay_images/ commit 736949b5fb325cc7880d0ff0bb13b78115b9f81c"
imgteam
parents:
diff changeset
14 import skimage.color
45613da1bf23 "planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/overlay_images/ commit 736949b5fb325cc7880d0ff0bb13b78115b9f81c"
imgteam
parents:
diff changeset
15 import skimage.io
45613da1bf23 "planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/overlay_images/ commit 736949b5fb325cc7880d0ff0bb13b78115b9f81c"
imgteam
parents:
diff changeset
16 import skimage.measure
45613da1bf23 "planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/overlay_images/ commit 736949b5fb325cc7880d0ff0bb13b78115b9f81c"
imgteam
parents:
diff changeset
17 import tifffile
2
f42d21fe65d8 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/overlay_images/ commit 71dae1df58f579b84d4f9d92fb0dd509c02dd48f
imgteam
parents: 0
diff changeset
18 from contours import ContourPaint
0
45613da1bf23 "planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/overlay_images/ commit 736949b5fb325cc7880d0ff0bb13b78115b9f81c"
imgteam
parents:
diff changeset
19
45613da1bf23 "planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/overlay_images/ commit 736949b5fb325cc7880d0ff0bb13b78115b9f81c"
imgteam
parents:
diff changeset
20
45613da1bf23 "planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/overlay_images/ commit 736949b5fb325cc7880d0ff0bb13b78115b9f81c"
imgteam
parents:
diff changeset
21 def read_im_gray(fn):
3
eb173a1fabc4 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/overlay_images/ commit 574caf027453f080a7f86f80eae9775ed1c8afa0
imgteam
parents: 2
diff changeset
22 img = giatools.io.imread(fn)
0
45613da1bf23 "planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/overlay_images/ commit 736949b5fb325cc7880d0ff0bb13b78115b9f81c"
imgteam
parents:
diff changeset
23 nDims = len(img.shape)
45613da1bf23 "planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/overlay_images/ commit 736949b5fb325cc7880d0ff0bb13b78115b9f81c"
imgteam
parents:
diff changeset
24 assert nDims in [2, 3], 'this tool only supports single 2D images'
45613da1bf23 "planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/overlay_images/ commit 736949b5fb325cc7880d0ff0bb13b78115b9f81c"
imgteam
parents:
diff changeset
25 if nDims == 3 and img.shape[-1] in [3, 4]:
45613da1bf23 "planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/overlay_images/ commit 736949b5fb325cc7880d0ff0bb13b78115b9f81c"
imgteam
parents:
diff changeset
26 img = skimage.color.rgb2gray(img)
45613da1bf23 "planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/overlay_images/ commit 736949b5fb325cc7880d0ff0bb13b78115b9f81c"
imgteam
parents:
diff changeset
27 if len(img.shape) == 3:
45613da1bf23 "planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/overlay_images/ commit 736949b5fb325cc7880d0ff0bb13b78115b9f81c"
imgteam
parents:
diff changeset
28 return img[:, :, 0]
45613da1bf23 "planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/overlay_images/ commit 736949b5fb325cc7880d0ff0bb13b78115b9f81c"
imgteam
parents:
diff changeset
29 else:
45613da1bf23 "planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/overlay_images/ commit 736949b5fb325cc7880d0ff0bb13b78115b9f81c"
imgteam
parents:
diff changeset
30 return img
45613da1bf23 "planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/overlay_images/ commit 736949b5fb325cc7880d0ff0bb13b78115b9f81c"
imgteam
parents:
diff changeset
31
45613da1bf23 "planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/overlay_images/ commit 736949b5fb325cc7880d0ff0bb13b78115b9f81c"
imgteam
parents:
diff changeset
32
4
61009b4361c1 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/overlay_images/ commit a11042fbd0da4bfcc54522b31919aa5efb862f3d
imgteam
parents: 3
diff changeset
33 def get_rgb8_copy(img, fp_lower, fp_upper):
2
f42d21fe65d8 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/overlay_images/ commit 71dae1df58f579b84d4f9d92fb0dd509c02dd48f
imgteam
parents: 0
diff changeset
34 img = np.squeeze(img)
f42d21fe65d8 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/overlay_images/ commit 71dae1df58f579b84d4f9d92fb0dd509c02dd48f
imgteam
parents: 0
diff changeset
35 assert img.ndim == 2 or (img.ndim == 3 and img.shape[-1] in (3, 4))
4
61009b4361c1 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/overlay_images/ commit a11042fbd0da4bfcc54522b31919aa5efb862f3d
imgteam
parents: 3
diff changeset
36 assert fp_lower == 'min' or abs(float(fp_lower)) < np.inf # 'min' or number
61009b4361c1 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/overlay_images/ commit a11042fbd0da4bfcc54522b31919aa5efb862f3d
imgteam
parents: 3
diff changeset
37 assert fp_upper == 'max' or abs(float(fp_upper)) < np.inf # 'max' or number
61009b4361c1 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/overlay_images/ commit a11042fbd0da4bfcc54522b31919aa5efb862f3d
imgteam
parents: 3
diff changeset
38
61009b4361c1 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/overlay_images/ commit a11042fbd0da4bfcc54522b31919aa5efb862f3d
imgteam
parents: 3
diff changeset
39 # Convert from floating point
2
f42d21fe65d8 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/overlay_images/ commit 71dae1df58f579b84d4f9d92fb0dd509c02dd48f
imgteam
parents: 0
diff changeset
40 if str(img.dtype).startswith('float'):
4
61009b4361c1 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/overlay_images/ commit a11042fbd0da4bfcc54522b31919aa5efb862f3d
imgteam
parents: 3
diff changeset
41 a = img.min() if fp_lower == 'min' else float(fp_lower)
61009b4361c1 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/overlay_images/ commit a11042fbd0da4bfcc54522b31919aa5efb862f3d
imgteam
parents: 3
diff changeset
42 b = img.max() if fp_upper == 'max' else float(fp_upper)
61009b4361c1 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/overlay_images/ commit a11042fbd0da4bfcc54522b31919aa5efb862f3d
imgteam
parents: 3
diff changeset
43
61009b4361c1 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/overlay_images/ commit a11042fbd0da4bfcc54522b31919aa5efb862f3d
imgteam
parents: 3
diff changeset
44 if a > b:
61009b4361c1 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/overlay_images/ commit a11042fbd0da4bfcc54522b31919aa5efb862f3d
imgteam
parents: 3
diff changeset
45 raise ValueError(
61009b4361c1 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/overlay_images/ commit a11042fbd0da4bfcc54522b31919aa5efb862f3d
imgteam
parents: 3
diff changeset
46 f'Lower bound ({a:g}) must be less than upper bound ({b:g}).'
61009b4361c1 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/overlay_images/ commit a11042fbd0da4bfcc54522b31919aa5efb862f3d
imgteam
parents: 3
diff changeset
47 )
61009b4361c1 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/overlay_images/ commit a11042fbd0da4bfcc54522b31919aa5efb862f3d
imgteam
parents: 3
diff changeset
48 if a == b:
61009b4361c1 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/overlay_images/ commit a11042fbd0da4bfcc54522b31919aa5efb862f3d
imgteam
parents: 3
diff changeset
49 raise ValueError(
61009b4361c1 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/overlay_images/ commit a11042fbd0da4bfcc54522b31919aa5efb862f3d
imgteam
parents: 3
diff changeset
50 'Floating point conversion is undefined'
61009b4361c1 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/overlay_images/ commit a11042fbd0da4bfcc54522b31919aa5efb862f3d
imgteam
parents: 3
diff changeset
51 ' because lower and upper bounds are identical.'
61009b4361c1 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/overlay_images/ commit a11042fbd0da4bfcc54522b31919aa5efb862f3d
imgteam
parents: 3
diff changeset
52 )
61009b4361c1 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/overlay_images/ commit a11042fbd0da4bfcc54522b31919aa5efb862f3d
imgteam
parents: 3
diff changeset
53
61009b4361c1 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/overlay_images/ commit a11042fbd0da4bfcc54522b31919aa5efb862f3d
imgteam
parents: 3
diff changeset
54 # Perform linear mapping to [0, 1]
61009b4361c1 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/overlay_images/ commit a11042fbd0da4bfcc54522b31919aa5efb862f3d
imgteam
parents: 3
diff changeset
55 img = img.clip(a, b)
61009b4361c1 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/overlay_images/ commit a11042fbd0da4bfcc54522b31919aa5efb862f3d
imgteam
parents: 3
diff changeset
56 img = (img - a) / (b - a)
61009b4361c1 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/overlay_images/ commit a11042fbd0da4bfcc54522b31919aa5efb862f3d
imgteam
parents: 3
diff changeset
57
61009b4361c1 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/overlay_images/ commit a11042fbd0da4bfcc54522b31919aa5efb862f3d
imgteam
parents: 3
diff changeset
58 # Convert to uint8
3
eb173a1fabc4 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/overlay_images/ commit 574caf027453f080a7f86f80eae9775ed1c8afa0
imgteam
parents: 2
diff changeset
59 img = np.round(img * 255).astype(np.uint8)
4
61009b4361c1 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/overlay_images/ commit a11042fbd0da4bfcc54522b31919aa5efb862f3d
imgteam
parents: 3
diff changeset
60
61009b4361c1 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/overlay_images/ commit a11042fbd0da4bfcc54522b31919aa5efb862f3d
imgteam
parents: 3
diff changeset
61 # Convert from uint16
3
eb173a1fabc4 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/overlay_images/ commit 574caf027453f080a7f86f80eae9775ed1c8afa0
imgteam
parents: 2
diff changeset
62 elif img.dtype == np.uint16:
eb173a1fabc4 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/overlay_images/ commit 574caf027453f080a7f86f80eae9775ed1c8afa0
imgteam
parents: 2
diff changeset
63 img = (img // 256).astype(np.uint8)
4
61009b4361c1 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/overlay_images/ commit a11042fbd0da4bfcc54522b31919aa5efb862f3d
imgteam
parents: 3
diff changeset
64
61009b4361c1 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/overlay_images/ commit a11042fbd0da4bfcc54522b31919aa5efb862f3d
imgteam
parents: 3
diff changeset
65 # Other dtypes than float, uint8, uint16 are not supported
3
eb173a1fabc4 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/overlay_images/ commit 574caf027453f080a7f86f80eae9775ed1c8afa0
imgteam
parents: 2
diff changeset
66 elif img.dtype != np.uint8:
2
f42d21fe65d8 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/overlay_images/ commit 71dae1df58f579b84d4f9d92fb0dd509c02dd48f
imgteam
parents: 0
diff changeset
67 raise ValueError(f'unknown dtype: {img.dtype}')
4
61009b4361c1 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/overlay_images/ commit a11042fbd0da4bfcc54522b31919aa5efb862f3d
imgteam
parents: 3
diff changeset
68
2
f42d21fe65d8 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/overlay_images/ commit 71dae1df58f579b84d4f9d92fb0dd509c02dd48f
imgteam
parents: 0
diff changeset
69 if img.ndim == 2:
3
eb173a1fabc4 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/overlay_images/ commit 574caf027453f080a7f86f80eae9775ed1c8afa0
imgteam
parents: 2
diff changeset
70 result = np.dstack([img] * 3).copy()
2
f42d21fe65d8 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/overlay_images/ commit 71dae1df58f579b84d4f9d92fb0dd509c02dd48f
imgteam
parents: 0
diff changeset
71 else:
3
eb173a1fabc4 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/overlay_images/ commit 574caf027453f080a7f86f80eae9775ed1c8afa0
imgteam
parents: 2
diff changeset
72 result = img[:, :, :3].copy()
eb173a1fabc4 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/overlay_images/ commit 574caf027453f080a7f86f80eae9775ed1c8afa0
imgteam
parents: 2
diff changeset
73 assert result.dtype == np.uint8, result.dtype
eb173a1fabc4 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/overlay_images/ commit 574caf027453f080a7f86f80eae9775ed1c8afa0
imgteam
parents: 2
diff changeset
74 return result
2
f42d21fe65d8 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/overlay_images/ commit 71dae1df58f579b84d4f9d92fb0dd509c02dd48f
imgteam
parents: 0
diff changeset
75
f42d21fe65d8 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/overlay_images/ commit 71dae1df58f579b84d4f9d92fb0dd509c02dd48f
imgteam
parents: 0
diff changeset
76
0
45613da1bf23 "planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/overlay_images/ commit 736949b5fb325cc7880d0ff0bb13b78115b9f81c"
imgteam
parents:
diff changeset
77 def coloc_vis(in_red_fn, in_green_fn, out_fn):
45613da1bf23 "planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/overlay_images/ commit 736949b5fb325cc7880d0ff0bb13b78115b9f81c"
imgteam
parents:
diff changeset
78 im1 = read_im_gray(in_red_fn)
45613da1bf23 "planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/overlay_images/ commit 736949b5fb325cc7880d0ff0bb13b78115b9f81c"
imgteam
parents:
diff changeset
79 im2 = read_im_gray(in_green_fn)
45613da1bf23 "planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/overlay_images/ commit 736949b5fb325cc7880d0ff0bb13b78115b9f81c"
imgteam
parents:
diff changeset
80 assert im1.shape == im2.shape, 'Two images should have the same dimension'
45613da1bf23 "planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/overlay_images/ commit 736949b5fb325cc7880d0ff0bb13b78115b9f81c"
imgteam
parents:
diff changeset
81
45613da1bf23 "planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/overlay_images/ commit 736949b5fb325cc7880d0ff0bb13b78115b9f81c"
imgteam
parents:
diff changeset
82 vmin = np.min([np.min(im1), np.min(im2)])
45613da1bf23 "planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/overlay_images/ commit 736949b5fb325cc7880d0ff0bb13b78115b9f81c"
imgteam
parents:
diff changeset
83 scal = 255.0 / (np.max([np.max(im1), np.max(im2)]) - vmin)
45613da1bf23 "planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/overlay_images/ commit 736949b5fb325cc7880d0ff0bb13b78115b9f81c"
imgteam
parents:
diff changeset
84
45613da1bf23 "planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/overlay_images/ commit 736949b5fb325cc7880d0ff0bb13b78115b9f81c"
imgteam
parents:
diff changeset
85 out_im = np.zeros(im1.shape + (3,), dtype=np.ubyte)
45613da1bf23 "planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/overlay_images/ commit 736949b5fb325cc7880d0ff0bb13b78115b9f81c"
imgteam
parents:
diff changeset
86 out_im[:, :, 0] = (im1 - vmin) * scal
45613da1bf23 "planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/overlay_images/ commit 736949b5fb325cc7880d0ff0bb13b78115b9f81c"
imgteam
parents:
diff changeset
87 out_im[:, :, 1] = (im2 - vmin) * scal
45613da1bf23 "planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/overlay_images/ commit 736949b5fb325cc7880d0ff0bb13b78115b9f81c"
imgteam
parents:
diff changeset
88 skimage.io.imsave(out_fn, out_im) # output is RGB
45613da1bf23 "planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/overlay_images/ commit 736949b5fb325cc7880d0ff0bb13b78115b9f81c"
imgteam
parents:
diff changeset
89
45613da1bf23 "planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/overlay_images/ commit 736949b5fb325cc7880d0ff0bb13b78115b9f81c"
imgteam
parents:
diff changeset
90
45613da1bf23 "planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/overlay_images/ commit 736949b5fb325cc7880d0ff0bb13b78115b9f81c"
imgteam
parents:
diff changeset
91 def blending(im1_fn, im2_fn, out_fn, alpha=0.5):
3
eb173a1fabc4 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/overlay_images/ commit 574caf027453f080a7f86f80eae9775ed1c8afa0
imgteam
parents: 2
diff changeset
92 im1 = giatools.io.imread(im1_fn)
eb173a1fabc4 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/overlay_images/ commit 574caf027453f080a7f86f80eae9775ed1c8afa0
imgteam
parents: 2
diff changeset
93 im2 = giatools.io.imread(im2_fn)
0
45613da1bf23 "planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/overlay_images/ commit 736949b5fb325cc7880d0ff0bb13b78115b9f81c"
imgteam
parents:
diff changeset
94 assert im1.shape == im2.shape, 'Two images should have the same dimension'
45613da1bf23 "planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/overlay_images/ commit 736949b5fb325cc7880d0ff0bb13b78115b9f81c"
imgteam
parents:
diff changeset
95 out_im = (1 - alpha) * im1 + alpha * im2
45613da1bf23 "planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/overlay_images/ commit 736949b5fb325cc7880d0ff0bb13b78115b9f81c"
imgteam
parents:
diff changeset
96 if len(im1.shape) > 3:
45613da1bf23 "planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/overlay_images/ commit 736949b5fb325cc7880d0ff0bb13b78115b9f81c"
imgteam
parents:
diff changeset
97 tifffile.imwrite(out_fn, out_im.astype(im1.dtype), imagej=True)
45613da1bf23 "planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/overlay_images/ commit 736949b5fb325cc7880d0ff0bb13b78115b9f81c"
imgteam
parents:
diff changeset
98 else:
45613da1bf23 "planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/overlay_images/ commit 736949b5fb325cc7880d0ff0bb13b78115b9f81c"
imgteam
parents:
diff changeset
99 skimage.io.imsave(out_fn, out_im.astype(im1.dtype)) # format of output is the same as input
45613da1bf23 "planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/overlay_images/ commit 736949b5fb325cc7880d0ff0bb13b78115b9f81c"
imgteam
parents:
diff changeset
100
45613da1bf23 "planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/overlay_images/ commit 736949b5fb325cc7880d0ff0bb13b78115b9f81c"
imgteam
parents:
diff changeset
101
4
61009b4361c1 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/overlay_images/ commit a11042fbd0da4bfcc54522b31919aa5efb862f3d
imgteam
parents: 3
diff changeset
102 def seg_contour(im1_fn, im2_fn, out_fn, fp_lower, fp_upper, linewidth, color='#ff0000', show_label=False, label_color='#ffff00'):
3
eb173a1fabc4 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/overlay_images/ commit 574caf027453f080a7f86f80eae9775ed1c8afa0
imgteam
parents: 2
diff changeset
103 img = giatools.io.imread(im1_fn)
eb173a1fabc4 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/overlay_images/ commit 574caf027453f080a7f86f80eae9775ed1c8afa0
imgteam
parents: 2
diff changeset
104 labels = giatools.io.imread(im2_fn)
2
f42d21fe65d8 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/overlay_images/ commit 71dae1df58f579b84d4f9d92fb0dd509c02dd48f
imgteam
parents: 0
diff changeset
105
4
61009b4361c1 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/overlay_images/ commit a11042fbd0da4bfcc54522b31919aa5efb862f3d
imgteam
parents: 3
diff changeset
106 result = get_rgb8_copy(img, fp_lower, fp_upper)
2
f42d21fe65d8 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/overlay_images/ commit 71dae1df58f579b84d4f9d92fb0dd509c02dd48f
imgteam
parents: 0
diff changeset
107 cp = ContourPaint(labels, linewidth, where='center')
f42d21fe65d8 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/overlay_images/ commit 71dae1df58f579b84d4f9d92fb0dd509c02dd48f
imgteam
parents: 0
diff changeset
108 color_rgb = np.multiply(255, matplotlib.colors.to_rgb(color))
0
45613da1bf23 "planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/overlay_images/ commit 736949b5fb325cc7880d0ff0bb13b78115b9f81c"
imgteam
parents:
diff changeset
109
2
f42d21fe65d8 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/overlay_images/ commit 71dae1df58f579b84d4f9d92fb0dd509c02dd48f
imgteam
parents: 0
diff changeset
110 for label in np.unique(labels):
f42d21fe65d8 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/overlay_images/ commit 71dae1df58f579b84d4f9d92fb0dd509c02dd48f
imgteam
parents: 0
diff changeset
111 if label > 0:
f42d21fe65d8 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/overlay_images/ commit 71dae1df58f579b84d4f9d92fb0dd509c02dd48f
imgteam
parents: 0
diff changeset
112 cc = (labels == label)
f42d21fe65d8 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/overlay_images/ commit 71dae1df58f579b84d4f9d92fb0dd509c02dd48f
imgteam
parents: 0
diff changeset
113 bd = cp.get_contour_mask(cc)
f42d21fe65d8 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/overlay_images/ commit 71dae1df58f579b84d4f9d92fb0dd509c02dd48f
imgteam
parents: 0
diff changeset
114 for i in range(3):
f42d21fe65d8 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/overlay_images/ commit 71dae1df58f579b84d4f9d92fb0dd509c02dd48f
imgteam
parents: 0
diff changeset
115 result[:, :, i][bd] = color_rgb[i]
f42d21fe65d8 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/overlay_images/ commit 71dae1df58f579b84d4f9d92fb0dd509c02dd48f
imgteam
parents: 0
diff changeset
116
0
45613da1bf23 "planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/overlay_images/ commit 736949b5fb325cc7880d0ff0bb13b78115b9f81c"
imgteam
parents:
diff changeset
117 if show_label:
2
f42d21fe65d8 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/overlay_images/ commit 71dae1df58f579b84d4f9d92fb0dd509c02dd48f
imgteam
parents: 0
diff changeset
118 fig = plt.figure(figsize=np.divide(img.shape[:2][::-1], 100), dpi=100)
f42d21fe65d8 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/overlay_images/ commit 71dae1df58f579b84d4f9d92fb0dd509c02dd48f
imgteam
parents: 0
diff changeset
119 ax = fig.add_axes([0, 0, 1, 1])
f42d21fe65d8 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/overlay_images/ commit 71dae1df58f579b84d4f9d92fb0dd509c02dd48f
imgteam
parents: 0
diff changeset
120 ax.axis('off')
f42d21fe65d8 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/overlay_images/ commit 71dae1df58f579b84d4f9d92fb0dd509c02dd48f
imgteam
parents: 0
diff changeset
121 ax.imshow(result)
f42d21fe65d8 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/overlay_images/ commit 71dae1df58f579b84d4f9d92fb0dd509c02dd48f
imgteam
parents: 0
diff changeset
122 for reg in skimage.measure.regionprops(labels):
f42d21fe65d8 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/overlay_images/ commit 71dae1df58f579b84d4f9d92fb0dd509c02dd48f
imgteam
parents: 0
diff changeset
123 ax.text(reg.centroid[1], reg.centroid[0], str(reg.label), color=label_color)
f42d21fe65d8 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/overlay_images/ commit 71dae1df58f579b84d4f9d92fb0dd509c02dd48f
imgteam
parents: 0
diff changeset
124 fig.canvas.print_png(out_fn)
0
45613da1bf23 "planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/overlay_images/ commit 736949b5fb325cc7880d0ff0bb13b78115b9f81c"
imgteam
parents:
diff changeset
125
45613da1bf23 "planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/overlay_images/ commit 736949b5fb325cc7880d0ff0bb13b78115b9f81c"
imgteam
parents:
diff changeset
126 else:
2
f42d21fe65d8 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/overlay_images/ commit 71dae1df58f579b84d4f9d92fb0dd509c02dd48f
imgteam
parents: 0
diff changeset
127 skimage.io.imsave(out_fn, result) # format of output is RGB8
0
45613da1bf23 "planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/overlay_images/ commit 736949b5fb325cc7880d0ff0bb13b78115b9f81c"
imgteam
parents:
diff changeset
128
45613da1bf23 "planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/overlay_images/ commit 736949b5fb325cc7880d0ff0bb13b78115b9f81c"
imgteam
parents:
diff changeset
129
45613da1bf23 "planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/overlay_images/ commit 736949b5fb325cc7880d0ff0bb13b78115b9f81c"
imgteam
parents:
diff changeset
130 if __name__ == "__main__":
45613da1bf23 "planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/overlay_images/ commit 736949b5fb325cc7880d0ff0bb13b78115b9f81c"
imgteam
parents:
diff changeset
131 parser = argparse.ArgumentParser(description="Overlay two images")
45613da1bf23 "planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/overlay_images/ commit 736949b5fb325cc7880d0ff0bb13b78115b9f81c"
imgteam
parents:
diff changeset
132 parser.add_argument("im1", help="The first image")
45613da1bf23 "planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/overlay_images/ commit 736949b5fb325cc7880d0ff0bb13b78115b9f81c"
imgteam
parents:
diff changeset
133 parser.add_argument("im2", help="The second image")
45613da1bf23 "planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/overlay_images/ commit 736949b5fb325cc7880d0ff0bb13b78115b9f81c"
imgteam
parents:
diff changeset
134 parser.add_argument("out", help="Output image")
45613da1bf23 "planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/overlay_images/ commit 736949b5fb325cc7880d0ff0bb13b78115b9f81c"
imgteam
parents:
diff changeset
135 parser.add_argument('--method', dest='method', default='coloc_vis', help='How to overlay images')
4
61009b4361c1 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/overlay_images/ commit a11042fbd0da4bfcc54522b31919aa5efb862f3d
imgteam
parents: 3
diff changeset
136 parser.add_argument('--fp_lower', default='0', type=str, help='Lower bound for floating point conversion')
61009b4361c1 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/overlay_images/ commit a11042fbd0da4bfcc54522b31919aa5efb862f3d
imgteam
parents: 3
diff changeset
137 parser.add_argument('--fp_upper', default='1', type=str, help='Upper bound for floating point conversion')
0
45613da1bf23 "planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/overlay_images/ commit 736949b5fb325cc7880d0ff0bb13b78115b9f81c"
imgteam
parents:
diff changeset
138 parser.add_argument('--alpha', dest='alpha', default=0.5, type=float, help='Blending weight')
2
f42d21fe65d8 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/overlay_images/ commit 71dae1df58f579b84d4f9d92fb0dd509c02dd48f
imgteam
parents: 0
diff changeset
139 parser.add_argument('--thickness', dest='thickness', default=2, type=int, help='Contour thickness')
f42d21fe65d8 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/overlay_images/ commit 71dae1df58f579b84d4f9d92fb0dd509c02dd48f
imgteam
parents: 0
diff changeset
140 parser.add_argument('--color', dest='color', default='#FF0000', help='Contour color')
f42d21fe65d8 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/overlay_images/ commit 71dae1df58f579b84d4f9d92fb0dd509c02dd48f
imgteam
parents: 0
diff changeset
141 parser.add_argument('--show_label', dest='show_label', action='store_true', help='Show labels')
f42d21fe65d8 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/overlay_images/ commit 71dae1df58f579b84d4f9d92fb0dd509c02dd48f
imgteam
parents: 0
diff changeset
142 parser.add_argument('--label_color', dest='label_color', default='#FFFF00', help='Label color')
0
45613da1bf23 "planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/overlay_images/ commit 736949b5fb325cc7880d0ff0bb13b78115b9f81c"
imgteam
parents:
diff changeset
143 args = parser.parse_args()
45613da1bf23 "planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/overlay_images/ commit 736949b5fb325cc7880d0ff0bb13b78115b9f81c"
imgteam
parents:
diff changeset
144
45613da1bf23 "planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/overlay_images/ commit 736949b5fb325cc7880d0ff0bb13b78115b9f81c"
imgteam
parents:
diff changeset
145 if args.method == 'coloc_vis':
45613da1bf23 "planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/overlay_images/ commit 736949b5fb325cc7880d0ff0bb13b78115b9f81c"
imgteam
parents:
diff changeset
146 coloc_vis(args.im1, args.im2, args.out)
45613da1bf23 "planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/overlay_images/ commit 736949b5fb325cc7880d0ff0bb13b78115b9f81c"
imgteam
parents:
diff changeset
147 elif args.method == 'blending':
45613da1bf23 "planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/overlay_images/ commit 736949b5fb325cc7880d0ff0bb13b78115b9f81c"
imgteam
parents:
diff changeset
148 blending(args.im1, args.im2, args.out, alpha=args.alpha)
45613da1bf23 "planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/overlay_images/ commit 736949b5fb325cc7880d0ff0bb13b78115b9f81c"
imgteam
parents:
diff changeset
149 elif args.method == 'seg_contour':
4
61009b4361c1 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/overlay_images/ commit a11042fbd0da4bfcc54522b31919aa5efb862f3d
imgteam
parents: 3
diff changeset
150 seg_contour(
61009b4361c1 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/overlay_images/ commit a11042fbd0da4bfcc54522b31919aa5efb862f3d
imgteam
parents: 3
diff changeset
151 args.im1,
61009b4361c1 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/overlay_images/ commit a11042fbd0da4bfcc54522b31919aa5efb862f3d
imgteam
parents: 3
diff changeset
152 args.im2,
61009b4361c1 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/overlay_images/ commit a11042fbd0da4bfcc54522b31919aa5efb862f3d
imgteam
parents: 3
diff changeset
153 args.out,
61009b4361c1 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/overlay_images/ commit a11042fbd0da4bfcc54522b31919aa5efb862f3d
imgteam
parents: 3
diff changeset
154 fp_lower=args.fp_lower,
61009b4361c1 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/overlay_images/ commit a11042fbd0da4bfcc54522b31919aa5efb862f3d
imgteam
parents: 3
diff changeset
155 fp_upper=args.fp_upper,
61009b4361c1 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/overlay_images/ commit a11042fbd0da4bfcc54522b31919aa5efb862f3d
imgteam
parents: 3
diff changeset
156 linewidth=args.thickness,
61009b4361c1 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/overlay_images/ commit a11042fbd0da4bfcc54522b31919aa5efb862f3d
imgteam
parents: 3
diff changeset
157 color=args.color,
61009b4361c1 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/overlay_images/ commit a11042fbd0da4bfcc54522b31919aa5efb862f3d
imgteam
parents: 3
diff changeset
158 show_label=args.show_label,
61009b4361c1 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/overlay_images/ commit a11042fbd0da4bfcc54522b31919aa5efb862f3d
imgteam
parents: 3
diff changeset
159 label_color=args.label_color,
61009b4361c1 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/overlay_images/ commit a11042fbd0da4bfcc54522b31919aa5efb862f3d
imgteam
parents: 3
diff changeset
160 )