Ejemplo n.º 1
0
 def __init__(self, module_manager):
     SimpleVTKClassModuleBase.__init__(
         self, module_manager,
         vtk.vtkImageSeedConnectivity(), 'Processing.',
         ('vtkImageData',), ('vtkImageData',),
         replaceDoc=True,
         inputFunctions=None, outputFunctions=None)
Ejemplo n.º 2
0
 def __init__(self, module_manager):
     SimpleVTKClassModuleBase.__init__(self,
                                       module_manager,
                                       vtk.vtkImageSeedConnectivity(),
                                       'Processing.', ('vtkImageData', ),
                                       ('vtkImageData', ),
                                       replaceDoc=True,
                                       inputFunctions=None,
                                       outputFunctions=None)
Ejemplo n.º 3
0
    def __init__(self, module_manager):

        # call parent constructor
        ModuleBase.__init__(self, module_manager)

        self._imageCast = vtk.vtkImageCast()
        self._imageCast.SetOutputScalarTypeToUnsignedChar()
        self._seedConnect = vtk.vtkImageSeedConnectivity()
        self._seedConnect.SetInput(self._imageCast.GetOutput())

        module_utils.setup_vtk_object_progress(self, self._seedConnect,
                                           'Performing region growing')
        
        
        module_utils.setup_vtk_object_progress(self, self._imageCast,
                                           'Casting data to unsigned char')
        
        
        # we'll use this to keep a binding (reference) to the passed object
        self._inputPoints = None
        # this will be our internal list of points
        self._seedPoints = []

        # now setup some defaults before our sync
        self._config.inputConnectValue = 1
        self._config.outputConnectedValue = 1
        self._config.outputUnconnectedValue = 0


        config_list = [
            ('Input connect value:', 'inputConnectValue', 'base:int', 'text',
             'Points connected to seed points with this value will be '
             'included.'),
            ('Output connected value:', 'outputConnectedValue', 'base:int',
             'text', 'Included points will get this value.'),
            ('Output unconnected value:', 'outputUnconnectedValue',
             'base:int', 'text', 'Non-included points will get this value.')]
             
        ScriptedConfigModuleMixin.__init__(
            self, config_list,
            {'Module (self)' : self,
             'vtkImageSeedConnectivity' : self._seedConnect,
             'vtkImageCast' : self._imageCast})

        self.sync_module_logic_with_config()
Ejemplo n.º 4
0
    def __init__(self, module_manager):
        ModuleBase.__init__(self, module_manager)

        self._image_threshold = vtk.vtkImageThreshold()
        # seedconnect wants unsigned char at input
        self._image_threshold.SetOutputScalarTypeToUnsignedChar()
        self._image_threshold.SetInValue(1)
        self._image_threshold.SetOutValue(0)

        self._seed_connect = vtk.vtkImageSeedConnectivity()
        self._seed_connect.SetInputConnectValue(1)
        self._seed_connect.SetOutputConnectedValue(1)
        self._seed_connect.SetOutputUnconnectedValue(0)

        self._seed_connect.SetInput(self._image_threshold.GetOutput())

        module_utils.setup_vtk_object_progress(self, self._seed_connect,
                                               'Performing region growing')

        module_utils.setup_vtk_object_progress(self, self._image_threshold,
                                               'Thresholding data')

        # we'll use this to keep a binding (reference) to the passed object
        self._input_points = None
        # this will be our internal list of points
        self._seed_points = []

        self._config._thresh_interval = 5

        config_list = [
            ('Auto threshold interval:', '_thresh_interval', 'base:float',
             'text', 'Used to calculate automatic threshold (unit %).')
        ]

        ScriptedConfigModuleMixin.__init__(
            self, config_list, {
                'Module (self)': self,
                'vtkImageSeedConnectivity': self._seed_connect,
                'vtkImageThreshold': self._image_threshold
            })

        self.sync_module_logic_with_config()
Ejemplo n.º 5
0
    def __init__(self, module_manager):
        ModuleBase.__init__(self, module_manager)

        self._image_threshold = vtk.vtkImageThreshold()
        # seedconnect wants unsigned char at input
        self._image_threshold.SetOutputScalarTypeToUnsignedChar()
        self._image_threshold.SetInValue(1)
        self._image_threshold.SetOutValue(0)
        
        self._seed_connect = vtk.vtkImageSeedConnectivity()
        self._seed_connect.SetInputConnectValue(1)
        self._seed_connect.SetOutputConnectedValue(1)
        self._seed_connect.SetOutputUnconnectedValue(0)

        self._seed_connect.SetInput(self._image_threshold.GetOutput())

        module_utils.setup_vtk_object_progress(self, self._seed_connect,
                                           'Performing region growing')
        
        module_utils.setup_vtk_object_progress(self, self._image_threshold,
                                           'Thresholding data')

        # we'll use this to keep a binding (reference) to the passed object
        self._input_points = None
        # this will be our internal list of points
        self._seed_points = []

        self._config._thresh_interval = 5

        config_list = [
            ('Auto threshold interval:', '_thresh_interval', 'base:float',
             'text',
             'Used to calculate automatic threshold (unit %).')]
             
        ScriptedConfigModuleMixin.__init__(
            self, config_list,
            {'Module (self)' : self,
             'vtkImageSeedConnectivity' : self._seed_connect,
             'vtkImageThreshold' : self._image_threshold})

        self.sync_module_logic_with_config()
