# Median Filter for 3d data

**URL:** https://discourse.itk.org/t/median-filter-for-3d-data/3376
**Category:** Beginner Questions
**Created:** [August 10, 2020, 8:44am UTC](https://discourse.itk.org/t/median-filter-for-3d-data/3376 "2020-08-10T08:44:59Z")
**Posts on this page:** 9
**Page:** 1

<div class="post-metadata">

### Author: ![Hasna](https://discourse.itk.org/letter_avatar_proxy/v4/letter/h/f07891/32.png) [@Hasna](https://discourse.itk.org/u/Hasna)
#### Post date: [August 10, 2020, 8:44am UTC](https://discourse.itk.org/t/median-filter-for-3d-data/3376/1 "2020-08-10T08:44:59Z")

</div>

Hi all,

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

---

<div class="post-metadata">

### Author: ![zivy](https://discourse.itk.org/user_avatar/discourse.itk.org/zivy/32/1726_2.png) [@zivy](https://discourse.itk.org/u/zivy)
#### Post date: [August 10, 2020, 11:19am UTC](https://discourse.itk.org/t/median-filter-for-3d-data/3376/2 "2020-08-10T11:19:02Z")

</div>

[MedianImageFilter](https://itk.org/Doxygen/html/classitk_1_1MedianImageFilter.html) works in n-D.

---

<div class="post-metadata">

### Author: ![Hasna](https://discourse.itk.org/letter_avatar_proxy/v4/letter/h/f07891/32.png) [@Hasna](https://discourse.itk.org/u/Hasna)
#### Post date: [August 10, 2020, 11:55am UTC](https://discourse.itk.org/t/median-filter-for-3d-data/3376/3 "2020-08-10T11:55:34Z")

</div>

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

---

<div class="post-metadata">

### Author: ![zivy](https://discourse.itk.org/user_avatar/discourse.itk.org/zivy/32/1726_2.png) [@zivy](https://discourse.itk.org/u/zivy)
#### Post date: [August 10, 2020, 12:46pm UTC](https://discourse.itk.org/t/median-filter-for-3d-data/3376/4 "2020-08-10T12:46:44Z")

</div>

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.

---

<div class="post-metadata">

### Author: ![blowekamp](https://discourse.itk.org/user_avatar/discourse.itk.org/blowekamp/32/79_2.png) [@blowekamp](https://discourse.itk.org/u/blowekamp)
#### Post date: [August 10, 2020, 12:58pm UTC](https://discourse.itk.org/t/median-filter-for-3d-data/3376/5 "2020-08-10T12:58:10Z")

</div>

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](https://itk.org/Doxygen/html/classitk_1_1RankImageFilter.html) which should be faster as it uses a moving histogram algorithm.

---

<div class="post-metadata">

### Author: ![Hasna](https://discourse.itk.org/letter_avatar_proxy/v4/letter/h/f07891/32.png) [@Hasna](https://discourse.itk.org/u/Hasna)
#### Post date: [August 11, 2020, 12:06pm UTC](https://discourse.itk.org/t/median-filter-for-3d-data/3376/6 "2020-08-11T12:06:59Z")

</div>

Hi @zivy  
The filter is working for smaller volumes.

---

<div class="post-metadata">

### Author: ![Hasna](https://discourse.itk.org/letter_avatar_proxy/v4/letter/h/f07891/32.png) [@Hasna](https://discourse.itk.org/u/Hasna)
#### Post date: [August 11, 2020, 12:18pm UTC](https://discourse.itk.org/t/median-filter-for-3d-data/3376/7 "2020-08-11T12:18:33Z")

</div>

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.

---

<div class="post-metadata">

### Author: ![blowekamp](https://discourse.itk.org/user_avatar/discourse.itk.org/blowekamp/32/79_2.png) [@blowekamp](https://discourse.itk.org/u/blowekamp)
#### Post date: [August 11, 2020, 12:34pm UTC](https://discourse.itk.org/t/median-filter-for-3d-data/3376/8 "2020-08-11T12:34:06Z")

</div>

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

- Your radius is 5. So the neighborhood has (2\*5+1)^3 = 1331 pixels. Which is a good number of elements to be selecting/sorting to find the middle element. You can read the Insight Journal article of the RankImageFilter to understand the optimization of the computations done: [https://www.insight-journal.org/browse/publication/160](https://www.insight-journal.org/browse/publication/160)
- If your goal is just to denoise perhaps an approximation would be sufficient to further improve performance: [https://itk.org/Doxygen/html/classitk\_1\_1FastApproximateRankImageFilter.html](https://itk.org/Doxygen/html/classitk_1_1FastApproximateRankImageFilter.html)

---

<div class="post-metadata">

### Author: ![Hasna](https://discourse.itk.org/letter_avatar_proxy/v4/letter/h/f07891/32.png) [@Hasna](https://discourse.itk.org/u/Hasna)
#### Post date: [August 12, 2020, 11:45am UTC](https://discourse.itk.org/t/median-filter-for-3d-data/3376/9 "2020-08-12T11:45:13Z")

</div>

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