-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhelper.py
More file actions
47 lines (34 loc) · 1.28 KB
/
Copy pathhelper.py
File metadata and controls
47 lines (34 loc) · 1.28 KB
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
"""
This script contains helper methods
"""
import numpy as np
import SimpleITK as sitk
import config
# Helper function to resample an image to a new spacing
def resample_image(itk_image, new_spacing=config.TARGET_SPACING, is_label=False):
original_spacing = itk_image.GetSpacing()
original_size = itk_image.GetSize()
new_size = [
int(round(osz * ospc / nspc))
for osz, ospc, nspc in zip(original_size, original_spacing, new_spacing)
]
resample = sitk.ResampleImageFilter()
resample.SetOutputSpacing(new_spacing)
resample.SetSize(new_size)
resample.SetOutputDirection(itk_image.GetDirection())
resample.SetOutputOrigin(itk_image.GetOrigin())
resample.SetTransform(sitk.Transform())
resample.SetDefaultPixelValue(itk_image.GetPixelIDValue())
if is_label:
resample.SetInterpolator(sitk.sitkNearestNeighbor)
else:
resample.SetInterpolator(sitk.sitkLinear)
return resample.Execute(itk_image)
# Helper function to normalize HU values
def normalize_hu(image_array):
# Clip HU values
image_array = np.clip(image_array, config.HU_MIN, config.HU_MAX)
# Normalize to [0, 1]
image_array = (image_array - config.HU_MIN) / \
(config.HU_MAX - config.HU_MIN)
return image_array.astype(np.float32)