Ejemplo n.º 6
0
def main():
    # colors = vtk.vtkNamedColors()

    fileName = get_program_parameters()

    # Read the image.
    readerFactory = vtk.vtkImageReader2Factory()
    reader = readerFactory.CreateImageReader2(fileName)
    reader.SetFileName(fileName)
    reader.Update()

    # Dilate
    dilate = vtk.vtkImageDilateErode3D()
    dilate.SetInputConnection(reader.GetOutputPort())
    dilate.SetDilateValue(0)
    dilate.SetErodeValue(255)
    dilate.SetKernelSize(31, 31, 1)

    # Erode
    erode = vtk.vtkImageDilateErode3D()
    erode.SetInputConnection(reader.GetOutputPort())
    erode.SetDilateValue(255)
    erode.SetErodeValue(0)
    erode.SetKernelSize(31, 31, 1)

    # Opening - dilate then erode.
    dilate1 = vtk.vtkImageDilateErode3D()
    dilate1.SetInputConnection(reader.GetOutputPort())
    dilate1.SetDilateValue(0)
    dilate1.SetErodeValue(255)
    dilate1.SetKernelSize(31, 31, 1)

    erode1 = vtk.vtkImageDilateErode3D()
    erode1.SetInputConnection(dilate1.GetOutputPort())
    erode1.SetDilateValue(255)
    erode1.SetErodeValue(0)
    erode1.SetKernelSize(31, 31, 1)

    # Closing - erode then dilate.
    erode2 = vtk.vtkImageDilateErode3D()
    erode2.SetInputConnection(reader.GetOutputPort())
    erode2.SetDilateValue(255)
    erode2.SetErodeValue(0)
    erode2.SetKernelSize(31, 31, 1)

    dilate2 = vtk.vtkImageDilateErode3D()
    dilate2.SetInputConnection(erode2.GetOutputPort())
    dilate2.SetDilateValue(0)
    dilate2.SetErodeValue(255)
    dilate2.SetKernelSize(31, 31, 1)

    # Connectivity
    con = vtk.vtkImageSeedConnectivity()
    con.SetInputConnection(reader.GetOutputPort())
    con.AddSeed(300, 200)
    con.SetInputConnectValue(0)
    con.SetOutputConnectedValue(0)
    con.SetOutputUnconnectedValue(255)

    # Actors
    originalActor = vtk.vtkImageActor()
    originalActor.GetMapper().SetInputConnection(reader.GetOutputPort())
    originalActor.GetProperty().SetInterpolationTypeToNearest()

    connectedActor = vtk.vtkImageActor()
    connectedActor.GetMapper().SetInputConnection(con.GetOutputPort())
    connectedActor.GetProperty().SetInterpolationTypeToNearest()

    erodeActor = vtk.vtkImageActor()
    erodeActor.GetMapper().SetInputConnection(erode.GetOutputPort())
    erodeActor.GetProperty().SetInterpolationTypeToNearest()

    dilateActor = vtk.vtkImageActor()
    dilateActor.GetMapper().SetInputConnection(dilate.GetOutputPort())
    dilateActor.GetProperty().SetInterpolationTypeToNearest()

    openingActor = vtk.vtkImageActor()
    openingActor.GetMapper().SetInputConnection(dilate2.GetOutputPort())
    openingActor.GetProperty().SetInterpolationTypeToNearest()

    closingActor = vtk.vtkImageActor()
    closingActor.GetMapper().SetInputConnection(erode1.GetOutputPort())
    closingActor.GetProperty().SetInterpolationTypeToNearest()

    # Setup renderers
    originalRenderer = vtk.vtkRenderer()
    originalRenderer.AddActor(originalActor)
    connectedRenderer = vtk.vtkRenderer()
    connectedRenderer.AddActor(connectedActor)
    dilateRenderer = vtk.vtkRenderer()
    dilateRenderer.AddActor(dilateActor)
    erodeRenderer = vtk.vtkRenderer()
    erodeRenderer.AddActor(erodeActor)
    closingRenderer = vtk.vtkRenderer()
    closingRenderer.AddActor(closingActor)
    openingRenderer = vtk.vtkRenderer()
    openingRenderer.AddActor(openingActor)

    renderers = list()
    renderers.append(originalRenderer)
    renderers.append(connectedRenderer)
    renderers.append(erodeRenderer)
    renderers.append(dilateRenderer)
    renderers.append(openingRenderer)
    renderers.append(closingRenderer)

    # Setup viewports for the renderers
    rendererSize = 300
    xGridDimensions = 2
    yGridDimensions = 3

    renderWindow = vtk.vtkRenderWindow()
    renderWindow.SetSize(rendererSize * xGridDimensions,
                         rendererSize * yGridDimensions)
    for row in range(0, yGridDimensions):
        for col in range(xGridDimensions):
            index = row * xGridDimensions + col
            # (xmin, ymin, xmax, ymax)
            viewport = [
                float(col) / xGridDimensions,
                float(yGridDimensions - (row + 1)) / yGridDimensions,
                float(col + 1) / xGridDimensions,
                float(yGridDimensions - row) / yGridDimensions
            ]
            renderers[index].SetViewport(viewport)
            renderWindow.AddRenderer(renderers[index])
    renderWindow.SetWindowName('MorphologyComparison')

    renderWindowInteractor = vtk.vtkRenderWindowInteractor()
    style = vtk.vtkInteractorStyleImage()

    renderWindowInteractor.SetInteractorStyle(style)
    renderWindowInteractor.SetRenderWindow(renderWindow)

    # The renderers share one camera.
    renderWindow.Render()
    renderers[0].GetActiveCamera().Dolly(1.5)
    renderers[0].ResetCameraClippingRange()

    for r in range(1, len(renderers)):
        renderers[r].SetActiveCamera(renderers[0].GetActiveCamera())
    renderWindowInteractor.Initialize()
    renderWindowInteractor.Start()
    def _calculate_selection(self):
        """Calculate the selection contour based on the input data and seedpoints
        """
        for actor in self.contour_selected_actors:
            self.renderer_top.RemoveActor(actor)
            self.renderer_side.RemoveActor(actor)
            self.renderer_front.RemoveActor(actor)
            self.renderer_3d.RemoveActor(actor)

        # initial cleanup
        self.selectedData = []
        self.contour_selected_actors = []
        self._contourObjectsDict1 = {}
        self._contourObjectsDict2 = {}
        self._contourObjectsDict3 = {}

        if self.frame.continuous_check.GetValue():
            for seed_point in self.seedPoints:

                #self._config._thresh_interval = 5
                _image_threshold = vtk.vtkImageThreshold()
                # seedconnect wants unsigned char at input
                _image_threshold.SetOutputScalarTypeToUnsignedChar()
                _image_threshold.SetInValue(1)
                _image_threshold.SetOutValue(0)
                _image_threshold.SetInput(self._inputs[0]['inputData'])
                
                _seed_connect = vtk.vtkImageSeedConnectivity()
                _seed_connect.SetInputConnectValue(1)
                _seed_connect.SetOutputConnectedValue(1)
                _seed_connect.SetOutputUnconnectedValue(0)
                _seed_connect.SetInput(_image_threshold.GetOutput())

                # extract a list from the input points
                _seed_connect.RemoveAllSeeds()
                # we need to call Modified() explicitly as RemoveAllSeeds()
                # doesn't.  AddSeed() does, but sometimes the list is empty at
                # this stage and AddSeed() isn't called.
                _seed_connect.Modified()
                
                for seedPoint in self.seedPoints:
                    if not seedPoint == None:
                        _seed_connect.AddSeed(seedPoint[0], seedPoint[1], seedPoint[2])

                        #Determine threshold
                        iso_value = seed_point[3]
                        lower_thresh = iso_value + self.frame.lower_slider.GetValue()
                        upper_thresh = iso_value + self.frame.upper_slider.GetValue()
                        _image_threshold.ThresholdBetween(lower_thresh, upper_thresh)

                        #Update all stuff
                        _image_threshold.GetInput().Update()
                        _image_threshold.Update()
                        _seed_connect.Update()

                        #Create the contour
                        contourFilter = vtk.vtkContourFilter()
                        contourFilter.SetInput(_seed_connect.GetOutput())
                        contourFilter.GenerateValues(contourFilter.GetNumberOfContours(), 1, 1) #because 1 is output in-value
                        contourFilter.Update()

                        # Setup Actor and Mapper
                        actor = vtk.vtkActor()
                        mapper = vtk.vtkPolyDataMapper()
                        mapper.ScalarVisibilityOff()
                        actor.SetMapper(mapper)
                        self.renderer_3d.AddActor(actor)

                        # Set output to mapper
                        data = contourFilter.GetOutput()
                        mapper.SetInput(data)
                        mapper.Update()

                        self.addSelectionTo2DViewers(data, actor)

                        # Save result
                        self.selectedData.append(data)
                        self.contour_selected_actors.append(actor)
                        #End for-loop

        else:
            for seedPoint in self.seedPoints:
                if not seedPoint == None:
                    iso_value = seedPoint[3]
                    #print "working on seedpoint with iso" + str(iso_value)

                    # Setup Actor and Mapper
                    actor = vtk.vtkActor()
                    mapper = vtk.vtkPolyDataMapper()
                    mapper.ScalarVisibilityOff()
                    actor.SetMapper(mapper)
                    self.renderer_3d.AddActor(actor)

                    # Calculate Polydata
                    contourFilter = vtk.vtkContourFilter()
                    contourFilter.SetInput(self._inputs[0]['inputData'])
                    contourFilter.GenerateValues(contourFilter.GetNumberOfContours(), iso_value + self.frame.lower_slider.GetValue(), iso_value + self.frame.upper_slider.GetValue())
                    contourFilter.Update()

                    # Set output to mapper
                    data = contourFilter.GetOutput()
                    mapper.SetInput(data)
                    mapper.Update()

                    self.addSelectionTo2DViewers(data, actor)

                    # Save result
                    self.selectedData.append(data)
                    self.contour_selected_actors.append(actor)

        # Set colors
        self._on_select_new_color()

        self.render()

        return self.selectedData
