-
Notifications
You must be signed in to change notification settings - Fork 0
/
Rotation_example.py
64 lines (50 loc) · 1.75 KB
/
Rotation_example.py
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
from sys import platform as sys_pf
if sys_pf == 'darwin':
import matplotlib
matplotlib.use("TkAgg")
# ! pip install simpleitk
import os
import numpy as np
# import nibabel as nib
from matplotlib import pyplot as plt
import matplotlib
import SimpleITK as sitk
from scipy import ndimage
import random
folder = 'mris/'
for item in os.listdir(folder):
if item.endswith(".nii"):
reader = sitk.ImageFileReader()
reader.SetImageIO("NiftiImageIO")
reader.SetFileName(folder + item)
image = reader.Execute()
# img1 = sitk.ReadImage(folder + item) # alternative way to pull in image
# convert image into np array & perform fft
img = sitk.GetArrayFromImage(image)
# print(img.shape)
orig_slice = img[100]
# FFT in 3d
v = np.fft.fftn(img)
back_fft2 = v.copy()
back_real = back_fft2.real
back_imag = back_fft2.imag
for i in range(100):
trans = random.randint(-50,50)
rot = random.randint(-50,50)
back_slice_r = back_real[i]
back_slice_i = back_imag[i]
# Testing translation
back_rot_r = ndimage.shift(back_slice_r,[trans,0], mode='constant', cval=100)
back_rot_i = ndimage.shift(back_slice_i,[trans,0], mode='constant', cval=100)
# Testing rotation
back_rot_r = ndimage.rotate(back_slice_r, rot, reshape = False)
back_rot_i = ndimage.rotate(back_slice_i, rot, reshape = False)
back_rot = back_rot_r + back_rot_i * 1j
back_fft2[i] = back_rot
back_vis = np.fft.ifftn(back_fft2)
back_display = back_vis.astype(int)
plt.subplot(121), plt.imshow(orig_slice, cmap='gray')
plt.title('Original'), plt.xticks([]), plt.yticks([])
plt.subplot(122), plt.imshow(back_display[100], cmap='gray')
plt.title('Blurred'), plt.xticks([]), plt.yticks([])
plt.show()