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 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 fastMarching(self, percentMax): self.fm = None # Get master volume image data import vtkSegmentationCorePython as vtkSegmentationCore masterImageData = self.scriptedEffect.masterVolumeImageData() # Get segmentation segmentationNode = self.scriptedEffect.parameterSetNode( ).GetSegmentationNode() # Cast master image if not short if masterImageData.GetScalarType() != vtk.VTK_SHORT: imageCast = vtk.vtkImageCast() imageCast.SetInputData(masterImageData) imageCast.SetOutputScalarTypeToShort() imageCast.ClampOverflowOn() imageCast.Update() masterImageDataShort = vtkSegmentationCore.vtkOrientedImageData() masterImageDataShort.ShallowCopy( imageCast.GetOutput()) # Copy image data masterImageDataShort.CopyDirections( masterImageData) # Copy geometry masterImageData = masterImageDataShort selectedSegmentLabelmap = self.scriptedEffect.selectedSegmentLabelmap() if not self.originalSelectedSegmentLabelmap: self.originalSelectedSegmentLabelmap = vtkSegmentationCore.vtkOrientedImageData( ) self.originalSelectedSegmentLabelmap.DeepCopy( selectedSegmentLabelmap) self.selectedSegmentId = self.scriptedEffect.parameterSetNode( ).GetSelectedSegmentID() # We need to know exactly the value of the segment voxels, apply threshold to make force the selected label value labelValue = 1 backgroundValue = 0 thresh = vtk.vtkImageThreshold() thresh.SetInputData(selectedSegmentLabelmap) thresh.ThresholdByLower(0) thresh.SetInValue(backgroundValue) thresh.SetOutValue(labelValue) thresh.SetOutputScalarType(vtk.VTK_UNSIGNED_SHORT) thresh.Update() labelImage = thresh.GetOutput() # collect seeds dim = masterImageData.GetDimensions() # initialize the filter self.fm = slicer.vtkPichonFastMarching() scalarRange = masterImageData.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.SetInputData(masterImageData) rescale.SetScale(scaleValue) rescale.SetShift(shiftValue) rescale.Update() masterImageData = rescale.GetOutput() scalarRange = masterImageData.GetScalarRange() depth = scalarRange[1] - scalarRange[0] self.fm.init(dim[0], dim[1], dim[2], depth, 1, 1, 1) caster = vtk.vtkImageCast() caster.SetOutputScalarTypeToShort() caster.SetInputData(masterImageData) self.fm.SetInputConnection(caster.GetOutputPort()) # self.fm.SetOutput(labelImage) npoints = int(dim[0] * dim[1] * dim[2] * percentMax / 100.) self.fm.setNPointsEvolution(npoints) self.fm.setActiveLabel(labelValue) spacing = self.originalSelectedSegmentLabelmap.GetSpacing() self.voxelVolume = spacing[0] * spacing[1] * spacing[2] self.totalNumberOfVoxels = npoints nSeeds = self.fm.addSeedsFromImage(labelImage) if nSeeds == 0: self.totalNumberOfVoxels = 0 return self.fm.Modified() self.fm.Update() # Need to call show() twice for data to be updated. # There are many other issues with the vtkPichonFastMarching filter # (expects extents to start at 0, crashes in debug mode, etc). self.fm.show(1) self.fm.Modified() self.fm.Update() self.fm.show(1) self.fm.Modified() self.fm.Update() logging.info('FastMarching march update completed')