<html>
<head>
<style><!--
.hmmessage P
{
margin:0px;
padding:0px
}
body.hmmessage
{
font-size: 12pt;
font-family:Calibri
}
--></style></head>
<body class='hmmessage'><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>http://www.vtk.org/doc/nightly/html/classvtkConnectivityFilter.html<br>http://www.vtk.org/Wiki/VTK/Examples/CSharp/PolyData/ColorDisconnectedRegions<br>http://public.kitware.com/pipermail/vtkusers/2014-October/085430.html<br>http://www.vtk.org/Wiki/VTK/Examples/CSharp/PolyData/PolyDataConnectivityFilter_LargestRegion<br>http://www.vtk.org/gitweb?p=VTK.git;a=blob;f=Common/DataModel/Testing/Python/scalarConn.py<br>http://www.vtk.org/Wiki/VTK/Examples/CSharp/PolyData/PolyDataConnectivityFilter_LargestRegion<br>http://www.vtk.org/Wiki/VTK/Examples/CSharp/PolyData/PolyDataConnectivityFilter_LargestRegion<br>http://www.vtk.org/gitweb?p=VTK.git;a=blob;f=Filters/Extraction/Testing/Python/extractUGrid.py<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: https://onedrive.live.com/redir?resid=C74503A42DE708!3267&authkey=!AMjKnLDNfJzYxGo&ithint=file%2cnii<br>The end result when using vtkConnectivityFilter: https://onedrive.live.com/redir?resid=C74503A42DE708!3269&authkey=!ACgeZNYmu0yYYTQ&ithint=file%2cstl<br>The end result when using vtkPolyDataConnectivityFilter: https://onedrive.live.com/redir?resid=C74503A42DE708!3268&authkey=!ADAeoPw8O09APE8&ithint=file%2cstl<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></body>
</html>