Mercurial > repos > imgteam > overlay_images
annotate overlay_images.py @ 4:37662cbf44b8 draft
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/overlay_images/ commit 85185439087caafebf1c75dc78b61a2a1f181225
author | imgteam |
---|---|
date | Fri, 17 Nov 2023 23:34:57 +0000 |
parents | b74693340624 |
children | 2495f8b2aefd |
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 |
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
|
10 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
|
11 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
|
12 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
|
13 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
|
14 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
|
15 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
|
16 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
|
17 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
|
18 |
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 def read_im_gray(fn): |
589af0005df5
"planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/overlay_images/ commit 736949b5fb325cc7880d0ff0bb13b78115b9f81c"
imgteam
parents:
diff
changeset
|
21 img = skimage.io.imread(fn) |
589af0005df5
"planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/overlay_images/ commit 736949b5fb325cc7880d0ff0bb13b78115b9f81c"
imgteam
parents:
diff
changeset
|
22 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
|
23 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
|
24 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
|
25 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
|
26 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
|
27 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
|
28 else: |
589af0005df5
"planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/overlay_images/ commit 736949b5fb325cc7880d0ff0bb13b78115b9f81c"
imgteam
parents:
diff
changeset
|
29 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
|
30 |
589af0005df5
"planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/overlay_images/ commit 736949b5fb325cc7880d0ff0bb13b78115b9f81c"
imgteam
parents:
diff
changeset
|
31 |
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
|
32 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
|
33 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
|
34 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
|
35 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
|
36 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
|
37 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
|
38 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
|
39 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
|
40 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
|
41 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
|
42 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
|
43 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
|
44 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
|
45 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
|
46 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
|
47 |
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 |
0
589af0005df5
"planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/overlay_images/ commit 736949b5fb325cc7880d0ff0bb13b78115b9f81c"
imgteam
parents:
diff
changeset
|
49 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
|
50 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
|
51 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
|
52 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
|
53 |
589af0005df5
"planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/overlay_images/ commit 736949b5fb325cc7880d0ff0bb13b78115b9f81c"
imgteam
parents:
diff
changeset
|
54 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
|
55 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
|
56 |
589af0005df5
"planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/overlay_images/ commit 736949b5fb325cc7880d0ff0bb13b78115b9f81c"
imgteam
parents:
diff
changeset
|
57 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
|
58 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
|
59 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
|
60 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
|
61 |
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 def blending(im1_fn, im2_fn, out_fn, alpha=0.5): |
589af0005df5
"planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/overlay_images/ commit 736949b5fb325cc7880d0ff0bb13b78115b9f81c"
imgteam
parents:
diff
changeset
|
64 im1 = skimage.io.imread(im1_fn) |
589af0005df5
"planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/overlay_images/ commit 736949b5fb325cc7880d0ff0bb13b78115b9f81c"
imgteam
parents:
diff
changeset
|
65 im2 = skimage.io.imread(im2_fn) |
589af0005df5
"planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/overlay_images/ commit 736949b5fb325cc7880d0ff0bb13b78115b9f81c"
imgteam
parents:
diff
changeset
|
66 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
|
67 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
|
68 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
|
69 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
|
70 else: |
589af0005df5
"planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/overlay_images/ commit 736949b5fb325cc7880d0ff0bb13b78115b9f81c"
imgteam
parents:
diff
changeset
|
71 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
|
72 |
589af0005df5
"planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/overlay_images/ commit 736949b5fb325cc7880d0ff0bb13b78115b9f81c"
imgteam
parents:
diff
changeset
|
73 |
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
|
74 def seg_contour(im1_fn, im2_fn, out_fn, linewidth, color='#ff0000', show_label=False, label_color='#ffff00'): |
0
589af0005df5
"planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/overlay_images/ commit 736949b5fb325cc7880d0ff0bb13b78115b9f81c"
imgteam
parents:
diff
changeset
|
75 img = skimage.io.imread(im1_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
|
76 labels = skimage.io.imread(im2_fn) |
b74693340624
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/overlay_images/ commit 71dae1df58f579b84d4f9d92fb0dd509c02dd48f
imgteam
parents:
0
diff
changeset
|
77 |
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 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
|
79 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
|
80 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
|
81 |
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
|
82 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
|
83 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
|
84 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
|
85 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
|
86 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
|
87 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
|
88 |
0
589af0005df5
"planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/overlay_images/ commit 736949b5fb325cc7880d0ff0bb13b78115b9f81c"
imgteam
parents:
diff
changeset
|
89 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
|
90 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
|
91 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
|
92 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
|
93 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
|
94 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
|
95 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
|
96 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
|
97 |
589af0005df5
"planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/overlay_images/ commit 736949b5fb325cc7880d0ff0bb13b78115b9f81c"
imgteam
parents:
diff
changeset
|
98 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
|
99 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
|
100 |
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 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
|
103 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
|
104 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
|
105 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
|
106 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
|
107 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
|
108 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
|
109 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
|
110 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
|
111 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
|
112 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
|
113 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
|
114 |
589af0005df5
"planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/overlay_images/ commit 736949b5fb325cc7880d0ff0bb13b78115b9f81c"
imgteam
parents:
diff
changeset
|
115 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
|
116 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
|
117 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
|
118 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
|
119 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
|
120 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
|
121 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
|
122 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
|
123 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
|
124 label_color=args.label_color) |