avazula
avazula

Reputation: 482

Compute the median value of an image using ITK

I am new to picture processing and especially in using ITK. I would like to compute the Canny filter of a Dicom image (CT scans) and for this, I need to tune the filter with an upper & a lower threshold value. I read there that it is possible to compute the threshold parameters with the median value of the image and statistical assumptions, and that it gives good results in most cases.

The problem is that I struggle with the calculation of this median value. I have tried to obtain it from the histogram computation of my image but this expands my code a lot and I find it difficult for anybody to easily understand what I've done in this part of the project.

Is there any other way to compute the median value of an image in ITK than by computing its histogram?

Thanks in advance for your help.

Upvotes: 1

Views: 1130

Answers (2)

blowekamp
blowekamp

Reputation: 1431

I believe you want to compute some basic image intensity statistics over the whole image. Please look at the itk::StatisticsImageFilter:

https://itk.org/Doxygen/html/classitk_1_1StatisticsImageFilter.html

This is close, it computes the mean and the standard deviation, but not the median.

The statistics filter which operator on label do include the median. The filter is call *itk::LabelStatisticsImageFilter:

https://itk.org/Doxygen/html/classitk_1_1LabelStatisticsImageFilter.html

The LabelStatisticsImageFilter is typically run on a label image. A label image is just an integer image which uses an integer value to represent a segmented object.

In this case we will create a label image which represents the entire image, so that we run the statistics on the entire image. If you copy the image, and use Image::Fill, to make a label image of all 1s you can get the statistics you want.

Here is some SimpleITK Python code to demonstrate the idea:

In [1]: import SimpleITK as sitk

In [2]: img = sitk.Image([10]*2, sitk.sitkUInt8)

In [3]: img = sitk.AdditiveGaussianNoise(img)

In [4]: stats = sitk.LabelStatisticsImageFilter()

In [5]: stats.UseHistogramsOn()

In [6]: onesImage = (img == img)

In [7]: stats.Execute(img+1, onesImage)

In [8]: stats.GetMean(1)
Out[8]: 1.43

In [9]: stats.GetMedian(1)
Out[9]: 1.0

Upvotes: 1

gavinb
gavinb

Reputation: 20038

Sure, there's an example of computing the median value of an image included in ITK's Examples:

You can apply this filter first to compute the median, then use this as an input to the thresholds for the Canny filter.

Upvotes: 0

Related Questions