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

D L lackodan at outlook.com
Wed Jun 8 11:56:57 EDT 2016


Hi David,

First of all, it's amazing how fast you and the other contributers on this list respond. Thanks for making the time! 

While your filter does indeed look better (and even has comprehensive comments explaining each step), I suspect it would not be easy to use with my precompiled VTK-version in Anaconda. If I'm not mistaking, to use custom VTK classes, you need a custom VTK build, right? I don't think that will be possible. I'm on a tight time schedule (I need to finish my current project ASAP so I have a chance at finishing my PhD thesis before August) and I only have a basic image processing background, so I'm currrently looking for the fastest and easiest solution. 

This might be a very obvious question, but with vtkImageConnectivityFilter, how would I know which seed point to use? Should I look at the image in Paraview (or use a vtkPicker) to find the coordinates of the region I'd like to extract?

Kind regards,

Daniel


From: david.gobbi at gmail.com
Date: Wed, 8 Jun 2016 09:06:14 -0600
Subject: Re: [vtkusers] Extracting largest region works with vtkPolyDataConnectivityFilter, but not with vtkConnectivityFilter
To: lackodan at outlook.com
CC: vtkusers at vtk.org

Hi Daniel,
Within VTK proper, there's the vtkImageThresholdConnectivity filter, but itrequires you to seed the regions you want, it can't just give you the largest:http://www.vtk.org/doc/nightly/html/classvtkImageThresholdConnectivity.html
I have a better filter called simply "vtkImageConnectivityFilter", but it hasn'tmade its way into VTK yet:https://github.com/dgobbi/AIRS/blob/master/ImageSegmentation/vtkImageConnectivityFilter.h
 - David


On Wed, Jun 8, 2016 at 8:52 AM, D L <lackodan at outlook.com> wrote:



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/0b8e010c/attachment.html>


More information about the vtkusers mailing list