Commit 3a906aa240f11aeaa716df8ad345a3cc40b5314e
1 parent
4e527a1a
Exists in
vti
Reading vti
Showing
3 changed files
with
80 additions
and
2 deletions
Show diff stats
invesalius/control.py
@@ -33,6 +33,7 @@ import data.volume as volume | @@ -33,6 +33,7 @@ import data.volume as volume | ||
33 | import gui.dialogs as dialog | 33 | import gui.dialogs as dialog |
34 | import project as prj | 34 | import project as prj |
35 | import reader.analyze_reader as analyze | 35 | import reader.analyze_reader as analyze |
36 | +import reader.vtk_reader as vtk_reader | ||
36 | import reader.dicom_grouper as dg | 37 | import reader.dicom_grouper as dg |
37 | import reader.dicom_reader as dcm | 38 | import reader.dicom_reader as dcm |
38 | import session as ses | 39 | import session as ses |
@@ -351,8 +352,12 @@ class Controller(): | @@ -351,8 +352,12 @@ class Controller(): | ||
351 | self.CreateAnalyzeProject(imagedata) | 352 | self.CreateAnalyzeProject(imagedata) |
352 | # OPTION 3: Nothing... | 353 | # OPTION 3: Nothing... |
353 | else: | 354 | else: |
354 | - utils.debug("No medical images found on given directory") | ||
355 | - return | 355 | + imagedata = vtk_reader.ReadDirectory(directory) |
356 | + if imagedata: | ||
357 | + self.CreateVTKProject(imagedata) | ||
358 | + else: | ||
359 | + utils.debug("No medical images found on given directory") | ||
360 | + return | ||
356 | self.LoadProject() | 361 | self.LoadProject() |
357 | Publisher.sendMessage("Enable state project", True) | 362 | Publisher.sendMessage("Enable state project", True) |
358 | 363 | ||
@@ -414,6 +419,44 @@ class Controller(): | @@ -414,6 +419,44 @@ class Controller(): | ||
414 | 419 | ||
415 | Publisher.sendMessage('End busy cursor') | 420 | Publisher.sendMessage('End busy cursor') |
416 | 421 | ||
422 | + def CreateVTKProject(self, imagedata): | ||
423 | + proj = prj.Project() | ||
424 | + proj.imagedata = None | ||
425 | + proj.name = _("Untitled") | ||
426 | + proj.SetAcquisitionModality("CT") | ||
427 | + #TODO: Verify if all Analyse are in AXIAL orientation | ||
428 | + | ||
429 | + # To get Z, X, Y (used by InVesaliu), not X, Y, Z | ||
430 | + | ||
431 | + proj.threshold_range = imagedata.GetScalarRange() | ||
432 | + proj.window = proj.threshold_range[1] - proj.threshold_range[0] | ||
433 | + proj.level = (0.5 * (proj.threshold_range[1] + proj.threshold_range[0])) | ||
434 | + proj.spacing = imagedata.GetSpacing() | ||
435 | + proj.matrix_shape = imagedata.GetDimensions()[::-1] | ||
436 | + proj.original_orientation = const.AXIAL | ||
437 | + | ||
438 | + matrix, matrix_filename = image_utils.vtk2mmap(imagedata) | ||
439 | + proj.matrix_dtype = matrix.dtype.name | ||
440 | + proj.matrix_filename = matrix_filename | ||
441 | + | ||
442 | + self.Slice = sl.Slice() | ||
443 | + self.Slice.matrix = matrix | ||
444 | + self.Slice.matrix_filename = matrix_filename | ||
445 | + | ||
446 | + self.Slice.window_level = proj.level | ||
447 | + self.Slice.window_width = proj.window | ||
448 | + self.Slice.spacing = proj.spacing | ||
449 | + | ||
450 | + session = ses.Session() | ||
451 | + filename = proj.name+".inv3" | ||
452 | + | ||
453 | + filename = filename.replace("/", "") #Fix problem case other/Skull_DICOM | ||
454 | + | ||
455 | + dirpath = session.CreateProject(filename) | ||
456 | + | ||
457 | + Publisher.sendMessage('Update threshold limits list', | ||
458 | + proj.threshold_range) | ||
459 | + | ||
417 | def CreateAnalyzeProject(self, imagedata): | 460 | def CreateAnalyzeProject(self, imagedata): |
418 | header = imagedata.get_header() | 461 | header = imagedata.get_header() |
419 | proj = prj.Project() | 462 | proj = prj.Project() |
invesalius/data/imagedata_utils.py
@@ -493,6 +493,16 @@ def dcm2memmap(files, slice_size, orientation, resolution_percentage): | @@ -493,6 +493,16 @@ def dcm2memmap(files, slice_size, orientation, resolution_percentage): | ||
493 | 493 | ||
494 | return matrix, scalar_range, temp_file | 494 | return matrix, scalar_range, temp_file |
495 | 495 | ||
496 | +def vtk2mmap(imagedata): | ||
497 | + temp_file = tempfile.mktemp() | ||
498 | + shape = imagedata.GetDimensions()[::-1] | ||
499 | + array = numpy_support.vtk_to_numpy(imagedata.GetPointData().GetScalars()) | ||
500 | + array.shape = shape | ||
501 | + matrix = numpy.memmap(temp_file, mode='w+', dtype='int16', shape=shape) | ||
502 | + matrix[:] = array | ||
503 | + matrix.flush() | ||
504 | + return matrix, temp_file | ||
505 | + | ||
496 | def analyze2mmap(analyze): | 506 | def analyze2mmap(analyze): |
497 | data = analyze.get_data() | 507 | data = analyze.get_data() |
498 | header = analyze.get_header() | 508 | header = analyze.get_header() |
@@ -0,0 +1,25 @@ | @@ -0,0 +1,25 @@ | ||
1 | +import os | ||
2 | +import multiprocessing | ||
3 | +import tempfile | ||
4 | + | ||
5 | +import vtk | ||
6 | + | ||
7 | +def ReadVTK(filename): | ||
8 | + r = vtk.vtkXMLImageDataReader() | ||
9 | + r.SetFileName(filename) | ||
10 | + r.Update() | ||
11 | + | ||
12 | + return r.GetOutput() | ||
13 | + | ||
14 | +def ReadDirectory(dir_): | ||
15 | + """ | ||
16 | + Looking for analyze files in the given directory | ||
17 | + """ | ||
18 | + imagedata = None | ||
19 | + for root, sub_folders, files in os.walk(dir_): | ||
20 | + for file in files: | ||
21 | + if file.split(".")[-1] == "vti": | ||
22 | + filename = os.path.join(root,file) | ||
23 | + imagedata = ReadVTK(filename) | ||
24 | + return imagedata | ||
25 | + return imagedata |