def CheckForVmtkLibrary(self): try: t = slicer.vtkvmtkFastMarchingUpwindGradientImageFilter() except Exception: d = slicer.vtkKWMessageDialog() d.SetParent(slicer.ApplicationGUI.GetMainSlicerWindow()) d.SetMasterWindow(slicer.ApplicationGUI.GetMainSlicerWindow()) d.SetStyleToMessage() d.SetText("VmtkSlicerModule not found! Please install the VmtkSlicerModule extension to use this module!") d.Create() d.Invoke() return 0 return 1
def ExecuteFastMarching(self,inVolumeNode,lowerThreshold,higherThreshold,sourceSeedsNode,targetSeedsNode): self._helper.debug("Starting execution of Fast Marching...") if not inVolumeNode or not sourceSeedsNode: self._helper.debug(inVolumeNode) self._helper.debug(lowerThreshold) self._helper.debug(higherThreshold) self._helper.debug(sourceSeedsNode) self._helper.debug(targetSeedsNode) slicer.Application.ErrorMessage("Not enough information!!! Aborting Fast Marching..\n") return else: sourceSeedIds = slicer.vtkIdList() targetSeedIds = slicer.vtkIdList() image = inVolumeNode.GetImageData() cast = slicer.vtkImageCast() cast.SetInput(image) cast.SetOutputScalarTypeToFloat() cast.Update() image = cast.GetOutput() for i in range(sourceSeedsNode.GetNumberOfFiducials()): rasPt = sourceSeedsNode.GetNthFiducialXYZ(i) ijkPt = self._helper.ConvertRAS2IJK(rasPt) sourceSeedIds.InsertNextId(image.ComputePointId(int(ijkPt[0]),int(ijkPt[1]),int(ijkPt[2]))) if targetSeedsNode: for i in range(targetSeedsNode.GetNumberOfFiducials()): rasPt = targetSeedsNode.GetNthFiducialXYZ(i) ijkPt = self._helper.ConvertRAS2IJK(rasPt) targetSeedIds.InsertNextId(image.ComputePointId(int(ijkPt[0]),int(ijkPt[1]),int(ijkPt[2]))) scalarRange = image.GetScalarRange() threshold = slicer.vtkImageThreshold() threshold.SetInput(image) threshold.ThresholdBetween(lowerThreshold,higherThreshold) threshold.ReplaceInOff() threshold.ReplaceOutOn() threshold.SetOutValue(scalarRange[0] - scalarRange[1]) threshold.Update() scalarRange = threshold.GetOutput().GetScalarRange() thresholdedImage = threshold.GetOutput() shiftScale = slicer.vtkImageShiftScale() shiftScale.SetInput(thresholdedImage) shiftScale.SetShift(-scalarRange[0]) shiftScale.SetScale(1/(scalarRange[1]-scalarRange[0])) shiftScale.Update() speedImage = shiftScale.GetOutput() fastMarching = slicer.vtkvmtkFastMarchingUpwindGradientImageFilter() fastMarching.SetInput(speedImage) fastMarching.SetSeeds(sourceSeedIds) fastMarching.GenerateGradientImageOff() fastMarching.SetTargetOffset(100.0) fastMarching.SetTargets(targetSeedIds) if targetSeedIds.GetNumberOfIds() > 0: fastMarching.SetTargetReachedModeToOneTarget() else: fastMarching.SetTargetReachedModeToNoTargets() fastMarching.Update() if targetSeedIds.GetNumberOfIds() > 0: subtract = slicer.vtkImageMathematics() subtract.SetInput(fastMarching.GetOutput()) subtract.SetOperationToAddConstant() subtract.SetConstantC(-fastMarching.GetTargetValue()) subtract.Update() else: subtract = slicer.vtkImageThreshold() subtract.SetInput(fastMarching.GetOutput()) subtract.ThresholdByLower(1000)#better value soon subtract.ReplaceInOff() subtract.ReplaceOutOn() subtract.SetOutValue(-1) subtract.Update() matrix = slicer.vtkMatrix4x4() inVolumeNode.GetIJKToRASMatrix(matrix) outVolumeData = slicer.vtkImageData() outVolumeData.DeepCopy(subtract.GetOutput()) outVolumeData.Update() # volume calculated... outVolumeNode = slicer.vtkMRMLScalarVolumeNode() outVolumeNode.SetAndObserveImageData(outVolumeData) outVolumeNode.SetIJKToRASMatrix(matrix) outputContainer = SlicerVMTKLevelSetContainer(outVolumeNode,0.0) self._helper.debug("Fast Marching done...") return outputContainer
def ExecuteFM(self, image, lowerThreshold, higherThreshold, sourceSeedIds, targetSeedIds, sideBranches): self._parentClass.GetHelper().debug("Starting FM..") cast = slicer.vtkImageCast() cast.SetInput(image) cast.SetOutputScalarTypeToFloat() cast.Update() image = cast.GetOutput() scalarRange = image.GetScalarRange() imageDimensions = image.GetDimensions() maxImageDimensions = max(imageDimensions) threshold = slicer.vtkImageThreshold() threshold.SetInput(image) threshold.ThresholdBetween(lowerThreshold, higherThreshold) threshold.ReplaceInOff() threshold.ReplaceOutOn() threshold.SetOutValue(scalarRange[0] - scalarRange[1]) threshold.Update() thresholdedImage = threshold.GetOutput() scalarRange = thresholdedImage.GetScalarRange() shiftScale = slicer.vtkImageShiftScale() shiftScale.SetInput(thresholdedImage) shiftScale.SetShift(-scalarRange[0]) shiftScale.SetScale(1 / (scalarRange[1] - scalarRange[0])) shiftScale.Update() speedImage = shiftScale.GetOutput() if sideBranches: # ignore sidebranches, use colliding fronts self._parentClass.GetHelper().debug("COLLIDINGFRONTS") fastMarching = slicer.vtkvmtkCollidingFrontsImageFilter() fastMarching.SetInput(speedImage) fastMarching.SetSeeds1(sourceSeedIds) fastMarching.SetSeeds2(targetSeedIds) fastMarching.ApplyConnectivityOn() fastMarching.StopOnTargetsOn() fastMarching.Update() subtract = slicer.vtkImageMathematics() subtract.SetInput(fastMarching.GetOutput()) subtract.SetOperationToAddConstant() subtract.SetConstantC(-10 * fastMarching.GetNegativeEpsilon()) subtract.Update() else: fastMarching = slicer.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 = slicer.vtkImageMathematics() subtract.SetInput(fastMarching.GetOutput()) subtract.SetOperationToAddConstant() subtract.SetConstantC(-fastMarching.GetTargetValue()) subtract.Update() else: #self._parentClass.GetHelper().debug("No target mode "+str(fastMarching.GetTargetValue())) subtract = slicer.vtkImageThreshold() subtract.SetInput(fastMarching.GetOutput()) subtract.ThresholdByLower(2000) # TODO find robuste value subtract.ReplaceInOff() subtract.ReplaceOutOn() subtract.SetOutValue(-1) subtract.Update() outVolumeData = slicer.vtkImageData() outVolumeData.DeepCopy(subtract.GetOutput()) outVolumeData.Update() self._parentClass.GetHelper().debug("End of FM..") return outVolumeData
def ExecuteFM(self,image,lowerThreshold,higherThreshold,sourceSeedIds,targetSeedIds,sideBranches): self._parentClass.GetHelper().debug("Starting FM..") cast = slicer.vtkImageCast() cast.SetInput(image) cast.SetOutputScalarTypeToFloat() cast.Update() image = cast.GetOutput() scalarRange = image.GetScalarRange() imageDimensions = image.GetDimensions() maxImageDimensions = max(imageDimensions) threshold = slicer.vtkImageThreshold() threshold.SetInput(image) threshold.ThresholdBetween(lowerThreshold,higherThreshold) threshold.ReplaceInOff() threshold.ReplaceOutOn() threshold.SetOutValue(scalarRange[0] - scalarRange[1]) threshold.Update() thresholdedImage = threshold.GetOutput() scalarRange = thresholdedImage.GetScalarRange() shiftScale = slicer.vtkImageShiftScale() shiftScale.SetInput(thresholdedImage) shiftScale.SetShift(-scalarRange[0]) shiftScale.SetScale(1/(scalarRange[1]-scalarRange[0])) shiftScale.Update() speedImage = shiftScale.GetOutput() if sideBranches: # ignore sidebranches, use colliding fronts self._parentClass.GetHelper().debug("COLLIDINGFRONTS") fastMarching = slicer.vtkvmtkCollidingFrontsImageFilter() fastMarching.SetInput(speedImage) fastMarching.SetSeeds1(sourceSeedIds) fastMarching.SetSeeds2(targetSeedIds) fastMarching.ApplyConnectivityOn() fastMarching.StopOnTargetsOn() fastMarching.Update() subtract = slicer.vtkImageMathematics() subtract.SetInput(fastMarching.GetOutput()) subtract.SetOperationToAddConstant() subtract.SetConstantC(-10*fastMarching.GetNegativeEpsilon()) subtract.Update() else: fastMarching = slicer.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 = slicer.vtkImageMathematics() subtract.SetInput(fastMarching.GetOutput()) subtract.SetOperationToAddConstant() subtract.SetConstantC(-fastMarching.GetTargetValue()) subtract.Update() else: #self._parentClass.GetHelper().debug("No target mode "+str(fastMarching.GetTargetValue())) subtract = slicer.vtkImageThreshold() subtract.SetInput(fastMarching.GetOutput()) subtract.ThresholdByLower(2000) # TODO find robuste value subtract.ReplaceInOff() subtract.ReplaceOutOn() subtract.SetOutValue(-1) subtract.Update() outVolumeData = slicer.vtkImageData() outVolumeData.DeepCopy(subtract.GetOutput()) outVolumeData.Update() self._parentClass.GetHelper().debug("End of FM..") return outVolumeData
def ExecuteFastMarching(self, inVolumeNode, lowerThreshold, higherThreshold, sourceSeedsNode, targetSeedsNode): self._helper.debug("Starting execution of Fast Marching...") if not inVolumeNode or not sourceSeedsNode: self._helper.debug(inVolumeNode) self._helper.debug(lowerThreshold) self._helper.debug(higherThreshold) self._helper.debug(sourceSeedsNode) self._helper.debug(targetSeedsNode) slicer.Application.ErrorMessage( "Not enough information!!! Aborting Fast Marching..\n") return else: sourceSeedIds = slicer.vtkIdList() targetSeedIds = slicer.vtkIdList() image = inVolumeNode.GetImageData() cast = slicer.vtkImageCast() cast.SetInput(image) cast.SetOutputScalarTypeToFloat() cast.Update() image = cast.GetOutput() for i in range(sourceSeedsNode.GetNumberOfFiducials()): rasPt = sourceSeedsNode.GetNthFiducialXYZ(i) ijkPt = self._helper.ConvertRAS2IJK(rasPt) sourceSeedIds.InsertNextId( image.ComputePointId(int(ijkPt[0]), int(ijkPt[1]), int(ijkPt[2]))) if targetSeedsNode: for i in range(targetSeedsNode.GetNumberOfFiducials()): rasPt = targetSeedsNode.GetNthFiducialXYZ(i) ijkPt = self._helper.ConvertRAS2IJK(rasPt) targetSeedIds.InsertNextId( image.ComputePointId(int(ijkPt[0]), int(ijkPt[1]), int(ijkPt[2]))) scalarRange = image.GetScalarRange() threshold = slicer.vtkImageThreshold() threshold.SetInput(image) threshold.ThresholdBetween(lowerThreshold, higherThreshold) threshold.ReplaceInOff() threshold.ReplaceOutOn() threshold.SetOutValue(scalarRange[0] - scalarRange[1]) threshold.Update() scalarRange = threshold.GetOutput().GetScalarRange() thresholdedImage = threshold.GetOutput() shiftScale = slicer.vtkImageShiftScale() shiftScale.SetInput(thresholdedImage) shiftScale.SetShift(-scalarRange[0]) shiftScale.SetScale(1 / (scalarRange[1] - scalarRange[0])) shiftScale.Update() speedImage = shiftScale.GetOutput() fastMarching = slicer.vtkvmtkFastMarchingUpwindGradientImageFilter( ) fastMarching.SetInput(speedImage) fastMarching.SetSeeds(sourceSeedIds) fastMarching.GenerateGradientImageOff() fastMarching.SetTargetOffset(100.0) fastMarching.SetTargets(targetSeedIds) if targetSeedIds.GetNumberOfIds() > 0: fastMarching.SetTargetReachedModeToOneTarget() else: fastMarching.SetTargetReachedModeToNoTargets() fastMarching.Update() if targetSeedIds.GetNumberOfIds() > 0: subtract = slicer.vtkImageMathematics() subtract.SetInput(fastMarching.GetOutput()) subtract.SetOperationToAddConstant() subtract.SetConstantC(-fastMarching.GetTargetValue()) subtract.Update() else: subtract = slicer.vtkImageThreshold() subtract.SetInput(fastMarching.GetOutput()) subtract.ThresholdByLower(1000) #better value soon subtract.ReplaceInOff() subtract.ReplaceOutOn() subtract.SetOutValue(-1) subtract.Update() matrix = slicer.vtkMatrix4x4() inVolumeNode.GetIJKToRASMatrix(matrix) outVolumeData = slicer.vtkImageData() outVolumeData.DeepCopy(subtract.GetOutput()) outVolumeData.Update() # volume calculated... outVolumeNode = slicer.vtkMRMLScalarVolumeNode() outVolumeNode.SetAndObserveImageData(outVolumeData) outVolumeNode.SetIJKToRASMatrix(matrix) outputContainer = SlicerVMTKLevelSetContainer(outVolumeNode, 0.0) self._helper.debug("Fast Marching done...") return outputContainer