comparison overlay_images.py @ 0:589af0005df5 draft

"planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/overlay_images/ commit 736949b5fb325cc7880d0ff0bb13b78115b9f81c"
author imgteam
date Sat, 26 Feb 2022 15:16:10 +0000
parents
children b74693340624
comparison
equal deleted inserted replaced
-1:000000000000 0:589af0005df5
1 """
2 Copyright 2022 Biomedical Computer Vision Group, Heidelberg University.
3
4 Distributed under the MIT license.
5 See file LICENSE for detail or copy at https://opensource.org/licenses/MIT
6
7 """
8
9 import argparse
10
11 import matplotlib.pyplot as plt
12 import numpy as np
13 import skimage.color
14 import skimage.io
15 import skimage.measure
16 import tifffile
17
18
19 def read_im_gray(fn):
20 img = skimage.io.imread(fn)
21 nDims = len(img.shape)
22 assert nDims in [2, 3], 'this tool only supports single 2D images'
23 if nDims == 3 and img.shape[-1] in [3, 4]:
24 img = skimage.color.rgb2gray(img)
25 if len(img.shape) == 3:
26 return img[:, :, 0]
27 else:
28 return img
29
30
31 def coloc_vis(in_red_fn, in_green_fn, out_fn):
32 im1 = read_im_gray(in_red_fn)
33 im2 = read_im_gray(in_green_fn)
34 assert im1.shape == im2.shape, 'Two images should have the same dimension'
35
36 vmin = np.min([np.min(im1), np.min(im2)])
37 scal = 255.0 / (np.max([np.max(im1), np.max(im2)]) - vmin)
38
39 out_im = np.zeros(im1.shape + (3,), dtype=np.ubyte)
40 out_im[:, :, 0] = (im1 - vmin) * scal
41 out_im[:, :, 1] = (im2 - vmin) * scal
42 skimage.io.imsave(out_fn, out_im) # output is RGB
43
44
45 def blending(im1_fn, im2_fn, out_fn, alpha=0.5):
46 im1 = skimage.io.imread(im1_fn)
47 im2 = skimage.io.imread(im2_fn)
48 assert im1.shape == im2.shape, 'Two images should have the same dimension'
49 out_im = (1 - alpha) * im1 + alpha * im2
50 if len(im1.shape) > 3:
51 tifffile.imwrite(out_fn, out_im.astype(im1.dtype), imagej=True)
52 else:
53 skimage.io.imsave(out_fn, out_im.astype(im1.dtype)) # format of output is the same as input
54
55
56 def seg_contour(im1_fn, im2_fn, out_fn, linewidth=0.3, color='#ff0000', show_label=False):
57 img = skimage.io.imread(im1_fn)
58 label = skimage.io.imread(im2_fn)
59
60 fig = plt.figure()
61 ax = fig.add_axes([0, 0, 1, 1])
62 ax.axis('off')
63 if show_label:
64 for reg in skimage.measure.regionprops(label):
65 ax.text(reg.centroid[1], reg.centroid[0], str(reg.label), color=color)
66
67 if len(img.shape) == 2:
68 plt.imshow(img, cmap=plt.cm.gray)
69 else:
70 plt.imshow(img)
71 plt.contour(label, linewidths=linewidth, colors=color)
72 fig.canvas.print_png(out_fn) # output is RGB
73
74
75 if __name__ == "__main__":
76 parser = argparse.ArgumentParser(description="Overlay two images")
77 parser.add_argument("im1", help="The first image")
78 parser.add_argument("im2", help="The second image")
79 parser.add_argument("out", help="Output image")
80 parser.add_argument('--method', dest='method', default='coloc_vis', help='How to overlay images')
81 parser.add_argument('--alpha', dest='alpha', default=0.5, type=float, help='Blending weight')
82 parser.add_argument('--thickness', dest='thickness', default=0.3, type=float, help='Contour thickness')
83 parser.add_argument('--color', dest='color', default='#FFFF00', help='Contour color')
84 parser.add_argument('--show_label', dest='show_label', action='store_true', help='Plot label')
85 args = parser.parse_args()
86
87 if args.method == 'coloc_vis':
88 coloc_vis(args.im1, args.im2, args.out)
89 elif args.method == 'blending':
90 blending(args.im1, args.im2, args.out, alpha=args.alpha)
91 elif args.method == 'seg_contour':
92 seg_contour(args.im1, args.im2, args.out,
93 linewidth=args.thickness,
94 color=args.color,
95 show_label=args.show_label)