annotate overlay_images.py @ 8:ca362a9bfa20 draft default tip

planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/overlay_images/ commit 574caf027453f080a7f86f80eae9775ed1c8afa0
author imgteam
date Mon, 23 Sep 2024 10:26:26 +0000
parents 2495f8b2aefd
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
589af0005df5 "planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/overlay_images/ commit 736949b5fb325cc7880d0ff0bb13b78115b9f81c"
imgteam
parents:
diff changeset
1 """
2
b74693340624 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
589af0005df5 "planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/overlay_images/ commit 736949b5fb325cc7880d0ff0bb13b78115b9f81c"
imgteam
parents:
diff changeset
3
589af0005df5 "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.
589af0005df5 "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
589af0005df5 "planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/overlay_images/ commit 736949b5fb325cc7880d0ff0bb13b78115b9f81c"
imgteam
parents:
diff changeset
6 """
589af0005df5 "planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/overlay_images/ commit 736949b5fb325cc7880d0ff0bb13b78115b9f81c"
imgteam
parents:
diff changeset
7
589af0005df5 "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
589af0005df5 "planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/overlay_images/ commit 736949b5fb325cc7880d0ff0bb13b78115b9f81c"
imgteam
parents:
diff changeset
9
6
2495f8b2aefd planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/overlay_images/ commit c86a1b93cb7732f7331a981d13465653cc1a2790
imgteam
parents: 4
diff changeset
10 import giatools.io
2
b74693340624 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
589af0005df5 "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
589af0005df5 "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
589af0005df5 "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
589af0005df5 "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
589af0005df5 "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
589af0005df5 "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
b74693340624 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
589af0005df5 "planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/overlay_images/ commit 736949b5fb325cc7880d0ff0bb13b78115b9f81c"
imgteam
parents:
diff changeset
19
589af0005df5 "planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/overlay_images/ commit 736949b5fb325cc7880d0ff0bb13b78115b9f81c"
imgteam
parents:
diff changeset
20
589af0005df5 "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):
6
2495f8b2aefd planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/overlay_images/ commit c86a1b93cb7732f7331a981d13465653cc1a2790
imgteam
parents: 4
diff changeset
22 img = giatools.io.imread(fn)
0
589af0005df5 "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)
589af0005df5 "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'
589af0005df5 "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]:
589af0005df5 "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)
589af0005df5 "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:
589af0005df5 "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]
589af0005df5 "planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/overlay_images/ commit 736949b5fb325cc7880d0ff0bb13b78115b9f81c"
imgteam
parents:
diff changeset
29 else:
589af0005df5 "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
589af0005df5 "planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/overlay_images/ commit 736949b5fb325cc7880d0ff0bb13b78115b9f81c"
imgteam
parents:
diff changeset
31
589af0005df5 "planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/overlay_images/ commit 736949b5fb325cc7880d0ff0bb13b78115b9f81c"
imgteam
parents:
diff changeset
32
2
b74693340624 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/overlay_images/ commit 71dae1df58f579b84d4f9d92fb0dd509c02dd48f
imgteam
parents: 0
diff changeset
33 def get_rgb8_copy(img):
b74693340624 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)
b74693340624 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))
b74693340624 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/overlay_images/ commit 71dae1df58f579b84d4f9d92fb0dd509c02dd48f
imgteam
parents: 0
diff changeset
36 if str(img.dtype).startswith('float'):
4
37662cbf44b8 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/overlay_images/ commit 85185439087caafebf1c75dc78b61a2a1f181225
imgteam
parents: 2
diff changeset
37 img = np.round(img * 255).astype(np.uint8)
37662cbf44b8 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/overlay_images/ commit 85185439087caafebf1c75dc78b61a2a1f181225
imgteam
parents: 2
diff changeset
38 elif img.dtype == np.uint16:
37662cbf44b8 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/overlay_images/ commit 85185439087caafebf1c75dc78b61a2a1f181225
imgteam
parents: 2
diff changeset
39 img = (img // 256).astype(np.uint8)
37662cbf44b8 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/overlay_images/ commit 85185439087caafebf1c75dc78b61a2a1f181225
imgteam
parents: 2
diff changeset
40 elif img.dtype != np.uint8:
2
b74693340624 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/overlay_images/ commit 71dae1df58f579b84d4f9d92fb0dd509c02dd48f
imgteam
parents: 0
diff changeset
41 raise ValueError(f'unknown dtype: {img.dtype}')
b74693340624 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/overlay_images/ commit 71dae1df58f579b84d4f9d92fb0dd509c02dd48f
imgteam
parents: 0
diff changeset
42 if img.ndim == 2:
4
37662cbf44b8 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/overlay_images/ commit 85185439087caafebf1c75dc78b61a2a1f181225
imgteam
parents: 2
diff changeset
43 result = np.dstack([img] * 3).copy()
2
b74693340624 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/overlay_images/ commit 71dae1df58f579b84d4f9d92fb0dd509c02dd48f
imgteam
parents: 0
diff changeset
44 else:
4
37662cbf44b8 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/overlay_images/ commit 85185439087caafebf1c75dc78b61a2a1f181225
imgteam
parents: 2
diff changeset
45 result = img[:, :, :3].copy()
37662cbf44b8 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/overlay_images/ commit 85185439087caafebf1c75dc78b61a2a1f181225
imgteam
parents: 2
diff changeset
46 assert result.dtype == np.uint8, result.dtype
37662cbf44b8 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/overlay_images/ commit 85185439087caafebf1c75dc78b61a2a1f181225
imgteam
parents: 2
diff changeset
47 return result
2
b74693340624 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/overlay_images/ commit 71dae1df58f579b84d4f9d92fb0dd509c02dd48f
imgteam
parents: 0
diff changeset
48
b74693340624 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/overlay_images/ commit 71dae1df58f579b84d4f9d92fb0dd509c02dd48f
imgteam
parents: 0
diff changeset
49
0
589af0005df5 "planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/overlay_images/ commit 736949b5fb325cc7880d0ff0bb13b78115b9f81c"
imgteam
parents:
diff changeset
50 def coloc_vis(in_red_fn, in_green_fn, out_fn):
589af0005df5 "planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/overlay_images/ commit 736949b5fb325cc7880d0ff0bb13b78115b9f81c"
imgteam
parents:
diff changeset
51 im1 = read_im_gray(in_red_fn)
589af0005df5 "planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/overlay_images/ commit 736949b5fb325cc7880d0ff0bb13b78115b9f81c"
imgteam
parents:
diff changeset
52 im2 = read_im_gray(in_green_fn)
589af0005df5 "planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/overlay_images/ commit 736949b5fb325cc7880d0ff0bb13b78115b9f81c"
imgteam
parents:
diff changeset
53 assert im1.shape == im2.shape, 'Two images should have the same dimension'
589af0005df5 "planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/overlay_images/ commit 736949b5fb325cc7880d0ff0bb13b78115b9f81c"
imgteam
parents:
diff changeset
54
589af0005df5 "planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/overlay_images/ commit 736949b5fb325cc7880d0ff0bb13b78115b9f81c"
imgteam
parents:
diff changeset
55 vmin = np.min([np.min(im1), np.min(im2)])
589af0005df5 "planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/overlay_images/ commit 736949b5fb325cc7880d0ff0bb13b78115b9f81c"
imgteam
parents:
diff changeset
56 scal = 255.0 / (np.max([np.max(im1), np.max(im2)]) - vmin)
589af0005df5 "planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/overlay_images/ commit 736949b5fb325cc7880d0ff0bb13b78115b9f81c"
imgteam
parents:
diff changeset
57
589af0005df5 "planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/overlay_images/ commit 736949b5fb325cc7880d0ff0bb13b78115b9f81c"
imgteam
parents:
diff changeset
58 out_im = np.zeros(im1.shape + (3,), dtype=np.ubyte)
589af0005df5 "planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/overlay_images/ commit 736949b5fb325cc7880d0ff0bb13b78115b9f81c"
imgteam
parents:
diff changeset
59 out_im[:, :, 0] = (im1 - vmin) * scal
589af0005df5 "planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/overlay_images/ commit 736949b5fb325cc7880d0ff0bb13b78115b9f81c"
imgteam
parents:
diff changeset
60 out_im[:, :, 1] = (im2 - vmin) * scal
589af0005df5 "planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/overlay_images/ commit 736949b5fb325cc7880d0ff0bb13b78115b9f81c"
imgteam
parents:
diff changeset
61 skimage.io.imsave(out_fn, out_im) # output is RGB
589af0005df5 "planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/overlay_images/ commit 736949b5fb325cc7880d0ff0bb13b78115b9f81c"
imgteam
parents:
diff changeset
62
589af0005df5 "planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/overlay_images/ commit 736949b5fb325cc7880d0ff0bb13b78115b9f81c"
imgteam
parents:
diff changeset
63
589af0005df5 "planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/overlay_images/ commit 736949b5fb325cc7880d0ff0bb13b78115b9f81c"
imgteam
parents:
diff changeset
64 def blending(im1_fn, im2_fn, out_fn, alpha=0.5):
6
2495f8b2aefd planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/overlay_images/ commit c86a1b93cb7732f7331a981d13465653cc1a2790
imgteam
parents: 4
diff changeset
65 im1 = giatools.io.imread(im1_fn)
2495f8b2aefd planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/overlay_images/ commit c86a1b93cb7732f7331a981d13465653cc1a2790
imgteam
parents: 4
diff changeset
66 im2 = giatools.io.imread(im2_fn)
0
589af0005df5 "planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/overlay_images/ commit 736949b5fb325cc7880d0ff0bb13b78115b9f81c"
imgteam
parents:
diff changeset
67 assert im1.shape == im2.shape, 'Two images should have the same dimension'
589af0005df5 "planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/overlay_images/ commit 736949b5fb325cc7880d0ff0bb13b78115b9f81c"
imgteam
parents:
diff changeset
68 out_im = (1 - alpha) * im1 + alpha * im2
589af0005df5 "planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/overlay_images/ commit 736949b5fb325cc7880d0ff0bb13b78115b9f81c"
imgteam
parents:
diff changeset
69 if len(im1.shape) > 3:
589af0005df5 "planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/overlay_images/ commit 736949b5fb325cc7880d0ff0bb13b78115b9f81c"
imgteam
parents:
diff changeset
70 tifffile.imwrite(out_fn, out_im.astype(im1.dtype), imagej=True)
589af0005df5 "planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/overlay_images/ commit 736949b5fb325cc7880d0ff0bb13b78115b9f81c"
imgteam
parents:
diff changeset
71 else:
589af0005df5 "planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/overlay_images/ commit 736949b5fb325cc7880d0ff0bb13b78115b9f81c"
imgteam
parents:
diff changeset
72 skimage.io.imsave(out_fn, out_im.astype(im1.dtype)) # format of output is the same as input
589af0005df5 "planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/overlay_images/ commit 736949b5fb325cc7880d0ff0bb13b78115b9f81c"
imgteam
parents:
diff changeset
73
589af0005df5 "planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/overlay_images/ commit 736949b5fb325cc7880d0ff0bb13b78115b9f81c"
imgteam
parents:
diff changeset
74
2
b74693340624 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/overlay_images/ commit 71dae1df58f579b84d4f9d92fb0dd509c02dd48f
imgteam
parents: 0
diff changeset
75 def seg_contour(im1_fn, im2_fn, out_fn, linewidth, color='#ff0000', show_label=False, label_color='#ffff00'):
6
2495f8b2aefd planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/overlay_images/ commit c86a1b93cb7732f7331a981d13465653cc1a2790
imgteam
parents: 4
diff changeset
76 img = giatools.io.imread(im1_fn)
2495f8b2aefd planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/overlay_images/ commit c86a1b93cb7732f7331a981d13465653cc1a2790
imgteam
parents: 4
diff changeset
77 labels = giatools.io.imread(im2_fn)
2
b74693340624 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/overlay_images/ commit 71dae1df58f579b84d4f9d92fb0dd509c02dd48f
imgteam
parents: 0
diff changeset
78
b74693340624 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/overlay_images/ commit 71dae1df58f579b84d4f9d92fb0dd509c02dd48f
imgteam
parents: 0
diff changeset
79 result = get_rgb8_copy(img)
b74693340624 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/overlay_images/ commit 71dae1df58f579b84d4f9d92fb0dd509c02dd48f
imgteam
parents: 0
diff changeset
80 cp = ContourPaint(labels, linewidth, where='center')
b74693340624 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/overlay_images/ commit 71dae1df58f579b84d4f9d92fb0dd509c02dd48f
imgteam
parents: 0
diff changeset
81 color_rgb = np.multiply(255, matplotlib.colors.to_rgb(color))
0
589af0005df5 "planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/overlay_images/ commit 736949b5fb325cc7880d0ff0bb13b78115b9f81c"
imgteam
parents:
diff changeset
82
2
b74693340624 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/overlay_images/ commit 71dae1df58f579b84d4f9d92fb0dd509c02dd48f
imgteam
parents: 0
diff changeset
83 for label in np.unique(labels):
b74693340624 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/overlay_images/ commit 71dae1df58f579b84d4f9d92fb0dd509c02dd48f
imgteam
parents: 0
diff changeset
84 if label > 0:
b74693340624 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/overlay_images/ commit 71dae1df58f579b84d4f9d92fb0dd509c02dd48f
imgteam
parents: 0
diff changeset
85 cc = (labels == label)
b74693340624 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/overlay_images/ commit 71dae1df58f579b84d4f9d92fb0dd509c02dd48f
imgteam
parents: 0
diff changeset
86 bd = cp.get_contour_mask(cc)
b74693340624 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/overlay_images/ commit 71dae1df58f579b84d4f9d92fb0dd509c02dd48f
imgteam
parents: 0
diff changeset
87 for i in range(3):
b74693340624 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/overlay_images/ commit 71dae1df58f579b84d4f9d92fb0dd509c02dd48f
imgteam
parents: 0
diff changeset
88 result[:, :, i][bd] = color_rgb[i]
b74693340624 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/overlay_images/ commit 71dae1df58f579b84d4f9d92fb0dd509c02dd48f
imgteam
parents: 0
diff changeset
89
0
589af0005df5 "planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/overlay_images/ commit 736949b5fb325cc7880d0ff0bb13b78115b9f81c"
imgteam
parents:
diff changeset
90 if show_label:
2
b74693340624 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/overlay_images/ commit 71dae1df58f579b84d4f9d92fb0dd509c02dd48f
imgteam
parents: 0
diff changeset
91 fig = plt.figure(figsize=np.divide(img.shape[:2][::-1], 100), dpi=100)
b74693340624 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/overlay_images/ commit 71dae1df58f579b84d4f9d92fb0dd509c02dd48f
imgteam
parents: 0
diff changeset
92 ax = fig.add_axes([0, 0, 1, 1])
b74693340624 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/overlay_images/ commit 71dae1df58f579b84d4f9d92fb0dd509c02dd48f
imgteam
parents: 0
diff changeset
93 ax.axis('off')
b74693340624 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/overlay_images/ commit 71dae1df58f579b84d4f9d92fb0dd509c02dd48f
imgteam
parents: 0
diff changeset
94 ax.imshow(result)
b74693340624 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/overlay_images/ commit 71dae1df58f579b84d4f9d92fb0dd509c02dd48f
imgteam
parents: 0
diff changeset
95 for reg in skimage.measure.regionprops(labels):
b74693340624 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/overlay_images/ commit 71dae1df58f579b84d4f9d92fb0dd509c02dd48f
imgteam
parents: 0
diff changeset
96 ax.text(reg.centroid[1], reg.centroid[0], str(reg.label), color=label_color)
b74693340624 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/overlay_images/ commit 71dae1df58f579b84d4f9d92fb0dd509c02dd48f
imgteam
parents: 0
diff changeset
97 fig.canvas.print_png(out_fn)
0
589af0005df5 "planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/overlay_images/ commit 736949b5fb325cc7880d0ff0bb13b78115b9f81c"
imgteam
parents:
diff changeset
98
589af0005df5 "planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/overlay_images/ commit 736949b5fb325cc7880d0ff0bb13b78115b9f81c"
imgteam
parents:
diff changeset
99 else:
2
b74693340624 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/overlay_images/ commit 71dae1df58f579b84d4f9d92fb0dd509c02dd48f
imgteam
parents: 0
diff changeset
100 skimage.io.imsave(out_fn, result) # format of output is RGB8
0
589af0005df5 "planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/overlay_images/ commit 736949b5fb325cc7880d0ff0bb13b78115b9f81c"
imgteam
parents:
diff changeset
101
589af0005df5 "planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/overlay_images/ commit 736949b5fb325cc7880d0ff0bb13b78115b9f81c"
imgteam
parents:
diff changeset
102
589af0005df5 "planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/overlay_images/ commit 736949b5fb325cc7880d0ff0bb13b78115b9f81c"
imgteam
parents:
diff changeset
103 if __name__ == "__main__":
589af0005df5 "planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/overlay_images/ commit 736949b5fb325cc7880d0ff0bb13b78115b9f81c"
imgteam
parents:
diff changeset
104 parser = argparse.ArgumentParser(description="Overlay two images")
589af0005df5 "planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/overlay_images/ commit 736949b5fb325cc7880d0ff0bb13b78115b9f81c"
imgteam
parents:
diff changeset
105 parser.add_argument("im1", help="The first image")
589af0005df5 "planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/overlay_images/ commit 736949b5fb325cc7880d0ff0bb13b78115b9f81c"
imgteam
parents:
diff changeset
106 parser.add_argument("im2", help="The second image")
589af0005df5 "planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/overlay_images/ commit 736949b5fb325cc7880d0ff0bb13b78115b9f81c"
imgteam
parents:
diff changeset
107 parser.add_argument("out", help="Output image")
589af0005df5 "planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/overlay_images/ commit 736949b5fb325cc7880d0ff0bb13b78115b9f81c"
imgteam
parents:
diff changeset
108 parser.add_argument('--method', dest='method', default='coloc_vis', help='How to overlay images')
589af0005df5 "planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/overlay_images/ commit 736949b5fb325cc7880d0ff0bb13b78115b9f81c"
imgteam
parents:
diff changeset
109 parser.add_argument('--alpha', dest='alpha', default=0.5, type=float, help='Blending weight')
2
b74693340624 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/overlay_images/ commit 71dae1df58f579b84d4f9d92fb0dd509c02dd48f
imgteam
parents: 0
diff changeset
110 parser.add_argument('--thickness', dest='thickness', default=2, type=int, help='Contour thickness')
b74693340624 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/overlay_images/ commit 71dae1df58f579b84d4f9d92fb0dd509c02dd48f
imgteam
parents: 0
diff changeset
111 parser.add_argument('--color', dest='color', default='#FF0000', help='Contour color')
b74693340624 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/overlay_images/ commit 71dae1df58f579b84d4f9d92fb0dd509c02dd48f
imgteam
parents: 0
diff changeset
112 parser.add_argument('--show_label', dest='show_label', action='store_true', help='Show labels')
b74693340624 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/overlay_images/ commit 71dae1df58f579b84d4f9d92fb0dd509c02dd48f
imgteam
parents: 0
diff changeset
113 parser.add_argument('--label_color', dest='label_color', default='#FFFF00', help='Label color')
0
589af0005df5 "planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/overlay_images/ commit 736949b5fb325cc7880d0ff0bb13b78115b9f81c"
imgteam
parents:
diff changeset
114 args = parser.parse_args()
589af0005df5 "planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/overlay_images/ commit 736949b5fb325cc7880d0ff0bb13b78115b9f81c"
imgteam
parents:
diff changeset
115
589af0005df5 "planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/overlay_images/ commit 736949b5fb325cc7880d0ff0bb13b78115b9f81c"
imgteam
parents:
diff changeset
116 if args.method == 'coloc_vis':
589af0005df5 "planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/overlay_images/ commit 736949b5fb325cc7880d0ff0bb13b78115b9f81c"
imgteam
parents:
diff changeset
117 coloc_vis(args.im1, args.im2, args.out)
589af0005df5 "planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/overlay_images/ commit 736949b5fb325cc7880d0ff0bb13b78115b9f81c"
imgteam
parents:
diff changeset
118 elif args.method == 'blending':
589af0005df5 "planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/overlay_images/ commit 736949b5fb325cc7880d0ff0bb13b78115b9f81c"
imgteam
parents:
diff changeset
119 blending(args.im1, args.im2, args.out, alpha=args.alpha)
589af0005df5 "planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/overlay_images/ commit 736949b5fb325cc7880d0ff0bb13b78115b9f81c"
imgteam
parents:
diff changeset
120 elif args.method == 'seg_contour':
589af0005df5 "planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/overlay_images/ commit 736949b5fb325cc7880d0ff0bb13b78115b9f81c"
imgteam
parents:
diff changeset
121 seg_contour(args.im1, args.im2, args.out,
589af0005df5 "planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/overlay_images/ commit 736949b5fb325cc7880d0ff0bb13b78115b9f81c"
imgteam
parents:
diff changeset
122 linewidth=args.thickness,
589af0005df5 "planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/overlay_images/ commit 736949b5fb325cc7880d0ff0bb13b78115b9f81c"
imgteam
parents:
diff changeset
123 color=args.color,
2
b74693340624 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/overlay_images/ commit 71dae1df58f579b84d4f9d92fb0dd509c02dd48f
imgteam
parents: 0
diff changeset
124 show_label=args.show_label,
b74693340624 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/overlay_images/ commit 71dae1df58f579b84d4f9d92fb0dd509c02dd48f
imgteam
parents: 0
diff changeset
125 label_color=args.label_color)