-
Notifications
You must be signed in to change notification settings - Fork 0
/
CT_Preprocess3D.py
74 lines (40 loc) · 1.61 KB
/
CT_Preprocess3D.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
65
66
67
68
69
#!/usr/bin/env python
# coding: utf-8
import numpy as np
import SimpleITK as sitk
import matplotlib.pyplot as plt
import os
from scipy import ndimage
def getScan(scanPath):
data = sitk.ReadImage(scanPath)
scan = sitk.GetArrayFromImage(data)
return scan
def normalizeScan(volume):
min = -1000
max = 400
volume[volume < min] = min
volume[volume > max] = max
volume = (volume-min)/(max-min)
volume = volume.astype("float32")
return volume
def resizeScan(scan,target_shape):
current_width,current_height,current_depth = scan.shape
target_width,target_height,target_depth = target_shape
# Compute depth factor by 1/(D/N)
# Refer to the paper Uniformizing Techniques to Process CT scans with 3D CNNs for Tuberculosis Prediction
width_factor = 1/(current_width/target_width)
height_factor = 1/(current_height/target_height)
depth_factor = 1/(current_depth/target_depth)
# ndimage.zoom(): The array is zoomed using spline interpolation of the requested order.
#scan = ndimage.rotate(scan, 90, reshape=False)
scan = ndimage.zoom(scan,(width_factor,height_factor,depth_factor),order = 1)
#print(width_factor,height_factor,depth_factor)
return scan
def preprocessScan(scanPath,target_shape):
scan = getScan(scanPath)
if(scan.shape[0] != scan.shape[1]):
scan = np.swapaxes(scan,0,1) #(64,128,128) => (128,128,64)
scan = np.swapaxes(scan,1,2)
scan = normalizeScan(scan)
scan = resizeScan(scan,target_shape)
return scan