Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ENH: Histogram equalization #539

Merged
merged 1 commit into from
Feb 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions ants/utils/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
from .get_neighborhood import (get_neighborhood_in_mask,
get_neighborhood_at_voxel)
from .histogram_match_image import histogram_match_image
from .histogram_equalize_image import histogram_equalize_image
from .hausdorff_distance import hausdorff_distance
from .image_similarity import image_similarity
from .image_to_cluster_images import image_to_cluster_images
Expand Down
38 changes: 38 additions & 0 deletions ants/utils/histogram_equalize_image.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
__all__ = ['histogram_equalize_image']

import numpy as np

from .. import core

def histogram_equalize_image(image, number_of_histogram_bins=256):
"""
Histogram equalize image

# from http://www.janeriksolem.net/histogram-equalization-with-python-and.html

Arguments
---------
image : ANTsImage
source image

number_of_histogram_bins : integer
number of bins for cumulative histogram

Example
-------
>>> import ants
>>> src_img = ants.image_read(ants.get_data('r16'))
>>> src_img_eq = ants.histogram_equalize_image(src_img)
"""

image_array = image.numpy()
image_histogram, bins = np.histogram(image_array.flatten(), number_of_histogram_bins, density=True)
cdf = image_histogram.cumsum()
cdf = (number_of_histogram_bins-1) * cdf / cdf[-1]
image_array_equalized_flat = np.interp(image_array.flatten(), bins[:-1], cdf)
image_array_equalized = image_array_equalized_flat.reshape(image_array.shape)
image_equalized = core.from_numpy(image_array_equalized)

return core.copy_image_info(image, image_equalized)


Loading