0% found this document useful (0 votes)
243 views46 pages

Lecture3 Contrast Enhancement PDF

The document discusses contrast enhancement techniques for digital images. It introduces various methods for enhancing contrast including linear stretching, nonlinear stretching, histogram equalization, histogram specification, and adaptive histogram modification. These techniques aim to improve contrast by modifying the distribution of pixel intensities to better utilize the available intensity range. The document provides examples of histograms for images with different contrast levels and illustrates how linear stretching can be used to enhance contrast by remapping intensity values.

Uploaded by

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

Lecture3 Contrast Enhancement PDF

The document discusses contrast enhancement techniques for digital images. It introduces various methods for enhancing contrast including linear stretching, nonlinear stretching, histogram equalization, histogram specification, and adaptive histogram modification. These techniques aim to improve contrast by modifying the distribution of pixel intensities to better utilize the available intensity range. The document provides examples of histograms for images with different contrast levels and illustrates how linear stretching can be used to enhance contrast by remapping intensity values.

Uploaded by

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

Contrast Enhancement

Yao Wang
Polytechnic University
University, Brooklyn
Brooklyn, NY 11201

With contribution from Zhu Liu, Onur Guleryuz, and


Gonzalez/Woods, Digital Image Processing, 2ed
Lecture Outline
• Introduction
• Linear stretching
• Nonlinear stretching
• Histogram equalization
• Histogram specification
• Adaptive histogram modification

Yao Wang, NYU-Poly EL5123: Contrast Enhancement 2


What is Contrast Enhancement

Original image with low contrast Enhanced image

Yao Wang, NYU-Poly EL5123: Contrast Enhancement 3


How to enhance the contrast

• Low contrast  image g values concentrated near a


narrow range (mostly dark, or mostly bright, or mostly
medium values)
• Contrast enhancement  change the image value
distribution to cover a wide range
• Contrast of an image can be revealed by its histogram

Yao Wang, NYU-Poly EL5123: Contrast Enhancement 4


Histogram
• Histogram of a monochrome image with L
possible gray levels
levels, f = 0
0, 1
1, …, L-1
L-1.
– P(l) = nl / n,
• nl is the number of pixels with gray level ll.
• n is the total number of pixels in the image.

Yao Wang, NYU-Poly EL5123: Contrast Enhancement 5


Histogram Calculation
function h = histogram(imgname)
% method 2
img = imread(imgname);
img = double(img);
figure; imshow(img);
h = zeros(256
zeros(256,1); 1);
for i=1:M,
% method 1
for j=1:N,
h = zeros(256,1);
f = img(i,j);
for l = 0:255
h(f+1) = h(f+1) + 1;
for i = 1:N,
end
for j = 1:M,
end
if img(i, j) == l,
h(l + 1) = h(l + 1) + 1
1;
% method 3
end
h = zeros(256,1);
end
for l = 0 : 255,
end
h(l + 1)
1)=sum(sum(img
( (i == l))
l));
end
end
figure; bar(h);
Photoshop
otos op has
as eextensive
te s e histogram
stog a ddisplay
sp ay too
tools
s
Matlab: imhist( ): can compute and display histograms

Yao Wang, NYU-Poly EL5123: Contrast Enhancement 6


Histogram vs
vs.
Image Contrast

Images with figure captions in this


and other slides are from
[Gonzalez02]
Examples of Histograms
p(f) p(f) p(f)

f f f

(a) Too dark (b) Too bright (c) Well balanced

Yao Wang, NYU-Poly EL5123: Contrast Enhancement 8


Very Different Images May Have Same Histogram!

Histogram reflects the pixel intensity


distribution, not the spatial distribution!

Yao Wang, NYU-Poly EL5123: Contrast Enhancement 9


Previous Example

Original image with low contrast Enhanced image

Yao Wang, NYU-Poly EL5123: Contrast Enhancement 10


