0% found this document useful (0 votes)
21 views

Contrast Enhancement

Contrast enhancement techniques improve the visibility of details in images by increasing the difference between pixel intensities. Common methods include histogram equalization, which spreads out intensity values across a wider range for higher contrast, and adaptive contrast enhancement, which adjusts contrast locally based on image properties. Histogram specification matches the histogram of an input image to a target histogram to adjust its contrast and brightness.

Uploaded by

Jayesh Singh
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
21 views

Contrast Enhancement

Contrast enhancement techniques improve the visibility of details in images by increasing the difference between pixel intensities. Common methods include histogram equalization, which spreads out intensity values across a wider range for higher contrast, and adaptive contrast enhancement, which adjusts contrast locally based on image properties. Histogram specification matches the histogram of an input image to a target histogram to adjust its contrast and brightness.

Uploaded by

Jayesh Singh
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 33

Contrast Enhancement

• Contrast enhancement is a digital image processing technique used to


improve the visibility of details in an image by increasing the difference
between the pixel intensities. The goal of contrast enhancement is to
make the image visually more appealing and easier for human perception.
• In digital images, the pixel intensity represents the brightness or color of
each pixel.
• Low contrast images have a limited range of intensity values, resulting in a
flat and dull appearance.
• Conversely, high contrast images have a wide range of intensity values,
leading to a more vibrant and distinct appearance.
various methods for 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

• Consider a 3-bit image of size 4×5 as shown below:

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:

• Find the frequency of each intensity value.


• [1, 6, 3, 2, 3, 2, 1, 2]
Step 3: Calculate the probability density function for
each frequency.

• 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

• For intensity 1, frequency is 6.


• PDF=6/20=0.3
Step 4
• Calculate CDF =cumulative frequency of each
intensity value = sum of all PDF value (<=i)
Step 5
• Multiply CDF with 7.
• Why 7?
• Because we have 3 bit image.
• So, range of intensity values is 0 to 7.
• Multiplication factor is maximum value of
intensity.
Step 6
• Round off the final value of intensity.
Solution

Range Frequency PDF CDF 7*CDF Round-off

0 1 0.0500 0.0500 0.3500 0

1 6 0.3000 0.3500 2.4500 2

2 3 0.1500 0.5000 3.5000 4

3 2 0.1000 0.6000 4.2000 4

4 3 0.1500 0.7500 5.2500 5

5 2 0.1000 0.8500 5.9500 6

6 1 0.0500 0.9000 6.3000 6

7 2 0.1000 1.0000 7.0000 7


Output

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

Old Image New Image


Example 2
• Consider a 3 bit image:
• [[3, 2, 6, 5, 0],
[1, 4, 7, 3, 2],
[0, 5, 1, 6, 4],
[7, 3, 2, 1, 0]]

• Calculate histogram of the image.


• Analyze the histogram.
• Perform histogram equalization.
Program for histogram equalization
# import Opencv
import cv2

# import Numpy
import numpy as np

# read a image using imread


