コード例 #1
0
 def __init__(self, module_manager):
     SimpleVTKClassModuleBase.__init__(
         self, module_manager,
         vtk.vtkImageOpenClose3D(), 'Processing.',
         ('vtkImageData',), ('vtkImageData',),
         replaceDoc=True,
         inputFunctions=None, outputFunctions=None)
コード例 #2
0
ファイル: imagingTools.py プロジェクト: hubitor/progs
def openClose3D(image, openValue, closeValue, kernel):
    image2 = vtk.vtkImageOpenClose3D()
    image2.SetInput(image)
    image2.SetOpenValue(openValue)
    image2.SetCloseValue(closeValue) 
    image2.SetKernelSize(kernel[0],kernel[1],kernel[2])
    image2.Update()
    return image2.GetOutput()
コード例 #3
0
 def __init__(self, module_manager):
     SimpleVTKClassModuleBase.__init__(self,
                                       module_manager,
                                       vtk.vtkImageOpenClose3D(),
                                       'Processing.', ('vtkImageData', ),
                                       ('vtkImageData', ),
                                       replaceDoc=True,
                                       inputFunctions=None,
                                       outputFunctions=None)
コード例 #4
0
    def applyOpenClose(self, imgData, oVal, cVal, kernel=[3, 3, 3]):

        ocImg = vtk.vtkImageOpenClose3D()
        ocImg.SetInputData(imgData.GetOutput())
        ocImg.SetOpenValue(oVal)
        ocImg.SetCloseValue(cVal)
        ocImg.SetKernelSize(kernel[0], kernel[1], kernel[2])
        ocImg.Update()

        return ocImg
コード例 #5
0
def imageopenclose(image, openvalue, closevalue, kernelsize):
    """Performs opening and closing morphological operations
        with a 3D ellipsoidal kernel."""
    openClose = vtk.vtkImageOpenClose3D()
    openClose.SetInput(image)
    openClose.SetOpenValue(openvalue)
    openClose.SetCloseValue(closevalue)
    openClose.SetKernelSize(kernelsize, kernelsize, kernelsize)
    openClose.ReleaseDataFlagOff()
    openClose.GetOutput()
    openClose.GetCloseValue()
    openClose.GetOpenValue()
    return openClose.GetOutput()
コード例 #6
0
    def OpenCloseImageFilter(self, image, settings):
        """Open or close an image"""

        image.SetReleaseDataFlag(1)
        _filter = vtk.vtkImageOpenClose3D()
        _filter.SetInput(image.GetRealImage())
        _filter.AddObserver('ProgressEvent', self.HandleVTKProgressEvent)
        _filter.SetProgressText("Opening/Closing...")
        _filter.SetKernelSize(settings['KernelSizeX'], settings['KernelSizeY'],
                              settings['KernelSizeZ'])
        _filter.SetOpenValue(settings['OpenValue'])
        _filter.SetCloseValue(settings['CloseValue'])
        logging.info("Open/close filter applied")
        return MVImage.MVImage(_filter.GetOutputPort(), input=image)
コード例 #7
0
    def OpenCloseImageFilter(self, image, settings):
        """Open or close an image"""

        image.SetReleaseDataFlag(1)
        _filter = vtk.vtkImageOpenClose3D()
        _filter.SetInput(image.GetRealImage())
        _filter.AddObserver('ProgressEvent', self.HandleVTKProgressEvent)
        _filter.SetProgressText("Opening/Closing...")
        _filter.SetKernelSize(
            settings['KernelSizeX'], settings['KernelSizeY'], settings['KernelSizeZ'])
        _filter.SetOpenValue(settings['OpenValue'])
        _filter.SetCloseValue(settings['CloseValue'])
        logging.info("Open/close filter applied")
        return MVImage.MVImage(_filter.GetOutputPort(), input=image)
