diff --git a/invesalius/control.py b/invesalius/control.py index b1520ab..9bdb703 100755 --- a/invesalius/control.py +++ b/invesalius/control.py @@ -9,6 +9,7 @@ import project as prj import data.imagedata_utils as utils import data.surface as surface import data.volume as volume +import reader.dicom_grouper import gui.dialogs as dialog import reader.dicom_reader as dcm import reader.analyze_reader as analyze @@ -23,23 +24,125 @@ class Controller(): self.__bind_events() def __bind_events(self): - ps.Publisher().subscribe(self.ImportDirectory, 'Import directory') + ps.Publisher().subscribe(self.OnImportMedicalImages, 'Import directory') ps.Publisher().subscribe(self.StartImportPanel, "Load data to import panel") ps.Publisher().subscribe(self.LoadRaycastingPreset, 'Load raycasting preset') ps.Publisher().subscribe(self.SaveRaycastingPreset, 'Save raycasting preset') + ps.Publisher().subscribe(self.OnOpenDicomGroup, + 'Open DICOM group') def StartImportPanel(self, pubsub_evt): # path to directory path = pubsub_evt.data # retrieve DICOM files splited into groups - dicom_series = dcm.GetDicomGroups(path) - ps.Publisher().sendMessage("Load import panel", dicom_series) + patient_series = dcm.GetDicomGroups(path) + ps.Publisher().sendMessage("Load import panel", patient_series) + first_patient = patient_series[0] + #ps.Publisher().sendMessage("Load dicom preview", first_patient) + + def OnImportMedicalImages(self, pubsub_evt): + directory = pubsub_evt.data + self.ImportMedicalImages(directory) + + def ImportMedicalImages(self, directory): + # OPTION 1: DICOM? + patients_groups = dcm.GetDicomGroups(directory) + if len(patients_groups): + group = dcm.SelectLargerDicomGroup(patients_groups) + imagedata, dicom = self.OpenDicomGroup(group, gui=False) + self.CreateDicomProject(imagedata, dicom) + # OPTION 2: ANALYZE? + else: + imagedata = analyze.ReadDirectory(directory) + if imagedata: + self.CreateAnalyzeProject(imagedata) + # OPTION 3: Nothing... + else: + print "No medical images found on given directory" + return + self.LoadProject() + + def LoadProject(self): + proj = prj.Project() + ps.Publisher().sendMessage('Set project name', proj.name) + ps.Publisher().sendMessage('Load slice to viewer', (proj.imagedata)) + self.LoadImagedataInfo() # TODO: where do we insert this << and # were swapped +import gdcm + ORIENT_MAP = {"SAGITTAL":0, "CORONAL":1, "AXIAL":2, "OBLIQUE":2} @@ -96,7 +98,28 @@ class DicomGroup: # (interpolated) return self.slices_dict.values() - def GetSortedList(self): + def GetFilenameList(self): + # Should be called when user selects this group + # This list will be used to create the vtkImageData + # (interpolated) + + filelist = [dicom.image.file for dicom in + self.slices_dict.values()] + + # Sort slices using GDCM + if (self.dicom.image.orientation_label <> "CORONAL"): + #Organize reversed image + sorter = gdcm.IPPSorter() + sorter.SetComputeZSpacing(True) + sorter.SetZSpacingTolerance(1e-10) + sorter.Sort(filelist) + filelist = sorter.GetFilenames() + + #Getting organized image + return filelist + + + def GetHandSortedList(self): # This will be used to fix problem 1, after merging # single DicomGroups of same study_id and orientation list_ = self.slices_dict.values() @@ -106,7 +129,7 @@ class DicomGroup: return list_ def UpdateZSpacing(self): - list_ = self.GetSortedList() + list_ = self.GetHandSortedList() if (len(list_) > 1): dicom = list_[0] @@ -246,7 +269,7 @@ class PatientGroup: group_counter = 0 for group_key in dict_to_change: # 2nd STEP: SORT - sorted_list = dict_to_change[group_key].GetSortedList() + sorted_list = dict_to_change[group_key].GetHandSortedList() # 3rd STEP: CHECK DIFFERENCES axis = ORIENT_MAP[group_key[0]] # based on orientation diff --git a/invesalius/reader/dicom_reader.py b/invesalius/reader/dicom_reader.py index 1be99a4..ca72b43 100644 --- a/invesalius/reader/dicom_reader.py +++ b/invesalius/reader/dicom_reader.py @@ -28,32 +28,26 @@ import dicom import dicom_grouper import data.imagedata_utils as iu -def LoadImages(dir_): +def ReadDicomGroup(dir_): patient_group = GetDicomGroups(dir_) - filelist, dicom, zspacing = SelectLargerDicomGroup(patient_group) - filelist = SortFiles(filelist, dicom) - imagedata = CreateImageData(filelist, zspacing) - - return imagedata, dicom + if len(patient_group) > 0: + filelist, dicom, zspacing = SelectLargerDicomGroup(patient_group) + filelist = SortFiles(filelist, dicom) + imagedata = CreateImageData(filelist, zspacing) + return imagedata, dicom + else: + return False def SelectLargerDicomGroup(patient_group): - nslices_old = 0 + maxslices = 0 for patient in patient_group: group_list = patient.GetGroups() for group in group_list: - nslices = group.nslices - print "nslices:", nslices - if (nslices >= nslices_old): - dicoms = group.GetList() - zspacing = group.zspacing - nslices_old = nslices - - filelist = [] - for dicom in dicoms: - filelist.append(dicom.image.file) - return filelist, dicom, zspacing + if group.nslices > maxslices: + larger_group = group + return larger_group def SortFiles(filelist, dicom): # Sort slices @@ -71,53 +65,6 @@ def SortFiles(filelist, dicom): return filelist -def CreateImageData(filelist, zspacing): - - if not(const.REDUCE_IMAGEDATA_QUALITY): - array = vtk.vtkStringArray() - for x in xrange(len(filelist)): - array.InsertValue(x,filelist[x]) - - reader = vtkgdcm.vtkGDCMImageReader() - reader.SetFileNames(array) - reader.Update() - - # The zpacing is a DicomGroup property, so we need to set it - imagedata = vtk.vtkImageData() - imagedata.DeepCopy(reader.GetOutput()) - spacing = imagedata.GetSpacing() - imagedata.SetSpacing(spacing[0], spacing[1], zspacing) - else: - # Reformat each slice and future append them - appender = vtk.vtkImageAppend() - appender.SetAppendAxis(2) #Define Stack in Z - - # Reformat each slice - for x in xrange(len(filelist)): - # TODO: We need to check this automatically according - # to each computer's architecture - # If the resolution of the matrix is too large - reader = vtkgdcm.vtkGDCMImageReader() - reader.SetFileName(filelist[x]) - reader.Update() - - #Resample image in x,y dimension - slice_imagedata = iu.ResampleImage2D(reader.GetOutput(), 256) - - #Stack images in Z axes - appender.AddInput(slice_imagedata) - appender.Update() - - # The zpacing is a DicomGroup property, so we need to set it - imagedata = vtk.vtkImageData() - imagedata.DeepCopy(appender.GetOutput()) - spacing = imagedata.GetSpacing() - - imagedata.SetSpacing(spacing[0], spacing[1], zspacing) - - imagedata.Update() - return imagedata - def GetDicomGroups(directory, recursive=True): """ -- libgit2 0.21.2