forked from JuliaHealth/MedImages.jl
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_basic_transformation.jl
460 lines (333 loc) · 13.4 KB
/
test_basic_transformation.jl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
"""
some issues that may occur and also why python rotation implementation is so clumsy here
is described in https://stackoverflow.com/questions/56171643/simpleitk-rotation-of-volumetric-data-e-g-mri
we have separate python function fro z rotation and rotations in other planes
Also transformations are nicely shown on couple first slides of
https://www.cs.cornell.edu/courses/cs4620/2010fa/lectures/03transforms3d.pdf
"""
include("../src/MedImage_data_struct.jl")
include("../src/Orientation_dicts.jl")
include("../src/Brute_force_orientation.jl")
include("../src/Utils.jl")
include("../src/Load_and_save.jl")
include("../src/Basic_transformations.jl")
# include("./test_visualize.jl")
include("./dicom_nifti.jl")
# using .dicom_nifti
using LinearAlgebra
using MedImage_data_struct
using Load_and_save
# sitk = pyimport("SimpleITK")
# using CondaPkg
# CondaPkg.add("simpleitk")
# CondaPkg.add("numpy")
# CondaPkg.add_pip("simpleitk", version="")
function load_image(path)
"""
load image from path
"""
# test_image_equality(p,p)
medimage_instance_array = load_images(path)
medimage_instance = medimage_instance_array[1]
return medimage_instance
end#load_image
# python implementation taken from https://stackoverflow.com/questions/56171643/simpleitk-rotation-of-volumetric-data-e-g-mri
function matrix_from_axis_angle(a)
""" Compute rotation matrix from axis-angle.
This is called exponential map or Rodrigues' formula.
Parameters
----------
a : array-like, shape (4,)
Axis of rotation and rotation angle: (x, y, z, angle)
Returns
-------
R : array-like, shape (3, 3)
Rotation matrix
"""
ux, uy, uz, theta = a
c = cos(theta)
s = sin(theta)
ci = 1.0 - c
R = [[ci * ux * ux + c,
ci * ux * uy - uz * s,
ci * ux * uz + uy * s],
[ci * uy * ux + uz * s,
ci * uy * uy + c,
ci * uy * uz - ux * s],
[ci * uz * ux - uy * s,
ci * uz * uy + ux * s,
ci * uz * uz + c],
]
# This is equivalent to
# R = (np.eye(3) * np.cos(theta) +
# (1.0 - np.cos(theta)) * a[:3, np.newaxis].dot(a[np.newaxis, :3]) +
# cross_product_matrix(a[:3]) * np.sin(theta))
return R
end #matrix_from_axis_angle
function resample(image, transform)
"""
This function resamples (updates) an image using a specified transform
:param image: The sitk image we are trying to transform
:param transform: An sitk transform (ex. resizing, rotation, etc.
:return: The transformed sitk image
"""
sitk = pyimport("SimpleITK")
np = pyimport("numpy")
reference_image = image
interpolator = sitk.sitkLinear
default_value = 0
return sitk.Resample(image, reference_image, transform,
interpolator, default_value)
end#resample
function get_center(img)
"""
from python to test
"""
width, height, depth = img.GetSize()
centt = (Int(ceil(width / 2)), Int(ceil(height / 2)), Int(ceil(depth / 2)))
# return img.TransformIndexToPhysicalPoint((np.ceil(width/2), np.ceil(height/2), np.ceil(depth/2)))
return img.TransformIndexToPhysicalPoint(centt)
end #get_center
function rotation3d(image, axis, theta)
"""
This function rotates an image across each of the x, y, z axes by theta_x, theta_y, and theta_z degrees
respectively
:return: The rotated image
"""
sitk = pyimport("SimpleITK")
np = pyimport("numpy")
theta = np.deg2rad(theta)
euler_transform = sitk.Euler3DTransform()
image_center = get_center(image)
euler_transform.SetCenter(image_center)
direction = image.GetDirection()
if (axis == 3)
axis_angle = (direction[3], direction[6], direction[9], theta)
elseif (axis == 2)
axis_angle = (direction[2], direction[5], direction[8], theta)
elseif (axis == 1)
axis_angle = (direction[1], direction[4], direction[7], theta)
end
np_rot_mat = matrix_from_axis_angle(axis_angle)
euler_transform.SetMatrix([np_rot_mat[1][1], np_rot_mat[1][2], np_rot_mat[1][3], np_rot_mat[2][1], np_rot_mat[2][2], np_rot_mat[2][3], np_rot_mat[3][1], np_rot_mat[3][2], np_rot_mat[3][3]])
# if(axis==3)
# axis_angle = (direction[2], direction[5], direction[8], theta)
# elseif (axis==2)
# axis_angle = (direction[1], direction[4], direction[7], theta)
# elseif (axis==1)
# axis_angle = (direction[0], direction[3], direction[6], theta)
# end
# np_rot_mat = matrix_from_axis_angle(axis_angle)
# euler_transform.SetMatrix([np_rot_mat[0][0],np_rot_mat[0][1],np_rot_mat[0][2]
# ,np_rot_mat[1][0],np_rot_mat[1][1],np_rot_mat[1][2]
# ,np_rot_mat[2][0],np_rot_mat[2][1],np_rot_mat[2][2] ])
resampled_image = resample(image, euler_transform)
return resampled_image
end #rotation3d
# function rotate_metadata(image, axis, theta)
# rots=[0.0,0.0,0.0]
# rots[axis]=theta
# # Create a rotation transform
# dimension = image.GetDimension()
# transform = sitk.Euler3DTransform()
# transform.SetRotation(np.deg2rad(rots[1]), np.deg2rad(rots[2]), np.deg2rad(rots[3]))
# # Create the TransformGeometryImageFilter and set the transform
# transform.SetCenter(
# image.TransformContinuousIndexToPhysicalPoint(
# [(sz - 1) / 2 for sz in image.GetSize()]
# )
# )
# rotated_image = sitk.TransformGeometry(image, transform)
# return rotated_image
# end #rotate_metadata
function test_single_rotation(medIm, sitk_image, axis::Int, theta::Float64, debug_folder_path, dummy_run=false)
"""
test if the rotation of the image lead to correct change in the pixel array
and the metadata the operation will be tasted against Python simple itk function
"""
sitk = pyimport("SimpleITK")
np = pyimport("numpy")
#sitk implementation
rotated = rotation3d(sitk_image, axis, theta)
if (dummy_run)
sitk.WriteImage(rotated, "$(debug_folder_path)/rotated_$(axis)_$(theta)_arr.nii.gz")
return
end
#our Julia implementation
medIm = rotate_mi(medIm, axis, theta, linear)
test_object_equality(medIm, rotated)
end #test_single_rotation
"""
testing rotations against Python simple itk function
"""
function test_rotation(path_nifti, debug_folder_path, dummy_run=false)
#we test rotations of diffrent exes and of diffrent angles
for ax in [1, 2, 3]
for theta in [30.0, 60.0, 90.0, 180.0, 270.0, 360.0, 400.0]
#purposfully reloading each time to avoid issues with pixel
#array mutation
#load image only in real run
med_im = []
if (!dummy_run)
med_im = load_image(path_nifti)
end
sitk_image = sitk.ReadImage(path_nifti)
test_single_rotation(med_im, sitk_image, ax, theta, debug_folder_path, dummy_run)
end#for
end#for
end
# debug_folder="/home/jakubmitura/projects/MedImage.jl/test_data/debug"
# p="/home/jakubmitura/projects/MedImage.jl/test_data/volume-0.nii.gz"
# test_rotation(p,debug_folder,false)
################################################# cropping tests
"""
crop image using simple itk function and return the cropped image
both beginning and size are tuples of 3 elements (x,y,z)
in case of begining it will mean first voxel and size how big will be the chunk to extract
"""
function sitk_crop(sitk_image, beginning, size)
# extract = sitk.ExtractImageFilter()
extracted_image = sitk.RegionOfInterest(sitk_image, [size[1], size[2], size[3]], [beginning[1], beginning[2], beginning[3]])
# extract.SetSize([size[1],size[2],size[3]])
# extract.SetIndex([beginning[1],beginning[2],beginning[3]])
# extracted_image = extract.Execute(sitk_image)
print(extracted_image.GetSize())
print(sitk_image.GetSize())
return extracted_image
end#sitk_crop
function test_single_crop(medIm, sitk_image, begining, size, debug_folder_path, dummy_run)
#sitk implementation
sitk = pyimport("SimpleITK")
np = pyimport("numpy")
cropped = sitk_crop(sitk_image, begining, size)
if (dummy_run)
sitk.WriteImage(cropped, "$(debug_folder_path)/cropped_$(begining)_$(size).nii.gz")
return
end#dummy_run
#our Julia implementation
medIm = crop_mi([medIm], begining, size, linear)[0]
test_object_equality(medIm, cropped)
end #test_single_rotation
function test_crops(path_nifti, debug_folder_path=" ", dummy_run=false)
"""
test if the cropping of the image lead to correct change in the pixel array
and the metadata the operation will be tasted against Python simple itk function
"""
for begining in [(0, 0, 0), (15, 17, 7)]
for size in [(151, 156, 50), (150, 150, 53), (148, 191, 56)]
med_im = []
if (!dummy_run)
med_im = load_image(path_nifti)
end
sitk_image = sitk.ReadImage(path_nifti)
test_single_crop(med_im, sitk_image, begining, size, debug_folder_path, dummy_run)
end#for
end#for
end
######################### padding tests
"""
pad image using simple itk function and return the cropped image
both beginning and end pad are tuples of 3 elements (x,y,z)
in case of begining it will mean first voxel and size how big will be the chunk to extract
"""
function sitk_pad(sitk_image, pad_beg, pad_end, pad_val)
sitk = pyimport("SimpleITK")
np = pyimport("numpy")
extract = sitk.ConstantPadImageFilter()
extract.SetConstant(pad_val)
extract.SetPadLowerBound(pad_beg)
extract.SetPadUpperBound(pad_end)
extracted_image = extract.Execute(sitk_image)
return extracted_image
end#sitk_crop
function test_pads(path_nifti, debug_folder_path, dummy_run=false)
"""
test if the padding of the image lead to correct change in the pixel array
and the metadata the operation will be tasted against Python simple itk function
"""
sitk = pyimport("SimpleITK")
np = pyimport("numpy")
for pad_beg in [(10, 11, 13), (15, 17, 19)]
for pad_end in [(10, 11, 13), (15, 17, 19), (30, 31, 32)]
for pad_val in [0.0, 111.5]
sitk_image = sitk.ReadImage(path_nifti)
sitk_padded = sitk_pad(sitk_image, pad_beg, pad_end, pad_val)
if (dummy_run)
sitk.WriteImage(sitk_padded, "$(debug_folder_path)/padded_$(pad_beg)_$(pad_end).nii.gz")
else
medIm = load_image(path_nifti)
mi_padded = pad_mi(medIm, pad_beg, pad_end, pad_val, linear)
test_object_equality(mi_padded, sitk_padded)
end
end#for
end#for
end#for
end
####################### translation tests
"""
reference sitk translate function
"""
function sitk_translate(image, translate_by, translate_in_axis)
sitk = pyimport("SimpleITK")
np = pyimport("numpy")
translatee = [0, 0, 0]
translatee[translate_in_axis] = translate_by
transform = sitk.TranslationTransform(3, translatee)
# reference_image = image
# extracted_image=sitk.Resample(image, reference_image, transform,
# sitk.sitkLinear, 0.0)
res = sitk.TransformGeometry(image, transform)
return res
end#sitk_translate
"""
test if the translation of the image lead to correct change in the pixel array
and the metadata the operation will be tasted against Python simple itk function
"""
function test_translate(path_nifti, debug_folder_path, dummy_run)
sitk = pyimport("SimpleITK")
np = pyimport("numpy")
# Load the image from path
for t_val in [1, 10, 16]
for axis in [1, 2, 3]
sitk_image = sitk.ReadImage(path_nifti)
sitk_trnanslated = sitk_translate(sitk_image, t_val, axis)
if (dummy_run)
sitk.WriteImage(sitk_image, "$(debug_folder_path)/translated_$(t_val)_$(axis).nii.gz")
else
medIm = load_image(path_nifti)
medIm = translate_mi(medIm, t_val, axis, linear)
test_object_equality(medIm, sitk_trnanslated)
end
end#for
end#for
end
################################################# scaling tests
"""
test if the scaling of the image lead to correct change in the pixel array
and the metadata the operation will be tasted against Python simple itk function
"""
# Scale the image using SimpleITK
function sitk_scale(image, zoom)
sitk = pyimport("SimpleITK")
np = pyimport("numpy")
scale_transform = sitk.ScaleTransform(3, [zoom, zoom, zoom])
res = sitk.Resample(image, scale_transform, sitk.sitkBSpline, 0.0)
return res
end
# Test if the scaling of the image leads to correct changes in the pixel array and metadata
function test_scale(path_nifti, debug_folder_path, dummy_run)
for zoom in [0.6, 0.9, 1.0, 1.3, 1.8]
sitk_image = sitk.ReadImage(path_nifti)
sitk_scaled = sitk_scale(sitk_image, zoom)
if (dummy_run)
sitk.WriteImage(sitk_scaled, "$(debug_folder_path)/scaled_$(zoom).nii.gz")
else
medIm = load_image(path_nifti)
medIm = scale_mi(medIm, zoom, linear)
test_object_equality(medIm, sitk_scaled)
end
end #for
end
# imagePath = "/workspaces/MedImage.jl/test_data/volume-0.nii.gz"
# debug_folder_path = "/workspaces/MedImage.jl/test_data/debug"
# test_scale(imagePath, debug_folder_path,true)