<div dir="ltr">Hi Daniel,<div><div><br class="">Within VTK proper, there's the vtkImageThresholdConnectivity filter, but it</div><div>requires you to seed the regions you want, it can't just give you the largest:</div><div><a href="http://www.vtk.org/doc/nightly/html/classvtkImageThresholdConnectivity.html">http://www.vtk.org/doc/nightly/html/classvtkImageThresholdConnectivity.html</a></div></div><div><br></div><div><div>I have a better filter called simply "vtkImageConnectivityFilter", but it hasn't</div><div>made its way into VTK yet:</div><div><a href="https://github.com/dgobbi/AIRS/blob/master/ImageSegmentation/vtkImageConnectivityFilter.h">https://github.com/dgobbi/AIRS/blob/master/ImageSegmentation/vtkImageConnectivityFilter.h</a></div></div><div><br></div><div> - David</div><div><br></div><div><br><div class="gmail_extra"><br><div class="gmail_quote">On Wed, Jun 8, 2016 at 8:52 AM, D L <span dir="ltr"><<a href="mailto:lackodan@outlook.com" target="_blank">lackodan@outlook.com</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left-width:1px;border-left-style:solid;border-left-color:rgb(204,204,204);padding-left:1ex">


<div><div dir="ltr">Hello again! <br><br>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:<br><br><a href="http://www.vtk.org/doc/nightly/html/classvtkConnectivityFilter.html" target="_blank">http://www.vtk.org/doc/nightly/html/classvtkConnectivityFilter.html</a><br><a href="http://www.vtk.org/Wiki/VTK/Examples/CSharp/PolyData/ColorDisconnectedRegions" target="_blank">http://www.vtk.org/Wiki/VTK/Examples/CSharp/PolyData/ColorDisconnectedRegions</a><br><a href="http://public.kitware.com/pipermail/vtkusers/2014-October/085430.html" target="_blank">http://public.kitware.com/pipermail/vtkusers/2014-October/085430.html</a><br><a href="http://www.vtk.org/Wiki/VTK/Examples/CSharp/PolyData/PolyDataConnectivityFilter_LargestRegion" target="_blank">http://www.vtk.org/Wiki/VTK/Examples/CSharp/PolyData/PolyDataConnectivityFilter_LargestRegion</a><br><a href="http://www.vtk.org/gitweb?p=VTK.git;a=blob;f=Common/DataModel/Testing/Python/scalarConn.py" target="_blank">http://www.vtk.org/gitweb?p=VTK.git;a=blob;f=Common/DataModel/Testing/Python/scalarConn.py</a><br><a href="http://www.vtk.org/Wiki/VTK/Examples/CSharp/PolyData/PolyDataConnectivityFilter_LargestRegion" target="_blank">http://www.vtk.org/Wiki/VTK/Examples/CSharp/PolyData/PolyDataConnectivityFilter_LargestRegion</a><br><a href="http://www.vtk.org/Wiki/VTK/Examples/CSharp/PolyData/PolyDataConnectivityFilter_LargestRegion" target="_blank">http://www.vtk.org/Wiki/VTK/Examples/CSharp/PolyData/PolyDataConnectivityFilter_LargestRegion</a><br><a href="http://www.vtk.org/gitweb?p=VTK.git;a=blob;f=Filters/Extraction/Testing/Python/extractUGrid.py" target="_blank">http://www.vtk.org/gitweb?p=VTK.git;a=blob;f=Filters/Extraction/Testing/Python/extractUGrid.py</a><br><br>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...<br><br>Here's a minimal example I'm using to test and compare:<br><br>    import vtk<br><br>    niftiReader = vtk.vtkNIFTIImageReader()<br>    niftiReader.SetFileName("test/MNI_0103-thresholded.nii")<br>    niftiReader.Update()<br>    thresholdedImage = niftiReader.GetOutput()<br><br>    # vtkConnectivityFilter - doesn't work<br>    connectivity = vtk.vtkConnectivityFilter()<br>    connectivity.SetInputData(thresholdedImage)<br>    connectivity.SetExtractionModeToLargestRegion()<br>    connectivity.Update()<br><br>    print("\nvtkConnectivityFilter number of extracted regions: " +<br>        str(connectivity.GetNumberOfExtractedRegions()))<br>    # outputs 1    <br><br>    # Connectivity filter produces an unstructured grid; convert back <br>    # to image data for further use.<br>    probeFilter = vtk.vtkProbeFilter()<br>    probeFilter.SetSourceConnection(connectivity.GetOutputPort())<br>    probeFilter.SetInputData(thresholdedImage)<br>    probeFilter.Update()<br><br>    marchingCubes = vtk.vtkMarchingCubes()<br>    marchingCubes.SetInputConnection(probeFilter.GetOutputPort())<br>    marchingCubes.GenerateValues(1,1,1)  # input was thresholded with 1 as output value<br>    marchingCubes.Update()<br><br>    stlWriter = vtk.vtkSTLWriter()<br>    stlWriter.SetInputConnection(marchingCubes.GetOutputPort())<br>    stlWriter.SetFileName("test/MNI_0103-vtkConnectivityFilter-largestRegion.stl")<br>    stlWriter.Write()<br><br>    <br>    # vtkPolyDataConnectivityFilter - works as expected<br>    marchingCubes2 = vtk.vtkMarchingCubes()<br>    marchingCubes2.SetInputData(thresholdedImage)<br>    marchingCubes2.GenerateValues(1,1,1)<br>    marchingCubes2.Update()<br><br>    connectivity2 = vtk.vtkPolyDataConnectivityFilter()<br>    connectivity2.SetInputConnection(marchingCubes2.GetOutputPort())<br>    connectivity2.SetExtractionModeToLargestRegion()<br>    connectivity2.Update()<br><br>    print("\nvtkPolyDataConnectivityFilter number of extracted regions: " +<br>        str(connectivity2.GetNumberOfExtractedRegions()))  <br>    # outputs 1673    <br><br>    stlWriter2 = vtk.vtkSTLWriter()<br>    stlWriter2.SetInputConnection(connectivity2.GetOutputPort())<br>    stlWriter2.SetFileName("test/MNI_0103-vtkPolyDataConnectivityFilter-largestRegion.stl")<br>    stlWriter2.Write()<br><br>The input volume can downloaded here: <a href="https://onedrive.live.com/redir?resid=C74503A42DE708!3267&authkey=!AMjKnLDNfJzYxGo&ithint=file%2cnii" target="_blank">https://onedrive.live.com/redir?resid=C74503A42DE708!3267&authkey=!AMjKnLDNfJzYxGo&ithint=file%2cnii</a><br>The end result when using vtkConnectivityFilter: <a href="https://onedrive.live.com/redir?resid=C74503A42DE708!3269&authkey=!ACgeZNYmu0yYYTQ&ithint=file%2cstl" target="_blank">https://onedrive.live.com/redir?resid=C74503A42DE708!3269&authkey=!ACgeZNYmu0yYYTQ&ithint=file%2cstl</a><br>The end result when using vtkPolyDataConnectivityFilter: <a href="https://onedrive.live.com/redir?resid=C74503A42DE708!3268&authkey=!ADAeoPw8O09APE8&ithint=file%2cstl" target="_blank">https://onedrive.live.com/redir?resid=C74503A42DE708!3268&authkey=!ADAeoPw8O09APE8&ithint=file%2cstl</a><br><br>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. <br><br>Could someone with a better understanding of VTK please explain?<br><br>Kind regards,<br><br>Daniel<br>                                     </div></div>
</blockquote></div><br></div></div></div>