comparison overlay_moving_and_fixed_image.py @ 0:bd06c3ba70b0 draft default tip

planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/overlay_moving_and_fixed_image/ commit 787ebcc8daa1834214bc92c201c921c704ef2d1f
author thomaswollmann
date Mon, 07 Jan 2019 05:37:21 -0500
parents
children
comparison
equal deleted inserted replaced
-1:000000000000 0:bd06c3ba70b0
1 import argparse
2 from PIL import Image
3 import skimage.io
4 import skimage.color
5 from skimage.transform import ProjectiveTransform
6 from scipy.ndimage import map_coordinates
7 import numpy as np
8 import pandas as pd
9
10
11 def _stackcopy(a, b):
12 if a.ndim == 3:
13 a[:] = b[:, :, np.newaxis]
14 else:
15 a[:] = b
16
17
18 def warp_coords_batch(coord_map, shape, dtype=np.float64, batch_size=1000000):
19 rows, cols = shape[0], shape[1]
20 coords_shape = [len(shape), rows, cols]
21 if len(shape) == 3:
22 coords_shape.append(shape[2])
23 coords = np.empty(coords_shape, dtype=dtype)
24
25 tf_coords = np.indices((cols, rows), dtype=dtype).reshape(2, -1).T
26
27 for i in range(0, (tf_coords.shape[0]//batch_size+1)):
28 tf_coords[batch_size*i:batch_size*(i+1)] = coord_map(tf_coords[batch_size*i:batch_size*(i+1)])
29 tf_coords = tf_coords.T.reshape((-1, cols, rows)).swapaxes(1, 2)
30
31 _stackcopy(coords[1, ...], tf_coords[0, ...])
32 _stackcopy(coords[0, ...], tf_coords[1, ...])
33 if len(shape) == 3:
34 coords[2, ...] = range(shape[2])
35
36 return coords
37
38
39 def transform(moving_image, fixed_image, warp_matrix):
40 trans = ProjectiveTransform(matrix=warp_matrix)
41 warped_coords = warp_coords_batch(trans, fixed_image.shape)
42 return map_coordinates(moving_image, warped_coords)
43
44
45 def overlay(moving_image, fixed_image, factor, overlay_out_path):
46 moving_image = Image.fromarray(moving_image).convert("RGBA")
47 fixed_image = Image.fromarray(fixed_image).convert("RGBA")
48 overlay_out = Image.blend(moving_image, fixed_image, factor)
49 overlay_out.save(overlay_out_path, "PNG")
50
51
52 if __name__=="__main__":
53 parser = argparse.ArgumentParser(description = "Overlay two images")
54 parser.add_argument("fixed_image", help = "Path to fixed image")
55 parser.add_argument("moving_image", help = "Path to moving image")
56 parser.add_argument("warp_matrix", help="Paste path to warp_matrix.csv that should be used for transformation")
57 parser.add_argument("--inverse_transform", dest='inverse_transform', action='store_true', help="Set if inverse transform should be visualized")
58 parser.add_argument("--factor", dest = "factor", help = "Enter the factor by which images should be blended, 1.0 returns a copy of second image", type = float, default = 0.5)
59 parser.add_argument("overlay_out", help = "Overlay output path")
60 args = parser.parse_args()
61
62 fixed_image = skimage.io.imread(args.fixed_image)
63 moving_image = skimage.io.imread(args.moving_image)
64
65 warp_matrix = pd.read_csv(args.warp_matrix, delimiter=",", header=None)
66 warp_matrix = np.array(warp_matrix)
67 if args.inverse_transform:
68 fixed_image = transform(fixed_image, moving_image, warp_matrix)
69 else:
70 warp_matrix = np.linalg.inv(warp_matrix)
71 moving_image = transform(moving_image, fixed_image, warp_matrix)
72
73 overlay(moving_image, fixed_image, args.factor, args.overlay_out)