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