[vtkusers] Extracting largest region works with vtkPolyDataConnectivityFilter, but not with vtkConnectivityFilter

D L lackodan at outlook.com
Wed Jun 8 10:52:19 EDT 2016


Hello again! 

Today, I'm having some trouble understanding how to use vtkConnectivityFilter in python 2.7 and VTK6.3. I've consulted a number of resources and examples, including:

http://www.vtk.org/doc/nightly/html/classvtkConnectivityFilter.html
http://www.vtk.org/Wiki/VTK/Examples/CSharp/PolyData/ColorDisconnectedRegions
http://public.kitware.com/pipermail/vtkusers/2014-October/085430.html
http://www.vtk.org/Wiki/VTK/Examples/CSharp/PolyData/PolyDataConnectivityFilter_LargestRegion
http://www.vtk.org/gitweb?p=VTK.git;a=blob;f=Common/DataModel/Testing/Python/scalarConn.py
http://www.vtk.org/Wiki/VTK/Examples/CSharp/PolyData/PolyDataConnectivityFilter_LargestRegion
http://www.vtk.org/Wiki/VTK/Examples/CSharp/PolyData/PolyDataConnectivityFilter_LargestRegion
http://www.vtk.org/gitweb?p=VTK.git;a=blob;f=Filters/Extraction/Testing/Python/extractUGrid.py

However, I still can't figure out how to use the filter. I have a number of operations in mind, but let's start with extracting the largest region. From the available documentation, I assumed that this could be done by using vtkConnectivityFilter.SetExtractionModeToLargestRegion(). However, when I apply this to a (segmented and thresholded) MRI image to remove some noise, it just extracts the entire image. On the other hand, if I convert it to a surface first, using vtkMarchingCubes, and then apply a vtkPolyDataConnectivityFilter, that does give me the expected result...

Here's a minimal example I'm using to test and compare:

    import vtk

    niftiReader = vtk.vtkNIFTIImageReader()
    niftiReader.SetFileName("test/MNI_0103-thresholded.nii")
    niftiReader.Update()
    thresholdedImage = niftiReader.GetOutput()

    # vtkConnectivityFilter - doesn't work
    connectivity = vtk.vtkConnectivityFilter()
    connectivity.SetInputData(thresholdedImage)
    connectivity.SetExtractionModeToLargestRegion()
    connectivity.Update()

    print("\nvtkConnectivityFilter number of extracted regions: " +
        str(connectivity.GetNumberOfExtractedRegions()))
    # outputs 1    

    # Connectivity filter produces an unstructured grid; convert back 
    # to image data for further use.
    probeFilter = vtk.vtkProbeFilter()
    probeFilter.SetSourceConnection(connectivity.GetOutputPort())
    probeFilter.SetInputData(thresholdedImage)
    probeFilter.Update()

    marchingCubes = vtk.vtkMarchingCubes()
    marchingCubes.SetInputConnection(probeFilter.GetOutputPort())
    marchingCubes.GenerateValues(1,1,1)  # input was thresholded with 1 as output value
    marchingCubes.Update()

    stlWriter = vtk.vtkSTLWriter()
    stlWriter.SetInputConnection(marchingCubes.GetOutputPort())
    stlWriter.SetFileName("test/MNI_0103-vtkConnectivityFilter-largestRegion.stl")
    stlWriter.Write()

    
    # vtkPolyDataConnectivityFilter - works as expected
    marchingCubes2 = vtk.vtkMarchingCubes()
    marchingCubes2.SetInputData(thresholdedImage)
    marchingCubes2.GenerateValues(1,1,1)
    marchingCubes2.Update()

    connectivity2 = vtk.vtkPolyDataConnectivityFilter()
    connectivity2.SetInputConnection(marchingCubes2.GetOutputPort())
    connectivity2.SetExtractionModeToLargestRegion()
    connectivity2.Update()

    print("\nvtkPolyDataConnectivityFilter number of extracted regions: " +
        str(connectivity2.GetNumberOfExtractedRegions()))  
    # outputs 1673    

    stlWriter2 = vtk.vtkSTLWriter()
    stlWriter2.SetInputConnection(connectivity2.GetOutputPort())
    stlWriter2.SetFileName("test/MNI_0103-vtkPolyDataConnectivityFilter-largestRegion.stl")
    stlWriter2.Write()

The input volume can downloaded here: https://onedrive.live.com/redir?resid=C74503A42DE708!3267&authkey=!AMjKnLDNfJzYxGo&ithint=file%2cnii
The end result when using vtkConnectivityFilter: https://onedrive.live.com/redir?resid=C74503A42DE708!3269&authkey=!ACgeZNYmu0yYYTQ&ithint=file%2cstl
The end result when using vtkPolyDataConnectivityFilter: https://onedrive.live.com/redir?resid=C74503A42DE708!3268&authkey=!ADAeoPw8O09APE8&ithint=file%2cstl

The reason I want to apply the connectivity filter before surface extraction is that I would like to so some other morphological operations on the image first (e.g. closing and flood fill). At first, I assumed it might have something to do with scalar connectivity, but using vtkConnectivityFilter.SetScalarConnectivityOff() didn't change anything. 

Could someone with a better understanding of VTK please explain?

Kind regards,

Daniel
 		 	   		  
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://public.kitware.com/pipermail/vtkusers/attachments/20160608/f7f50982/attachment.html>


More information about the vtkusers mailing list