Search code examples
pythonitksimpleitk

Histogram equalization with SimpleITK


On this SO answer suggest me this code:

import SimpleITK as sitk
import numpy as np

# Create a noise Gaussian blob test image
img = sitk.GaussianSource(sitk.sitkFloat32, size=[240,240,48], mean=[120,120,24])
img = img + sitk.AdditiveGaussianNoise(img,10)

# Create a ramp image of the same size
h = np.arange(0.0, 255,1.0666666666, dtype='f4')
h2 = np.reshape(np.repeat(h, 240*48), (48,240,240))
himg = sitk.GetImageFromArray(h2)
print(himg.GetSize())

# Match the histogram of the Gaussian image with the ramp
result=sitk.HistogramMatching(img, himg)

# Display the 3d image
import itkwidgets
itkwidgets.view(result)

Why do I need two images to do Histogram equalization?

Because I want to do Histogram Equalization, and this is Histogram Matching. In this article explain the different.


Solution

  • It's a bit of a work-around to achieve histogram equalization through histogram matching.

    'himg' is a ramp image, so the intensities go from 0 to 255. It has all intensities equally represented, so it's histogram is flat.

    So we're matching your image's histogram with a flat histogram. The net result is histogram equalization.