def load(self,loadable): """Load the selection as a scalar volume, but rescale the values """ scalarVolumePlugin = slicer.modules.dicomPlugins['DICOMScalarVolumePlugin']() vNode = scalarVolumePlugin.loadFilesWithArchetype(loadable.files, loadable.name) if vNode: intercept = float(slicer.dicomDatabase.fileValue(loadable.files[0], self.tags['ScaleIntercept'])) slope = float(slicer.dicomDatabase.fileValue(loadable.files[0], self.tags['ScaleSlope'])) privateIntercept = float(slicer.dicomDatabase.fileValue(loadable.files[0], self.tags['PrivateScaleIntercept'])) privateSlope = float(slicer.dicomDatabase.fileValue(loadable.files[0], self.tags['PrivateScaleSlope'])) print('Slope: '+str(slope)+' private: '+str(privateSlope)+' Intercept: '+str(intercept)+' private: '+str(privateIntercept)) # vtkImageShiftScale first shifts, then scales # First, revert the scaling applied on ITK-level read reverseShift = vtk.vtkImageShiftScale() reverseShift.SetShift(-1.*intercept) reverseShift.SetScale(1) reverseShift.SetInput(vNode.GetImageData()) reverseShift.SetOutputScalarTypeToFloat() reverseScale = vtk.vtkImageShiftScale() reverseScale.SetShift(0) reverseScale.SetScale(1./slope) reverseScale.SetInput(reverseShift.GetOutput()) reverseScale.SetOutputScalarTypeToFloat() # Second, apply scaling using the private tags information rescale = vtk.vtkImageShiftScale() rescale.SetShift(-1.*privateIntercept) rescale.SetScale(1./privateSlope) rescale.SetOutputScalarTypeToFloat() rescale.SetInput(reverseScale.GetOutput()) rescale.Update() imageData = vtk.vtkImageData() imageData.DeepCopy(rescale.GetOutput()) # Note: the assumption here is that intercept/slope are identical for all # slices in the series. According to Tom Chenevert, this is typically the # case: "The exception is when there are multiple image types in a series, # such as real, imaginary, magnitude and phase images all stored in the # series. But this is not common." vNode.SetAndObserveImageData(imageData) return vNode
def load(self,loadable): """Load the selection as a scalar volume, but rescale the values """ scalarVolumePlugin = slicer.modules.dicomPlugins['DICOMScalarVolumePlugin']() vNode = scalarVolumePlugin.loadFilesWithArchetype(loadable.files, loadable.name) if vNode: intercept = float(slicer.dicomDatabase.fileValue(loadable.files[0], self.tags['ScaleIntercept'])) slope = float(slicer.dicomDatabase.fileValue(loadable.files[0], self.tags['ScaleSlope'])) privateIntercept = float(slicer.dicomDatabase.fileValue(loadable.files[0], self.tags['PrivateScaleIntercept'])) privateSlope = float(slicer.dicomDatabase.fileValue(loadable.files[0], self.tags['PrivateScaleSlope'])) print('Slope: '+str(slope)+' private: '+str(privateSlope)+' Intercept: '+str(intercept)+' private: '+str(privateIntercept)) # vtkImageShiftScale first shifts, then scales # First, revert the scaling applied on ITK-level read reverseShift = vtk.vtkImageShiftScale() reverseShift.SetShift(-1.*intercept) reverseShift.SetScale(1) reverseShift.SetInputData(vNode.GetImageData()) reverseShift.SetOutputScalarTypeToFloat() reverseScale = vtk.vtkImageShiftScale() reverseScale.SetShift(0) reverseScale.SetScale(1./slope) reverseScale.SetInputData(reverseShift.GetOutput()) reverseScale.SetOutputScalarTypeToFloat() # Second, apply scaling using the private tags information rescale = vtk.vtkImageShiftScale() rescale.SetShift(-1.*privateIntercept) rescale.SetScale(1./privateSlope) rescale.SetOutputScalarTypeToFloat() rescale.SetInputData(reverseScale.GetOutput()) rescale.Update() imageData = vtk.vtkImageData() imageData.DeepCopy(rescale.GetOutput()) # Note: the assumption here is that intercept/slope are identical for all # slices in the series. According to Tom Chenevert, this is typically the # case: "The exception is when there are multiple image types in a series, # such as real, imaginary, magnitude and phase images all stored in the # series. But this is not common." vNode.SetAndObserveImageData(imageData) return vNode
def run(self,inputVolumeNode,outputVolumeNode,outputScalarType): """ Run the actual algorithm """ castor = vtk.vtkImageShiftScale() castor.SetInput(inputVolumeNode.GetImageData()) castor.SetShift(0.0) castor.SetScale(1.0) if outputScalarType == "Char": castor.SetOutputScalarTypeToChar() elif outputScalarType == "UnsignedChar": castor.SetOutputScalarTypeToUnsignedChar() elif outputScalarType == "Short": castor.SetOutputScalarTypeToShort() elif outputScalarType == "UnsignedShort": castor.SetOutputScalarTypeToUnsignedShort() elif outputScalarType == "Int": castor.SetOutputScalarTypeToInt() elif outputScalarType == "UnsignedInt": castor.SetOutputScalarTypeToUnsignedInt() elif outputScalarType == "Float": castor.SetOutputScalarTypeToFloat() elif outputScalarType == "Double": castor.SetOutputScalarTypeToDouble() else: pass castor.Update() outputVolumeNode.CopyOrientation(inputVolumeNode) outputVolumeNode.SetAndObserveImageData(Castor.GetOutput()) return True
def __init__(self, shaderComputation, textureUnit, volumeNode): FieldSampler.__init__(self, shaderComputation, textureUnit, volumeNode) self.shiftScale = vtk.vtkImageShiftScale() try: from vtkSlicerShadedActorModuleLogicPython import vtkOpenGLTextureImage except ImportError: import vtkAddon vtkOpenGLTextureImage=vtkAddon.vtkOpenGLTextureImage self.textureImage=vtkOpenGLTextureImage() self.textureImage.SetShaderComputation(self.shaderComputation) self.updateFromMRML()
def __init__(self, shaderComputation, textureUnit, volumeNode): FieldSampler.__init__(self, shaderComputation, textureUnit, volumeNode) self.shiftScale = vtk.vtkImageShiftScale() try: from vtkSlicerShadedActorModuleLogicPython import vtkOpenGLTextureImage except ImportError: import vtkAddon vtkOpenGLTextureImage = vtkAddon.vtkOpenGLTextureImage self.textureImage = vtkOpenGLTextureImage() self.textureImage.SetShaderComputation(self.shaderComputation) self.updateFromMRML()
def fastMarching(self, percentMax): self.fm = None # allocate a new filter each time March is hit bgImage = self.editUtil.getBackgroundImage() labelImage = self.editUtil.getLabelImage() # collect seeds dim = bgImage.GetWholeExtent() # initialize the filter self.fm = slicer.vtkPichonFastMarching() scalarRange = bgImage.GetScalarRange() depth = scalarRange[1] - scalarRange[0] # this is more or less arbitrary; large depth values will bring the # algorithm to the knees scaleValue = 0 shiftValue = 0 if depth > 300: scaleValue = 300. / depth if scalarRange[0] < 0: shiftValue = scalarRange[0] * -1 if scaleValue or shiftValue: rescale = vtk.vtkImageShiftScale() rescale.SetInput(bgImage) rescale.SetScale(scaleValue) rescale.SetShift(shiftValue) rescale.Update() bgImage = rescale.GetOutput() scalarRange = bgImage.GetScalarRange() depth = scalarRange[1] - scalarRange[0] print('Input scalar range: ' + str(depth)) self.fm.init(dim[1] + 1, dim[3] + 1, dim[5] + 1, depth, 1, 1, 1) caster = vtk.vtkImageCast() caster.SetOutputScalarTypeToShort() caster.SetInput(bgImage) caster.Update() self.fm.SetInput(caster.GetOutput()) # self.fm.SetOutput(labelImage) npoints = int( (dim[1] + 1) * (dim[3] + 1) * (dim[5] + 1) * percentMax / 100.) self.fm.setNPointsEvolution(npoints) print('Setting active label to ' + str(self.editUtil.getLabel())) self.fm.setActiveLabel(self.editUtil.getLabel()) nSeeds = self.fm.addSeedsFromImage(labelImage) if nSeeds == 0: return 0 self.fm.Modified() self.fm.Update() # TODO: need to call show() twice for data to be updated self.fm.show(1) self.fm.Modified() self.fm.Update() self.fm.show(1) self.fm.Modified() self.fm.Update() self.undoRedo.saveState() self.editUtil.getLabelImage().DeepCopy(self.fm.GetOutput()) self.editUtil.markVolumeNodeAsModified( self.sliceLogic.GetLabelLayer().GetVolumeNode()) # print('FastMarching output image: '+str(output)) print('FastMarching march update completed') return npoints
def fastMarching(self,percentMax): self.fm = None # allocate a new filter each time March is hit bgImage = EditUtil.getBackgroundImage() labelImage = EditUtil.getLabelImage() # collect seeds if vtk.VTK_MAJOR_VERSION <= 5: dim = bgImage.GetWholeExtent() else: dim = bgImage.GetDimensions() print dim # initialize the filter self.fm = slicer.vtkPichonFastMarching() scalarRange = bgImage.GetScalarRange() depth = scalarRange[1]-scalarRange[0] # this is more or less arbitrary; large depth values will bring the # algorithm to the knees scaleValue = 0 shiftValue = 0 if depth>300: scaleValue = 300./depth if scalarRange[0] < 0: shiftValue = scalarRange[0]*-1 if scaleValue or shiftValue: rescale = vtk.vtkImageShiftScale() if vtk.VTK_MAJOR_VERSION <= 5: rescale.SetInput(bgImage) else: rescale.SetInputData(bgImage) rescale.SetScale(scaleValue) rescale.SetShift(shiftValue) rescale.Update() bgImage = rescale.GetOutput() scalarRange = bgImage.GetScalarRange() depth = scalarRange[1]-scalarRange[0] print('Input scalar range: '+str(depth)) if vtk.VTK_MAJOR_VERSION <= 5: self.fm.init(dim[1]+1, dim[3]+1, dim[5]+1, depth, 1, 1, 1) else: self.fm.init(dim[0], dim[1], dim[2], depth, 1, 1, 1) caster = vtk.vtkImageCast() caster.SetOutputScalarTypeToShort() if vtk.VTK_MAJOR_VERSION <= 5: caster.SetInput(bgImage) caster.Update() self.fm.SetInput(caster.GetOutput()) else: caster.SetInputData(bgImage) self.fm.SetInputConnection(caster.GetOutputPort()) # self.fm.SetOutput(labelImage) if vtk.VTK_MAJOR_VERSION <= 5: npoints = int((dim[1]+1)*(dim[3]+1)*(dim[5]+1)*percentMax/100.) else: npoints = int(dim[0]*dim[1]*dim[2]*percentMax/100.) self.fm.setNPointsEvolution(npoints) print('Setting active label to '+str(EditUtil.getLabel())) self.fm.setActiveLabel(EditUtil.getLabel()) nSeeds = self.fm.addSeedsFromImage(labelImage) if nSeeds == 0: return 0 self.fm.Modified() self.fm.Update() # TODO: need to call show() twice for data to be updated self.fm.show(1) self.fm.Modified() self.fm.Update() self.fm.show(1) self.fm.Modified() self.fm.Update() self.undoRedo.saveState() EditUtil.getLabelImage().DeepCopy(self.fm.GetOutput()) EditUtil.markVolumeNodeAsModified(self.sliceLogic.GetLabelLayer().GetVolumeNode()) # print('FastMarching output image: '+str(output)) print('FastMarching march update completed') return npoints
def performInitialization( self, image, lowerThreshold, upperThreshold, sourceSeedIds, targetSeedIds, ignoreSideBranches=0 ): ''' ''' # import the vmtk libraries try: #from libvtkvmtkSegmentationPython import * import libvtkvmtkSegmentationPython as s except ImportError: print "FAILURE: Unable to import the SlicerVmtk libraries!" cast = vtk.vtkImageCast() cast.SetInput( image ) cast.SetOutputScalarTypeToFloat() cast.Update() image = cast.GetOutput() scalarRange = image.GetScalarRange() imageDimensions = image.GetDimensions() maxImageDimensions = max( imageDimensions ) threshold = vtk.vtkImageThreshold() threshold.SetInput( image ) threshold.ThresholdBetween( lowerThreshold, upperThreshold ) threshold.ReplaceInOff() threshold.ReplaceOutOn() threshold.SetOutValue( scalarRange[0] - scalarRange[1] ) threshold.Update() thresholdedImage = threshold.GetOutput() scalarRange = thresholdedImage.GetScalarRange() shiftScale = vtk.vtkImageShiftScale() shiftScale.SetInput( thresholdedImage ) shiftScale.SetShift( -scalarRange[0] ) shiftScale.SetScale( 1.0 / ( scalarRange[1] - scalarRange[0] ) ) shiftScale.Update() speedImage = shiftScale.GetOutput() if ignoreSideBranches: # ignore sidebranches, use colliding fronts fastMarching = s.vtkvmtkCollidingFrontsImageFilter() fastMarching.SetInput( speedImage ) fastMarching.SetSeeds1( sourceSeedIds ) fastMarching.SetSeeds2( targetSeedIds ) fastMarching.ApplyConnectivityOn() fastMarching.StopOnTargetsOn() fastMarching.Update() subtract = vtk.vtkImageMathematics() subtract.SetInput( fastMarching.GetOutput() ) subtract.SetOperationToAddConstant() subtract.SetConstantC( -10 * fastMarching.GetNegativeEpsilon() ) subtract.Update() else: fastMarching = s.vtkvmtkFastMarchingUpwindGradientImageFilter() fastMarching.SetInput( speedImage ) fastMarching.SetSeeds( sourceSeedIds ) fastMarching.GenerateGradientImageOn() fastMarching.SetTargetOffset( 0.0 ) fastMarching.SetTargets( targetSeedIds ) if targetSeedIds.GetNumberOfIds() > 0: fastMarching.SetTargetReachedModeToOneTarget() else: fastMarching.SetTargetReachedModeToNoTargets() fastMarching.Update() if targetSeedIds.GetNumberOfIds() > 0: subtract = vtk.vtkImageMathematics() subtract.SetInput( fastMarching.GetOutput() ) subtract.SetOperationToAddConstant() subtract.SetConstantC( -fastMarching.GetTargetValue() ) subtract.Update() else: subtract = vtk.vtkImageThreshold() subtract.SetInput( fastMarching.GetOutput() ) subtract.ThresholdByLower( 2000 ) # TODO find robuste value subtract.ReplaceInOff() subtract.ReplaceOutOn() subtract.SetOutValue( -1 ) subtract.Update() outImageData = vtk.vtkImageData() outImageData.DeepCopy( subtract.GetOutput() ) outImageData.Update() return outImageData
def performInitialization(self, image, lowerThreshold, upperThreshold, sourceSeedIds, targetSeedIds, ignoreSideBranches=0): ''' ''' # import the vmtk libraries try: import vtkvmtkSegmentationPython as vtkvmtkSegmentation except ImportError: logging.error("Unable to import the SlicerVmtk libraries") cast = vtk.vtkImageCast() cast.SetInputData(image) cast.SetOutputScalarTypeToFloat() cast.Update() image = cast.GetOutput() scalarRange = image.GetScalarRange() imageDimensions = image.GetDimensions() maxImageDimensions = max(imageDimensions) threshold = vtk.vtkImageThreshold() threshold.SetInputData(image) threshold.ThresholdBetween(lowerThreshold, upperThreshold) threshold.ReplaceInOff() threshold.ReplaceOutOn() threshold.SetOutValue(scalarRange[0] - scalarRange[1]) threshold.Update() thresholdedImage = threshold.GetOutput() scalarRange = thresholdedImage.GetScalarRange() shiftScale = vtk.vtkImageShiftScale() shiftScale.SetInputData(thresholdedImage) shiftScale.SetShift(-scalarRange[0]) shiftScale.SetScale(1.0 / (scalarRange[1] - scalarRange[0])) shiftScale.Update() speedImage = shiftScale.GetOutput() if ignoreSideBranches: # ignore sidebranches, use colliding fronts fastMarching = vtkvmtkSegmentation.vtkvmtkCollidingFrontsImageFilter( ) fastMarching.SetInputData(speedImage) fastMarching.SetSeeds1(sourceSeedIds) fastMarching.SetSeeds2(targetSeedIds) fastMarching.ApplyConnectivityOn() fastMarching.StopOnTargetsOn() fastMarching.Update() subtract = vtk.vtkImageMathematics() subtract.SetInputData(fastMarching.GetOutput()) subtract.SetOperationToAddConstant() subtract.SetConstantC(-10 * fastMarching.GetNegativeEpsilon()) subtract.Update() else: fastMarching = vtkvmtkSegmentation.vtkvmtkFastMarchingUpwindGradientImageFilter( ) fastMarching.SetInputData(speedImage) fastMarching.SetSeeds(sourceSeedIds) fastMarching.GenerateGradientImageOn() fastMarching.SetTargetOffset(0.0) fastMarching.SetTargets(targetSeedIds) if targetSeedIds.GetNumberOfIds() > 0: fastMarching.SetTargetReachedModeToOneTarget() else: fastMarching.SetTargetReachedModeToNoTargets() fastMarching.Update() if targetSeedIds.GetNumberOfIds() > 0: subtract = vtk.vtkImageMathematics() subtract.SetInputData(fastMarching.GetOutput()) subtract.SetOperationToAddConstant() subtract.SetConstantC(-fastMarching.GetTargetValue()) subtract.Update() else: subtract = vtk.vtkImageThreshold() subtract.SetInputData(fastMarching.GetOutput()) subtract.ThresholdByLower(2000) # TODO find robuste value subtract.ReplaceInOff() subtract.ReplaceOutOn() subtract.SetOutValue(-1) subtract.Update() outImageData = vtk.vtkImageData() outImageData.DeepCopy(subtract.GetOutput()) return outImageData