Histograms of Example Images
1600
1600
1400
1400
1200
1200
1000
1000

800 800

600 600

400 400

200 200

0 0

0 50 100 150 200 250 0 50 100 150 200 250

Original girl image with low Enhancement image with


contrast histogram equalization

Yao Wang, NYU-Poly EL5123: Contrast Enhancement 11


How to change the histogram?
Usingg Point-Wise Transformation
• Use a “function” g(f) to generate a new image B
from a given image A via:
B (i, j )  g ( A(i, j )), i  0,..., N  1, j  0,..., M  1
• The function g(f) operates on each image pixel
independently All pixels with original gray level f
independently.
are changed to have gray level g(f)
• Properties that g(f) should satisfy
–MMonotonically
i ll non-decreasing,
d i so that
h relative
l i
brightness of pixels do not change.
– G(f) in the same range as original f, i.e. with same min
(
(e.g. 0) and d max values
l ((e.g. 255)
255), anddb
be iintegers
t
for digital images.
• Rounding/truncation may be needed

Yao Wang, NYU-Poly EL5123: Contrast Enhancement 12


How to Determine the
Transformation Function?
• How to design the transformation function g(f)?
– depends on the histogram of the original image hA(f)
and the desired histogram of the transformed image
hB(f).
– To enhance contrast, we like hB(f) to be as flat as
possible.
• Different approaches
– Using fixed functional forms: linear, non-linear
– Using adaptive transform, that is determined from
hA(f) and hB(f):
• Histogram equalitzation (hB(f) is uniform): Fully automatic!
• Histogram specification or matching

Yao Wang, NYU-Poly EL5123: Contrast Enhancement 13


Linear Stretching
• Enhance the dynamic range by linearly
stretching the original gray levels to the range of
 af  b
0g (tof )255. g(f)
t
s1 s2   t1 t2 
2

g ( f )  ( f  s1 ) * (t 2  t1 ) /( s2  s1 )  t1
a  (t 2  t1 ) /( s2  s1 ), b  t1  s1 * (t 2  t1 ) /( s2  s1 ) t1

s1 s2 f
• Example
– The original
g g
gray
y levels are [[100,, 150].
]
– The target gray levels are [0, 255].
– The
g ( f transformation
)  (( f  100) /function
50) * 255, ffor 100  f  150.

Yao Wang, NYU-Poly EL5123: Contrast Enhancement 14


Illustration of Linear Stretching
p(f) g(f) p(g)

255

f f g
100 150 100 150 255

Linear stretching

Yao Wang, NYU-Poly EL5123: Contrast Enhancement 15


Piecewise Linear Stretching
• K segments
– Starting position of input {fk, k = 0,
0 …KK-1}
1}
– Starting position of output {gk, k = 0, …, K-1}
g
– Transform
T f function
f ti
g4
g3
f  fk
g( f )  * ( g k 1  g k )  g k ;
f k 1  f k
for f k  f  f k 1 , k  0,1,..., K  1. g2

g1
g0 f
f0 f1 f2 f3 f4

Yao Wang, NYU-Poly EL5123: Contrast Enhancement 16


Piece-Wise Linear for Middle Range
Image

Yao Wang, NYU-Poly EL5123: Contrast Enhancement 17


Special Cases
g(f)
• Thresholding (slicing) g1

g0 f T g0
g( f )   f
 g1 f T T
g(f)
• Multilevel Slicing g2
g1
g0
g( f )  gk f k  f  f k 1 f
f0 f1 f2

• The mapping function should be monotonically


non-decreasing, so that the relative brightness
orders of pixels do not change

Yao Wang, NYU-Poly EL5123: Contrast Enhancement 18


Nonlinear Stretching
• Nonlinear functions with a fixed form
• Fewer parameters to adjust
• Satisfying 0  f min  g  f max  L  1
• Examples
– Logarithmic transformation g  b log(af  1)
• Stretch dark region,
g , suppress
pp bright
g region
g
– Exponential transformation g  b(e af  1)
• Expand bright region
– Power Law g  aff k

