Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Histogram equalization of grayscale images with NumPy

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)
like image 250
pbu Avatar asked Feb 14 '15 18:02

pbu


People also ask

How to do histogram equalization in Python?

Numpy Histogram() in Python for Equalization. Brighter images have all pixels confined to high values. But good images will have pixels from all regions of the image. To get a good image of a brighter picture. Moreover, it is needed to stretch the histogram of the image to either end. This is what Histogram equalization means in simple terms.

What is the output of the histogram equalized image?

If its input is just grayscale image, then output is our histogram equalized image. If it is colored (RGB) image, we can segregate all three different streams — red, green, blue; call cv2.equalizeHist () individually on these channels and finally merge back, as shown in the code below.

What is histogram equalization in face recognition?

In face recognition techniques, before training the face data, the images of faces are histogram equalized to make them all with same lighting conditions. For starters, convert an image to gray and black & white using the following code.

How to use histogram equalization in OpenCV?

The resultant image from histogram equalization can be seen on the right in (b). We can do this in OpenCV using a function cv2.equalizeHist (). If its input is just grayscale image, then output is our histogram equalized image.


2 Answers

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

For completeness, I give an example 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/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]
like image 145
Trilarion Avatar answered Sep 30 '22 20:09

Trilarion


Very fast and easy way is to use the cumulative distribution function provided by the skimage module. Basically what you do mathematically to proof it.

from skimage import exposure
import numpy as np
def histogram_equalize(img):
    img = rgb2gray(img)
    img_cdf, bin_centers = exposure.cumulative_distribution(img)
    return np.interp(img, bin_centers, img_cdf)
like image 43
Dodo Avatar answered Sep 30 '22 20:09

Dodo