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

Yao Wang, NYU-Poly

EL5123: Contrast Enhancement

Enhanced image

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) img = imread(imgname); figure; imshow(img); % method 1 h = zeros(256,1); for l = 0:255 for i = 1:N, for j = 1:M, if img(i, j) == l, h(l + 1) = h(l + 1) + 1 1; end end end end figure; bar(h);

% method 2 img = double(img); h = zeros(256 zeros(256,1); 1); for i=1:M, for j=1:N, f = img(i,j); h(f+1) = h(f+1) + 1; end end % method 3 h = zeros(256,1); for l = 0 : 255, h(l + 1) 1)=sum(sum(img ( (i == l)) l)); end

Photoshop otos op has as e extensive te s e histogram stog a d display 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

(a) Too dark Yao Wang, NYU-Poly

f

(b) Too bright EL5123: Contrast Enhancement

f

(c) Well balanced 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

Yao Wang, NYU-Poly

EL5123: Contrast Enhancement

Enhanced image

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

Original girl image with low contrast

Yao Wang, NYU-Poly

0

50

100

150

200

250

Enhancement image with histogram equalization

EL5123: Contrast Enhancement

11

How to change the histogram? Using g 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 –M Monotonically 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), and db 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 g(f)  af  b 0g (tof )255. 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 )

s1

• Example – – –

t1

f

s2

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

100 150

f

100 150

f

255

g

Linear stretching

Yao Wang, NYU-Poly

EL5123: Contrast Enhancement

15

Piecewise Linear Stretching • K segments – Starting position of input {fk, k = 0, 0 …K K-1} 1} – Starting position of output {gk, k = 0, …, K-1} g – Transform T f function f ti 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.

g4 g3

g2

g1 g0

Yao Wang, NYU-Poly

EL5123: Contrast Enhancement

f0

f1

f2

f3

f

f4 16

Piece-Wise Linear for Middle Range Image

Yao Wang, NYU-Poly

EL5123: Contrast Enhancement

17

Special Cases • Thresholding (slicing) g0 g( f )    g1

f T f T

• Multilevel Slicing g( f )  gk

f k  f  f k 1

g(f) g1 g0 f

T

g(f) g2 g1 g0 f0

f1

f2

f

• 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 • Expand bright region

– Power Law

g  b(e af  1) 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

Transformation function: “log” function: g=c log (1+f) Or Power law: g= c f^r, 0adjustments >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 have ea 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) 255 h(f) g1(f) 128 a 128 0

2.

128

255

f

0

128

255

f

0

128

255

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

f

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). The two 8-level 8 level images have histograms hA=[.1, =[ 1 .2, 2 .3, 3 0 0, .1, 1 0 0, 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.

4 4.

h1(f)

h2(f)

a

h3(f) a

a 0

128

255

f

192

0 64

f

0

g1(f) 255

g2(f) 255

g3(f) 255

128

128

128

0 64 Yao Wang, NYU-Poly

192 255 f

0

128

255

EL5123: Contrast Enhancement

f

0

128

255

f

128

255

f 44

Computer Assignment 1.

2.

3.

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. 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 and d 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 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