コード例 #8
0
ファイル: utils.py プロジェクト: osmsc/SimVascular
def label_open_close(im, label_id, bg_id, size):
    """
    Open or close 

    Args: 
        im: vtkImage of the label map
        label_id: class id to close
        bg_id: class id of background to open
        size: number of pixels of the erosion
    Return: processed vtk image
    """

    filt = vtk.vtkImageOpenClose3D()
    filt.SetInputData(im)
    filt.SetOpenValue(bg_id)
    filt.SetCloseValue(label_id)
    filt.SetKernelSize(int(size), int(size), int(size))
    filt.Update()
    return filt.GetOutput()
コード例 #9
0
    def smoothSelectedSegment(self, maskImage=None, maskExtent=None):
        try:
            # Get modifier labelmap
            modifierLabelmap = self.scriptedEffect.defaultModifierLabelmap()
            selectedSegmentLabelmap = self.scriptedEffect.selectedSegmentLabelmap(
            )

            smoothingMethod = self.scriptedEffect.parameter("SmoothingMethod")

            if smoothingMethod == GAUSSIAN:
                maxValue = 255
                radiusFactor = 4.0
                standardDeviationMM = self.scriptedEffect.doubleParameter(
                    "GaussianStandardDeviationMm")
                spacing = modifierLabelmap.GetSpacing()
                standardDeviationPixel = [1.0, 1.0, 1.0]
                radiusPixel = [3, 3, 3]
                for idx in range(3):
                    standardDeviationPixel[
                        idx] = standardDeviationMM / spacing[idx]
                    radiusPixel[idx] = int(
                        standardDeviationPixel[idx] * radiusFactor) + 1
                if maskExtent:
                    clippedSelectedSegmentLabelmap = self.clipImage(
                        selectedSegmentLabelmap, maskExtent, radiusPixel)
                else:
                    clippedSelectedSegmentLabelmap = selectedSegmentLabelmap

                thresh = vtk.vtkImageThreshold()
                thresh.SetInputData(clippedSelectedSegmentLabelmap)
                thresh.ThresholdByLower(0)
                thresh.SetInValue(0)
                thresh.SetOutValue(maxValue)
                thresh.SetOutputScalarType(vtk.VTK_UNSIGNED_CHAR)

                gaussianFilter = vtk.vtkImageGaussianSmooth()
                gaussianFilter.SetInputConnection(thresh.GetOutputPort())
                gaussianFilter.SetStandardDeviation(*standardDeviationPixel)
                gaussianFilter.SetRadiusFactor(radiusFactor)

                thresh2 = vtk.vtkImageThreshold()
                thresh2.SetInputConnection(gaussianFilter.GetOutputPort())
                thresh2.ThresholdByUpper(int(maxValue / 2))
                thresh2.SetInValue(1)
                thresh2.SetOutValue(0)
                thresh2.SetOutputScalarType(
                    selectedSegmentLabelmap.GetScalarType())
                thresh2.Update()

                self.modifySelectedSegmentByLabelmap(thresh2.GetOutput(),
                                                     selectedSegmentLabelmap,
                                                     modifierLabelmap,
                                                     maskImage, maskExtent)

            else:
                # size rounded to nearest odd number. If kernel size is even then image gets shifted.
                kernelSizePixel = self.getKernelSizePixel()

                if maskExtent:
                    clippedSelectedSegmentLabelmap = self.clipImage(
                        selectedSegmentLabelmap, maskExtent, kernelSizePixel)
                else:
                    clippedSelectedSegmentLabelmap = selectedSegmentLabelmap

                if smoothingMethod == MEDIAN:
                    # Median filter does not require a particular label value
                    smoothingFilter = vtk.vtkImageMedian3D()
                    smoothingFilter.SetInputData(
                        clippedSelectedSegmentLabelmap)

                else:
                    # 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(clippedSelectedSegmentLabelmap)
                    thresh.ThresholdByLower(0)
                    thresh.SetInValue(backgroundValue)
                    thresh.SetOutValue(labelValue)
                    thresh.SetOutputScalarType(
                        clippedSelectedSegmentLabelmap.GetScalarType())

                    smoothingFilter = vtk.vtkImageOpenClose3D()
                    smoothingFilter.SetInputConnection(thresh.GetOutputPort())
                    if smoothingMethod == MORPHOLOGICAL_OPENING:
                        smoothingFilter.SetOpenValue(labelValue)
                        smoothingFilter.SetCloseValue(backgroundValue)
                    else:  # must be smoothingMethod == MORPHOLOGICAL_CLOSING:
                        smoothingFilter.SetOpenValue(backgroundValue)
                        smoothingFilter.SetCloseValue(labelValue)

                smoothingFilter.SetKernelSize(kernelSizePixel[0],
                                              kernelSizePixel[1],
                                              kernelSizePixel[2])
                smoothingFilter.Update()

                self.modifySelectedSegmentByLabelmap(
                    smoothingFilter.GetOutput(), selectedSegmentLabelmap,
                    modifierLabelmap, maskImage, maskExtent)

        except IndexError:
            logging.error('apply: Failed to apply smoothing')
