What is histogram stretching?

In digital image processing, various enhancement techniques are used to improve the quality and visual appearance of images. One of these techniques is histogram stretching.

Histogram stretching in digital image processing

Histogram stretching is a simple yet effective technique to increase the contrast of an image and enhance its visual appearance. This enhancement is achieved by redistributing the intensity values of the image's pixels.

A histogram represents the distribution of pixel intensities across the image. Similarly, histogram stretching aims toward maximizing an image's dynamic range by stretching the histogram across the entire intensity spectrum. It changes the intensity values of an image from minimum to maximum, in turn signifying the contrast of an image.

Mathematical implementation

Histogram stretching is basically achieved through the following mathematical formula.

Mathematical representation of Histogram stretching
Mathematical representation of Histogram stretching

This equation takes an input image pixel I(r,c) and rescales it by using the above-mentioned formula. This is why this technique is also called histogram scaling.

  • I(r,c)min is the minimum pixel intensity available in the image.

  • I(r,c)max is the maximum pixel intensity available in the image.

  • Min and Max are the two extreme gray values. For example, for an 8-bit image, these are 0 and 255.

Graphical representation

We can best illustrate the effects of histogram stretching through the following graphs.

Original pixel
Original pixel

After histogram stretching, the histogram attains a shape like this.

Stretched histogram
Stretched histogram

Matlab implementation

We can perform histogram stretching using Matlab and the mathematical formula given below.

function stretched_image = stretch_histogram(image)
% Convert image to grayscale
gray_image = rgb2gray(image);
% Calculate histogram
hist = imhist(gray_image);
% Calculate cumulative distribution function (CDF)
cdf = cumsum(hist) / sum(hist);
% Perform histogram stretching
new_min = min(gray_image(:));
new_max = max(gray_image(:));
stretched_image = uint8((double(gray_image) - new_min) ./ (new_max - new_min) * 255);
end
% Read the image
image = imread('image.jpg');
% Perform histogram stretching
stretched_image = stretch_histogram(image);
% Display the original and stretched images
subplot(1, 2, 1), imshow(image), title('Original');
subplot(1, 2, 2), imshow(stretched_image), title('Stretched');

Explanation

  • The stretch_histogram function takes an input image and returns an output image that has a stretched histogram. The histogram stretching increases the contrast of the input image and provides visual enhancement.

  • Lines 3–6: We convert the image to grayscale using rgb2gray and calculates the histogram using imhist.

  • Line 9: The cumulative distribution function (CDF)The cumulative distribution function (CDF) is a statistical function that provides the probability that a random variable takes on a value less than or equal to a given value. is obtained by dividing the cumulative sum of the histogram by the sum of the histogram values.

  • Lines 11–15: The minimum and maximum intensity values of the image are determined, and the stretching formula is applied to each pixel to map it to the new intensity range.

  • Lines 17–25: We read the image.jpg file using imread function and call the stretched_image function. Lastly, we plot the original and stretched images.

Conclusion

Histogram stretching is a valuable technique in digital image processing that allows us to enhance the contrast and improve the visual quality of images. By redistributing the intensity values and expanding the dynamic range, histogram stretching reveals previously hidden details and makes images more visually appealing.

Free Resources

Copyright ©2025 Educative, Inc. All rights reserved