forked from UFF-RSNA-2019/brainct-preprocessing
-
Notifications
You must be signed in to change notification settings - Fork 0
/
lib.py
180 lines (147 loc) · 4.65 KB
/
lib.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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
import configparser
import numpy as np
import pydicom
import datetime
import sys
import cv2
import imutils
from matplotlib import pyplot as plt
from skimage.color import label2rgb
from skimage.filters import threshold_multiotsu
# inicializacao
configparser = configparser.ConfigParser()
configparser.read('config.ini')
try:
config = configparser['Geral']
except KeyError:
now = datetime.datetime.now()
msg = "Problema ao obter configuração, verifique se você está rodando o programa no diretório principal do projeto."
print("[ERROR] {} : {}".format(now.strftime("%Y-%m-%d %H:%M:%S.%f"), msg), flush=True)
sys.exit()
# CONSTANTES
DEFAULT_SIZE = 512
HEMORRAGE_MIN=48 #65
HEMORRAGE_MAX=95
VENTRICULO_MIN=0
VENTRICULO_MAX=15
PARENCHYMA_MIN=20
PARENCHYMA_MAX=45
SKULL_MIN=1000
SKULL_MAX=2048
DICOM_MIN=-1024
DICOM_MAX=2048
# FUNCOES PARA SEGMENTACAO
def hemorrage_threshold(image):
img = image.copy()
img[img < HEMORRAGE_MIN] = DICOM_MIN
img[img > HEMORRAGE_MAX] = DICOM_MIN
return img
def ventriculo_threshold(image):
img = image.copy()
img[img < VENTRICULO_MIN] = DICOM_MIN
img[img > VENTRICULO_MAX] = DICOM_MIN
return img
def parenchyma_threshold(image):
img = image.copy()
img[img < PARENCHYMA_MIN] = DICOM_MIN
img[img > PARENCHYMA_MAX] = DICOM_MIN
return img
def skull_threshold(image):
img = image.copy()
img[img < SKULL_MIN] = DICOM_MIN
img[img > SKULL_MAX] = DICOM_MIN
return img
def normalize(image, min, max):
image = abs(image - min) / abs(max - min)
return image
def multiotsu(image, regions):
thresholds = threshold_multiotsu(image, classes=regions)
regions = np.digitize(image, bins=thresholds)
regions_colorized = label2rgb(regions)
return (regions_colorized, regions, thresholds)
# plota um histograma da imagem
def histogram(image, remove_min=False):
max = np.max(image)
min = np.min(image)
if remove_min:
min = np.min(image[image > min])
plt.hist(image.ravel(), 256, [min, max])
plt.title("histogram")
plt.show()
# obtem classificacao de um elemento dos dados de treinamento
def get_classification(filename):
train_file = config['GroundTruth']
classes = np.zeros(6)
id = filename[:12]
try:
idx = 0
with open(train_file) as myfile:
for line in myfile:
pos = line.find(id)
if (pos == 0):
pos = line.find(",")
value = int(line[pos + 1:pos + 2])
classes[idx] = value
idx += 1
if idx == 6:
break
except:
e = sys.exc_info()[0]
log("erro ao abrir arquivo de treinamento: {}".format(e))
return classes
# FUNCOES PARA MANIPULAR DICOM
def obtem_imagem(path, id):
input_filepath = "{}/{}.dcm".format(path, id)
try:
# carrega a imagem a partir do filesystem
image = read_image(input_filepath)
return image
except ValueError:
error("arquivo dicom corrompido: {}".format(id))
return np.zeros((DEFAULT_SIZE, DEFAULT_SIZE))
def read_image(filename):
ds = pydicom.dcmread(filename)
b = ds.RescaleIntercept
m = ds.RescaleSlope
image = m * ds.pixel_array + b
return image
def update_dicom(path_original, path_alterado, data):
ds = pydicom.dcmread(path_original)
ds.PixelData = data.tostring()
ds.Rows, ds.Columns = data.shape
ds.save_as(path_alterado)
# FUNCOES UTILITARIAS
def log(mensagem):
now = datetime.datetime.now()
print("[INFO] {} : {}".format(now.strftime("%Y-%m-%d %H:%M:%S.%f"), mensagem), flush=True)
def error(mensagem):
now = datetime.datetime.now()
print("[ERROR] {} : {}".format(now.strftime("%Y-%m-%d %H:%M:%S.%f"), mensagem), flush=True)
def debug(mensagem):
now = datetime.datetime.now()
print("[DEBUG] {} : {}".format(now.strftime("%Y-%m-%d %H:%M:%S.%f"), mensagem), flush=True)
def plot(title, image, color_map=plt.cm.bone):
plt.imshow(image, cmap=color_map)
plt.title(title)
plt.show()
def show(imagem):
cv2.imshow("uff", imagem)
cv2.waitKey(0)
# FUNCOES PARA TESTE DOS EXTRATORES
def get_train_images():
import configparser
import os
configparser = configparser.ConfigParser()
configparser.read('config.ini')
config = configparser['Geral']
train_path = config['TrainPath']
train_folder = os.fsencode(train_path)
files = os.listdir(train_folder)
imagens = []
for file in files:
filename = os.fsdecode(file)
id = filename[:12]
image = obtem_imagem(train_path, id)
if (image.any()):
imagens.append((id, image))
return imagens