Contrast Enhancement
Contrast Enhancement
• Histogram Equalization
• Adaptive Contrast Enhancement
• Histogram Specification
• Enhancement using Image Processing Filters:
Histogram Equalization
• Histogram equalization is a popular contrast
enhancement technique used in digital image
processing to improve the visibility of details
in an image by spreading out the intensity
values across a wider range.
• The primary goal of histogram equalization is
to obtain a uniform histogram, which results in
a higher contrast image with enhanced
brightness and detail.
histogram equalization Algorithm
• Find the frequency of each value represented on the horizontal axis of the
histogram i.e. intensity in the case of an image.
• Calculate the probability density function for each intensity value.
• After finding the PDF, calculate the cumulative density function for each intensity’s
frequency.
• The CDF value is in the range 0-1, so we multiply all CDF values by the largest value
of intensity i.e. 255.
• Round off the final values to integer values.
Numerical Example
0 1 1 3 4
7 2 5 5 7
6 3 2 1 1
1 4 4 2 1
Step 1:
• Find the range of intensity values.
• [0, 1, 2, 3, 4, 5, 6, 7]
Step 2:
• total = 20 = 4*5
• Calculate PDF = frequency of each intensity/Total sum of all
frequencies, for each i value of intensity
• For intensity 0, frequency is 1.
• PDF=1/20=0.05
0 1 1 3 4 0 2 2 4 5
7 2 5 5 7 7 4 6 6 7
6 4 4 2 2
6 3 2 1 1
2 5 5 4 2
1 4 4 2 1
# import Numpy
import numpy as np
cv2.waitKey(0)
cv2.destroyAllWindows()
Adaptive Contrast Enhancement
• Adaptive contrast enhancement is a variation of contrast enhancement
techniques that aims to improve the visibility and quality of an image by
adjusting the contrast locally based on the characteristics of the image
content.
• In this approach, the contrast enhancement is applied differently to
different regions of the image, depending on factors such as local intensity
variations, gradients, and other image properties.
• One common method for adaptive contrast enhancement is the adaptive
histogram equalization, particularly the "Contrast Limited Adaptive
Histogram Equalization" (CLAHE) algorithm. CLAHE prevents over-
amplification of noise in flat regions by limiting the contrast amplification
for each local region.
• The general steps for performing Adaptive Histogram Equalization (CLAHE)
are as follows:
– Divide the image into small overlapping tiles or regions.
– Apply histogram equalization to each of these regions independently.
– Limit the contrast amplification in each region to prevent excessive enhancement.
Program for Adaptive histogram equalization
import cv2
import numpy as np
f g
Numerical
Lets consider a 8 bit gray scale image representation:
30 50 80 100
120 150 200 220
• Step 1: Find the minimum and maximum pixel values in the original image.
– Minimum pixel value (min_val) = 30
– Maximum pixel value (max_val) = 220
• Step 2: Apply the linear transformation to stretch the pixel values to a new
range (0 to 255).
• Let's assume we want to set the new_min_val to 0 and new_max_val to 255.
• new_pixel_value = (old_pixel_value - min_val) * (new_max_val -
new_min_val) / (max_val - min_val) + new_min_val
• Applying the formula to each pixel value in the original image:
• new_pixel_value = (30 - 30) * (255 - 0) / (220 - 30) + 0 = 0
• new_pixel_value = (50 - 30) * (255 - 0) / (220 - 30) + 0 = 9
• new_pixel_value = (80 - 30) * (255 - 0) / (220 - 30) + 0 = 72
• new_pixel_value = (100 - 30) * (255 - 0) / (220 - 30) + 0 = 111
• new_pixel_value = (120 - 30) * (255 - 0) / (220 - 30) + 0 = 150
• new_pixel_value = (150 - 30) * (255 - 0) / (220 - 30) + 0 = 208
• new_pixel_value = (200 - 30) * (255 - 0) / (220 - 30) + 0 = 252
• new_pixel_value = (220 - 30) * (255 - 0) / (220 - 30) + 0 = 255
New Image (After Contrast Stretching):
0 9 72 111
150 208 252 255
Program: Contrast Stretching
import cv2
import numpy as np
def contrast_stretching(image):
# Finding the minimum and maximum pixel values in the image
min_val = np.min(image)
max_val = np.max(image)
return stretched_image
# Wait for a key press and then close all OpenCV windows
cv2.waitKey(0)
cv2.destroyAllWindows()
Thanks