Histogram Equalization in Image Processing is a technique which is mainly used to enhance the contrast of images. It works by adjusting the intensity distribution of an image to make it more uniform, leading to better visualization and interpretation of images, especially in areas of low contrast.

In this article, we will learn how histogram equalization works, its mathematical background, and how to implement it in practice.

Intensity Transformation

Intensity transformation in image processing refers to changing the pixel values (intensities) of an image to achieve a specific goal, such as increasing contrast or brightness. These transformations are applied to enhance the visual quality of images. Common intensity transformations can be linear or non-linear, and they operate directly on the pixel values of the given image.

For example, if an image appears too dark, you can apply a transformation to increase its brightness by adjusting the intensity levels.

Common Intensity Transformation Functions

There are several types of intensity transformation functions commonly used in image processing:

Linear Transformation: This increases or decreases the pixel values by a constant factor. Itâ€™s simple but may not always result in good contrast improvement. Example:

(x)=aâ‹…x+b, where a and b are constants.

Logarithmic Transformation: It compresses the dynamic range of an image, often used when you want to bring out details in dark areas.

Example:

f(x)=câ‹…log(1+x), where c is a constant.

Power-Law (Gamma) Transformation: This function can either enhance or reduce the contrast of an image based on the value of the gamma parameter. Example:

f(x)=câ‹…xÎ³, where Î³ controls the nature of the transformation.

Histogram Equalization: A non-linear transformation that improves the contrast of an image by redistributing its intensity values to achieve a uniform histogram.

Lets understand how Histogram Equalization works in detail:

Histogram Equalization

Histogram Equalization is a specific intensity transformation technique used to enhance the contrast of an image. It works by spreading out the most frequent intensity values over a wider range, making low-contrast images look more detailed and clearer. This technique is especially useful in images where pixel intensity values are clustered within a narrow range, such as in medical or satellite imagery.

How Does Histogram Equalization Work?

Calculate the Histogram: First, you calculate the histogram of the image, which shows the distribution of pixel intensities.

Create a Cumulative Distribution Function (CDF): The CDF is then computed from the histogram, which helps to spread the pixel values.

Apply the Transformation: Finally, the pixel intensities of the image are mapped using the CDF to achieve a more balanced contrast.

Python implementation to perform histogram equalization using OpenCV:

Python

Python

import cv2 import numpy as np from matplotlib import pyplot as plt import urllib.request

# URL of a chessboard image from the OpenCV GitHub repository url = 'https://raw.githubusercontent.com/opencv/opencv/master/samples/data/left01.jpg'

# Download the image from the URL resp = urllib.request.urlopen(url) image_array = np.asarray(bytearray(resp.read()), dtype=np.uint8)

# Convert the image to grayscale image = cv2.imdecode(image_array, cv2.IMREAD_GRAYSCALE)

The output of the chessboard code will display two images side by side:

Left Image: The original grayscale image of a chessboard.

Right Image: The equalized image of the same chessboard, where the contrast has been enhanced using histogram equalization.

In the equalized image, you will observe a noticeable improvement in contrast. Darker areas will appear brighter, and brighter areas may appear slightly darker, making the overall image clearer. This is particularly helpful for highlighting details that may be hard to distinguish in the original image due to low contrast.

Time Complexity

Calculating the histogram: The algorithm first needs to compute the histogram of pixel intensities, which takes O(N) time, where N is the total number of pixels in the image.

Applying the transformation: Once the histogram is computed, the transformation function is applied to adjust the intensity levels across all pixels. This step also takes O(N) time.

Thus, the overall time complexity is O(N).

Space Complexity

The space complexity depends on:

L, the number of intensity levels in the image, typically 256 for an 8-bit grayscale image.

A small, constant amount of space is needed to store the histogram, which is an array of size L.

Thus, the space complexity is O(L), where L = 256 for grayscale images.

In summary:

Time Complexity: O(N), where N is the number of pixels.

Space Complexity: O(L), where L is the number of intensity levels (usually 256 for grayscale images).

Mathematical Derivation of Transformation Function for Histogram Equalization

The transformation function used in histogram equalization is derived using the cumulative distribution function (CDF). Let's break this down:

Calculate the Probability Distribution

Let n be the number of pixels in the image.

h(i) is the number of pixels with intensity i, so the probability of intensity i is:

Cumulative Distribution Function (CDF)

The CDF at intensity i is:

Transformation Function

The transformation function T(i) maps the original intensity to the new one, where L is the total number of intensity levels (usually 256 for grayscale images).

Steps Involved in Histogram Equalization

Compute Histogram: First, calculate the histogram of the image. A histogram is a graphical representation of the distribution of data points. In the context of an image, it represents the frequency of occurrence of each intensity level.

Calculate Cumulative Distribution Function (CDF): Transform the histogram into a cumulative histogram. This is essentially the cumulative distribution function (CDF) of the pixel intensities.

Normalize the CDF: The CDF must be normalized so that the range is between the minimum and maximum possible intensity values of the image. This scaling ensures that the output image has the appropriate brightness.

Create Equalized Histogram: Use the normalized CDF to map the intensity levels of the original image to new ones, which spreads the intensities more uniformly across the available range. This remapping is done by replacing each pixel in the input image with its corresponding value in the normalized CDF.

Generate Output Image: The final image is constructed using the new intensity values provided by the equalized histogram. This image will show enhanced contrast if the original image had a limited dynamic range.

Algorithm Overview

1. Compute the histogram (H) of the input image.

2. Compute the cumulative sum of the histogram to get the CDF: CDF(v) = sum(H(u) for u = 0 to v)

3. Normalize the CDF so that it ranges from 0 to the maximum pixel value: CDF_normalized = (CDF - CDF_min) * (Max_pixel_value / (Total_number_of_pixels - CDF_min))

4. Remap the pixel values of the original image using the normalized CDF to get the new pixel values.

5. Construct the output image using the new pixel values, resulting in enhanced contrast.

This process effectively redistributes the image's pixels in terms of intensity, leading to a generally better visualization of details and textures in the image due to the increased dynamic range. This technique is particularly useful in improving the quality of medical images, satellite images, and photographs taken in poor lighting conditions.

Example

Letâ€™s assume an image has only four intensity levels (0, 1, 2, 3) with pixel distribution as follows:

0: 40 pixels

1: 30 pixels

2: 20 pixels

3: 10 pixels

Calculate the Probability

Calculate the CDF

Apply the Transformation

The new pixel values after histogram equalization would be 1, 2, 3, and 3.

Implementation

Letâ€™s implement this example using Python to show how histogram equalization works on a small dataset:

This demonstrates the pixel values after applying histogram equalization.

Frequently Asked Questions

What is histogram equalization used for in image processing?

Histogram equalization is used to improve the contrast of an image by redistributing the intensity values, making features more distinguishable.

What is the time complexity of histogram equalization?

The time complexity of histogram equalization is O(N), where N is the number of pixels in the image.

Can histogram equalization be applied to color images?

Yes, histogram equalization can be applied to color images by equalizing each channel (R, G, B) individually or by converting the image to grayscale.

Conclusion

In this article, we have explored Histogram Equalization in Image Processing which is key technique for enhancing image contrast. We discussed its mathematical foundation and provided a Python code implementation to demonstrate how the technique works. Understanding histogram equalization is essential for anyone interested in working with image processing, especially for enhancing images for visual and analytical purposes.