Skip to content

Commit

Permalink
ENH: changed parameter files and addede BRAINSROIInitializer (issue S…
Browse files Browse the repository at this point in the history
  • Loading branch information
che85 committed Jan 30, 2018
1 parent 23d05f0 commit ebc4efd
Show file tree
Hide file tree
Showing 4 changed files with 67 additions and 96 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,20 +11,6 @@
(FixedInternalImagePixelType "float")
(MovingInternalImagePixelType "float")

// The dimensions of the fixed and moving image
// NB: This has to be specified by the user. The dimension of
// the images is currently NOT read from the images.
// Also note that some other settings may have to specified
// for each dimension separately.
(MovingImageDimension 3)

// Specify whether you want to take into account the so-called
// direction cosines of the images. Recommended: true.
// In some cases, the direction cosines of the image are corrupt,
// due to image format conversions for example. In that case, you
// may want to set this option to "false".
(UseDirectionCosines "true")

// **************** Main Components **************************

// The following components should usually be left as they are:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,20 +11,6 @@
(FixedInternalImagePixelType "float")
(MovingInternalImagePixelType "float")

// The dimensions of the fixed and moving image
// NB: This has to be specified by the user. The dimension of
// the images is currently NOT read from the images.
// Also note that some other settings may have to specified
// for each dimension separately.
(MovingImageDimension 3)

// Specify whether you want to take into account the so-called
// direction cosines of the images. Recommended: true.
// In some cases, the direction cosines of the image are corrupt,
// due to image format conversions for example. In that case, you
// may want to set this option to "false".
(UseDirectionCosines "true")

// **************** Main Components **************************

// The following components should usually be left as they are:
Expand Down Expand Up @@ -55,7 +41,7 @@

// Automatically guess an initial translation by aligning the
// geometric centers of the fixed and moving.
(AutomaticTransformInitialization "true")
(AutomaticTransformInitialization "false")

// Whether transforms are combined by composition or by addition.
// In generally, Compose is the best option in most cases.
Expand All @@ -81,7 +67,7 @@
// The number of resolutions. 1 Is only enough if the expected
// deformations are small. 3 or 4 mostly works fine. For large
// images and large deformations, 5 or 6 may even be useful.
(NumberOfResolutions 4)
(NumberOfResolutions 2)

// The downsampling/blurring factors for the image pyramids.
// By default, the images are downsampled by a factor of 2
Expand Down
128 changes: 64 additions & 64 deletions SliceTracker/SliceTrackerUtils/algorithms/registration.py
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ def __runRigidRegistration(self):
self.registrationResult.labels.fixed, self.registrationResult.labels.moving,
self.registrationResult.transforms.rigid, self.registrationResult.volumes.rigid)
reg.run()
self.registrationResult.cmdArguments += reg.params
self.registrationResult.cmdArguments += reg.paramsInfo

def __runAffineRegistration(self):
self.updateProgress(labelText='\nAffine registration', value=2)
Expand All @@ -118,7 +118,7 @@ def __runAffineRegistration(self):
self.registrationResult.transforms.affine, self.registrationResult.volumes.affine,
self.registrationResult.transforms.rigid)
reg.run()
self.registrationResult.cmdArguments += reg.params
self.registrationResult.cmdArguments += reg.paramsInfo

def __runBSplineRegistration(self):
self.updateProgress(labelText='\nBSpline registration', value=3)
Expand All @@ -128,7 +128,7 @@ def __runBSplineRegistration(self):
self.registrationResult.transforms.bSpline, self.registrationResult.volumes.bSpline,
self.registrationResult.transforms.affine)
reg.run()
self.registrationResult.cmdArguments += reg.params
self.registrationResult.cmdArguments += reg.paramsInfo

self.updateProgress(labelText='\nCompleted registration', value=4)

Expand All @@ -137,67 +137,64 @@ class IBRAINSRegistrationType(object):

__metaclass__ = ABCMeta

