@@ -14,6 +14,43 @@ def create_zero_centered_coordinate_mesh(shape):
14
14
coords [d ] -= ((np .array (shape ).astype (float )) / 2. )[d ]
15
15
return coords
16
16
17
+ def create_matrix_rotation_x_3d (angle , matrix = None ):
18
+ rotation_x = np .array ([[1 , 0 , 0 ],
19
+ [0 , np .cos (angle ), - np .sin (angle )],
20
+ [0 , np .sin (angle ), np .cos (angle )]])
21
+ if matrix is None :
22
+ return rotation_x
23
+
24
+ return np .dot (matrix , rotation_x )
25
+
26
+
27
+ def create_matrix_rotation_y_3d (angle , matrix = None ):
28
+ rotation_y = np .array ([[np .cos (angle ), 0 , np .sin (angle )],
29
+ [0 , 1 , 0 ],
30
+ [- np .sin (angle ), 0 , np .cos (angle )]])
31
+ if matrix is None :
32
+ return rotation_y
33
+
34
+ return np .dot (matrix , rotation_y )
35
+
36
+
37
+ def create_matrix_rotation_z_3d (angle , matrix = None ):
38
+ rotation_z = np .array ([[np .cos (angle ), - np .sin (angle ), 0 ],
39
+ [np .sin (angle ), np .cos (angle ), 0 ],
40
+ [0 , 0 , 1 ]])
41
+ if matrix is None :
42
+ return rotation_z
43
+
44
+ return np .dot (matrix , rotation_z )
45
+
46
+ def rotate_coords_3d (coords , angle_x , angle_y , angle_z ):
47
+ rot_matrix = np .identity (len (coords ))
48
+ rot_matrix = create_matrix_rotation_x_3d (angle_x , rot_matrix )
49
+ rot_matrix = create_matrix_rotation_y_3d (angle_y , rot_matrix )
50
+ rot_matrix = create_matrix_rotation_z_3d (angle_z , rot_matrix )
51
+ coords = np .dot (coords .reshape (len (coords ), - 1 ).transpose (), rot_matrix ).transpose ().reshape (coords .shape )
52
+ return coords
53
+
17
54
def elastic_deform_coordinates (coordinates , alpha , sigma ):
18
55
n_dim = len (coordinates )
19
56
offsets = []
@@ -37,21 +74,37 @@ def elastic_deform_coordinates(coordinates, alpha, sigma):
37
74
start = time .time ()
38
75
coords = create_zero_centered_coordinate_mesh (array_image .shape )
39
76
40
- # alpha=(0., 1000.)
41
- # sigma=(10., 13.)
42
- # a = np.random.uniform(alpha[0], alpha[1])
43
- # s = np.random.uniform(sigma[0], sigma[1])
44
- # coords = elastic_deform_coordinates(coords, a, s)
45
-
46
-
47
- for d in range (len (array_image .shape )):
48
- ctr = int (np .round (array_image .shape [d ] / 2. ))
49
- coords [d ] += ctr
77
+ alpha = (0. , 1000. )
78
+ sigma = (10. , 13. )
79
+ a = np .random .uniform (alpha [0 ], alpha [1 ])
80
+ s = np .random .uniform (sigma [0 ], sigma [1 ])
81
+ coords = elastic_deform_coordinates (coords , a , s )
82
+
83
+ # angle_x=(0, 2 * np.pi)
84
+ # angle_y=(0, 2 * np.pi)
85
+ # angle_z=(0, 2 * np.pi)
86
+ # if angle_x[0] == angle_x[1]:
87
+ # a_x = angle_x[0]
88
+ # else:
89
+ # a_x = np.random.uniform(angle_x[0], angle_x[1])
90
+ # if angle_y[0] == angle_y[1]:
91
+ # a_y = angle_y[0]
92
+ # else:
93
+ # a_y = np.random.uniform(angle_y[0], angle_y[1])
94
+ # if angle_z[0] == angle_z[1]:
95
+ # a_z = angle_z[0]
96
+ # else:
97
+ # a_z = np.random.uniform(angle_z[0], angle_z[1])
98
+ # coords = rotate_coords_3d(coords, a_x, a_y, a_z)
99
+
100
+ # for d in range(len(array_image.shape)):
101
+ # ctr = int(np.round(array_image.shape[d] / 2.))
102
+ # coords[d] += ctr
50
103
e_time = time .time ()
51
104
elastic_time += e_time - start
52
105
53
106
ret = np .zeros_like (array_image )
54
- map_coordinates (ret .astype (float ), coords , order = 3 , mode = 'mirror' ).astype (ret .dtype )
107
+ map_coordinates (ret .astype (float ), coords , order = 1 , mode = 'mirror' ).astype (ret .dtype )
55
108
m_time = time .time ()
56
109
map_coordinates_time += m_time - e_time
57
110
0 commit comments