-
Notifications
You must be signed in to change notification settings - Fork 1
/
readDicom.py
76 lines (62 loc) · 2.35 KB
/
readDicom.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
from Image import Image3D
from SaveImage import SaveImage
__author__ = 'Agnieszka'
from os import walk
from os.path import join
import dicom
import numpy as np
class ReadDirWithDicom(object):
def __init__(self, path):
my_path = path
files_in_dir = [join(my_path, fn) for fn in next(walk(my_path))[2]]
image3D = []
for f in files_in_dir:
if ".IMA" in f:
dicom_data_set = dicom.read_file(f)
image3D.append(dicom_data_set.pixel_array)
else:
raise IOError('wrong file- probably not DICOM')
self.Image3D = np.dstack(image3D)
print('Reading data done')
def get_all_slices_in_3D(self):
return self.Image3D
class ReadDirWithBinaryData(object):
def __init__(self, path):
"""
:param path: path to data containing data for one patient
:return:void
"""
self.my_path = path
#read sizing
meta_bin = open(self.my_path + 'hdr_CT.bin.txt')
self.width = int(meta_bin.readline().split(' = ')[1][:-2])
self.high = int(meta_bin.readline().split(' = ')[1][:-2])
self.depth = int(meta_bin.readline().split(' = ')[1][:-2])
self.data_type = meta_bin.readline().split(' = ')[1][:-2]
#read image
l = open(self.my_path + 'CT.bin', "r")
f = (np.array(np.fromfile(l, dtype="<f4")))
self.Image3D = np.reshape(f, (self.width, self.high, self.depth), order='F')
#hackig for weird binary values
if np.max(self.Image3D) < 0.1:
self.Image3D = self.Image3D.byteswap()
#read spacing
self.spacing = np.fromfile(self.my_path + 'spacing.txt', dtype=float, sep=" ")
self.im_agregation=Image3D(self.Image3D,self.spacing,self.width,self.high,self.depth,0)
SaveImage(path+'/CT_analysesClassification/1/').saveImage(self.im_agregation)
print('Reading data done')
def get_image3D(self):
"""
:return: Image from binary data as np.array with size self.width, self.high, self.depth
"""
return self.Image3D
def get_spacing(self):
"""
:return: return size of pixels in mm x|,y-,z /
"""
return self.spacing
def get_image_aggregation(self):
"""
:return:Image aggregation object
"""
return self.im_agregation