def __init__(self, fixedVolume, movingVolume, fixedLabel, movingLabel, outputTransform, outputVolume,
def __init__(self, fixedVolume, movingVolume, fixedLabel, movingLabel, outputTransform, outputVolume=None,
initialTransform=None):
self.fixedVolume = fixedVolume
self.movingVolume = movingVolume
self.fixedLabel = fixedLabel
self.movingLabel = movingLabel
self.outputTransform = outputTransform
self.outputVolume = outputVolume
self.initialTransform = initialTransform
self.params = None
self.params = dict(fixedVolume=fixedVolume,
movingVolume=movingVolume,
fixedBinaryVolume=fixedLabel,
movingBinaryVolume=movingLabel,
outputTransform=outputTransform.GetID())
if outputVolume:
self.params['outputVolume'] = outputVolume.GetID()
if initialTransform:
self.params['initialTransform'] = initialTransform

@abstractmethod
def run(self):
pass

def getGeneralParams(self):
return {'fixedVolume': self.fixedVolume,
'movingVolume': self.movingVolume,
'fixedBinaryVolume': self.fixedLabel,
'movingBinaryVolume': self.movingLabel,
'outputTransform': self.outputTransform.GetID(),
'outputVolume': self.outputVolume.GetID()}


class BRAINSRigidRegistration(IBRAINSRegistrationType):

def run(self):
params = {'maskProcessingMode': "ROI",
'initializeTransformMode': "useCenterOfROIAlign",
'useRigid': True}
params.update(self.getGeneralParams())
slicer.cli.run(slicer.modules.brainsfit, None, params, wait_for_completion=True)
self.params = "Rigid Registration Parameters: %s" % str(params) + "\n\n"
self.params.update({'maskProcessingMode': "ROI",
'initializeTransformMode': "useCenterOfROIAlign",
'useRigid': True})
slicer.cli.run(slicer.modules.brainsfit, None, self.params, wait_for_completion=True)
self.paramsInfo = "Rigid Registration Parameters: %s" % str(self.params) + "\n\n"


class BRAINSROIInitializer(IBRAINSRegistrationType):

def run(self):
self.params.update({'maskProcessingMode': "ROI",
'initializeTransformMode': "useCenterOfROIAlign"})
slicer.cli.run(slicer.modules.brainsfit, None, self.params, wait_for_completion=True)
self.paramsInfo = "ROI Initializer Parameters: %s" % str(self.params) + "\n\n"


class BRAINSAffineRegistration(IBRAINSRegistrationType):

def run(self):
params = {'maskProcessingMode': "ROI",
'useAffine': True,
'initialTransform': self.initialTransform}
params.update(self.getGeneralParams())
slicer.cli.run(slicer.modules.brainsfit, None, params, wait_for_completion=True)
self.params = "Affine Registration Parameters: %s" % str(params) + "\n\n"
self.params.update({'maskProcessingMode': "ROI",
'useAffine': True})
slicer.cli.run(slicer.modules.brainsfit, None, self.params, wait_for_completion=True)
self.paramsInfo = "Affine Registration Parameters: %s" % str(self.params) + "\n\n"


class BRAINSBSplineRegistration(IBRAINSRegistrationType):

def run(self):
params = {'useROIBSpline': True,
'useBSpline': True,
'splineGridSize': "3,3,3",
'maskProcessing': "ROI",
'minimumStepLength': "0.005",
'maximumStepLength': "0.2",
'costFunctionConvergenceFactor': "1.00E+09",
'maskProcessingMode': "ROI",
'initialTransform': self.initialTransform}
params.update(self.getGeneralParams())
slicer.cli.run(slicer.modules.brainsfit, None, params, wait_for_completion=True)
self.params = "BSpline Registration Parameters: %s" % str(params) + "\n\n"
self.params.update({'useROIBSpline': True,
'useBSpline': True,
'splineGridSize': "3,3,3",
'maskProcessing': "ROI",
'minimumStepLength': "0.005",
'maximumStepLength': "0.2",
'costFunctionConvergenceFactor': "1.00E+09",
'maskProcessingMode': "ROI"})
slicer.cli.run(slicer.modules.brainsfit, None, self.params, wait_for_completion=True)
self.paramsInfo = "BSpline Registration Parameters: %s" % str(self.params) + "\n\n"


class ElastixRegistration(ImageRegistrationTool):
Expand Down Expand Up @@ -229,49 +226,52 @@ def run(self, parameterNode, result, progressCallback=None):
self.registrationResult = result
self._processParameterNode(parameterNode)

registrationTypes = ['rigid', 'bSpline']
registrationTypes = ['bSpline']
self.createVolumeAndTransformNodes(registrationTypes, prefix=str(result.seriesNumber), suffix=result.suffix,
deformableRegistrationNodeClass=slicer.vtkMRMLTransformNode)

self.__runRigidBRAINSRegistration()
initTransform = self.__runBRAINSROIInitializer()
self.hardenTransform(nodes=[self.registrationResult.volumes.moving, self.registrationResult.labels.moving],
transform=self.registrationResult.transforms.rigid)
transform=initTransform)
self.__runElastixRegistration()

