Image contrast can be improved using techniques such as histogram equalization, contrast stretching, or applying filters that enhance edges. Additionally, adjusting brightness and using image processing algorithms can enhance the visibility of details in an image.
How to improve the image contrast using histogram equalization
Key takeaways:
Histogram equalization is a technique in image processing used to enhance the contrast of an image by redistributing its intensity values. It helps in making the image clearer and more detailed, improving visibility between intensity levels.
The types of histogram equalization are:
Global histogram equalization
Contrast limited adaptive histogram equalization (CLAHE)
Adaptive histogram equalization (AHE)
It can be used in different applications. like medical imaging, surveillance, machine learning and computer vision.
While histogram equalization improves contrast, it can amplify noise in certain images. More advanced techniques like contrast stretching may be preferred in such cases.
Histogram equalization in image processing enhances the contrast and improves the visual quality of an image. Histogram equalization is to spread out the
It makes the image appear clearer and more detailed, enhancing the differences between different intensity levels.
Types of histogram equalization
There are different variations derived from the primary histogram equalization method. These variations aim to address certain limitations or improve specific aspects of the equalization process. Some of the notable types of histogram equalization include:
Global histogram equalization: It is the standard histogram equalization technique. It operates on the entire image and enhances the global contrast by spreading the intensity values.
Contrast limited adaptive histogram equalization (CLAHE): CLAHE is an adaptive version of histogram equalization. It divides the image into smaller blocks or tiles and applies histogram equalization to each block individually. It avoids
and small intensity variations in flat regions while enhancing local contrast. CLAHE is particularly useful for images with varying lighting conditions.over-amplifying noise When histogram equalization is applied to an image, it stretches the range of pixel values to enhance contrast. If there are minor changes or spikes in pixel values as a result of noise, those variations can also be stretched. This can amplify and bring out noise in the output image, resulting in undesired effects. Adaptive histogram equalization (AHE): Similar to CLAHE, AHE is an adaptive method that works on small regions of an image. However, unlike CLAHE, AHE does not include a
. As a result, AHE can amplify noise in some cases.contrast-limiting mechanism It is a technique used to control the extent to which the contrast of an image is enhanced or adjusted.
Applications
Histogram equalization has different applications in image processing and computer vision due to its ability to enhance the contrast and improve the visual quality of images. Here are some common applications:
Medical imaging: Histogram equalization can improve the visibility of structures in medical images, such as CT scans, X-rays, and MRI images. Poor contrast may obscure essential details.
Enhancement for surveillance: Surveillance cameras capture scenes with varying lighting conditions. Histogram equalization can help improve the visibility of objects and people in well-lit and poorly-lit areas.
Machine Learning and Computer Vision: Machine learning and Computer vision algorithms usually use high-contrast images. Histogram equalization can contribute to better feature extraction and pattern recognition.
Example code
Press "Run" to see how we can improve the image contrast using histogram equalization.
import cv2import numpy as npimport matplotlib.pyplot as pltdef hist_equalization(img):# Convert the input image to grayscalegrayscale = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)# Calculating the histogram of the grayscale imagehist, _ = np.histogram(img, bins=256, range=(0, 256))# Calculating the cumulative distribution function (CDF)cdf = hist.cumsum()cdf_normalized = cdf * 255 / cdf[-1]# Apply histogram equalization using the CDFequalized_img = cdf_normalized[grayscale]# Convert the equalized image back to uint8 typeequalized_img = equalized_img.astype(np.uint8)return equalized_img# Load an image using OpenCVinput_img = cv2.imread('/input.jpeg')# Apply histogram equalizationoutput_img = hist_equalization(input_img)f, axarr = plt.subplots(1,2, figsize=(12, 12))
Explanation
Let's analyze the code step by step:
Line 9: Computes the histogram of the input image array, dividing it into specified
binswith intensityrange, and returns histogram values and bin edges.np.histogram()takes three arguments, that are described below:img, refers to the image whose histogram will be calculated.bin, specifies the number of bins (categories) into which the range of intensity values will be divided.range, defines the range of values that will be considered when calculating the histogram.
Line 12: Computes the histogram's cumulative distribution function (CDF).
Line 13: Normalizes the CDF to a 0-255 scale. It's an essential step to ensure the intensity values remain within the valid range.
Line 16: Uses the normalized CDF to transform the intensity values of the grayscale image, resulting in the equalized image.
Line 19: Converts the data type of the equalized image to unsigned 8-bit integers (uint8).
Line 23: Uses OpenCV's
cv2.imread()function to load an image from the file system into memory.Line 25: Applies histogram equalization to the loaded image (
input_img), transforming its pixel values to enhance the contrast or dynamic range.Line 26: Creates a figure with subplots to display multiple images side by side using
matplotlib.
In summary, this code loads an image, performs histogram equalization on the grayscale version of the image, and displays both the original and equalized images for visual comparison. The equalization process enhances the image's contrast, making it visually appealing.
Conclusion
While histogram equalization is useful, it may not always be suitable for all images and applications. It can amplify noise; in some instances, more advanced techniques like contrast stretching are preferred to address these limitations.
Frequently asked questions
Haven’t found what you were looking for? Contact Us
How can image contrast be improved?
What is the histogram of a good contrast image?
How does histogram equalization make image intensity changes?
What are two ways in which contrast can be improved?
Does histogram equalization increase contrast?
Free Resources