Ejemplo n.º 8
0
extractImage = vtk.vtkImageExtractComponents()
extractImage.SetInputConnection(toHSV.GetOutputPort())
extractImage.SetComponents(2)
extractImage.GetExecutive().SetReleaseDataFlag(0, 0)

threshold1 = vtk.vtkImageThreshold()
threshold1.SetInputConnection(extractImage.GetOutputPort())
threshold1.ThresholdByUpper(230)
threshold1.SetInValue(255)
threshold1.SetOutValue(0)
threshold1.Update()

extent = threshold1.GetExecutive().GetWholeExtent(
    threshold1.GetOutputInformation(0))

connect = vtk.vtkImageSeedConnectivity()
connect.SetInputConnection(threshold1.GetOutputPort())
connect.SetInputConnectValue(255)
connect.SetOutputConnectedValue(255)
connect.SetOutputUnconnectedValue(0)
connect.AddSeed(extent[0], extent[2])
connect.AddSeed(extent[1], extent[2])
connect.AddSeed(extent[1], extent[3])
connect.AddSeed(extent[0], extent[3])

smooth = vtk.vtkImageGaussianSmooth()
smooth.SetDimensionality(2)
smooth.SetStandardDeviation(1, 1)
smooth.SetInputConnection(connect.GetOutputPort())

