annotate projective_transformation.py @ 1:974cf4357707 draft

"planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/projective_transformation/ commit 3d389fdec0db29cf6fbd783c0501455bf624fa90"
author imgteam
date Wed, 18 Dec 2019 05:03:09 -0500
parents 17f5d0c3f8a3
children 1ffdb07020ee
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
17f5d0c3f8a3 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/projective_transformation/ commit c3f4b766f03770f094fda6bda0a5882c0ebd4581
imgteam
parents:
diff changeset
1 import skimage.io
17f5d0c3f8a3 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/projective_transformation/ commit c3f4b766f03770f094fda6bda0a5882c0ebd4581
imgteam
parents:
diff changeset
2 from skimage.transform import ProjectiveTransform
17f5d0c3f8a3 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/projective_transformation/ commit c3f4b766f03770f094fda6bda0a5882c0ebd4581
imgteam
parents:
diff changeset
3 from scipy.ndimage import map_coordinates
17f5d0c3f8a3 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/projective_transformation/ commit c3f4b766f03770f094fda6bda0a5882c0ebd4581
imgteam
parents:
diff changeset
4 import numpy as np
17f5d0c3f8a3 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/projective_transformation/ commit c3f4b766f03770f094fda6bda0a5882c0ebd4581
imgteam
parents:
diff changeset
5 import pandas as pd
1
974cf4357707 "planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/projective_transformation/ commit 3d389fdec0db29cf6fbd783c0501455bf624fa90"
imgteam
parents: 0
diff changeset
6 import argparse
0
17f5d0c3f8a3 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/projective_transformation/ commit c3f4b766f03770f094fda6bda0a5882c0ebd4581
imgteam
parents:
diff changeset
7 import warnings
1
974cf4357707 "planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/projective_transformation/ commit 3d389fdec0db29cf6fbd783c0501455bf624fa90"
imgteam
parents: 0
diff changeset
8 import shutil
0
17f5d0c3f8a3 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/projective_transformation/ commit c3f4b766f03770f094fda6bda0a5882c0ebd4581
imgteam
parents:
diff changeset
9
17f5d0c3f8a3 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/projective_transformation/ commit c3f4b766f03770f094fda6bda0a5882c0ebd4581
imgteam
parents:
diff changeset
10
17f5d0c3f8a3 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/projective_transformation/ commit c3f4b766f03770f094fda6bda0a5882c0ebd4581
imgteam
parents:
diff changeset
11 def _stackcopy(a, b):
17f5d0c3f8a3 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/projective_transformation/ commit c3f4b766f03770f094fda6bda0a5882c0ebd4581
imgteam
parents:
diff changeset
12 if a.ndim == 3:
17f5d0c3f8a3 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/projective_transformation/ commit c3f4b766f03770f094fda6bda0a5882c0ebd4581
imgteam
parents:
diff changeset
13 a[:] = b[:, :, np.newaxis]
17f5d0c3f8a3 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/projective_transformation/ commit c3f4b766f03770f094fda6bda0a5882c0ebd4581
imgteam
parents:
diff changeset
14 else:
17f5d0c3f8a3 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/projective_transformation/ commit c3f4b766f03770f094fda6bda0a5882c0ebd4581
imgteam
parents:
diff changeset
15 a[:] = b
17f5d0c3f8a3 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/projective_transformation/ commit c3f4b766f03770f094fda6bda0a5882c0ebd4581
imgteam
parents:
diff changeset
16
17f5d0c3f8a3 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/projective_transformation/ commit c3f4b766f03770f094fda6bda0a5882c0ebd4581
imgteam
parents:
diff changeset
17
17f5d0c3f8a3 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/projective_transformation/ commit c3f4b766f03770f094fda6bda0a5882c0ebd4581
imgteam
parents:
diff changeset
18 def warp_coords_batch(coord_map, shape, dtype=np.float64, batch_size=1000000):
17f5d0c3f8a3 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/projective_transformation/ commit c3f4b766f03770f094fda6bda0a5882c0ebd4581
imgteam
parents:
diff changeset
19 rows, cols = shape[0], shape[1]
17f5d0c3f8a3 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/projective_transformation/ commit c3f4b766f03770f094fda6bda0a5882c0ebd4581
imgteam
parents:
diff changeset
20 coords_shape = [len(shape), rows, cols]
17f5d0c3f8a3 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/projective_transformation/ commit c3f4b766f03770f094fda6bda0a5882c0ebd4581
imgteam
parents:
diff changeset
21 if len(shape) == 3:
17f5d0c3f8a3 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/projective_transformation/ commit c3f4b766f03770f094fda6bda0a5882c0ebd4581
imgteam
parents:
diff changeset
22 coords_shape.append(shape[2])
17f5d0c3f8a3 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/projective_transformation/ commit c3f4b766f03770f094fda6bda0a5882c0ebd4581
imgteam
parents:
diff changeset
23 coords = np.empty(coords_shape, dtype=dtype)
17f5d0c3f8a3 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/projective_transformation/ commit c3f4b766f03770f094fda6bda0a5882c0ebd4581
imgteam
parents:
diff changeset
24
17f5d0c3f8a3 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/projective_transformation/ commit c3f4b766f03770f094fda6bda0a5882c0ebd4581
imgteam
parents:
diff changeset
25 tf_coords = np.indices((cols, rows), dtype=dtype).reshape(2, -1).T
17f5d0c3f8a3 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/projective_transformation/ commit c3f4b766f03770f094fda6bda0a5882c0ebd4581
imgteam
parents:
diff changeset
26
17f5d0c3f8a3 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/projective_transformation/ commit c3f4b766f03770f094fda6bda0a5882c0ebd4581
imgteam
parents:
diff changeset
27 for i in range(0, (tf_coords.shape[0]//batch_size+1)):
17f5d0c3f8a3 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/projective_transformation/ commit c3f4b766f03770f094fda6bda0a5882c0ebd4581
imgteam
parents:
diff changeset
28 tf_coords[batch_size*i:batch_size*(i+1)] = coord_map(tf_coords[batch_size*i:batch_size*(i+1)])
17f5d0c3f8a3 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/projective_transformation/ commit c3f4b766f03770f094fda6bda0a5882c0ebd4581
imgteam
parents:
diff changeset
29 tf_coords = tf_coords.T.reshape((-1, cols, rows)).swapaxes(1, 2)
17f5d0c3f8a3 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/projective_transformation/ commit c3f4b766f03770f094fda6bda0a5882c0ebd4581
imgteam
parents:
diff changeset
30
17f5d0c3f8a3 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/projective_transformation/ commit c3f4b766f03770f094fda6bda0a5882c0ebd4581
imgteam
parents:
diff changeset
31 _stackcopy(coords[1, ...], tf_coords[0, ...])
17f5d0c3f8a3 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/projective_transformation/ commit c3f4b766f03770f094fda6bda0a5882c0ebd4581
imgteam
parents:
diff changeset
32 _stackcopy(coords[0, ...], tf_coords[1, ...])
17f5d0c3f8a3 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/projective_transformation/ commit c3f4b766f03770f094fda6bda0a5882c0ebd4581
imgteam
parents:
diff changeset
33 if len(shape) == 3:
17f5d0c3f8a3 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/projective_transformation/ commit c3f4b766f03770f094fda6bda0a5882c0ebd4581
imgteam
parents:
diff changeset
34 coords[2, ...] = range(shape[2])
17f5d0c3f8a3 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/projective_transformation/ commit c3f4b766f03770f094fda6bda0a5882c0ebd4581
imgteam
parents:
diff changeset
35
17f5d0c3f8a3 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/projective_transformation/ commit c3f4b766f03770f094fda6bda0a5882c0ebd4581
imgteam
parents:
diff changeset
36 return coords
17f5d0c3f8a3 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/projective_transformation/ commit c3f4b766f03770f094fda6bda0a5882c0ebd4581
imgteam
parents:
diff changeset
37
17f5d0c3f8a3 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/projective_transformation/ commit c3f4b766f03770f094fda6bda0a5882c0ebd4581
imgteam
parents:
diff changeset
38
17f5d0c3f8a3 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/projective_transformation/ commit c3f4b766f03770f094fda6bda0a5882c0ebd4581
imgteam
parents:
diff changeset
39 def transform(moving_image, fixed_image, warp_matrix, out):
17f5d0c3f8a3 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/projective_transformation/ commit c3f4b766f03770f094fda6bda0a5882c0ebd4581
imgteam
parents:
diff changeset
40 moving_image = skimage.io.imread(moving_image)
17f5d0c3f8a3 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/projective_transformation/ commit c3f4b766f03770f094fda6bda0a5882c0ebd4581
imgteam
parents:
diff changeset
41 fixed_image = skimage.io.imread(fixed_image)
17f5d0c3f8a3 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/projective_transformation/ commit c3f4b766f03770f094fda6bda0a5882c0ebd4581
imgteam
parents:
diff changeset
42 warp_matrix = pd.read_csv(warp_matrix, delimiter="\t", header=None)
17f5d0c3f8a3 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/projective_transformation/ commit c3f4b766f03770f094fda6bda0a5882c0ebd4581
imgteam
parents:
diff changeset
43 warp_matrix = np.array(warp_matrix)
17f5d0c3f8a3 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/projective_transformation/ commit c3f4b766f03770f094fda6bda0a5882c0ebd4581
imgteam
parents:
diff changeset
44
17f5d0c3f8a3 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/projective_transformation/ commit c3f4b766f03770f094fda6bda0a5882c0ebd4581
imgteam
parents:
diff changeset
45 trans = ProjectiveTransform(matrix=warp_matrix)
17f5d0c3f8a3 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/projective_transformation/ commit c3f4b766f03770f094fda6bda0a5882c0ebd4581
imgteam
parents:
diff changeset
46 warped_coords = warp_coords_batch(trans, fixed_image.shape)
17f5d0c3f8a3 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/projective_transformation/ commit c3f4b766f03770f094fda6bda0a5882c0ebd4581
imgteam
parents:
diff changeset
47 t = map_coordinates(moving_image, warped_coords, mode='reflect')
17f5d0c3f8a3 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/projective_transformation/ commit c3f4b766f03770f094fda6bda0a5882c0ebd4581
imgteam
parents:
diff changeset
48
17f5d0c3f8a3 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/projective_transformation/ commit c3f4b766f03770f094fda6bda0a5882c0ebd4581
imgteam
parents:
diff changeset
49 with warnings.catch_warnings():
17f5d0c3f8a3 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/projective_transformation/ commit c3f4b766f03770f094fda6bda0a5882c0ebd4581
imgteam
parents:
diff changeset
50 warnings.simplefilter("ignore")
17f5d0c3f8a3 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/projective_transformation/ commit c3f4b766f03770f094fda6bda0a5882c0ebd4581
imgteam
parents:
diff changeset
51 skimage.io.imsave(out, t)
17f5d0c3f8a3 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/projective_transformation/ commit c3f4b766f03770f094fda6bda0a5882c0ebd4581
imgteam
parents:
diff changeset
52
17f5d0c3f8a3 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/projective_transformation/ commit c3f4b766f03770f094fda6bda0a5882c0ebd4581
imgteam
parents:
diff changeset
53
17f5d0c3f8a3 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/projective_transformation/ commit c3f4b766f03770f094fda6bda0a5882c0ebd4581
imgteam
parents:
diff changeset
54 if __name__ == "__main__":
17f5d0c3f8a3 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/projective_transformation/ commit c3f4b766f03770f094fda6bda0a5882c0ebd4581
imgteam
parents:
diff changeset
55 parser = argparse.ArgumentParser(description="Transform the image")
17f5d0c3f8a3 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/projective_transformation/ commit c3f4b766f03770f094fda6bda0a5882c0ebd4581
imgteam
parents:
diff changeset
56 parser.add_argument("fixed_image", help="Paste path to image.png that should be transformed")
17f5d0c3f8a3 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/projective_transformation/ commit c3f4b766f03770f094fda6bda0a5882c0ebd4581
imgteam
parents:
diff changeset
57 parser.add_argument("moving_image", help="Paste path to fixed image.png")
17f5d0c3f8a3 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/projective_transformation/ commit c3f4b766f03770f094fda6bda0a5882c0ebd4581
imgteam
parents:
diff changeset
58 parser.add_argument("warp_matrix", help="Paste path to warp_matrix.csv that should be used for transformation")
17f5d0c3f8a3 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/projective_transformation/ commit c3f4b766f03770f094fda6bda0a5882c0ebd4581
imgteam
parents:
diff changeset
59 parser.add_argument("out", help="Paste path to file in which transformed image should be saved")
17f5d0c3f8a3 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/projective_transformation/ commit c3f4b766f03770f094fda6bda0a5882c0ebd4581
imgteam
parents:
diff changeset
60 args = parser.parse_args()
17f5d0c3f8a3 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/projective_transformation/ commit c3f4b766f03770f094fda6bda0a5882c0ebd4581
imgteam
parents:
diff changeset
61 transform(args.moving_image, args.fixed_image, args.warp_matrix, args.out)