Histogram equalization of grayscale images with NumPy

pbu

How to do histogram equalization for multiple grayscaled images stored in a NumPy array easily?

I have the 96x96 pixel NumPy data in this 4D format:

(1800, 1, 96,96)
Trilarion

Moose's comment which points to this blog entry does the job quite nicely.

For completeness I give an axample here using nicer variable names and a looped execution on 1000 96x96 images which are in a 4D array as in the question. It is fast (1-2 seconds on my computer) and only needs NumPy.

import numpy as np

def image_histogram_equalization(image, number_bins=256):
    # from http://www.janeriksolem.net/2009/06/histogram-equalization-with-python-and.html

    # get image histogram
    image_histogram, bins = np.histogram(image.flatten(), number_bins, density=True)
    cdf = image_histogram.cumsum() # cumulative distribution function
    cdf = 255 * cdf / cdf[-1] # normalize

    # use linear interpolation of cdf to find new pixel values
    image_equalized = np.interp(image.flatten(), bins[:-1], cdf)

    return image_equalized.reshape(image.shape), cdf

if __name__ == '__main__':

    # generate some test data with shape 1000, 1, 96, 96
    data = np.random.rand(1000, 1, 96, 96)

    # loop over them
    data_equalized = np.zeros(data.shape)
    for i in range(data.shape[0]):
        image = data[i, 0, :, :]
        data_equalized[i, 0, :, :] = image_histogram_equalization(image)[0]

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Histogram Equalization Python (No Numpy and No Plotting)

From Dev

How to implement histogram equalization for images in tensorflow?

From Dev

local histogram equalization

From Dev

Histogram Equalization Results

From Dev

Histogram Equalization Results

From Dev

histogram equalization skimage

From Dev

Tensorflow numpy image reshape [grayscale images]

From Dev

Why after histogram equalization (scikit image) and Otsu mahotas method in some images get out big white squares?

From Dev

Histogram Equalization based on HSI model

From Dev

Histogram Equalization yielding unexpected results

From Dev

Tonal equalization on different images

From Dev

Tonal equalization on different images

From Dev

Selective histogram equalization (only on a specified area of the image)

From Dev

Montage of grayscale and colour images

From Dev

MSE calculation for grayscale images

From Dev

Spot segmentation on grayscale images

From Dev

PIL images are saving in grayscale

From Dev

Histogram Equalization before extracting color on a real time video

From Dev

How can I make an histogram equalization using GLSL shader?

From Dev

grayscale gradient with skimage or numpy

From Dev

Erosion/Dilation for binary and grayscale images

From Dev

Applying CSS grayscale property to images

From Dev

Erosion/Dilation for binary and grayscale images

From Dev

Histogram datetime objects in Numpy

From Dev

NumPy - calculate histogram intersection

From Dev

Numpy Histogram - Python

From Dev

Getting a 2D histogram of a grayscale image in Julia

From Dev

How to compare the images not only with histogram?

From Dev

Image processing - TIFF images in Matlab in grayscale

Related Related

HotTag

Archive