Skip to content

Commit

Permalink
ENH: improved handling of labels with different orientation
Browse files Browse the repository at this point in the history
* geometry checks now include direction matrix consistency check
* conversion script now includes resampling pre-processing step
   to enforce consistent ordering of the pixel data between the image
   and label volumes
  • Loading branch information
fedorov committed Jul 2, 2014
1 parent 8292401 commit 81131a8
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 16 deletions.
17 changes: 13 additions & 4 deletions Py/SlicerReportingModuleWidgetHelper.py
Original file line number Diff line number Diff line change
Expand Up @@ -477,12 +477,21 @@ def GeometriesMatch(vol1, vol2):
dim1 = image1.GetDimensions()
dim2 = image2.GetDimensions()

print dim1,' ',dim2
if dim1[0] != dim2[0] or dim1[1] != dim2[1] or dim1[2] != dim2[2]:
return False

if dim1[0] == dim2[0] and dim1[1] == dim2[1] and dim1[2] == dim2[2]:
return True
# check orientation
dir1 = vtk.vtkMatrix4x4()
dir2 = vtk.vtkMatrix4x4()
vol1.GetIJKToRASDirectionMatrix(dir1)
vol2.GetIJKToRASDirectionMatrix(dir2)

return False
for i in range(4):
for j in range(4):
if dir1.GetElement(i,j) != dir2.GetElement(i,j):
return False

return True

@staticmethod
def getEditorParameterNode():
Expand Down
38 changes: 26 additions & 12 deletions Util/LabelToDICOMSEGConverterScript.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,14 +44,15 @@

from __main__ import slicer, vtk, ctk
import sys, glob, shutil, qt
# import args
import SimpleITK as sitk

from DICOMLib import DICOMPlugin
from DICOMLib import DICOMLoadable

def DoIt(inputDir, labelFile, outputDir, forceLabel):

dbDir1 = slicer.app.temporaryPath+'/LabelConverter'
reportingLogic = slicer.modules.reporting.logic()

print('Temporary directory location: '+dbDir1)
qt.QDir().mkpath(dbDir1)
Expand Down Expand Up @@ -88,6 +89,7 @@ def DoIt(inputDir, labelFile, outputDir, forceLabel):
exit()

inputVolume = scalarVolumePlugin.load(loadables[0])
slicer.mrmlScene.AddNode(inputVolume)
print('Input volume loaded!')

# read the label volume
Expand All @@ -110,25 +112,37 @@ def DoIt(inputDir, labelFile, outputDir, forceLabel):
thresh.Update()
labelImage = thresh.GetOutput()
labelVolume.SetAndObserveImageData(labelImage)

reportingLogic = slicer.modules.reporting.logic()

slicer.mrmlScene.AddNode(labelVolume)

# resample label to the input volume raster
resampledLabel = slicer.vtkMRMLScalarVolumeNode()
slicer.mrmlScene.AddNode(resampledLabel)
eye = slicer.vtkMRMLLinearTransformNode()
slicer.mrmlScene.AddNode(eye)
parameters = {}
parameters['inputVolume'] = labelVolume.GetID()
parameters['referenceVolume'] = inputVolume.GetID()
parameters['outputVolume'] = resampledLabel.GetID()
parameters['warpTransform'] = eye.GetID()
parameters['interpolationMode'] = 'NearestNeighbor'
parameters['pixelType'] = 'ushort'
cliNode = None
cliNode = slicer.cli.run(slicer.modules.brainsresample, None, parameters, 1)
labelVolume = resampledLabel

displayNode = slicer.vtkMRMLLabelMapVolumeDisplayNode()
displayNode.SetAndObserveColorNodeID(reportingLogic.GetDefaultColorNode().GetID())
slicer.mrmlScene.AddNode(displayNode)
slicer.mrmlScene.AddNode(labelVolume)

labelVolume.SetAttribute('AssociatedNodeID',inputVolume.GetID())
labelVolume.LabelMapOn()
labelVolume.SetAndObserveDisplayNodeID(displayNode.GetID())
print('Display node set up, ID = '+str(displayNode.GetID()))

# save as DICOM SEG

# initialize the DICOM DB for Reporting logic, save as DICOM SEG
labelCollection = vtk.vtkCollection()
labelCollection.AddItem(labelVolume)

slicer.mrmlScene.AddNode(inputVolume)
labelVolume.SetAttribute('AssociatedNodeID',inputVolume.GetID())

# initialize the DICOM DB for Reporting logic

print('About to write DICOM SEG!')
dbFileName = slicer.dicomDatabase.databaseFilename
reportingLogic.InitializeDICOMDatabase(dbFileName)
Expand Down

0 comments on commit 81131a8

Please sign in to comment.