comparison projective_transformation.py @ 0:17f5d0c3f8a3 draft

planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/projective_transformation/ commit c3f4b766f03770f094fda6bda0a5882c0ebd4581
author imgteam
date Sat, 09 Feb 2019 14:44:40 -0500
parents
children 974cf4357707
comparison
equal deleted inserted replaced
-1:000000000000 0:17f5d0c3f8a3
1 import skimage.io
2 from skimage.transform import ProjectiveTransform
3 from scipy.ndimage import map_coordinates
4 import numpy as np
5 import pandas as pd
6 import argparse
7 import warnings
8 import shutil
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, out):
40 moving_image = skimage.io.imread(moving_image)
41 fixed_image = skimage.io.imread(fixed_image)
42 warp_matrix = pd.read_csv(warp_matrix, delimiter="\t", header=None)
43 warp_matrix = np.array(warp_matrix)
44
45 trans = ProjectiveTransform(matrix=warp_matrix)
46 warped_coords = warp_coords_batch(trans, fixed_image.shape)
47 t = map_coordinates(moving_image, warped_coords, mode='reflect')
48
49 with warnings.catch_warnings():
50 warnings.simplefilter("ignore")
51 skimage.io.imsave(out, t)
52
53
54 if __name__ == "__main__":
55 parser = argparse.ArgumentParser(description="Transform the image")
56 parser.add_argument("fixed_image", help="Paste path to image.png that should be transformed")
57 parser.add_argument("moving_image", help="Paste path to fixed image.png")
58 parser.add_argument("warp_matrix", help="Paste path to warp_matrix.csv that should be used for transformation")
59 parser.add_argument("out", help="Paste path to file in which transformed image should be saved")
60 args = parser.parse_args()
61 transform(args.moving_image, args.fixed_image, args.warp_matrix, args.out)