From 0ce0c1ddee765505bde26017a4f55019c75cc0f0 Mon Sep 17 00:00:00 2001 From: paulojamorim Date: Mon, 11 Jan 2010 17:05:11 +0000 Subject: [PATCH] ENH: Choose the best image resolution in accordance with the amount of memory installed, (Tested in the Windows) #76 --- invesalius/constants.py | 13 ++++--------- invesalius/data/imagedata_utils.py | 90 +++++++++++++++++++++++++++++++++++++++++++++++------------------------------------------- invesalius/utils.py | 87 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++-------------- 3 files changed, 124 insertions(+), 66 deletions(-) diff --git a/invesalius/constants.py b/invesalius/constants.py index 47274c2..0f59bc0 100644 --- a/invesalius/constants.py +++ b/invesalius/constants.py @@ -227,12 +227,7 @@ WINDOW_LEVEL = {_("Abdomen"):(350,50), _("Vasculature - Hard"):(240,80), _("Vasculature - Soft"):(650,160)} -if (sys.platform == 'win32') and (platform.architecture()[0] == '32bit'): - REDUCE_IMAGEDATA_QUALITY = 1 -elif (sys.platform == 'darwin'): - REDUCE_IMAGEDATA_QUALITY = 1 -else: - REDUCE_IMAGEDATA_QUALITY = 0 +REDUCE_IMAGEDATA_QUALITY = 0 ICON_DIR = os.path.abspath(os.path.join('..', 'icons')) @@ -257,8 +252,8 @@ _("Black & White") _("Bone + Skin") _("Bone + Skin II") _("Dark Bone") -_("Glossy") -_("Glossy II") +_("Glossy") +_("Glossy II") _("Gold Bone") _("High Contrast") _("Low Contrast") @@ -339,7 +334,7 @@ FILETYPE_VRML = wx.NewId() FILETYPE_OBJ = wx.NewId() FILETYPE_VTP = wx.NewId() FILETYPE_PLY = wx.NewId() - + FILETYPE_IMAGEDATA = wx.NewId() FILETYPE_BMP = wx.NewId() diff --git a/invesalius/data/imagedata_utils.py b/invesalius/data/imagedata_utils.py index 877f7c9..020177d 100644 --- a/invesalius/data/imagedata_utils.py +++ b/invesalius/data/imagedata_utils.py @@ -49,16 +49,14 @@ def ResampleImage3D(imagedata, value): return resample.GetOutput() -def ResampleImage2D(imagedata, xy_dimension, +def ResampleImage2D(imagedata, px, py, update_progress = None): """ Resample vtkImageData matrix. """ extent = imagedata.GetExtent() - print "-----------------------------" - print "extent:", extent spacing = imagedata.GetSpacing() - print "spacing:", spacing + #if extent[1]==extent[3]: # f = extent[1] @@ -68,26 +66,24 @@ def ResampleImage2D(imagedata, xy_dimension, # f = extent[3] if abs(extent[1]-extent[3]) < abs(extent[3]-extent[5]): - print 1 f = extent[1] elif abs(extent[1]-extent[5]) < abs(extent[1] - extent[3]): - print 2 f = extent[1] elif abs(extent[3]-extent[5]) < abs(extent[1] - extent[3]): - print 3 f = extent[3] else: - print 4 f = extent[1] - - factor = xy_dimension/float(f+1) + + factor_x = px/float(f+1) + factor_y = py/float(f+1) + resample = vtk.vtkImageResample() resample.SetInput(imagedata) - resample.SetAxisMagnificationFactor(0, factor) - resample.SetAxisMagnificationFactor(1, factor) - resample.SetOutputSpacing(spacing[0] * factor, spacing[1] * factor, spacing[2]) + resample.SetAxisMagnificationFactor(0, factor_x) + resample.SetAxisMagnificationFactor(1, factor_y) + resample.SetOutputSpacing(spacing[0] * factor_x, spacing[1] * factor_y, spacing[2]) if (update_progress): message = "Generating multiplanar visualization..." resample.AddObserver("ProgressEvent", lambda obj, @@ -189,7 +185,7 @@ def View(imagedata): viewer.SetColorWindow(200) viewer.SetColorLevel(100) viewer.Render() - + import time time.sleep(10) @@ -199,40 +195,50 @@ def ViewGDCM(imagedata): viewer.SetColorWindow(500.) viewer.SetColorLevel(50.) viewer.Render() - + import time time.sleep(5) - + def ExtractVOI(imagedata,xi,xf,yi,yf,zi,zf): """ - Cropping the vtkImagedata according + Cropping the vtkImagedata according with values. - """ + """ voi = vtk.vtkExtractVOI() voi.SetVOI(xi,xf,yi,yf,zi,zf) voi.SetInput(imagedata) voi.SetSampleRate(1, 1, 1) - voi.Update() + voi.Update() return voi.GetOutput() def CreateImageData(filelist, zspacing, size, bits): message = "Generating multiplanar visualization..." - + if not const.VTK_WARNING: fow = vtk.vtkFileOutputWindow() fow.SetFileName('vtkoutput.txt') ow = vtk.vtkOutputWindow() ow.SetInstance(fow) - + + x,y = size + px, py = utils.PredictingMemory(len(filelist), x, y, bits) + + print "Image Resized to >>>", px, "x", py + + if (x == px) and (y == py): + const.REDUCE_IMAGEDATA_QUALITY = 0 + else: + const.REDUCE_IMAGEDATA_QUALITY = 1 + if not(const.REDUCE_IMAGEDATA_QUALITY): update_progress= vtk_utils.ShowProgress(1, dialog_type = "ProgressDialog") array = vtk.vtkStringArray() for x in xrange(len(filelist)): array.InsertValue(x,filelist[x]) - + reader = vtkgdcm.vtkGDCMImageReader() reader.SetFileNames(array) reader.AddObserver("ProgressEvent", lambda obj,evt: @@ -245,16 +251,14 @@ def CreateImageData(filelist, zspacing, size, bits): spacing = imagedata.GetSpacing() imagedata.SetSpacing(spacing[0], spacing[1], zspacing) else: - + update_progress= vtk_utils.ShowProgress(2*len(filelist), dialog_type = "ProgressDialog") # Reformat each slice and future append them appender = vtk.vtkImageAppend() appender.SetAppendAxis(2) #Define Stack in Z - - x,y = size - p = utils.PredictingMemory(len(filelist), x, y, bits) + # Reformat each slice for x in xrange(len(filelist)): @@ -266,9 +270,9 @@ def CreateImageData(filelist, zspacing, size, bits): reader.AddObserver("ProgressEvent", lambda obj,evt: update_progress(reader,message)) reader.Update() - + #Resample image in x,y dimension - slice_imagedata = ResampleImage2D(reader.GetOutput(), p, update_progress) + slice_imagedata = ResampleImage2D(reader.GetOutput(), px, py, update_progress) #Stack images in Z axes appender.AddInput(slice_imagedata) #appender.AddObserver("ProgressEvent", lambda obj,evt:update_progress(appender)) @@ -284,7 +288,7 @@ def CreateImageData(filelist, zspacing, size, bits): imagedata.AddObserver("ProgressEvent", lambda obj,evt: update_progress(imagedata,message)) imagedata.Update() - + return imagedata @@ -292,25 +296,25 @@ def CreateImageData(filelist, zspacing, size, bits): class ImageCreator: def __init__(self): ps.Publisher().sendMessage("Cancel imagedata load", self.CancelImageDataLoad) - + def CancelImageDataLoad(self, evt_pusub): self.running = evt_pusub.data - + def CreateImageData(filelist, zspacing): message = "Generating multiplanar visualization..." if not(const.REDUCE_IMAGEDATA_QUALITY): update_progress= vtk_utils.ShowProgress(1) - + array = vtk.vtkStringArray() for x in xrange(len(filelist)): array.InsertValue(x,filelist[x]) - + reader = vtkgdcm.vtkGDCMImageReader() reader.SetFileNames(array) reader.AddObserver("ProgressEvent", lambda obj,evt: update_progress(reader,message)) reader.Update() - + # The zpacing is a DicomGroup property, so we need to set it imagedata = vtk.vtkImageData() imagedata.DeepCopy(reader.GetOutput()) @@ -319,11 +323,11 @@ class ImageCreator: else: update_progress= vtk_utils.ShowProgress(2*len(filelist), dialog_type = "ProgressDialog") - + # 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 @@ -334,27 +338,27 @@ class ImageCreator: reader.AddObserver("ProgressEvent", lambda obj,evt: update_progress(reader,message)) reader.Update() - + #Resample image in x,y dimension - + slice_imagedata = ResampleImage2D(reader.GetOutput(), 256, update_progress) - + #Stack images in Z axes appender.AddInput(slice_imagedata) #appender.AddObserver("ProgressEvent", lambda obj,evt:update_progress(appender)) 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.AddObserver("ProgressEvent", lambda obj,evt: update_progress(imagedata,message)) imagedata.Update() - + return imagedata """ diff --git a/invesalius/utils.py b/invesalius/utils.py index d397829..2723352 100755 --- a/invesalius/utils.py +++ b/invesalius/utils.py @@ -16,9 +16,11 @@ # PARTICULAR. Consulte a Licenca Publica Geral GNU para obter mais # detalhes. #-------------------------------------------------------------------------- +import os +import platform import subprocess import re -import os +import sys def debug(error_str): @@ -91,19 +93,73 @@ def frange(start, end=None, inc=None): def PredictingMemory(qtd, x, y, p): m = qtd * (x * y * p) - #314859200 = 350 MB - #house 25 MB increases the - #factor 0.4 - if (m >= 314859200): - porcent = 1.5 + (m - 314859200) / 26999999 * 0.04 - x = x/porcent - y = y/porcent - return x + if (sys.platform == 'win32'): + + physical_memory = GetWindowsInformation()[3] + + if (platform.architecture()[0] == '32bit'): + #(314859200 = 300 MB) + #(26999999 = 25 MB) + + #case occupy more than 300 MB image is reduced to 1.5, + #and 25 MB each image is resized 0.04. + if (m >= 314859200): + porcent = 1.5 + (m - 314859200) / 26999999 * 0.04 + else: + return (x, y) + else: #64 bits architecture + + if (physical_memory <= 2.0) and (qtd <= 1200): + porcent = 1.5 + (m - 314859200) / 26999999 * 0.02 + + elif(physical_memory <= 2.0) and (qtd > 1200): + porcent = 1.5 + (m - 314859200) / 26999999 * 0.03 + + elif(physical_memory > 2.0) and (physical_memory <= 4.0) and (qtd <= 1200): + porcent = 1.5 + (m - 314859200) / 26999999 * 0.01 + + else: + return (x,y) + + return (x/porcent, y/porcent) - else: - return x + elif(sys.platform == 'linux2'): + + physical_memory = GetLinuxInformation()[2] + + if (platform.architecture()[0] == '32bit'): + # 839000000 = 800 MB + if (m <= 839000000) and (physical_memory <= 2.0): + return (x,y) + elif (m > 839000000) and (physical_memory <= 2.0) and (qtd <= 1200): + porcent = 1.5 + (m - 314859200) / 26999999 * 0.02 + else: + return (x,y) + + else: + + if (m <= 839000000) and (physical_memory <= 2.0): + return (x, y) + elif (m > 839000000) and (physical_memory <= 2.0) and (qtd <= 1200): + porcent = 1.5 + (m - 314859200) / 26999999 * 0.02 + else: + return (x,y) + + return (x/porcent, y/porcent) + + elif(sys.platform == 'darwin'): + + physical_memory = GetDarwinInformation() + + if (m <= 839000000) and (physical_memory <= 2.0): + return (x, y) + elif (m > 839000000) and (physical_memory <= 2.0) and (qtd <= 1200): + porcent = 1.5 + (m - 314859200) / 26999999 * 0.02 + else: + return (x, y) + + return (x/porcent,y/porcent) - return x def BytesConvert(bytes): @@ -155,6 +211,10 @@ def GetWindowsInformation(): processor_clock, total_physical_memory, available_physical_memory) +def GetDarwinInformation(): + memory = 2.0 + return (memory) + def GetLinuxInformation(): @@ -169,8 +229,7 @@ def GetLinuxInformation(): #processor_clock = float(re.findall('[0-9]+', processor_clock)[0]) - print architecture - print processor_clock + return (architecture, processor_clock, 2.0) def LinuxCommand(command): -- libgit2 0.21.2