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.
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)minis the minimum pixel intensity available in the image.I(r,c)maxis the maximum pixel intensity available in the image.MinandMaxare 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.
After histogram stretching, the histogram attains a shape like this.
Matlab implementation
We can perform histogram stretching using Matlab and the mathematical formula given below.
function stretched_image = stretch_histogram(image)% Convert image to grayscalegray_image = rgb2gray(image);% Calculate histogramhist = imhist(gray_image);% Calculate cumulative distribution function (CDF)cdf = cumsum(hist) / sum(hist);% Perform histogram stretchingnew_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 imageimage = imread('image.jpg');% Perform histogram stretchingstretched_image = stretch_histogram(image);% Display the original and stretched imagessubplot(1, 2, 1), imshow(image), title('Original');subplot(1, 2, 2), imshow(stretched_image), title('Stretched');
Explanation
The
stretch_histogramfunction 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
rgb2grayand calculates the histogram usingimhist.Line 9: The
is obtained by dividing the cumulative sum of the histogram by the sum of the histogram values.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. 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.jpgfile usingimreadfunction and call thestretched_imagefunction. 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