Commit 1a61d02abd994b1126db3ce3d6a007d035ee7bf9

Authored by tfmoraes
1 parent bdb92e42

ENH: Transforming dicom in images to show in import panel. Ticket #74

invesalius/gui/dicom_preview_panel.py
@@ -274,8 +274,8 @@ class Preview(wx.Panel): @@ -274,8 +274,8 @@ class Preview(wx.Panel):
274 # Will it be white? 274 # Will it be white?
275 self.select_on = False 275 self.select_on = False
276 self._init_ui() 276 self._init_ui()
277 - self._init_vtk()  
278 - self._bind_events() 277 + #self._init_vtk()
  278 + #self._bind_events()
279 279
280 def _init_ui(self): 280 def _init_ui(self):
281 281
@@ -285,7 +285,9 @@ class Preview(wx.Panel): @@ -285,7 +285,9 @@ class Preview(wx.Panel):
285 self.subtitle = wx.StaticText(self, -1, "Image", 285 self.subtitle = wx.StaticText(self, -1, "Image",
286 style=wx.ALIGN_CENTER) 286 style=wx.ALIGN_CENTER)
287 287
288 - self.panel = wx.Panel(self, -1) 288 + self.image_viewer = wx.StaticBitmap(self, -1)
  289 +
  290 + #self.panel = wx.Panel(self, -1)
289 291
290 self.SetBackgroundColour((255,255,255)) 292 self.SetBackgroundColour((255,255,255))
291 293
@@ -295,7 +297,7 @@ class Preview(wx.Panel): @@ -295,7 +297,7 @@ class Preview(wx.Panel):
295 wx.GROW|wx.EXPAND|wx. ALIGN_CENTER_HORIZONTAL) 297 wx.GROW|wx.EXPAND|wx. ALIGN_CENTER_HORIZONTAL)
296 self.sizer.Add(self.subtitle, 1, 298 self.sizer.Add(self.subtitle, 1,
297 wx.GROW|wx.EXPAND|wx.ALIGN_CENTER_HORIZONTAL) 299 wx.GROW|wx.EXPAND|wx.ALIGN_CENTER_HORIZONTAL)
298 - self.sizer.Add(self.panel, 5, wx.GROW|wx.EXPAND|wx.ALL, 4) 300 + self.sizer.Add(self.image_viewer, 5, wx.GROW|wx.EXPAND|wx.ALL, 4)
299 self.sizer.Fit(self) 301 self.sizer.Fit(self)
300 302
301 303
@@ -408,7 +410,7 @@ class Preview(wx.Panel): @@ -408,7 +410,7 @@ class Preview(wx.Panel):
408 def SetSubtitle(self, subtitle): 410 def SetSubtitle(self, subtitle):
409 self.subtitle.SetLabel(subtitle) 411 self.subtitle.SetLabel(subtitle)
410 412
411 - def SetImage(self, image_file): 413 + def SetDicomToPreview(self, image_file):
412 """ 414 """
413 Set a Image to preview. 415 Set a Image to preview.
414 """ 416 """
@@ -424,31 +426,35 @@ class Preview(wx.Panel): @@ -424,31 +426,35 @@ class Preview(wx.Panel):
424 #image_reader.SetFileName(image_file[0]) 426 #image_reader.SetFileName(image_file[0])
425 #image = image_reader.GetOutput() 427 #image = image_reader.GetOutput()
426 428
427 - image = image_file[0]  
428 -  
429 - scale = image.GetScalarRange()  
430 -  
431 - cast = vtk.vtkImageMapToWindowLevelColors()  
432 - #cast.SetShift(abs(scale[0]))  
433 - #cast.SetScale(255.0/(scale[1] - scale[0]))  
434 - #cast.ClampOverflowOn()  
435 - cast.SetInput(image)  
436 - #cast.SetOutputScalarTypeToUnsignedChar()  
437 - try:  
438 - window = float(image_file[1])  
439 - level = float(image_file[2])  
440 - except TypeError:  
441 - #TODO: These values are good?  
442 - level = 230  
443 - window = 150  
444 -  
445 - self.data = image_file[-1]  
446 -  
447 - cast.SetWindow(window)  
448 - cast.SetLevel(level)  
449 - self.actor.SetInput(cast.GetOutput())  
450 - self.render.ResetCamera()  
451 - self.interactor.Render() 429 + image = image_file[-1].image.jpeg_file
  430 + #img = wx.Image(image, wx.BITMAP_TYPE_JPEG)
  431 + #img.Rescale(70, 70)
  432 + #bmp = wx.BitmapFromImage(img)
  433 + self.image_viewer.SetBitmap(image)
  434 +
  435 + #scale = image.GetScalarRange()
  436 +
  437 + #cast = vtk.vtkImageMapToWindowLevelColors()
  438 + ##cast.SetShift(abs(scale[0]))
  439 + ##cast.SetScale(255.0/(scale[1] - scale[0]))
  440 + ##cast.ClampOverflowOn()
  441 + #cast.SetInput(image)
  442 + ##cast.SetOutputScalarTypeToUnsignedChar()
  443 + #try:
  444 + # window = float(image_file[1])
  445 + # level = float(image_file[2])
  446 + #except TypeError:
  447 + # #TODO: These values are good?
  448 + # level = 230
  449 + # window = 150
  450 +
  451 + #self.data = image_file[-1]
  452 +
  453 + #cast.SetWindow(window)
  454 + #cast.SetLevel(level)
  455 + #self.actor.SetInput(cast.GetOutput())
  456 + #self.render.ResetCamera()
  457 + #self.interactor.Render()