• K = 2: square law, similar to exponential


• K = 1/3:
/3 cub
cubic
c root,
oot, ssimilar
a to logarithmic
oga t c

Yao Wang, NYU-Poly EL5123: Contrast Enhancement 19


Enhancement of Too-Dark Images

H(f)

g
gmax
New histogram

fmax
H(g)

Original histogram
fmax f

Transformation function:
“log” function: g=c log (1+f)
Or gmax g
Power law: g= c f^r, 0<r<1
Yao Wang, NYU-Poly EL5123: Contrast Enhancement 20
Example of Log Transformation

Eq. (3.2-2)
“log” function:
g=c log (1+f)

Yao Wang, NYU-Poly EL5123: Contrast Enhancement 21


Power Law for Too-Dark Image

Eq. (3.2-3)
Power law:
g= c f^r

Need to boost dark values,


use power law γ<1
(expansion of gray levels)

Yao Wang, NYU-Poly EL5123: Contrast Enhancement 22


Enhancement of Too-Bright Images

H(f)

g
gmax
New histogram

fmax
H(g)

Original histogram
fmax f

Transformation function:
Power law: g=c f^r, r>1
gmax g

Yao Wang, NYU-Poly EL5123: Contrast Enhancement 23


Power Law for Too-Light Image

Eq. (3.2-3)
Power law:
g= c f^r

Need to suppress high


values, use power law  >1
(Compression of gray levels)

Yao Wang, NYU-Poly EL5123: Contrast Enhancement 24


Power Law Transformations

Yao Wang, NYU-Poly EL5123: Contrast Enhancement 25


Enhancement of Images Centered
near the Middle Range

H(f)

g
gmax
New histogram

fmax
H(g)

Original histogram
fmax f

Transformation function

gmax g

Yao Wang, NYU-Poly EL5123: Contrast Enhancement 26


Contrast Enhancement by Gray Level
Transformation

Yao Wang, NYU-Poly EL5123: Contrast Enhancement 27


Histogram Equalization
• Transforms an image with an arbitrary
histogram to one with a flat histogram
– Suppose f has PDF pF(f), 0 ≤ f ≤ 1
– Transform function (continuous version)
f
g ( f )   p F (t )dt
0

