-
Notifications
You must be signed in to change notification settings - Fork 0
/
image_tests1.py
94 lines (58 loc) · 2.05 KB
/
image_tests1.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
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
# -*- coding: utf-8 -*-
"""
Created on Tue Apr 30 18:00:20 2019
@author: Hadi
"""
import os
import numpy as np
import nibabel as nib
import matplotlib.pyplot as plt
import matplotlib.image as mpimg
import tensorflow as tf
from pathlib import Path
HGG_dir = "G:/My Drive/Sem2/MachineLearning/Project/data/BraTS/BraTS18_small/training/HGG"
HGG_path = Path(HGG_dir)
image_types = ['flair', 't1', 't1ce', 't2'] # 1:flair, 2:t1, 3:t1ce, 4:t2
HGG_folder_list = os.listdir(HGG_path)
norms = []
class norm:
def __init__(self, img_type, mean, std):
self.img_type = img_type
self.mean = mean
self.std = std
for i in image_types:
img_norm = norm(i,0.0,1.0)
norms.append(img_norm)
type_number = 0
for i in image_types:
data_temp_list = []
for j in HGG_folder_list:
img_path = os.path.join(HGG_path, j, j + '_' + i + '.nii.gz')
img = nib.load(img_path).get_data() #img is a numpy array
data_temp_list.append(img)
print("pre_ image:", i, "____5" , "\n================")
plt.imshow(data_temp_list[5][:,:,100])
plt.ioff()
plt.show()
data_temp_list = np.asarray(data_temp_list)
m = np.mean(data_temp_list) #105.57924098558448 for t2
s = np.std(data_temp_list) #609.0591653806654 for t2
norms[type_number].img_type = i
norms[type_number].mean = m
norms[type_number].std = s
type_number += 1
del data_temp_list
type_number = 0
for i in image_types:
data_temp_list = []
for j in HGG_folder_list:
img_path = os.path.join(HGG_path, j, j + '_' + i + '.nii.gz')
img = nib.load(img_path).get_data() #img is a numpy array
img = (img - norms[type_number].mean)/norms[type_number].std
data_temp_list.append(img)
print("post_ image:", i, "____5" , "\n================")
plt.imshow(data_temp_list[5][:,:,100])
plt.ioff()
plt.show()
type_number += 1
print("iamges normalized: ", norms)