コード例 #10
0
    def smoothSelectedSegment(self):
        try:

            # Get master volume image data
            import vtkSegmentationCorePython

            # Get modifier labelmap
            modifierLabelmap = self.scriptedEffect.defaultModifierLabelmap()
            selectedSegmentLabelmap = self.scriptedEffect.selectedSegmentLabelmap(
            )

            smoothingMethod = self.scriptedEffect.parameter("SmoothingMethod")

            if smoothingMethod == GAUSSIAN:
                maxValue = 255

                thresh = vtk.vtkImageThreshold()
                thresh.SetInputData(selectedSegmentLabelmap)
                thresh.ThresholdByLower(0)
                thresh.SetInValue(0)
                thresh.SetOutValue(maxValue)
                thresh.SetOutputScalarType(vtk.VTK_UNSIGNED_CHAR)

                standardDeviationMm = self.scriptedEffect.doubleParameter(
                    "GaussianStandardDeviationMm")
                gaussianFilter = vtk.vtkImageGaussianSmooth()
                gaussianFilter.SetInputConnection(thresh.GetOutputPort())
                gaussianFilter.SetStandardDeviation(standardDeviationMm)
                gaussianFilter.SetRadiusFactor(4)

                thresh2 = vtk.vtkImageThreshold()
                thresh2.SetInputConnection(gaussianFilter.GetOutputPort())
                thresh2.ThresholdByUpper(int(maxValue / 2))
                thresh2.SetInValue(1)
                thresh2.SetOutValue(0)
                thresh2.SetOutputScalarType(
                    selectedSegmentLabelmap.GetScalarType())
                thresh2.Update()
                modifierLabelmap.DeepCopy(thresh2.GetOutput())

            else:
                # size rounded to nearest odd number. If kernel size is even then image gets shifted.
                kernelSizePixel = self.getKernelSizePixel()

                if smoothingMethod == MEDIAN:
                    # Median filter does not require a particular label value
                    smoothingFilter = vtk.vtkImageMedian3D()
                    smoothingFilter.SetInputData(selectedSegmentLabelmap)

                else:
                    # 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(
                        selectedSegmentLabelmap.GetScalarType())

                    smoothingFilter = vtk.vtkImageOpenClose3D()
                    smoothingFilter.SetInputConnection(thresh.GetOutputPort())
                    if smoothingMethod == MORPHOLOGICAL_OPENING:
                        smoothingFilter.SetOpenValue(labelValue)
                        smoothingFilter.SetCloseValue(backgroundValue)
                    else:  # must be smoothingMethod == MORPHOLOGICAL_CLOSING:
                        smoothingFilter.SetOpenValue(backgroundValue)
                        smoothingFilter.SetCloseValue(labelValue)

                smoothingFilter.SetKernelSize(kernelSizePixel[0],
                                              kernelSizePixel[1],
                                              kernelSizePixel[2])
                smoothingFilter.Update()
                modifierLabelmap.DeepCopy(smoothingFilter.GetOutput())

        except IndexError:
            logging.error('apply: Failed to apply smoothing')

        # Apply changes
        self.scriptedEffect.modifySelectedSegmentByLabelmap(
            modifierLabelmap,
            slicer.qSlicerSegmentEditorAbstractEffect.ModificationModeSet)
