-
Notifications
You must be signed in to change notification settings - Fork 1
/
test_spacing.jl
50 lines (37 loc) · 1.63 KB
/
test_spacing.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
using Conda, PyCall, Pkg
using Statistics
Conda.pip_interop(true)
Conda.pip("install", "SimpleITK")
Conda.pip("install", "h5py")
sitk = pyimport("SimpleITK")
np = pyimport("numpy")
path = "D:/mingw_installation/home/hurtbadly/Downloads/ct_soft_pat_3_sudy_0.nii.gz"
# Step 2: Read the NIfTI image
image = sitk.ReadImage(path)
# Step 3: Define the new spacing
new_spacing = [1.0, 2.0, 3.0]
# Step 4: Compute the new size
original_size = size(np.array(sitk.GetArrayViewFromImage(image)))
original_spacing = image.GetSpacing()
new_size = [round(Int, original_size[i] * (original_spacing[i] / new_spacing[i])) for i in 1:3]
# Convert new_size to a Python list of uint32 using PyVector
new_size_py = PyVector(np.array(new_size, dtype=np.uint32))
# Step 5: Define the resampling parameters
resample = sitk.ResampleImageFilter()
resample.SetOutputSpacing(new_spacing)
resample.SetSize(new_size_py)
resample.SetInterpolator(sitk.sitkLinear)
resample.SetOutputDirection(image.GetDirection())
resample.SetOutputOrigin(image.GetOrigin())
# Step 6: Resample the image
resampled_image = resample.Execute(image)
# Step 7: Save or use the resampled image
sitk.WriteImage(resampled_image, "D:/mingw_installation/home/hurtbadly/Downloads/resampled_image.nii.gz")
data_arr = zeros(205, 100, 300)
data_arr[50:100, 50:100, 50:100] .= 1
data_arr_spacing = [5.0, 0.5, 2.0]
image = sitk.GetImageFromArray(data_arr)
# Step 5: Set the spacing for the image
image.SetSpacing(data_arr_spacing)
# Step 6: Save the image as a NIfTI file
sitk.WriteImage(image, "D:/mingw_installation/home/hurtbadly/Downloads/extreme_test_one.nii.gz")