452 458
453 def ShowShadow(self): 459 def ShowShadow(self):
454 self._nImgSize = 16 460 self._nImgSize = 16
@@ -605,13 +611,14 @@ class DicomPreviewSeries(wx.Panel): @@ -605,13 +611,14 @@ class DicomPreviewSeries(wx.Panel):
605 self.group_list = group_list 611 self.group_list = group_list
606 n = 0 612 n = 0
607 for group in group_list: 613 for group in group_list:
608 - info = (group.dicom.image.imagedata, 614 + info = (group.dicom.image,
609 float(group.dicom.image.window), 615 float(group.dicom.image.window),
610 float(group.dicom.image.level), 616 float(group.dicom.image.level),
611 group.title, 617 group.title,
612 "%d Images" %(group.nslices), 618 "%d Images" %(group.nslices),
613 n, 619 n,
614 - group_list) 620 + group_list,
  621 + group.dicom)
615 self.files.append(info) 622 self.files.append(info)
616 n+=1 623 n+=1
617 624
@@ -647,7 +654,7 @@ class DicomPreviewSeries(wx.Panel): @@ -647,7 +654,7 @@ class DicomPreviewSeries(wx.Panel):
647 654
648 for f, p in zip(self.files[initial:final], self.previews): 655 for f, p in zip(self.files[initial:final], self.previews):
649 #print "f", f 656 #print "f", f
650 - p.SetImage(f) 657 + p.SetDicomToPreview(f)
651 #p.interactor.Render() 658 #p.interactor.Render()
652 659
653 for f, p in zip(self.files[initial:final], self.previews): 660 for f, p in zip(self.files[initial:final], self.previews):
@@ -751,12 +758,13 @@ class DicomPreview(wx.Panel): @@ -751,12 +758,13 @@ class DicomPreview(wx.Panel):
751 dicom_files = group.GetHandSortedList() 758 dicom_files = group.GetHandSortedList()
752 n = 0 759 n = 0
753 for dicom in dicom_files: 760 for dicom in dicom_files:
754 - info = (dicom.image.imagedata, 761 + info = (dicom.image,
755 dicom.image.window, 762 dicom.image.window,
756 dicom.image.level, 763 dicom.image.level,
757 "Image %d" % (dicom.image.number), 764 "Image %d" % (dicom.image.number),
758 "%.2f" % (dicom.image.position[2]), 765 "%.2f" % (dicom.image.position[2]),
759 - n) 766 + n,
  767 + dicom)
760 self.files.append(info) 768 self.files.append(info)
761 n+=1 769 n+=1
762 770
@@ -793,7 +801,7 @@ class DicomPreview(wx.Panel): @@ -793,7 +801,7 @@ class DicomPreview(wx.Panel):
793 self.nhidden_last_display = 0 801 self.nhidden_last_display = 0
794 802
795 for f, p in zip(self.files[initial:final], self.previews): 803 for f, p in zip(self.files[initial:final], self.previews):
796 - p.SetImage(f) 804 + p.SetDicomToPreview(f)
797 #p.interactor.Render() 805 #p.interactor.Render()
798 806
799 for f, p in zip(self.files[initial:final], self.previews): 807 for f, p in zip(self.files[initial:final], self.previews):
invesalius/reader/dicom.py
@@ -17,7 +17,16 @@ @@ -17,7 +17,16 @@
17 # PARTICULAR. Consulte a Licenca Publica Geral GNU para obter mais 17 # PARTICULAR. Consulte a Licenca Publica Geral GNU para obter mais
18 # detalhes. 18 # detalhes.
19 #--------------------------------------------------------------------- 19 #---------------------------------------------------------------------
20 -from numpy.lib import type_check 20 +import tempfile
  21 +import time
  22 +
  23 +import gdcm
  24 +import Image
  25 +import vtk
  26 +import vtkgdcm
  27 +import wx
  28 +
  29 +from vtk.util import numpy_support