コード例 #11
0
from vtk.util.misc import vtkGetDataRoot

VTK_DATA_ROOT = vtkGetDataRoot()

# Tst the OpenClose3D filter.
# Image pipeline
reader = vtk.vtkPNGReader()
reader.SetFileName("" + str(VTK_DATA_ROOT) + "/Data/fullhead15.png")
thresh = vtk.vtkImageThreshold()
thresh.SetInputConnection(reader.GetOutputPort())
thresh.SetOutputScalarTypeToUnsignedChar()
thresh.ThresholdByUpper(2000.0)
thresh.SetInValue(255)
thresh.SetOutValue(0)
thresh.ReleaseDataFlagOff()
my_close = vtk.vtkImageOpenClose3D()
my_close.SetInputConnection(thresh.GetOutputPort())
my_close.SetOpenValue(0)
my_close.SetCloseValue(255)
my_close.SetKernelSize(5, 5, 3)
my_close.ReleaseDataFlagOff()
# for coverage (we could compare results to see if they are correct).
my_close.DebugOn()
my_close.DebugOff()
my_close.GetOutput()
my_close.GetCloseValue()
my_close.GetOpenValue()
#my_close AddObserver ProgressEvent {set pro [my_close GetProgress]; puts "Completed $pro"; flush stdout}
viewer = vtk.vtkImageViewer()
viewer.SetInputConnection(my_close.GetOutputPort())
viewer.SetColorWindow(255)
コード例 #12
0
  def smoothSelectedSegment(self):
    try:

      # Get master volume image data
      import vtkSegmentationCorePython

      # Get modifier labelmap
      modifierLabelmap = self.scriptedEffect.defaultModifierLabelmap()
      selectedSegmentLabelmap = self.scriptedEffect.selectedSegmentLabelmap()

      smoothingMethod = self.scriptedEffect.parameter("SmoothingMethod")

      if smoothingMethod == GAUSSIAN:
        maxValue = 255

        thresh = vtk.vtkImageThreshold()
        thresh.SetInputData(selectedSegmentLabelmap)
        thresh.ThresholdByLower(0)
        thresh.SetInValue(0)
        thresh.SetOutValue(maxValue)
        thresh.SetOutputScalarType(vtk.VTK_UNSIGNED_CHAR)

        standardDeviationMm = self.scriptedEffect.doubleParameter("GaussianStandardDeviationMm")
        gaussianFilter = vtk.vtkImageGaussianSmooth()
        gaussianFilter.SetInputConnection(thresh.GetOutputPort())
        gaussianFilter.SetStandardDeviation(standardDeviationMm)
        gaussianFilter.SetRadiusFactor(4)

        thresh2 = vtk.vtkImageThreshold()
        thresh2.SetInputConnection(gaussianFilter.GetOutputPort())
        thresh2.ThresholdByUpper(int(maxValue / 2))
        thresh2.SetInValue(1)
        thresh2.SetOutValue(0)
        thresh2.SetOutputScalarType(selectedSegmentLabelmap.GetScalarType())
        thresh2.Update()
        modifierLabelmap.DeepCopy(thresh2.GetOutput())

      else:
        # size rounded to nearest odd number. If kernel size is even then image gets shifted.
        kernelSizePixel = self.getKernelSizePixel()

        if smoothingMethod == MEDIAN:
          # Median filter does not require a particular label value
          smoothingFilter = vtk.vtkImageMedian3D()
          smoothingFilter.SetInputData(selectedSegmentLabelmap)

        else:
          # 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(selectedSegmentLabelmap.GetScalarType())

          smoothingFilter = vtk.vtkImageOpenClose3D()
          smoothingFilter.SetInputConnection(thresh.GetOutputPort())
          if smoothingMethod == MORPHOLOGICAL_OPENING:
            smoothingFilter.SetOpenValue(labelValue)
            smoothingFilter.SetCloseValue(backgroundValue)
          else: # must be smoothingMethod == MORPHOLOGICAL_CLOSING:
            smoothingFilter.SetOpenValue(backgroundValue)
            smoothingFilter.SetCloseValue(labelValue)

        smoothingFilter.SetKernelSize(kernelSizePixel[0],kernelSizePixel[1],kernelSizePixel[2])
        smoothingFilter.Update()
        modifierLabelmap.DeepCopy(smoothingFilter.GetOutput())

    except IndexError:
      logging.error('apply: Failed to apply smoothing')

    # Apply changes
    self.scriptedEffect.modifySelectedSegmentByLabelmap(modifierLabelmap, slicer.qSlicerSegmentEditorAbstractEffect.ModificationModeSet)
