diff --git a/invesalius/control.py b/invesalius/control.py index c94d0dc..3ec06f7 100644 --- a/invesalius/control.py +++ b/invesalius/control.py @@ -33,6 +33,7 @@ import data.volume as volume import gui.dialogs as dialog import project as prj import reader.analyze_reader as analyze +import reader.vtk_reader as vtk_reader import reader.dicom_grouper as dg import reader.dicom_reader as dcm import session as ses @@ -351,8 +352,12 @@ class Controller(): self.CreateAnalyzeProject(imagedata) # OPTION 3: Nothing... else: - utils.debug("No medical images found on given directory") - return + imagedata = vtk_reader.ReadDirectory(directory) + if imagedata: + self.CreateVTKProject(imagedata) + else: + utils.debug("No medical images found on given directory") + return self.LoadProject() Publisher.sendMessage("Enable state project", True) @@ -414,6 +419,44 @@ class Controller(): Publisher.sendMessage('End busy cursor') + def CreateVTKProject(self, imagedata): + proj = prj.Project() + proj.imagedata = None + proj.name = _("Untitled") + proj.SetAcquisitionModality("CT") + #TODO: Verify if all Analyse are in AXIAL orientation + + # To get Z, X, Y (used by InVesaliu), not X, Y, Z + + proj.threshold_range = imagedata.GetScalarRange() + proj.window = proj.threshold_range[1] - proj.threshold_range[0] + proj.level = (0.5 * (proj.threshold_range[1] + proj.threshold_range[0])) + proj.spacing = imagedata.GetSpacing() + proj.matrix_shape = imagedata.GetDimensions()[::-1] + proj.original_orientation = const.AXIAL + + matrix, matrix_filename = image_utils.vtk2mmap(imagedata) + proj.matrix_dtype = matrix.dtype.name + proj.matrix_filename = matrix_filename + + self.Slice = sl.Slice() + self.Slice.matrix = matrix + self.Slice.matrix_filename = matrix_filename + + self.Slice.window_level = proj.level + self.Slice.window_width = proj.window + self.Slice.spacing = proj.spacing + + session = ses.Session() + filename = proj.name+".inv3" + + filename = filename.replace("/", "") #Fix problem case other/Skull_DICOM + + dirpath = session.CreateProject(filename) + + Publisher.sendMessage('Update threshold limits list', + proj.threshold_range) + def CreateAnalyzeProject(self, imagedata): header = imagedata.get_header() proj = prj.Project() diff --git a/invesalius/data/imagedata_utils.py b/invesalius/data/imagedata_utils.py index bb48376..df4af7f 100644 --- a/invesalius/data/imagedata_utils.py +++ b/invesalius/data/imagedata_utils.py @@ -493,6 +493,16 @@ def dcm2memmap(files, slice_size, orientation, resolution_percentage): return matrix, scalar_range, temp_file +def vtk2mmap(imagedata): + temp_file = tempfile.mktemp() + shape = imagedata.GetDimensions()[::-1] + array = numpy_support.vtk_to_numpy(imagedata.GetPointData().GetScalars()) + array.shape = shape + matrix = numpy.memmap(temp_file, mode='w+', dtype='int16', shape=shape) + matrix[:] = array + matrix.flush() + return matrix, temp_file + def analyze2mmap(analyze): data = analyze.get_data() header = analyze.get_header() diff --git a/invesalius/reader/vtk_reader.py b/invesalius/reader/vtk_reader.py new file mode 100644 index 0000000..e4ed03a --- /dev/null +++ b/invesalius/reader/vtk_reader.py @@ -0,0 +1,25 @@ +import os +import multiprocessing +import tempfile + +import vtk + +def ReadVTK(filename): + r = vtk.vtkXMLImageDataReader() + r.SetFileName(filename) + r.Update() + + return r.GetOutput() + +def ReadDirectory(dir_): + """ + Looking for analyze files in the given directory + """ + imagedata = None + for root, sub_folders, files in os.walk(dir_): + for file in files: + if file.split(".")[-1] == "vti": + filename = os.path.join(root,file) + imagedata = ReadVTK(filename) + return imagedata + return imagedata -- libgit2 0.21.2