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