File size: 3,289 Bytes
911c613
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
# ------------------------------------------------------------ #
#
# file : preprocessing/threshold.py
# author : CM
# Preprocess function for Bullitt dataset
#
# ------------------------------------------------------------ #
import os
import sys
import numpy as np
import nibabel as nib
from utils.io.write import npToNii
from utils.config.read import readConfig

# Get the threshold for preprocessing
def getThreshold(dataset_mra, dataset_gd):
    threshold = dataset_mra.max()

    for i in range(0,len(dataset_mra)):
        mra = dataset_mra[i]
        gd = dataset_gd[i]

        for x in range(0, mra.shape[0]):
            for y in range(0, mra.shape[1]):
                for z in range(0, mra.shape[2]):
                    if(gd[x,y,z] == 1 and mra[x,y,z] < threshold):
                        threshold = mra[x,y,z]

    return threshold

# Apply threshold to an image
def thresholding(data, threshold):
    output = np.copy(data)
    for x in range(0, data.shape[0]):
        for y in range(0, data.shape[1]):
            for z in range(0, data.shape[2]):
                    if data[x,y,z] > threshold:
                        output[x,y,z] = data[x,y,z]
                    else:
                        output[x,y,z] = 0
    return output

config_filename = sys.argv[1]
if(not os.path.isfile(config_filename)):
    sys.exit(1)

config = readConfig(config_filename)

output_folder = sys.argv[2]
if(not os.path.isdir(output_folder)):
    sys.exit(1)

print("Loading training dataset")

train_mra_dataset = np.empty((30, config["image_size_x"], config["image_size_y"], config["image_size_z"]))
i = 0
files = os.listdir(config["dataset_train_mra_path"])
files.sort()

for filename in files:
    if(i>=30):
       break
    print(filename)
    train_mra_dataset[i, :, :, :] = nib.load(os.path.join(config["dataset_train_mra_path"], filename)).get_data()
    i = i + 1


train_gd_dataset = np.empty((30, config["image_size_x"], config["image_size_y"], config["image_size_z"]))
i = 0
files = os.listdir(config["dataset_train_gd_path"])
files.sort()

for filename in files:
    if(i>=30):
       break
    print(filename)
    train_gd_dataset[i, :, :, :] = nib.load(os.path.join(config["dataset_train_gd_path"], filename)).get_data()
    i = i + 1

print("Compute threshold")
threshold = getThreshold(train_mra_dataset, train_gd_dataset)

train_mra_dataset = None
train_gd_dataset = None

print("Apply preprocessing to test image")
files = os.listdir(config["dataset_test_mra_path"])
files.sort()

for filename in files:
    print(filename)
    data = nib.load(os.path.join(config["dataset_test_mra_path"], filename)).get_data()
    print(np.average(data))
    preprocessed = thresholding(data, threshold)
    print(np.average(preprocessed))
    npToNii(preprocessed,os.path.join(output_folder+'/test_Images', 'pre_'+filename))


print("Apply threshold to train image : ", threshold)
files = os.listdir(config["dataset_train_mra_path"])
files.sort()

for filename in files:
    print(filename)
    data = nib.load(os.path.join(config["dataset_train_mra_path"], filename)).get_data()
    print(np.average(data))
    preprocessed = thresholding(data, threshold)
    print(np.average(preprocessed))
    npToNii(preprocessed,os.path.join(output_folder+'/train_Images', 'pre_'+filename))