– g is
i uniformly
if l di
distributed
t ib t d iin (0
(0, 1)

Histogram
Equalization

Yao Wang, NYU-Poly EL5123: Contrast Enhancement 28


Proof

f
g( f )   f min
p F (t )dt ,

pF ( f )
pG ( g )  , g  (0,1)
dg
df
dg
 pF ( f )
df
pG ( g )  1, g  (0,1)

Yao Wang, NYU-Poly EL5123: Contrast Enhancement 29


Example
 4 f f  (0,1 / 2)
pF ( f )  
4(1  f ) f  (1 / 2,1)
 f
  f  (0,1 / 2)
2
 4 fdf 2 f
g ( f )   1/ 2 f
0

0 4 fdf  1/ 2 4(1  f )df  1  2( f  1) f  (1 / 2,1)


2

pG ( g )  1, g  (0,1)
pF(f) g(f) pG(g)
1

2
1/2
1

0 1/2 1 f 0 1/2 1 f 1 g
0

Yao Wang, NYU-Poly EL5123: Contrast Enhancement 30


Discrete Implementation
• For a discrete image f which takes values
k=0
k=0,…,K-1,
K-1 use ~ l
g (l )  p
k 0
F ( k ), l  0,1,..., K  1.

• To convert the transformed values to the


range of (0, L-1):
 l  
g (l )  round  
 k 00
p F (k )  * ( L  1)
 

– Note: {x} is the rounding of x

Yao Wang, NYU-Poly EL5123: Contrast Enhancement 31


Example
g~l  k 0 pF (k )
l
fk pF(l) g l  [ g~l * 7] pG(l) gk
0 0.19 0.19 [1.33]=1 0 0
1 0.25 0.44 [3.08]=3 0.19 1
2 0.21 0.65 [4.55]=5 0 2
3 0 16
0.16 0 81
0.81 [5 67]=6
[5.67]=6 0 25
0.25 3
4 0.08 0.89 [6.03]=6 0 4
5 0.06 0.95 [6.65]=7 0.21 5
6 0.03 0.98 [6.86]=7 0.16+0.08=0.24 6
7 0.02 1.00 [7]=7 0.06+0.03+0.02=0.11 7
pF(l) pG(l)

l l
0 1 2 3 4 5 6 7 0 1 2 3 4 5 6 7
We don’t get perfectly flat histogram
Yao Wang, NYU-Poly EL5123: with discrete implementation!
Contrast Enhancement 32
Sample Matlab Code
function histogram_eq(inimgname)

iimg=imread(imgname);
i d(i ) %perform
% f mapping
i
figure; imshow(img); for (i=1:M)
[M,N]=size(img); for (j=1:N)
f=double(img(i,j))+1;
histeqimg(i j)=C(f);
histeqimg(i,j)=C(f);
H=imhist(img);
end;
H=H/(M*N);
end;
figure; bar(H);
%note the above loop can be replaced by:
%Computing the mapping function %histeqimg=C(double(img)+1);
for (k=1:256) %this will be much faster!
C(k)=uint8(sum(H(1:k))*255);
end; g ;
figure;
% C = uint8(cumsum(H)*255); imshow(histeqimg);

figure;plot(C);

Yao Wang, NYU-Poly EL5123: Contrast Enhancement 33


Histogram Specification
• What if the desired histogram is not flat?

f Histogram Histogram z
g=s
pF(f) Equalization Equalization
pZ(z)

Specified
pG ( g )  1 PS ( s )  1 Histogram
g  g( f ) s  s( z )
f
  pF ( f )df
z
  pZ ( z )dz
0 0

z  s 1 ( g )  s 1 ( g ( f ))

Yao Wang, NYU-Poly EL5123: Contrast Enhancement 34


Example
g k  i 0 pF (i ) sk  i 0 pZ (i )
k
fk pF(f) k
pZ(k) zk
0 0.19 0.19 0.0 0.0 0
1 0.25 0.44 0.0 0.0 1
2 0.21 0.65 0.0 0.0 2
3 0.16 0.81 0.15 0.15 3
4 0.08 0.89 0.35 0.20 4
5 0 06
0.06 0 95
0.95 0 65
0.65 0 30
0.30 5
6 0.03 0.98 0.85 0.20 6
7 0 02
0.02 1 00
1.00 1 00
1.00 0 15
0.15 7

f 0 1 2 3 4 5 6 7 z 0 1 2 3 4 5 6 7
z 3 4 5 6 6 7 7 7 pZ(z)
( ) 0 0 0 .19
19 .25
25 .21
21 .24
24 .11
11

Yao Wang, NYU-Poly EL5123: Contrast Enhancement 35


Adaptive Histogram Modification
• Local histogram equalization

Yao Wang, NYU-Poly EL5123: Contrast Enhancement 36


Contrast Enhancement for Color Images
• How should we apply the previous
techniques to color images
– To all three color components (RGB or CMY)
separately
– To the Intensity component only while
keeping the Hue and Saturation in the HSI
coordinate
– Can also enhance saturation for more vivid
colors
– Can change individual color components to
add certain tone to an image

Yao Wang, NYU-Poly EL5123: Contrast Enhancement 37


Examples for Color Image (1)

Yao Wang, NYU-Poly EL5123: Contrast Enhancement 38


Examples for Color Image (2)

Yao Wang, NYU-Poly EL5123: Contrast Enhancement 39


Examples for Color Image (3)

Equalize
intensity
Equalize and
intensity enhance
only saturation

Yao Wang, NYU-Poly EL5123: Contrast Enhancement 40


Demo
• Photoshop
– Use “image
image->adjustments
>adjustments
• Try brightness/contrast, curves, equalize
• Matlab
– Imhist, histeq, imadjust, imcontrast
– imadjdemo
i djd

Yao Wang, NYU-Poly EL5123: Contrast Enhancement 41


Summary
• What is image histogram?
• How
Ho to tell whether
hether an an image ha haveea
good contrast from its histogram?
• Given the histogram of an image, can you
sketch a transformation that will likely
improve the image contrast.
• The principle of histogram equalization
• The principle of histogram specification
• Color image enhancement
Yao Wang, NYU-Poly EL5123: Contrast Enhancement 42
Homework
1. Following figure shows the histogram of an image and two transformation
functions. Sketch the histograms of the images obtained by applying the
two functions to the original image.
g2(f)
h(f) g1(f) 255
a 128 128

0 128 255 f 0 128 255 f 0 128 255 f

2. Following figure (next slide) shows the histograms of three different


images, and three possible point functions. For each original image,
which p
point operation
p can best equalize
q its histogram?
g Briefly
y explain
p
your reasoning.

Yao Wang, NYU-Poly EL5123: Contrast Enhancement 43


Homework (cntd)
3. For the histogram h3(f) given in the following figure, determine analytically
the histogram equalizing function, assuming the dynamic range of the
signal f is from 0 to 1 (i.e. replacing 128 by ½, 255 by 1).
4
4. The two 8-level
8 level images have histograms hA=[.1, =[ 1 .2,
2 .3,3 0
0, .1,
1 00, 0
0, .3],
3]
hB=[.1, 0, .2, 0, .4, 0, .3, 0]. Find a point function g(f) that when operating
on image A will produce an image C that has a histogram that is similar
to hB.

h1(f) h2(f) h3(f)


a a a

0 128 255 f 0 64 192 f 0 128 255 f


g1(f) g2(f) g3(f)
255 255 255

128 128 128

0 64 192 255 f 0 128 255 f 0 128 255 f

Yao Wang, NYU-Poly EL5123: Contrast Enhancement 44


Computer Assignment
1. Write a Matlab function that can compute the histogram of a grayscale
image (assuming 256 levels of gray). Try the three possible ways
described in slide 7, and see which one is faster. Finalize your program
with the fastest method.
method In a separate main program,
program apply the program
to a test image, and display the histogram as a stem plot besides the
image (using “subplot” function). You are not allowed to simply use the
“imhist” or “hist” function in Matlab, although you are encouraged to
compare
p yyour results with those obtained using g these functions.
2. Write a Matlab program that performs histogram equalization on a
grayscale image. Your program should: i) compute the histogram of the
input image; ii) compute the histogram equalizing transformation function;
iii) apply the function to the input image; iv) compute the histogram of the
equalized
li d iimage; v)) di
display
l ((and
d print)
i t) th
the original
i i l andd equalized
li d iimages,
as well as their corresponding histograms, all in one figure. You are not
allowed to simply use the “histeq” function in Matlab, although you are
encouraged to compare your results with those obtained using these
functions.
functions
3. Play with the “imadjdemo” program in Matlab, to see the effect of different
choice of the transformation functions on image contrast and brightness.
Write down your observations in your report.

Yao Wang, NYU-Poly EL5123: Contrast Enhancement 45


Readings
• Gonzalez & Woods, “Digital Image Processing”,
Chapter 3 (Section 3.1 – 3.3)
• Jain, “Fundamentals of Digital Image
Processing”,
g , Chapter
p 7 ((Section 7.1 – 7.3))

Yao Wang, NYU-Poly EL5123: Contrast Enhancement 46

You might also like