comparison overlay_images.py @ 3:eb173a1fabc4 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:14 +0000
parents f42d21fe65d8
children
comparison
equal deleted inserted replaced
2:f42d21fe65d8 3:eb173a1fabc4
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 """ 6 """
7 7
8 import argparse 8 import argparse
9 9
10 import giatools.io
10 import matplotlib.colors 11 import matplotlib.colors
11 import matplotlib.pyplot as plt 12 import matplotlib.pyplot as plt
12 import numpy as np 13 import numpy as np
13 import skimage.color 14 import skimage.color
14 import skimage.io 15 import skimage.io
16 import tifffile 17 import tifffile
17 from contours import ContourPaint 18 from contours import ContourPaint
18 19
19 20
20 def read_im_gray(fn): 21 def read_im_gray(fn):
21 img = skimage.io.imread(fn) 22 img = giatools.io.imread(fn)
22 nDims = len(img.shape) 23 nDims = len(img.shape)
23 assert nDims in [2, 3], 'this tool only supports single 2D images' 24 assert nDims in [2, 3], 'this tool only supports single 2D images'
24 if nDims == 3 and img.shape[-1] in [3, 4]: 25 if nDims == 3 and img.shape[-1] in [3, 4]:
25 img = skimage.color.rgb2gray(img) 26 img = skimage.color.rgb2gray(img)
26 if len(img.shape) == 3: 27 if len(img.shape) == 3:
31 32
32 def get_rgb8_copy(img): 33 def get_rgb8_copy(img):
33 img = np.squeeze(img) 34 img = np.squeeze(img)
34 assert img.ndim == 2 or (img.ndim == 3 and img.shape[-1] in (3, 4)) 35 assert img.ndim == 2 or (img.ndim == 3 and img.shape[-1] in (3, 4))
35 if str(img.dtype).startswith('float'): 36 if str(img.dtype).startswith('float'):
36 img = np.round(img * 255).astype('uint8') 37 img = np.round(img * 255).astype(np.uint8)
37 elif img.dtype == 'uint16': 38 elif img.dtype == np.uint16:
38 img = img // 256 39 img = (img // 256).astype(np.uint8)
39 elif img.dtype != 'uint8': 40 elif img.dtype != np.uint8:
40 raise ValueError(f'unknown dtype: {img.dtype}') 41 raise ValueError(f'unknown dtype: {img.dtype}')
41 if img.ndim == 2: 42 if img.ndim == 2:
42 return np.dstack([img] * 3).copy() 43 result = np.dstack([img] * 3).copy()
43 else: 44 else:
44 return img[:, :, :3].copy() 45 result = img[:, :, :3].copy()
46 assert result.dtype == np.uint8, result.dtype
47 return result
45 48
46 49
47 def coloc_vis(in_red_fn, in_green_fn, out_fn): 50 def coloc_vis(in_red_fn, in_green_fn, out_fn):
48 im1 = read_im_gray(in_red_fn) 51 im1 = read_im_gray(in_red_fn)
49 im2 = read_im_gray(in_green_fn) 52 im2 = read_im_gray(in_green_fn)
57 out_im[:, :, 1] = (im2 - vmin) * scal 60 out_im[:, :, 1] = (im2 - vmin) * scal
58 skimage.io.imsave(out_fn, out_im) # output is RGB 61 skimage.io.imsave(out_fn, out_im) # output is RGB
59 62
60 63
61 def blending(im1_fn, im2_fn, out_fn, alpha=0.5): 64 def blending(im1_fn, im2_fn, out_fn, alpha=0.5):
62 im1 = skimage.io.imread(im1_fn) 65 im1 = giatools.io.imread(im1_fn)
63 im2 = skimage.io.imread(im2_fn) 66 im2 = giatools.io.imread(im2_fn)
64 assert im1.shape == im2.shape, 'Two images should have the same dimension' 67 assert im1.shape == im2.shape, 'Two images should have the same dimension'
65 out_im = (1 - alpha) * im1 + alpha * im2 68 out_im = (1 - alpha) * im1 + alpha * im2
66 if len(im1.shape) > 3: 69 if len(im1.shape) > 3:
67 tifffile.imwrite(out_fn, out_im.astype(im1.dtype), imagej=True) 70 tifffile.imwrite(out_fn, out_im.astype(im1.dtype), imagej=True)
68 else: 71 else:
69 skimage.io.imsave(out_fn, out_im.astype(im1.dtype)) # format of output is the same as input 72 skimage.io.imsave(out_fn, out_im.astype(im1.dtype)) # format of output is the same as input
70 73
71 74
72 def seg_contour(im1_fn, im2_fn, out_fn, linewidth, color='#ff0000', show_label=False, label_color='#ffff00'): 75 def seg_contour(im1_fn, im2_fn, out_fn, linewidth, color='#ff0000', show_label=False, label_color='#ffff00'):
73 img = skimage.io.imread(im1_fn) 76 img = giatools.io.imread(im1_fn)
74 labels = skimage.io.imread(im2_fn) 77 labels = giatools.io.imread(im2_fn)
75 78
76 result = get_rgb8_copy(img) 79 result = get_rgb8_copy(img)
77 cp = ContourPaint(labels, linewidth, where='center') 80 cp = ContourPaint(labels, linewidth, where='center')
78 color_rgb = np.multiply(255, matplotlib.colors.to_rgb(color)) 81 color_rgb = np.multiply(255, matplotlib.colors.to_rgb(color))
79 82