Search code examples
pythonfiltersimpleitk

SimpleITK Python apply BinaryMorphologicalClosingImageFilter to a TIFF image


I am trying to apply a BinaryMorphologicalClosingImageFilter to a binary TIFF image to fill empty spaces between structures (where ImageJ Fill Holes doesn't help). Here is the code I use:

import SimpleITK as sitk

#Import TIFF image
image = sitk.ReadImage("C:/Users/Christian Nikolov/Desktop/STL/4_bina.tif")

#Apply Filter
sitk.BinaryMorphologicalClosingImageFilter()

#Export Image
sitk.WriteImage(image, "C:/Users/Christian Nikolov/Desktop/STL/4_bina_itk.tif")

The code runs without an error but the problem is that I can't figure out how to set the Kernel Size of the filter and the image doesn't experience a change. Any advise? (I got the idea to use this filter from the following post on SO: Fill holes on a 3D image)


Solution

  • You've created the BinaryMorphologicalClosingImageFilter object, but you haven't actually applied it to your input image. Your code should be something like this:

    import SimpleITK as sitk
    
    #Import TIFF image
    image = sitk.ReadImage("C:/Users/Christian Nikolov/Desktop/STL/4_bina.tif")
    
    #Apply Filter
    filter = sitk.BinaryMorphologicalClosingImageFilter()
    filter.SetKernelRadius([2, 2])
    output_image = filter.Execute(image)
    
    
    #Export Image
    sitk.WriteImage(output_image, "C:/Users/Christian Nikolov/Desktop/STL/4_bina_itk.tif")
    

    I set the kernel size to [2, 2], but you can use whatever unsigned integer size works best for you.