コード例 #13
0
ファイル: TestOpenClose3D.py プロジェクト: 0004c/VTK
from vtk.test import Testing
from vtk.util.misc import vtkGetDataRoot
VTK_DATA_ROOT = vtkGetDataRoot()

# Tst the OpenClose3D filter.
# Image pipeline
reader = vtk.vtkPNGReader()
reader.SetFileName("" + str(VTK_DATA_ROOT) + "/Data/fullhead15.png")
thresh = vtk.vtkImageThreshold()
thresh.SetInputConnection(reader.GetOutputPort())
thresh.SetOutputScalarTypeToUnsignedChar()
thresh.ThresholdByUpper(2000.0)
thresh.SetInValue(255)
thresh.SetOutValue(0)
thresh.ReleaseDataFlagOff()
my_close = vtk.vtkImageOpenClose3D()
my_close.SetInputConnection(thresh.GetOutputPort())
my_close.SetOpenValue(0)
my_close.SetCloseValue(255)
my_close.SetKernelSize(5,5,3)
my_close.ReleaseDataFlagOff()
# for coverage (we could compare results to see if they are correct).
my_close.DebugOn()
my_close.DebugOff()
my_close.GetOutput()
my_close.GetCloseValue()
my_close.GetOpenValue()
#my_close AddObserver ProgressEvent {set pro [my_close GetProgress]; puts "Completed $pro"; flush stdout}
viewer = vtk.vtkImageViewer()
viewer.SetInputConnection(my_close.GetOutputPort())
viewer.SetColorWindow(255)
コード例 #14
0
reader.Update()

#check binary
vtk_data = reader.GetOutput().GetPointData().GetScalars()
numpy_data = numpy_support.vtk_to_numpy(vtk_data)
u = np.unique(numpy_data).shape[0]
if u != 2:
    print('ERROR: Inputfile is not binary')
    sys.exit(2)
del u
del numpy_data
del vtk_data  # free memory

#closing filter
print('Apllying closing filter')
close_filter = vtk.vtkImageOpenClose3D()
close_filter.SetInputConnection(reader.GetOutputPort())
close_filter.SetOpenValue(0)
close_filter.SetCloseValue(1)
close_filter.SetKernelSize(3, 3, 3)
close_filter.Update()
del reader  # free memory

#convert to float
tofloat = vtk.vtkImageCast()
tofloat.SetInputConnection(close_filter.GetOutputPort())
tofloat.SetOutputScalarTypeToFloat()
tofloat.Update()
del close_filter  # free memory

#normalize to 1