comparison 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
comparison
equal deleted inserted replaced
1:bf590a9733ed 2:b74693340624
1 """ 1 """
2 Copyright 2022 Biomedical Computer Vision Group, Heidelberg University. 2 Copyright 2022-2023 Biomedical Computer Vision Group, Heidelberg University.
3 3
4 Distributed under the MIT license. 4 Distributed under the MIT license.
5 See file LICENSE for detail or copy at https://opensource.org/licenses/MIT 5 See file LICENSE for detail or copy at https://opensource.org/licenses/MIT
6
7 """ 6 """
8 7
9 import argparse 8 import argparse
10 9
10 import matplotlib.colors
11 import matplotlib.pyplot as plt 11 import matplotlib.pyplot as plt
12 import numpy as np 12 import numpy as np
13 import skimage.color 13 import skimage.color
14 import skimage.io 14 import skimage.io
15 import skimage.measure 15 import skimage.measure
16 import tifffile 16 import tifffile
17 from contours import ContourPaint
17 18
18 19
19 def read_im_gray(fn): 20 def read_im_gray(fn):
20 img = skimage.io.imread(fn) 21 img = skimage.io.imread(fn)
21 nDims = len(img.shape) 22 nDims = len(img.shape)
24 img = skimage.color.rgb2gray(img) 25 img = skimage.color.rgb2gray(img)
25 if len(img.shape) == 3: 26 if len(img.shape) == 3:
26 return img[:, :, 0] 27 return img[:, :, 0]
27 else: 28 else:
28 return img 29 return img
30
31
32 def get_rgb8_copy(img):
33 img = np.squeeze(img)
34 assert img.ndim == 2 or (img.ndim == 3 and img.shape[-1] in (3, 4))
35 if str(img.dtype).startswith('float'):
36 img = np.round(img * 255).astype('uint8')
37 elif img.dtype == 'uint16':
38 img = img // 256
39 elif img.dtype != 'uint8':
40 raise ValueError(f'unknown dtype: {img.dtype}')
41 if img.ndim == 2:
42 return np.dstack([img] * 3).copy()
43 else:
44 return img[:, :, :3].copy()
29 45
30 46
31 def coloc_vis(in_red_fn, in_green_fn, out_fn): 47 def coloc_vis(in_red_fn, in_green_fn, out_fn):
32 im1 = read_im_gray(in_red_fn) 48 im1 = read_im_gray(in_red_fn)
33 im2 = read_im_gray(in_green_fn) 49 im2 = read_im_gray(in_green_fn)
51 tifffile.imwrite(out_fn, out_im.astype(im1.dtype), imagej=True) 67 tifffile.imwrite(out_fn, out_im.astype(im1.dtype), imagej=True)
52 else: 68 else:
53 skimage.io.imsave(out_fn, out_im.astype(im1.dtype)) # format of output is the same as input 69 skimage.io.imsave(out_fn, out_im.astype(im1.dtype)) # format of output is the same as input
54 70
55 71
56 def seg_contour(im1_fn, im2_fn, out_fn, linewidth=0.3, color='#ff0000', show_label=False): 72 def seg_contour(im1_fn, im2_fn, out_fn, linewidth, color='#ff0000', show_label=False, label_color='#ffff00'):
57 img = skimage.io.imread(im1_fn) 73 img = skimage.io.imread(im1_fn)
58 label = skimage.io.imread(im2_fn) 74 labels = skimage.io.imread(im2_fn)
59 75
60 fig = plt.figure() 76 result = get_rgb8_copy(img)
61 ax = fig.add_axes([0, 0, 1, 1]) 77 cp = ContourPaint(labels, linewidth, where='center')
62 ax.axis('off') 78 color_rgb = np.multiply(255, matplotlib.colors.to_rgb(color))
79
80 for label in np.unique(labels):
81 if label > 0:
82 cc = (labels == label)
83 bd = cp.get_contour_mask(cc)
84 for i in range(3):
85 result[:, :, i][bd] = color_rgb[i]
86
63 if show_label: 87 if show_label:
64 for reg in skimage.measure.regionprops(label): 88 fig = plt.figure(figsize=np.divide(img.shape[:2][::-1], 100), dpi=100)
65 ax.text(reg.centroid[1], reg.centroid[0], str(reg.label), color=color) 89 ax = fig.add_axes([0, 0, 1, 1])
90 ax.axis('off')
91 ax.imshow(result)
92 for reg in skimage.measure.regionprops(labels):
93 ax.text(reg.centroid[1], reg.centroid[0], str(reg.label), color=label_color)
94 fig.canvas.print_png(out_fn)
66 95
67 if len(img.shape) == 2:
68 plt.imshow(img, cmap=plt.cm.gray)
69 else: 96 else:
70 plt.imshow(img) 97 skimage.io.imsave(out_fn, result) # format of output is RGB8
71 plt.contour(label, linewidths=linewidth, colors=color)
72 fig.canvas.print_png(out_fn) # output is RGB
73 98
74 99
75 if __name__ == "__main__": 100 if __name__ == "__main__":
76 parser = argparse.ArgumentParser(description="Overlay two images") 101 parser = argparse.ArgumentParser(description="Overlay two images")
77 parser.add_argument("im1", help="The first image") 102 parser.add_argument("im1", help="The first image")
78 parser.add_argument("im2", help="The second image") 103 parser.add_argument("im2", help="The second image")
79 parser.add_argument("out", help="Output image") 104 parser.add_argument("out", help="Output image")
80 parser.add_argument('--method', dest='method', default='coloc_vis', help='How to overlay images') 105 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') 106 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') 107 parser.add_argument('--thickness', dest='thickness', default=2, type=int, help='Contour thickness')
83 parser.add_argument('--color', dest='color', default='#FFFF00', help='Contour color') 108 parser.add_argument('--color', dest='color', default='#FF0000', help='Contour color')
84 parser.add_argument('--show_label', dest='show_label', action='store_true', help='Plot label') 109 parser.add_argument('--show_label', dest='show_label', action='store_true', help='Show labels')
110 parser.add_argument('--label_color', dest='label_color', default='#FFFF00', help='Label color')
85 args = parser.parse_args() 111 args = parser.parse_args()
86 112
87 if args.method == 'coloc_vis': 113 if args.method == 'coloc_vis':
88 coloc_vis(args.im1, args.im2, args.out) 114 coloc_vis(args.im1, args.im2, args.out)
89 elif args.method == 'blending': 115 elif args.method == 'blending':
90 blending(args.im1, args.im2, args.out, alpha=args.alpha) 116 blending(args.im1, args.im2, args.out, alpha=args.alpha)
91 elif args.method == 'seg_contour': 117 elif args.method == 'seg_contour':
92 seg_contour(args.im1, args.im2, args.out, 118 seg_contour(args.im1, args.im2, args.out,
93 linewidth=args.thickness, 119 linewidth=args.thickness,
94 color=args.color, 120 color=args.color,
95 show_label=args.show_label) 121 show_label=args.show_label,
122 label_color=args.label_color)