(3) intensity transformation and spatial...

25
3-1 ( ( 3 3 ) ) I I n n t t e e n n s s i i t t y y T T r r a a n n s s f f o o r r m m a a t t i i o o n n a a n n d d S S p p a a t t i i a a l l F F i i l l t t e e r r i i n n g g - Intensity transformation Change the intensity of each pixel in order to enhance the image: g(x, y) = T[f(x, y)], where f(x, y): input image, g(x, y): processed image, and T: operator Or: s = T(r), where r: input pixel, and s: output pixel ( ( a a ) ) H H i i s s t t o o g g r r a a m m - Histogram: occurrences of pixels h(r k ) = n k r k : gray level, r 1 = 0, r 2 = 1, … r L = G, k = 1, 2, …, L MATLAB: G = 255 for uint8, 65,536 for uint16, and 1.0 for double n k : number of pixels of gray level r k Normalize:

Upload: others

Post on 03-Aug-2020

31 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: (3) Intensity Transformation and Spatial Filteringweb.nchu.edu.tw/pweb/users/ykchan/lesson/6597.pdf3-1 (3) Intensity Transformation and Spatial Filtering - Intensity transformation

3-1

((33)) IInntteennssiittyy TTrraannssffoorrmmaattiioonn aanndd SSppaattiiaall FFiilltteerriinngg - Intensity transformation

Change the intensity of each pixel in order to enhance the image: g(x, y) = T[f(x, y)],

where f(x, y): input image, g(x, y): processed image, and T: operator Or: s = T(r), where r: input pixel, and s: output pixel

((aa)) HHiissttooggrraamm - Histogram: occurrences of pixels

h(rk) = nk rk: gray level, r1 = 0, r2 = 1, … rL = G, k = 1, 2, …, L MATLAB: G = 255 for uint8, 65,536 for uint16, and 1.0 for double nk: number of pixels of gray level rk Normalize:

Page 2: (3) Intensity Transformation and Spatial Filteringweb.nchu.edu.tw/pweb/users/ykchan/lesson/6597.pdf3-1 (3) Intensity Transformation and Spatial Filtering - Intensity transformation

3-2

p(rk) = nk/n n: total number of pixels May consider p(r) as the pixel’s probability: 0 ≤ p(r) ≤ 1

E.g., Tiffany:

(Pixels concentrate at the high gray levels, low contrast)

Examples of histograms:

Bright object, dark background

r L

p(r)

Dark

Page 3: (3) Intensity Transformation and Spatial Filteringweb.nchu.edu.tw/pweb/users/ykchan/lesson/6597.pdf3-1 (3) Intensity Transformation and Spatial Filtering - Intensity transformation

3-3

- MATLAB imhist function imhist(im): Display histogram h = imhist(im, b): h: histogram, b: number of histogram bins

((bb)) EEnnhhaanncciinngg iimmaaggeess uussiinngg ppooiinntt pprroocceessssiinngg - No change on pixel values

s = T(r) = r s: output pixel value r: input pixel value

Good exposal Bright

s

r

T(r)

L

L 0

Page 4: (3) Intensity Transformation and Spatial Filteringweb.nchu.edu.tw/pweb/users/ykchan/lesson/6597.pdf3-1 (3) Intensity Transformation and Spatial Filtering - Intensity transformation

3-4

- Contrast reversal: negative s = T(r) = L – r

Tiffany

- Contrast stretching: enhance contrast through histogram stretching Underexposed

<

==otherwise. ,