self.hardenTransform(nodes=[self.registrationResult.transforms.bSpline],
transform=self.registrationResult.transforms.rigid)
initTransform.Inverse()
self.hardenTransform(nodes=[self.registrationResult.labels.moving], transform=initTransform)
initTransform.Inverse()
self.hardenTransform(nodes=[initTransform], transform=self.registrationResult.transforms.bSpline)

self.registrationResult.transforms.rigid.Inverse()
self.hardenTransform(nodes=[self.registrationResult.labels.moving],
transform=self.registrationResult.transforms.rigid)
self.registrationResult.transforms.rigid.Inverse()
initTransform.SetName(self.registrationResult.transforms.bSpline.GetName())
slicer.mrmlScene.RemoveNode(self.registrationResult.transforms.bSpline)
self.registrationResult.transforms.bSpline = initTransform

targetsNodeID = parameterNode.GetAttribute('TargetsNodeID')
if targetsNodeID:
result.targets.original = slicer.mrmlScene.GetNodeByID(targetsNodeID)
self.transformTargets(registrationTypes, result.targets.original, str(result.seriesNumber), suffix=result.suffix)

slicer.mrmlScene.RemoveNode(result.volumes.moving)
result.volumes.moving = slicer.mrmlScene.GetNodeByID(parameterNode.GetAttribute('MovingImageNodeID'))

def __runRigidBRAINSRegistration(self):
self.updateProgress(labelText='\nRigid registration', value=2)
def __runBRAINSROIInitializer(self):
self.updateProgress(labelText='\nROI initialization', value=2)

rigidRegistration = BRAINSRigidRegistration(self.registrationResult.volumes.fixed,
self.registrationResult.volumes.moving,
self.registrationResult.labels.fixed,
self.registrationResult.labels.moving,
self.registrationResult.transforms.rigid,
self.registrationResult.volumes.rigid)
transform = self.createLinearTransformNode(self.registrationResult.volumes.moving.GetName()+"_temp_transform")
slicer.mrmlScene.AddNode(transform)
rigidRegistration = BRAINSROIInitializer(self.registrationResult.volumes.fixed,
self.registrationResult.volumes.moving,
self.registrationResult.labels.fixed,
self.registrationResult.labels.moving,
outputTransform=transform)
rigidRegistration.run()

self.registrationResult.cmdArguments += rigidRegistration.params
self.registrationResult.cmdArguments += rigidRegistration.paramsInfo
return transform

def __runElastixRegistration(self):
self.updateProgress(labelText='\nElastix registration', value=3)
from Elastix import ElastixLogic
import os
logic = ElastixLogic()
pFileNames = ["Parameters_BSpline.txt"] # "Parameters_Rigid.txt", "Parameters_Affine.txt"
pFileNames = ["Parameters_Rigid.txt", "Parameters_BSpline.txt"]
parameterFileNames = [os.path.join(self.parametersDirectory, pFile) for pFile in pFileNames]
logic.registerVolumes(self.registrationResult.volumes.fixed, self.registrationResult.volumes.moving,
parameterFileNames, self.registrationResult.volumes.bSpline,
Expand Down
3 changes: 1 addition & 2 deletions SliceTracker/SliceTrackerUtils/session.py
Original file line number Diff line number Diff line change
Expand Up @@ -690,9 +690,8 @@ def applyInitialRegistration(self, retryMode, segmentationData, progressCallback
def applyRegistration(self, progressCallback=None):

coverProstateRegResult = self.data.getMostRecentApprovedCoverProstateRegistration()
lastRigidTfm = self.data.getLastApprovedRigidTransformation()
lastApprovedTfm = self.data.getMostRecentApprovedTransform()
initialTransform = lastApprovedTfm if lastApprovedTfm else lastRigidTfm
initialTransform = lastApprovedTfm if lastApprovedTfm else self.data.getLastApprovedRigidTransformation()

fixedLabel = self.volumesLogic.CreateAndAddLabelVolume(slicer.mrmlScene, self.currentSeriesVolume,
self.currentSeriesVolume.GetName() + '-label')
Expand Down

0 comments on commit ebc4efd

Please sign in to comment.