shrink = vtk.vtkImageShrink3D()
Ejemplo n.º 9
0
imagePowerOf2.SetOutputWholeExtent(0,expr.expr(globals(), locals(),["padX","-","1"]),0,expr.expr(globals(), locals(),["padY","-","1"]),0,0)
toHSV = vtk.vtkImageRGBToHSV()
toHSV.SetInputConnection(imageIn.GetOutputPort())
toHSV.GetExecutive().SetReleaseDataFlag(0,0)
extractImage = vtk.vtkImageExtractComponents()
extractImage.SetInputConnection(toHSV.GetOutputPort())
extractImage.SetComponents(2)
extractImage.GetExecutive().SetReleaseDataFlag(0,0)
threshold1 = vtk.vtkImageThreshold()
threshold1.SetInputConnection(extractImage.GetOutputPort())
threshold1.ThresholdByUpper(230)
threshold1.SetInValue(255)
threshold1.SetOutValue(0)
threshold1.Update()
extent = threshold1.GetExecutive().GetWholeExtent(threshold1.GetOutputInformation(0))
connect = vtk.vtkImageSeedConnectivity()
connect.SetInputConnection(threshold1.GetOutputPort())
connect.SetInputConnectValue(255)
connect.SetOutputConnectedValue(255)
connect.SetOutputUnconnectedValue(0)
connect.AddSeed(lindex(extent,0),lindex(extent,2))
connect.AddSeed(lindex(extent,1),lindex(extent,2))
connect.AddSeed(lindex(extent,1),lindex(extent,3))
connect.AddSeed(lindex(extent,0),lindex(extent,3))
smooth = vtk.vtkImageGaussianSmooth()
smooth.SetDimensionality(2)
smooth.SetStandardDeviation(1,1)
smooth.SetInputConnection(connect.GetOutputPort())
shrink = vtk.vtkImageShrink3D()
shrink.SetInputConnection(smooth.GetOutputPort())
shrink.SetShrinkFactors(2,2,1)