if ,)( 11

L

rrrrL

rTs

Overexposed

s

r

T(r)L

L 0

r1

Page 5: (3) Intensity Transformation and Spatial Filteringweb.nchu.edu.tw/pweb/users/ykchan/lesson/6597.pdf3-1 (3) Intensity Transformation and Spatial Filtering - Intensity transformation

3-5

−−

<== otherwise.,

if ,0)(

1

1

1

1

rLLrr

rLL

rrrTs

Low-contrast, mid-gray-level

<≤−

−−

<

==

otherwise. ,

if ,

if ,0

)( 2112

1

12

1

L

rrrrr

Lrrrr

Lrr

rTs

r1 r2

r1 r2

r1

Page 6: (3) Intensity Transformation and Spatial Filteringweb.nchu.edu.tw/pweb/users/ykchan/lesson/6597.pdf3-1 (3) Intensity Transformation and Spatial Filtering - Intensity transformation

3-6

Most images:

−−

−+−−

<≤−−

−+−−

<

==

otherwise. ,)(

if ,)(

if ,

)(

2

222

2

2

2112

1211

12

12

11

1

rLsLrsr

rLsL

rrrrrssrsr

rrss

rrrrs

rTs

Non-linear stretching

L–1 s1 s2

AB

C

L–1r1 r2

AB

C

(r1, s1)

(r2, s2)

Page 7: (3) Intensity Transformation and Spatial Filteringweb.nchu.edu.tw/pweb/users/ykchan/lesson/6597.pdf3-1 (3) Intensity Transformation and Spatial Filtering - Intensity transformation

3-7

- MATLAB imadjust function Syntax: g = imadjust(f, [low_in high_in] [low_out high_out], gamma) Map input gray level range [low_in high_in] to range [low_out high_out] gamma: a real number indicating the mapping curve

* E.g.: Fig. 3.3(a)

# g = imadjust(f, [0 1], [1 0]): Fig. 3.3(b) contrast reversal # g = imadjust(f, [0.5 0.75], [0 1]): Fig. 3.3(c) # g = imadjust(f, [ ], [ ], 2): Fig. 3.3(d)

high_in

gamma < 1

low_in

high_out

low_out

gamma = 1 gamma > 1

Page 8: (3) Intensity Transformation and Spatial Filteringweb.nchu.edu.tw/pweb/users/ykchan/lesson/6597.pdf3-1 (3) Intensity Transformation and Spatial Filtering - Intensity transformation

3-8

Fig. 3.3(a): Original digital mammogram

Fig. 3.3(b): Negative image

Fig. 3.3(c): Expending intensity

Fig. 3.3(d): gamma = 2

((cc)) HHiissttooggrraamm eeqquuaalliizzaattiioonn - Histogram equalization

Stretch histogram such that pixel values cover entire gray level range

Continuous quantities * Assume gray levels are continuous quantities normalized to range [0, 1] * Intensity transformation: s = T(r) * Probability of input pixel: p(r) = nr/N2

Page 9: (3) Intensity Transformation and Spatial Filteringweb.nchu.edu.tw/pweb/users/ykchan/lesson/6597.pdf3-1 (3) Intensity Transformation and Spatial Filtering - Intensity transformation

3-9

* Probability of output pixel: q(s) = ns/N2

* Accumulated number of pixels from gray level 0 to r: Nr = N2∫0

r p(w) dw

(w: dummy variable)

* Define cumulative distribution function: cdf(r) ≡ ∫0

r p(w) dw

* To generate an output image whose gray levels are equally likely, q(s) should be a constant (Q), and ∫0

L Q dw = 1; hence Q = 1/L

Ns = N2∫0

s 1/L dw = N2s/L = N2T(r)/L

Ns = Nr T(r) = LNs/N2 = LNr/N2 = L∫0

r p(w) dw = L × cdf(r)

p(r) q(s)

1/L

s L r

Page 10: (3) Intensity Transformation and Spatial Filteringweb.nchu.edu.tw/pweb/users/ykchan/lesson/6597.pdf3-1 (3) Intensity Transformation and Spatial Filtering - Intensity transformation

3-10

Discrete quantities * Probability of output pixels is not a constant

However, in terms of regions, the number of pixels in a unit region can be constant

Σ0

L Q = 1, ∴ Q = 1/L T(r) = L × cdf(r)

* E.g.: an image with 100 pixels and gray levels: 0 ~ 7 Input gray

level r Number of

pixels p(r) cdf Output gray level s

Gray level s

Number of pixels

0 1 0.01 0.01 7×0.01 = 0 0 4 1 1 0.01 0.02 7×0.02 = 0 1 12 2 1 0.01 0.03 7×0.03 = 0 2 18 3 1 0.01 0.04 7×0.04 = 0 3 0 4 12 0.12 0.16 7×0.16 = 1 4 26 5 18 0.18 0.34 7×0.34 = 2 5 0 6 26 0.26 0.60 7×0.60 = 4 6 0 7 40 0.40 1.00 7×1.00 = 7 7 40

Page 11: (3) Intensity Transformation and Spatial Filteringweb.nchu.edu.tw/pweb/users/ykchan/lesson/6597.pdf3-1 (3) Intensity Transformation and Spatial Filtering - Intensity transformation

3-11

- MATLAB histeq function im1 = histeq(im), e.g., car:

0 1 2 3 4 5 6 7

4026

1812

1 1 1 1

0 1 2 3 4 5 6 7

40

4 1218

26

Page 12: (3) Intensity Transformation and Spatial Filteringweb.nchu.edu.tw/pweb/users/ykchan/lesson/6597.pdf3-1 (3) Intensity Transformation and Spatial Filtering - Intensity transformation

3-12

- Advantages of histogram equalization Optimal contrast Fully automated (adaptive, no human intervention) Image normalization: easy to compare two images taken under different lighting conditions

((dd)) IImmaaggee aavveerraaggiinngg - There is always noise in natural images

Assume noise is added into every pixel of an ideal image: g(x, y) = f(x, y) + η(x, y) g: resulting image, f: ideal image, η: noise

- Image averaging: several images are taken for the same scene, and then take their average Assume Gaussian noise: η ~ N(0, σn

2) Totally M images: gi(x, y) = f(x, y) + ηi(x, y), i = 1, 2, …, M

Page 13: (3) Intensity Transformation and Spatial Filteringweb.nchu.edu.tw/pweb/users/ykchan/lesson/6597.pdf3-1 (3) Intensity Transformation and Spatial Filtering - Intensity transformation

3-13

* Average: ∑=i i yxg

Myxg ),(1),(

* Expected value:

{ } [ ]

+=

= ∑∑∑ i iii i yxyxf

MEyxg

MEyxgE ),(),(1),(1),( η

{ } { }∑∑ +=i ii

yxEM

yxfEM

),(1),(1 η

* { } ),(),(1 yxfyxfEM i

=∑ , independent noise: { } 0),( ≈∑i i yxE η * Hence, ),( yxg ≈ f(x, y) ideal image

ηη σσσσMM gg1 1 22 =→= (Increasing M will reduce the variance of noise)

((ee)) SSppaattiiaall ffiilltteerriinngg - Spatial filtering

Using a mask (filter) to process the image

Page 14: (3) Intensity Transformation and Spatial Filteringweb.nchu.edu.tw/pweb/users/ykchan/lesson/6597.pdf3-1 (3) Intensity Transformation and Spatial Filtering - Intensity transformation

3-14

Mask operation: output pixel value = some algorithm performed on all the pixels in the neighborhood of the corresponding input pixel * Size of neighborhood = size of mask * Mask slides from left to right, top to bottom * Same operation is performed on every pixel * Neighborhood exceeds image boundary: zero padding or replication of

border pixels

Page 15: (3) Intensity Transformation and Spatial Filteringweb.nchu.edu.tw/pweb/users/ykchan/lesson/6597.pdf3-1 (3) Intensity Transformation and Spatial Filtering - Intensity transformation

3-15

* Example:

1 2 3 0.11 0.11 0.11 1.33 2.33 1.784 5 6 * 0.11 0.11 0.11 = 3.00 5.00 3.677 8 9 0.11 0.11 0.11 2.67 4.33 3.11

- Smoothing filter Spatial domain: pixel averaging, blurring Frequency domain: attenuate high frequency components * Smooth area: low frequency * Noise and edge: high frequency Lowpass spatial filtering

Image blurring, noise reduction

* 3×3 and5×5 mean filters:

1 1 1 1 11 1 1 1 1 1 1 11 1 1 1 1 1 1 11 1 1 1 1 1 1 1

× 91

× 251

1 1 1 1 1

Page 16: (3) Intensity Transformation and Spatial Filteringweb.nchu.edu.tw/pweb/users/ykchan/lesson/6597.pdf3-1 (3) Intensity Transformation and Spatial Filtering - Intensity transformation

3-16

* Gaussian smoothing: 222 2)(

221),( σ

πσyxeyxG +−=

σ = 1 5×5 Gaussian mask (σ = 0.5):

0.00000007 0.00002809 0.00020755 0.00002809 0.00000007 0.00002809 0.01133177 0.08373106 0.01133177 0.00002809 0.00020755 0.08373106 0.61869351 0.08373106 0.00020755 0.00002809 0.01133177 0.08373106 0.01133177 0.00002809 0.00000007 0.00002809 0.00020755 0.00002809 0.00000007

MATLAB * w = fspecial('gaussian', 3, 0.5), im1 = imfilter(im, w)

Generate a 3×3 Gaussian filter with σ = 0.5 and filter the image * Filter: 'gaussian', 'sobel', 'prewitt', 'laplacian', 'log', 'average', 'unsharp'

Page 17: (3) Intensity Transformation and Spatial Filteringweb.nchu.edu.tw/pweb/users/ykchan/lesson/6597.pdf3-1 (3) Intensity Transformation and Spatial Filtering - Intensity transformation

3-17

Original image

Result of smoothing

- Sharpening filter Enhance details and edges

Spatial domain: differentiation; frequency domain: attenuate low-frequency components (highpass)

Gradient filter * Gradient: difference between two neighboring points

x direction: ),(),1(),( yxfyxfyxfx

−+=∂∂

y direction: ),()1,(),( yxfyxfyxfy

−+=∂∂

Page 18: (3) Intensity Transformation and Spatial Filteringweb.nchu.edu.tw/pweb/users/ykchan/lesson/6597.pdf3-1 (3) Intensity Transformation and Spatial Filtering - Intensity transformation

3-18

* Gradient vector:

∂∂∂∂

=∇

yfxf

f , magnitude: 2/122

)(mag

∂∂

+

∂∂

=∇=∇yf

xff f

* Roberts cross-gradient operators 1 0 0 1 0 –1 –1 0 g(x, y) = |f(x, y) – f(x+1, y+1)| + | f(x+1, y) – f(x, y+1)|

* Prewitt operators (G = |Gx| + |Gy|) Gx –1 0 1 Gy –1 –1 –1 –1 0 1 0 0 0 –1 0 1 1 1 1

* Sobel operators (G = |Gx| + |Gy|) Gx –1 0 1 Gy –1 –2 –1 –2 0 2 0 0 0 –1 0 1 1 2 1

Page 19: (3) Intensity Transformation and Spatial Filteringweb.nchu.edu.tw/pweb/users/ykchan/lesson/6597.pdf3-1 (3) Intensity Transformation and Spatial Filtering - Intensity transformation

3-19

* Sharpening: g(x, y) = f(x, y) + G(x, y) # Example of Roberts operation:

Input image (F): Gx: 50 70 70 70 70 70 70 70 0 0 0 0 0 0 050 50 70 70 70 70 70 70 0 0 0 0 0 0 050 50 50 70 70 70 70 70 0 0 0 0 0 0 050 50 50 50 70 70 70 70 0 0 0 0 0 0 050 50 50 50 50 70 70 70 0 0 0 0 -20 0 050 50 50 50 50 70 70 70 0 0 0 -20 -20 0 050 50 50 50 70 70 70 70 0 -20 -20 -20 0 0 050 50 70 70 70 70 70 70 -20 -20 0 0 0 0 050 70 70 70 70 70 70 70

Output image = F + Gx + Gy: Gy: 70 90 70 70 70 70 70 70 20 20 0 0 0 0 0 50 70 90 70 70 70 70 70 0 20 20 0 0 0 0 50 50 70 90 70 70 70 70 0 0 20 20 0 0 0 50 50 50 70 90 70 70 70 0 0 0 20 20 0 0 50 50 50 50 50 70 70 70 0 0 0 0 20 0 0 50 50 50 30 30 70 70 70 0 0 0 0 0 0 0 50 30 10 30 70 70 70 70 0 0 -20 0 0 0 0 30 30 70 70 70 70 70 70 0 0 0 0 0 0 0 50 70 70 70 70 70 70 70

Page 20: (3) Intensity Transformation and Spatial Filteringweb.nchu.edu.tw/pweb/users/ykchan/lesson/6597.pdf3-1 (3) Intensity Transformation and Spatial Filtering - Intensity transformation

3-20

* E.g.:

Gx Gy G

Laplacian filter

2

2

2

22 ),(),(),(

yyxf

xyxfyxf

∂∂

+∂

∂=∇

≈ [f(x+1, y) + f(x–1, y) – 2f(x, y)] + [f(x, y+1) + f(x, y–1) – 2f(x, y)]

= f(x+1, y) + f(x–1, y) + f(x, y+1) + f(x, y–1) – 4f(x, y)

* Laplacian mask: 0 1 0 1 −4 1 0 1 0

Page 21: (3) Intensity Transformation and Spatial Filteringweb.nchu.edu.tw/pweb/users/ykchan/lesson/6597.pdf3-1 (3) Intensity Transformation and Spatial Filtering - Intensity transformation

3-21

* Sharpening: g(x, y) = f(x, y) – ∇2f(x, y) * Sharpening mask:

0 0 0 0 1 0 0 −1 0 0 1 0 − 1 −4 1 = −1 5 −1 0 0 0 0 1 0 0 −1 0

* Example of Laplacian operation: Input image (F): G: 50 70 70 70 70 70 70 70 20 -40 0 0 0 0 0 0 50 50 70 70 70 70 70 70 0 40 -40 0 0 0 0 0 50 50 50 70 70 70 70 70 0 0 40 -40 0 0 0 0 50 50 50 50 70 70 70 70 0 0 0 40 -40 0 0 0 50 50 50 50 50 70 70 70 0 0 0 0 40 -20 0 0 50 50 50 50 50 70 70 70 0 0 0 0 40 -20 0 0 50 50 50 50 70 70 70 70 0 0 20 40 -40 0 0 0 50 50 70 70 70 70 70 70 0 40 -40 -20 0 0 0 0 50 70 70 70 70 70 70 70 20 -40 0 0 0 0 0 0

Page 22: (3) Intensity Transformation and Spatial Filteringweb.nchu.edu.tw/pweb/users/ykchan/lesson/6597.pdf3-1 (3) Intensity Transformation and Spatial Filtering - Intensity transformation

3-22

Output image = F – G: 30 110 70 70 70 70 70 7050 10 110 70 70 70 70 7050 50 10 110 70 70 70 7050 50 50 10 110 70 70 7050 50 50 50 10 90 70 7050 50 50 50 10 90 70 7050 50 30 10 110 70 70 7050 10 110 90 70 70 70 7030 110 70 70 70 70 70 70

* Lena:

(a) Lena

(b) Result of Laplacian filtering

(c) Result: = (a) – (b)

Page 23: (3) Intensity Transformation and Spatial Filteringweb.nchu.edu.tw/pweb/users/ykchan/lesson/6597.pdf3-1 (3) Intensity Transformation and Spatial Filtering - Intensity transformation

3-23

* Laplacian filter is very sensitive to noise # Improvement: reduce noise using Gaussian filter before Laplacian

filtering (Laplacian of Gaussian filter, LOG)

[ ] 2

2

2

22 ),(),(

yG

xGyxGyxLOG

∂∂

+∂∂

=∇= ;

∂∂

⋅=

∂∂

=∂∂ +−+− 222222 2)(

222)(

22

2

2

2

21

21 σσ

σπσπσyxyx ex

xe

xxG

−=

⋅−

+−

= +−+−+− 222222222 2)(4

22

22)(

222)(

22 211

21 σσσ

σσ

πσσσσπσyxyxyx exexxe ;

∂∂

⋅=

∂∂

=∂∂ +−+− 222222 2)(

222)(

22

2

2

2

21

21 σσ

σπσπσyxyx ey

ye

yyG

−=

⋅−

+−

= +−+−+− 222222222 2)(4

22

22)(

222)(

22 211

21 σσσ

σσ

πσσσσπσyxyxyx eyeyye ;

Page 24: (3) Intensity Transformation and Spatial Filteringweb.nchu.edu.tw/pweb/users/ykchan/lesson/6597.pdf3-1 (3) Intensity Transformation and Spatial Filtering - Intensity transformation

3-24

0 0 −1 0 0 0 −1 −2 −1 0 −1 −2 16 −2 −1 0 −1 −2 −1 0

−+=∴ +− 222 2)(

4

222

2

22

1),( σ

σσ

πσyxeyxyxLOG , mask:

0 0 −1 0 0 * Matlab: w = fspecial('log')

0.0448 0.0468 0.0564 0.0468 0.0448 0.0468 0.3167 0.7146 0.3167 0.0468 0.0564 0.7146 -4.9048 0.7146 0.0564 0.0468 0.3167 0.7146 0.3167 0.0468 0.0448 0.0468 0.0564 0.0468 0.0448

MATLAB: Fig. 3.17 * w = fspecial('laplacian', 0), im1 = imfilter(im, w, 'replicate');

im2 = im - im1, imshow(im2); - Median filtering

Ordering the pixels under the mask, and replace the output center pixel with the median one (remove extreme values)

Best for filtering the salt-and-pepper noise: g = imnoise(f, 'salt &

Page 25: (3) Intensity Transformation and Spatial Filteringweb.nchu.edu.tw/pweb/users/ykchan/lesson/6597.pdf3-1 (3) Intensity Transformation and Spatial Filtering - Intensity transformation

3-25

pepper')

Salt-and-pepper noise

Result of median filtering

MATLAB, e.g.: * im1 = medfilt2(im) Default: 3×3 mask * im1 = medfilt2(im, [5 5], 'zeros') 5×5 median filter, padded with zeros * im1 = ordfilt2(im, 5, ones(3)) 3×3 ordered filter (median: 5th element) * im1 = ordfilt2(im, median[1:m*n], ones[m n]) m×n ordered filter