21 30
22 # In DICOM file format, if multiple values are present for the 31 # In DICOM file format, if multiple values are present for the
23 # "Window Center" (Level) and "Window Width", both attributes 32 # "Window Center" (Level) and "Window Width", both attributes
@@ -68,10 +77,6 @@ INFO_KEYS = \ @@ -68,10 +77,6 @@ INFO_KEYS = \
68 'StudyAdmittingDiagnosis', 77 'StudyAdmittingDiagnosis',
69 ] 78 ]
70 79
71 -import vtkgdcm  
72 -import gdcm  
73 -import time  
74 -  
75 class Parser(): 80 class Parser():
76 """ 81 """
77 Medical image parser. Used to parse medical image tags. 82 Medical image parser. Used to parse medical image tags.
@@ -1796,6 +1801,8 @@ class Dicom(object): @@ -1796,6 +1801,8 @@ class Dicom(object):
1796 1801
1797 1802
1798 1803
  1804 +
  1805 +
1799 class Patient(object): 1806 class Patient(object):
1800 def __init__(self): 1807 def __init__(self):
1801 pass 1808 pass
@@ -1833,7 +1840,7 @@ class Acquisition(object): @@ -1833,7 +1840,7 @@ class Acquisition(object):
1833 class Image(object): 1840 class Image(object):
1834 1841
1835 def __init__(self): 1842 def __init__(self):
1836 - pass 1843 + self._jpeg_file = None
1837 1844
1838 def SetParser(self, parser): 1845 def SetParser(self, parser):
1839 self.level = parser.GetImageWindowLevel() 1846 self.level = parser.GetImageWindowLevel()
@@ -1867,5 +1874,38 @@ class Image(object): @@ -1867,5 +1874,38 @@ class Image(object):
1867 spacing[1] = round(spacing[1],2) 1874 spacing[1] = round(spacing[1],2)
1868 spacing[2] = round(spacing[2],2) 1875 spacing[2] = round(spacing[2],2)
1869 self.spacing = spacing 1876 self.spacing = spacing
  1877 +
  1878 + def SetTempDir(self, directory):
  1879 + self.directory = directory
1870 1880
1871 - 1881 + @property
  1882 + def jpeg_file(self):
  1883 + if self._jpeg_file:
  1884 + return self._jpeg_file
  1885 + else:
  1886 + self._jpeg_file = tempfile.mktemp(suffix='.jpeg', dir=self.directory)
  1887 +
  1888 + colorer = vtk.vtkImageMapToWindowLevelColors()
  1889 + colorer.SetInput(self.imagedata)
  1890 + colorer.SetWindow(float(self.window))
  1891 + colorer.SetLevel(float(self.level))
  1892 + colorer.SetOutputFormatToRGB()
  1893 + colorer.Update()
  1894 +
  1895 + width, height, z = colorer.GetOutput().GetDimensions()
  1896 +
  1897 + #jpeg_writer = vtk.vtkJPEGWriter()
  1898 + #jpeg_writer.SetInput(colorer.GetOutput())
  1899 + ##jpeg_writer.SetFileName(self._jpeg_file)
  1900 + #jpeg_writer.WriteToMemoryOn()
  1901 + #jpeg_writer.Write()
  1902 + ##jpeg_writer.Write()
  1903 +
  1904 + r = colorer.GetOutput().GetPointData().GetScalars()
  1905 + ni = numpy_support.vtk_to_numpy(r)
  1906 + #ni = ni.reshape(width, height, 3)
  1907 + print width, height, z,ni.shape
  1908 + img = wx.ImageFromBuffer(width, height, ni)
  1909 + img.Rescale(70, 70)
  1910 + self._jpeg_file = wx.BitmapFromImage(img)
  1911 + return self._jpeg_file