annotate overlay_images.py @ 2:b74693340624 draft

planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/overlay_images/ commit 71dae1df58f579b84d4f9d92fb0dd509c02dd48f
author imgteam
date Thu, 10 Aug 2023 07:29:34 +0000
parents 589af0005df5
children 37662cbf44b8
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
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'):
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 img = np.round(img * 255).astype('uint8')
b74693340624 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/overlay_images/ commit 71dae1df58f579b84d4f9d92fb0dd509c02dd48f
imgteam
parents: 0
diff changeset
37 elif img.dtype == 'uint16':
b74693340624 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/overlay_images/ commit 71dae1df58f579b84d4f9d92fb0dd509c02dd48f
imgteam
parents: 0
diff changeset
38 img = img // 256
b74693340624 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/overlay_images/ commit 71dae1df58f579b84d4f9d92fb0dd509c02dd48f
imgteam
parents: 0
diff changeset
39 elif img.dtype != 'uint8':
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:
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 return np.dstack([img] * 3).copy()
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:
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 return img[:, :, :3].copy()
b74693340624 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/overlay_images/ commit 71dae1df58f579b84d4f9d92fb0dd509c02dd48f
imgteam
parents: 0
diff changeset
45
b74693340624 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/overlay_images/ commit 71dae1df58f579b84d4f9d92fb0dd509c02dd48f
imgteam
parents: 0
diff changeset
46
0
589af0005df5 "planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/overlay_images/ commit 736949b5fb325cc7880d0ff0bb13b78115b9f81c"
imgteam
parents:
diff changeset
47 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
48 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
49 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
50 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
51
589af0005df5 "planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/overlay_images/ commit 736949b5fb325cc7880d0ff0bb13b78115b9f81c"
imgteam
parents:
diff changeset
52 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
53 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
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 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
56 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
57 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
58 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
59
589af0005df5 "planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/overlay_images/ commit 736949b5fb325cc7880d0ff0bb13b78115b9f81c"
imgteam
parents:
diff changeset
60
589af0005df5 "planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/overlay_images/ commit 736949b5fb325cc7880d0ff0bb13b78115b9f81c"
imgteam
parents:
diff changeset
61 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
62 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
63 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
64 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
65 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
66 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
67 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
68 else:
589af0005df5 "planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/overlay_images/ commit 736949b5fb325cc7880d0ff0bb13b78115b9f81c"
imgteam
parents:
diff changeset
69 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
70
589af0005df5 "planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/overlay_images/ commit 736949b5fb325cc7880d0ff0bb13b78115b9f81c"
imgteam
parents:
diff changeset
71
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
72 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
73 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
74 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
75
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 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
77 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
78 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
79
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
80 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
81 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
82 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
83 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
84 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
85 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
86
0
589af0005df5 "planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/overlay_images/ commit 736949b5fb325cc7880d0ff0bb13b78115b9f81c"
imgteam
parents:
diff changeset
87 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
88 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
89 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
90 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
91 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
92 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
93 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
94 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
95
589af0005df5 "planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/overlay_images/ commit 736949b5fb325cc7880d0ff0bb13b78115b9f81c"
imgteam
parents:
diff changeset
96 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
97 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
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
589af0005df5 "planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/overlay_images/ commit 736949b5fb325cc7880d0ff0bb13b78115b9f81c"
imgteam
parents:
diff changeset
100 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
101 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
102 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
103 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
104 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
105 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
106 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
107 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
108 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
109 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
110 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
111 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
112
589af0005df5 "planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/overlay_images/ commit 736949b5fb325cc7880d0ff0bb13b78115b9f81c"
imgteam
parents:
diff changeset
113 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
114 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
115 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
116 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
117 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
118 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
119 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
120 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
121 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
122 label_color=args.label_color)