What is histogram shrinking?
In the world of digital image processing, a number of various techniques are used to enhance the quality and visual appearance of images to modify them according to the user's demands. One of these techniques is histogram shrinking.
Histogram shrinking in digital image processing
Contrary to histogram stretching, histogram shrinking is a powerful method that can significantly improve an image's contrast and dynamic range. This process can bring out the finer details and enhance the aspects of an image that could have been lost due to poor lighting or limited dynamic range.
Mathematical implementation
Here's the mathematical implementation of histogram shrinking.
In this formula:
represents the original pixel value of an image. and showcase the original image's minimum and maximum pixel values. After histogram shrinking, the new maximum and minimum values of the image pixels are represented as
and , respectively.
Graphical representation
We can best illustrate the effects of histogram stretching through the following graphs.
After applying the histogram shrinking procedure, the abovementioned histogram changes into something like this.
Matlab implementation
We can perform histogram shrinking using Matlab through the following function.
function enhancedImage = histogramShrinking(originalImage, minimumValue, maximumValue, newMinValue, newMaxValue)% Compute the histogram of the original imagehistogram = imhist(originalImage);% Compute the cumulative distribution function (CDF)cdf = cumsum(histogram) / numel(originalImage);% Calculate the mapping functionmappingFunction = ((0:255) - minimumValue) / (maximumValue - minimumValue) ...* (newMaxValue - newMinValue) + newMinValue;% Apply the mapping function to the original imageenhancedImage = uint8(mappingFunction(double(originalImage) + 1));% Display the original and enhanced imagesfigure;subplot(2,1,1); imshow(originalImage); title('Original Image');subplot(2,1,2); imshow(enhancedImage); title('Enhanced Image');end
Code explanation
The enhancedImage function takes a couple of parameters like
originalImageminimumValuemaximumValuenewMinValuenewMaxValue
and returns an enhancedImage whose histogram has been manipulated according to the user's inputs.
Lines 3–6: We compute the histogram and the
of the original image usingcumulative distributive function The cumulative distribution function (CDF) is calculated by taking the cumulative sum of the histogram values and dividing it by the total number of pixels in the originalImage. imhist()andcumsum()functions.Lines 8–10: The mapping function is calculated based on the provided
minimumValue,maximumValue,newMinValue, andnewMaxValue. After histogram shrinking, it maps the original pixel values to the desired range of pixel values. This is done by linearly scaling the original pixel values within the range of[minimumValue, maximumValue]to the new range of[newMinValue, newMaxValue].Lines 12–13: The mapping function is applied to
originalImageby first converting it to double precision. The+1is added to ensure that the pixel values fall within the range of the mapping function indices (0-255). The result is then converted back touint8data type to obtain the enhanced image.Lines 15–18: The
enhancedImageis displayed along with theoriginalImagein a figure for visual comparison. Thesubplotfunction creates two subplots: the first subplot displays the original image, and the second displays the enhanced image. Thetitlefunction is used to add titles to the subplots.
% Load the original imageoriginalImage = imread('input_image.jpg');% Define the parameters for histogram shrinkingminimumValue = 0;maximumValue = 255;newMinValue = 0;newMaxValue = 200;% Perform histogram shrinkingenhancedImage = histogramShrinking(originalImage, minimumValue, maximumValue, newMinValue, newMaxValue);
This code will compute the histogram of the original image, calculate the mapping function based on the provided parameters, and then apply the mapping function to the original image. The enhanced image will be displayed for visual comparison.
Conclusion
Whether it's improving contrast, expanding the dynamic range, reducing noise, or adjusting saturation, histogram shrinking offers a versatile approach to enhancing the visual quality of digital images. As image processing technologies evolve, histogram shrinking remains a valuable tool for image enhancement.
Free Resources