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 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.
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)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.
We can best illustrate the effects of histogram stretching through the following graphs.
After histogram stretching, the histogram attains a shape like this.
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');
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
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.
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