Commit 70d41721ccfcf24423efb76f3cf8ec81d59f22ce
1 parent
b2ea3928
Exists in
master
and in
68 other branches
ENH: Organized functions related to DICOM group selection/reconstruction
Showing
1 changed file
with
58 additions
and
85 deletions
Show diff stats
invesalius/reader/dicom_reader.py
... | ... | @@ -26,15 +26,19 @@ import gdcm |
26 | 26 | import constants as const |
27 | 27 | import dicom |
28 | 28 | import dicom_grouper |
29 | -from data.imagedata_utils import ResampleImage2D | |
29 | +import data.imagedata_utils as iu | |
30 | 30 | |
31 | 31 | def LoadImages(dir_): |
32 | - # TODO!!! SUPER GAMBIARRA!!! DO THIS BETTER | |
33 | 32 | |
34 | - patient_group = GetDicomFiles(dir_) | |
35 | - print "1111111111111" | |
36 | - #select the series with the largest | |
37 | - #number of slices. | |
33 | + patient_group = GetDicomGroups(dir_) | |
34 | + filelist, dicom, zspacing = SelectLargerDicomGroup(patient_group) | |
35 | + filelist = SortFiles(filelist, dicom) | |
36 | + imagedata = CreateImageData(filelist, zspacing) | |
37 | + | |
38 | + return imagedata, dicom | |
39 | + | |
40 | + | |
41 | +def SelectLargerDicomGroup(patient_group): | |
38 | 42 | nslices_old = 0 |
39 | 43 | for patient in patient_group: |
40 | 44 | group_list = patient.GetGroups() |
... | ... | @@ -43,107 +47,76 @@ def LoadImages(dir_): |
43 | 47 | print "nslices:", nslices |
44 | 48 | if (nslices >= nslices_old): |
45 | 49 | dicoms = group.GetList() |
46 | - spacing = group.spacing | |
50 | + zspacing = group.zspacing | |
47 | 51 | nslices_old = nslices |
48 | - print "22222222222222" | |
49 | - | |
50 | - file_list = [] | |
51 | - for dicom in dicoms: | |
52 | - file_list.append(dicom.image.file) | |
53 | - | |
54 | 52 | |
53 | + filelist = [] | |
54 | + for dicom in dicoms: | |
55 | + filelist.append(dicom.image.file) | |
56 | + return filelist, dicom, zspacing | |
55 | 57 | |
56 | - #Coronal Crash. necessary verify | |
58 | +def SortFiles(filelist, dicom): | |
59 | + # Sort slices | |
60 | + # FIXME: Coronal Crash. necessary verify | |
57 | 61 | if (dicom.image.orientation_label <> "CORONAL"): |
58 | 62 | #Organize reversed image |
59 | 63 | sorter = gdcm.IPPSorter() |
60 | 64 | sorter.SetComputeZSpacing(True) |
61 | 65 | sorter.SetZSpacingTolerance(1e-10) |
62 | - sorter.Sort(file_list) | |
66 | + sorter.Sort(filelist) | |
63 | 67 | |
64 | 68 | #Getting organized image |
65 | - files = sorter.GetFilenames() | |
69 | + filelist = sorter.GetFilenames() | |
66 | 70 | |
67 | - print "33333333333" | |
71 | + return filelist | |
68 | 72 | |
69 | - array = vtk.vtkStringArray() | |
70 | 73 | |
71 | - img_app = vtk.vtkImageAppend() | |
72 | - img_app.SetAppendAxis(2) #Define Stack in Z | |
74 | +def CreateImageData(filelist, zspacing): | |
73 | 75 | |
74 | 76 | if not(const.REDUCE_IMAGEDATA_QUALITY): |
75 | - print "not reduce" | |
76 | - for x in xrange(len(files)): | |
77 | - array.InsertValue(x,files[x]) | |
78 | - | |
79 | - read = vtkgdcm.vtkGDCMImageReader() | |
80 | - read.SetFileNames(array) | |
81 | - read.Update() | |
82 | - | |
83 | - image_data = vtk.vtkImageData() | |
84 | - image_data.DeepCopy(read.GetOutput()) | |
85 | - | |
86 | - image_data.SetSpacing(spacing) | |
77 | + array = vtk.vtkStringArray() | |
78 | + for x in xrange(len(filelist)): | |
79 | + array.InsertValue(x,filelist[x]) | |
80 | + | |
81 | + reader = vtkgdcm.vtkGDCMImageReader() | |
82 | + reader.SetFileNames(array) | |
83 | + reader.Update() | |
84 | + | |
85 | + # The zpacing is a DicomGroup property, so we need to set it | |
86 | + imagedata = vtk.vtkImageData() | |
87 | + imagedata.DeepCopy(reader.GetOutput()) | |
88 | + spacing = imagedata.GetSpacing() | |
89 | + imagedata.SetSpacing(spacing[0], spacing[1], zspacing) | |
87 | 90 | else: |
88 | - print "reduce" | |
89 | - for x in xrange(len(files)): | |
90 | - #SIf the resolution of the | |
91 | - #matrix is very large | |
92 | - read = vtkgdcm.vtkGDCMImageReader() | |
93 | - read.SetFileName(files[x]) | |
94 | - read.Update() | |
91 | + # Reformat each slice and future append them | |
92 | + appender = vtk.vtkImageAppend() | |
93 | + appender.SetAppendAxis(2) #Define Stack in Z | |
94 | + | |
95 | + # Reformat each slice | |
96 | + for x in xrange(len(filelist)): | |
97 | + # TODO: We need to check this automatically according | |
98 | + # to each computer's architecture | |
99 | + # If the resolution of the matrix is too large | |
100 | + reader = vtkgdcm.vtkGDCMImageReader() | |
101 | + reader.SetFileName(filelist[x]) | |
102 | + reader.Update() | |
95 | 103 | |
96 | 104 | #Resample image in x,y dimension |
97 | - img = ResampleImage2D(read.GetOutput(), 256) | |
105 | + slice_imagedata = iu.ResampleImage2D(reader.GetOutput(), 256) | |
98 | 106 | |
99 | 107 | #Stack images in Z axes |
100 | - img_app.AddInput(img) | |
101 | - img_app.Update() | |
102 | - | |
108 | + appender.AddInput(slice_imagedata) | |
109 | + appender.Update() | |
103 | 110 | |
104 | - print "444444444444444" | |
105 | - image_data = vtk.vtkImageData() | |
106 | - image_data.DeepCopy(img_app.GetOutput()) | |
107 | - image_data.SetSpacing(image_data.GetSpacing()[0],\ | |
108 | - image_data.GetSpacing()[1], spacing) | |
109 | - print "555555555" | |
111 | + # The zpacing is a DicomGroup property, so we need to set it | |
112 | + imagedata = vtk.vtkImageData() | |
113 | + imagedata.DeepCopy(appender.GetOutput()) | |
114 | + spacing = imagedata.GetSpacing() | |
110 | 115 | |
111 | - image_data.Update() | |
112 | - print "66666666666666" | |
116 | + imagedata.SetSpacing(spacing[0], spacing[1], zspacing) | |
113 | 117 | |
114 | - return image_data, dicom | |
115 | - | |
116 | -def GetDicomFilesOld(path, recursive = False): | |
117 | - # TODO!!! SUPER GAMBIARRA!!! DO THIS BETTER | |
118 | - """ | |
119 | - Separate only files of a DICOM Determined | |
120 | - directory. You can go recursively within | |
121 | - the directory. (recursive = True) | |
122 | - """ | |
123 | - result = [] | |
124 | - | |
125 | - if (recursive == True): | |
126 | - | |
127 | - if os.path.isdir(path) and not os.path.islink(path): | |
128 | - | |
129 | - files = os.listdir(path) | |
130 | - | |
131 | - for file in files: | |
132 | - path_ = os.path.join(path, file) | |
133 | - FindDicom(path_, True) | |
134 | - else: | |
135 | - read_dicom = dicom.Parser() | |
136 | - if(read_dicom.SetFileName(path)): | |
137 | - if (read_dicom.GetImagePosition()): | |
138 | - result.append(path) | |
139 | - else: | |
140 | - files = glob.glob(path + os.sep + "*") | |
141 | - for x in xrange(len(files)): | |
142 | - read_dicom = dicom.Parser() | |
143 | - if (read_dicom.SetFileName(files[x])): | |
144 | - result.append(files[x]) | |
145 | - # TODO!!! SUPER GAMBIARRA!!! DO THIS BETTER | |
146 | - return result | |
118 | + imagedata.Update() | |
119 | + return imagedata | |
147 | 120 | |
148 | 121 | def GetSeries(path): |
149 | 122 | """ |
... | ... | @@ -157,7 +130,7 @@ def GetSeries(path): |
157 | 130 | |
158 | 131 | return dcm_series.GetOutput() |
159 | 132 | |
160 | -def GetDicomFiles(directory, recursive=True): | |
133 | +def GetDicomGroups(directory, recursive=True): | |
161 | 134 | """ |
162 | 135 | Return all full paths to DICOM files inside given directory. |
163 | 136 | """ | ... | ... |