img = cv2.imread(\'F:\\do_nawab.png\', 0)

# creating a Histograms Equalization


# of a image using cv2.equalizeHist()
equ = cv2.equalizeHist(img)

# stacking images side-by-side


res = np.hstack((img, equ))

# show image input vs output


cv2.imshow(\'image\', res)

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

# Reading the image from the present directory


image = cv2.imread("image.jpg")
# Resizing the image for compatibility
image = cv2.resize(image, (500, 600))

# The initial processing of the image


# image = cv2.medianBlur(image, 3)
image_bw = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)

# The declaration of CLAHE


# clipLimit -> Threshold for contrast limiting
clahe = cv2.createCLAHE(clipLimit=5)
final_img = clahe.apply(image_bw) + 30

# Ordinary thresholding the same image


_, ordinary_img = cv2.threshold(image_bw, 155, 255, cv2.THRESH_BINARY)

# Showing the two images


cv2.imshow("ordinary threshold", ordinary_img)
cv2.imshow("CLAHE image", final_img)
Histogram Specification (Matching)
• Histogram specification, also known as histogram matching or
histogram equalization with a specified target, is a technique
used to transform the intensity values of an image so that its
histogram matches the specified target histogram.
• The main goal of histogram specification is to adjust the
contrast and brightness of an image to match a desired
histogram distribution.
Histogram Specification
• Calculate the cumulative distribution function (CDF) of
the input image's histogram.
• Calculate the cumulative distribution function (CDF) of
the target histogram.
• Create a mapping function that maps each intensity level
of the input image to the corresponding intensity level in
the target histogram. This mapping function can be
obtained by matching the CDFs of the input and target
histograms.
• Apply the mapping function to the input image to obtain
the final output image with the desired histogram.
Program: Histogram Specification
# import packages for aa in (ax1, ax2, ax3):
import matplotlib.pyplot as plt aa.set_axis_off()
from skimage import exposure
from skimage.exposure import match_histograms ax1.imshow(image)
import cv2 ax1.set_title('Source')
ax2.imshow(reference)
# reading main image ax2.set_title('Reference')
img1 = cv2.imread("img.jpeg") ax3.imshow(matched)
ax3.set_title('Matched')
# checking the number of channels
plt.tight_layout()
print('No of Channel is: ' + str(img1.ndim))
plt.show()

# reading reference image


fig, axes = plt.subplots(nrows=3, ncols=3, figsize=(8, 8))
img2 = cv2.imread("2Fw13.jpeg")
for i, img in enumerate((image, reference, matched)):
# checking the number of channels
for c, c_color in enumerate(('red', 'green', 'blue')):
print('No of Channel is: ' + str(img2.ndim)) img_hist, bins = exposure.histogram(img[..., c],
source_range='dtype')
image = img1 axes[c, i].plot(bins, img_hist / img_hist.max())
reference = img2 img_cdf, bins = exposure.cumulative_distribution(img[..., c])
axes[c, i].plot(bins, img_cdf)
matched = match_histograms(image, reference , axes[c, 0].set_ylabel(c_color)
multichannel=True)
axes[0, 0].set_title('Source')
axes[0, 1].set_title('Reference')
fig, (ax1, ax2, ax3) = plt.subplots(nrows=1, ncols=3, axes[0, 2].set_title('Matched')
figsize=(8, 3),
sharex=True, sharey=True) plt.tight_layout()
plt.show()
Contrast Stretching
• Contrast stretching is a technique used in image
processing to improve the contrast of an image.
• The goal of contrast stretching is to enhance the
visual appearance of an image by spreading out
the intensity values over a broader range, thus
increasing the difference between the darkest
and brightest parts of the image.
• This process makes the image visually more
appealing and easier to interpret.
Contrast Strecthing
• The main idea behind contrast stretching is to map the
original pixel values to a new range of values using a simple
linear transformation. The process involves two main steps:
– Finding the minimum and maximum pixel values in the original image
(min_val and max_val, respectively).
– Applying the linear transformation to map the pixel values to a new
range (e.g., 0 to 255 for an 8-bit image).

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)

# Define the new range for pixel values (0 to 255)


new_min_val = 0
new_max_val = 255

# Linear transformation to stretch the pixel values


stretched_image = ((image - min_val) * (new_max_val - new_min_val) / (max_val - min_val)).astype(np.uint8)

return stretched_image

# Replace 'image_path' with the path to your image file


image_path = 'path/to/your/image.jpg'

# Read the image using OpenCV


original_image = cv2.imread(image_path, cv2.IMREAD_GRAYSCALE)

# Apply contrast stretching


stretched_image = contrast_stretching(original_image)

# Display the original and contrast-stretched images


cv2.imshow('Original Image', original_image)
cv2.imshow('Contrast Stretched Image', stretched_image)

# Wait for a key press and then close all OpenCV windows
cv2.waitKey(0)
cv2.destroyAllWindows()
Thanks

You might also like