Median Filter for 3d data

Hi all,

Is there median filter which works with 3D data in itk?

MedianImageFilter works in n-D.

I have a volume of size 500X500X1000. when I tried this filter ,even after 40 minutes the code did not exit from median filter update function. This is the code I wrote.

typedef itk::Image<unsigned char, 3>              ImageType3D;
typedef itk::ImageFileReader< ImageType3D >      ReaderType;
ReaderType::Pointer inputReader = ReaderType::New();      
inputReader ->SetFileName("D:\\SampleData.mha");
    inputReader ->SetImageIO(itk::MetaImageIO::New());
    inputReader ->Update();

ImageType3D::Pointer imageData = inputReader ->GetOutput();
imageData->Update();
using FilterType = itk::MedianImageFilter<ImageType3D, ImageType3D>;
FilterType::Pointer medianFilter = FilterType::New();


	OutputImageType2D::SizeType indexRadius;
	indexRadius[0] = 5; // radius along x
	indexRadius[1] = 5;	
	indexRadius[2] = 5;

	medianFilter->SetRadius(indexRadius);
	medianFilter->SetInput(imageData);
	medianFilter->Update();


	using WriterType = itk::ImageFileWriter<ImageType3D>;
	WriterType::Pointer writer = WriterType::New();
	writer->SetInput(medianFilter->GetOutput());
	writer->SetFileName("D:\\Median-output.mha");
	writer->SetImageIO(itk::MetaImageIO::New());
	writer->Update();

Am I writing anything wrong?

Thanks

Code looks good. You are working with a very large image, I would suggest trying it with a small image. If it works, then it is possible that the particular implementation of median is inefficient.

1 Like

There are a couple of things to check:

  • Check you are compiling in “Release” mode, as it can be 100x faster than Debug.
  • Make sure you are not running out of memory while the algorithm is running.

You can also check out the RankImageFilter which should be faster as it uses a moving histogram algorithm.

2 Likes

Hi @zivy
The filter is working for smaller volumes.

Hi ,
I am compiling the code in release mode only. While the algorithm is running system has 9 more GB left in available memory.

Thank you for suggesting RankImageFilter . It seems to be working. For me rank Image filter finished in 2 minutes whereas median filter finished in 32 minutes.

1 Like

Some details to think about the computational complexity and the true requirements of the operation you are trying to do:

Thank you for the suggestions. I will go through the paper.

1 Like