ImageProcessing-SpatialFiltering

download ImageProcessing-SpatialFiltering

of 56

Transcript of ImageProcessing-SpatialFiltering

  • 7/30/2019 ImageProcessing-SpatialFiltering

    1/56

    1of 19

    Digital Image Processing

    Image Enhancement-Spatial Filtering

    From:Digital Image Processing, Chapter 3

  • 7/30/2019 ImageProcessing-SpatialFiltering

    2/56

    2of 19

    Contents

    Next, we will look at spatial filteringtechniques:

    What is spatial filtering? Smoothing Spatial filters. Sharpening Spatial Filters.

    Combining Spatial Enhancement Methods

  • 7/30/2019 ImageProcessing-SpatialFiltering

    3/56

    3of 19

    Neighbourhood Operations

    Neighbourhood operations simply operateon a larger neighbourhood of pixels thanpoint operations

    Neighbourhoods aremostly a rectanglearound a central pixel

    Any size rectangleand any shape filter are possible

    Origin x

    y Image f (x, y)

    (x, y)Neighbourhood

  • 7/30/2019 ImageProcessing-SpatialFiltering

    4/56

    4of 19

    Neighbourhood Operations

    For each pixel in the origin image, theoutcome is written on the same location atthe target image.

    Origin x

    y Image f (x, y)

    (x, y)Neighbourhood

    Target

    Origin

  • 7/30/2019 ImageProcessing-SpatialFiltering

    5/56

  • 7/30/2019 ImageProcessing-SpatialFiltering

    6/56

    6of 19

    The Spatial Filtering Process

    j k l

    m n o

    p q r

    Origin x

    y Image f (x, y)

    e processed = n*e + j*a + k *b + l *c +

    m*d + o*f + p*g + q*h + r *i

    Filter ( w )Simple 3*3

    Neighbourhood

    e

    3*3 Filter

    a b c

    d e f

    g h i

    Original ImagePixels

    *

    The above is repeated for every pixel in theoriginal image to generate the filtered image

  • 7/30/2019 ImageProcessing-SpatialFiltering

    7/56

    7of 19

    Spatial Filtering: Equation Form

    a

    a s

    b

    bt

    t y s x f t sw y x g ),(),(),(

    Filtering can be givenin equation form asshown aboveNotations are basedon the image shownto the left

    I m a g e s

    t a k e n

    f r o m

    G o n z a

    l e z

    & W o o

    d s , D

    i g i t a l I m a g e

    P r o c e s s

    i n g

    ( 2 0 0 2 )

  • 7/30/2019 ImageProcessing-SpatialFiltering

    8/56

    8of 19

    Smoothing Spatial Filters

    One of the simplest spatial filteringoperations we can perform is a smoothingoperation

    Simply average all of the pixels in aneighbourhood around a central value

    Especially usefulin removing noisefrom images

    Also useful for highlighting gross

    detail

    1/9 1/9 1/9

    1/9 1/9 1/9

    1/9 1/9 1/9

    Simpleaveragingfilter

  • 7/30/2019 ImageProcessing-SpatialFiltering

    9/56

    9of 19

    Smoothing Spatial Filtering

    1/9 1/9 1/91/9 1/9 1/91/9 1/9 1/9

    Origin x

    y Image f (x, y)

    e = 1 / 9 *106 +1 / 9 *104 +

    1 / 9 *100 +1 / 9 *108 +

    1

    / 9 *99 +1

    / 9 *98 +1 / 9 *95 +

    1 / 9 *90 +1 / 9 *85

    = 98.3333

    Filter Simple 3*3Neighbourhood

    106

    104

    99

    95

    100 108

    98

    90 85

    1/9 1/9 1/91/9 1/9 1/91/9 1/9 1/9

    3*3 Smoothing Filter

    104 100 108

    99 106 98

    95 90 85

    Original ImagePixels

    *

    The above is repeated for every pixel in theoriginal image to generate the smoothed image

  • 7/30/2019 ImageProcessing-SpatialFiltering

    10/56

    10of 19

    Image Smoothing Example

    The image at the top leftis an original image of size 500*500 pixels

    The subsequent imagesshow the image after filtering with an averaging

    filter of increasing sizes 3, 5, 9, 15 and 35Notice how detail begins

    to disappear I m a g e s

    t a k e n

    f r o m

    G o n z a

    l e z

    & W o o

    d s , D

    i g i t a l I m a g e

    P r o c e s s

    i n g

    ( 2 0 0 2 )

  • 7/30/2019 ImageProcessing-SpatialFiltering

    11/56

    11of 19

    Image Smoothing Example

    I m a g e s

    t a k e n

    f r o m

    G o n z a

    l e z

    & W o o

    d s , D

    i g i t a l I m a g e

    P r o c e s s

    i n g

    ( 2 0 0 2 )

  • 7/30/2019 ImageProcessing-SpatialFiltering

    12/56

    12of 19

    Image Smoothing Example

    I m a g e s

    t a k e n

    f r o m

    G o n z a

    l e z

    & W o o

    d s , D

    i g i t a l I m a g e

    P r o c e s s

    i n g

    ( 2 0 0 2 )

  • 7/30/2019 ImageProcessing-SpatialFiltering

    13/56

    13of 19

    Image Smoothing Example

    I m a g e s

    t a k e n

    f r o m

    G o n z a

    l e z

    & W o o

    d s , D

    i g i t a l I m a g e

    P r o c e s s

    i n g

    ( 2 0 0 2 )

  • 7/30/2019 ImageProcessing-SpatialFiltering

    14/56

    14of 19

    Image Smoothing Example

    I m a g e s

    t a k e n

    f r o m

    G o n z a

    l e z

    & W o o

    d s , D

    i g i t a l I m a g e

    P r o c e s s

    i n g

    ( 2 0 0 2 )

  • 7/30/2019 ImageProcessing-SpatialFiltering

    15/56

    15of 19

    Image Smoothing Example

    I m a g e s

    t a k e n

    f r o m

    G o n z a

    l e z

    & W o o

    d s , D

    i g i t a l I m a g e

    P r o c e s s

    i n g

    ( 2 0 0 2 )

  • 7/30/2019 ImageProcessing-SpatialFiltering

    16/56

    16of 19

    Image Smoothing Example

    I m a g e s

    t a k e n

    f r o m

    G o n z a

    l e z

    & W o o

    d s , D

    i g i t a l I m a g e

    P r o c e s s

    i n g

    ( 2 0 0 2 )

  • 7/30/2019 ImageProcessing-SpatialFiltering

    17/56

    17of 19

    Weighted Smoothing Filters

    More effective smoothing filters can begenerated by allowing different pixels in theneighbourhood different weights in the

    averaging function Pixels closer to the

    central pixel are moreimportant

    Often referred to as aweighted averaging

    1/16 2/16 1/16

    2/16 4/16 2/16

    1/16 2/16 1/16

    Weighted

    averaging filter

  • 7/30/2019 ImageProcessing-SpatialFiltering

    18/56

    18of 19

    Another Smoothing Example

    By smoothing the original image we get ridof lots of the finer detail which leaves onlythe gross features for thresholding

    I m a g e s

    t a k e n

    f r o m

    G o n z a

    l e z

    & W o o

    d s , D

    i g i t a l I m a g e

    P r o c e s s

    i n g

    ( 2 0 0 2 )

    Original Image Smoothed Image Thresholded Image

    * Image taken from Hubble Space Telescope

  • 7/30/2019 ImageProcessing-SpatialFiltering

    19/56

  • 7/30/2019 ImageProcessing-SpatialFiltering

    20/56

    20of 19

    Averaging Filter Vs. Median Filter Example

    I m a g e s

    t a k e n

    f r o m

    G o n z a

    l e z

    & W o o

    d s , D

    i g i t a l I m a g e

    P r o c e s s

    i n g

    ( 2 0 0 2 )

    Original

  • 7/30/2019 ImageProcessing-SpatialFiltering

    21/56

    21of 19

    Averaging Filter Vs. Median Filter Example

    I m a g e s

    t a k e n

    f r o m

    G o n z a

    l e z

    & W o o

    d s , D

    i g i t a l I m a g e

    P r o c e s s

    i n g

    ( 2 0 0 2 )

    AveragingFilter

  • 7/30/2019 ImageProcessing-SpatialFiltering

    22/56

    22of 19

    Averaging Filter Vs. Median Filter Example

    I m a g e s

    t a k e n

    f r o m

    G o n z a

    l e z

    & W o o

    d s , D

    i g i t a l I m a g e

    P r o c e s s

    i n g

    ( 2 0 0 2 )

    MedianFilter

  • 7/30/2019 ImageProcessing-SpatialFiltering

    23/56

    23of 19

    Strange Things Happen At The Edges!

    Origin x

    y Image f (x, y)

    e

    e

    e

    e

    At the edges of an image we are missingpixels to form a neighbourhood

    e e

    e

  • 7/30/2019 ImageProcessing-SpatialFiltering

    24/56

    24of 19

    Strange Things Happen At The Edges!(cont)

    There are a few approaches to dealing withmissing edge pixels:

    Omit missing pixels

    Only works with some filters Can add extra code and slow down processing

    Pad the image Typically with either all white or all black pixels

    Replicate border pixels Truncate the image

  • 7/30/2019 ImageProcessing-SpatialFiltering

    25/56

    25of 19

    Correlation & Convolution

    The filtering we have been talking about sofar is referred to as correlation with the filter itself referred to as the correlation kernel

    Convolution is a similar operation, with justone subtle difference

    For symmetric filters it makes no difference

    e processed = v*e +

    z *a + y*b + x*c +w*d + u*e +t *f + s*g + r *h

    r s t

    u v w x y z

    Filter

    a b c

    d e e f g h

    Original ImagePixels

    *

  • 7/30/2019 ImageProcessing-SpatialFiltering

    26/56

    26of 19

    Sharpening Spatial Filters

    Previously we have looked at smoothingfilters which remove fine detailSharpening spatial filters seek to highlight

    fine detail Remove blurring from images Highlight edges

    Sharpening filters are based on spatial differentiation

  • 7/30/2019 ImageProcessing-SpatialFiltering

    27/56

    27of 19

    Spatial Differentiation

    Differentiation measures the rate of change of a functionLets consider a simple 1 dimensional

    example

    I m a g e s

    t a k e n

    f r o m

    G o n z a

    l e z

    & W o o

    d s , D

    i g i t a l I m a g e

    P r o c e s s

    i n g

    ( 2 0 0 2 )

  • 7/30/2019 ImageProcessing-SpatialFiltering

    28/56

    28of 19

    Spatial Differentiation

    I m a g e s

    t a k e n

    f r o m

    G o n z a

    l e z

    & W o o

    d s , D

    i g i t a l I m a g e

    P r o c e s s

    i n g

    ( 2 0 0 2 )

    A B

  • 7/30/2019 ImageProcessing-SpatialFiltering

    29/56

    29of 19

    1 st Derivative

    The formula for the 1 st derivative of afunction is as follows:

    Its just the difference between subsequentvalues and measures the rate of change of

    the function

    )()1( x f x f x

    f

  • 7/30/2019 ImageProcessing-SpatialFiltering

    30/56

    30of 19

    1 st Derivative (cont)

    01

    2

    3

    4

    5

    6

    7

    8

    -8

    -6

    -4

    -2

    0

    2

    4

    6

    8

    5 5 4 3 2 1 0 0 0 6 0 0 0 0 1 3 1 0 0 0 0 7 7 7 7

    0 -1 -1 -1 -1 0 0 6 -6 0 0 0 1 2 -2 -1 0 0 0 7 0 0 0

    f(x)

    f(x)

  • 7/30/2019 ImageProcessing-SpatialFiltering

    31/56

    31of 19

    2nd Derivative

    The formula for the 2 nd derivative of afunction is as follows:

    Simply takes into account the values bothbefore and after the current value

    )(2)1()1(2

    2

    x f x f x f x

    f

  • 7/30/2019 ImageProcessing-SpatialFiltering

    32/56

    32of 19

    2nd Derivative (cont)

    01

    2

    3

    4

    5

    6

    7

    8

    5 5 4 3 2 1 0 0 0 6 0 0 0 0 1 3 1 0 0 0 0 7 7 7 7

    -15

    -10

    -5

    0

    5

    10

    -1 0 0 0 0 1 0 6 -12 6 0 0 1 1 -4 1 1 0 0 7 -7 0 0

    f(x)

    f(x)

  • 7/30/2019 ImageProcessing-SpatialFiltering

    33/56

    33of 19

    1 st and 2 nd Derivative

    0

    1

    2

    3

    4

    5

    6

    78

    -8

    -6

    -4

    -2

    0

    2

    4

    6

    8

    -15

    -10

    -5

    0

    5

    10

    f(x)

    f(x)

    f(x)

    U i S d D i i F I

  • 7/30/2019 ImageProcessing-SpatialFiltering

    34/56

    34of 19

    Using Second Derivatives For ImageEnhancement

    The 2nd

    derivative is more useful for imageenhancement than the 1 st derivative Stronger response to fine detail Simpler implementation We will come back to the 1 st order derivative

    later onThe first sharpening filter we will look at is

    the Laplacian Isotropic One of the simplest sharpening filters We will look at a digital implementation

  • 7/30/2019 ImageProcessing-SpatialFiltering

    35/56

    35of 19

    The Laplacian

    The Laplacian is defined as follows:

    where the partial 1 st order derivative in the x direction is defined as follows:

    and in the y direction as follows:

    y f

    x f

    f 22

    2

    22

    ),(2),1(),1(2

    2

    y x f y x f y x f x

    f

    ),(2)1,()1,(2

    2

    y x f y x f y x f y

    f

  • 7/30/2019 ImageProcessing-SpatialFiltering

    36/56

    36of 19

    The Laplacian (cont)

    So, the Laplacian can be given as follows:

    We can easily build a filter based on this

    ),1(),1([2 y x f y x f f )]1,()1,( y x f y x f

    ),(4 y x f

    0 1 0

    1 -4 1

    0 1 0

    37

  • 7/30/2019 ImageProcessing-SpatialFiltering

    37/56

    37of 19

    The Laplacian (cont)

    Applying the Laplacian to an image we get anew image that highlights edges and other discontinuities

    I m a g e s

    t a k e n

    f r o m

    G o n z a

    l e z

    & W o o

    d s , D

    i g i t a l I m a g e

    P r o c e s s

    i n g

    ( 2 0 0 2 )

    OriginalImage

    LaplacianFiltered Image

    LaplacianFiltered Image

    Scaled for Display

    38

  • 7/30/2019 ImageProcessing-SpatialFiltering

    38/56

    38of 19

    But That Is Not Very Enhanced!

    The result of a Laplacian filteringis not an enhanced imageWe have to do more work in

    order to get our final imageSubtract the Laplacian resultfrom the original image to

    generate our final sharpenedenhanced image

    LaplacianFiltered Image

    Scaled for Display

    I m

    a g e s

    t a k e n

    f r o m

    G o n z a

    l e z

    & W o o

    d s , D

    i g i t a l I m a g e

    P r o c e s s

    i n g

    ( 2 0 0 2 )

    f y x f y x g 2),(),(

    39

  • 7/30/2019 ImageProcessing-SpatialFiltering

    39/56

    39of 19

    Laplacian Image Enhancement

    In the final sharpened image edges and finedetail are much more obvious

    I m

    a g e s

    t a k e n

    f r o m

    G o n z a

    l e z

    & W o o

    d s , D

    i g i t a l I m a g e

    P r o c e s s

    i n g

    ( 2 0 0 2 )

    - =

    OriginalImage

    LaplacianFiltered Image

    SharpenedImage

    40

  • 7/30/2019 ImageProcessing-SpatialFiltering

    40/56

    40of 19

    Laplacian Image Enhancement

    I m

    a g e s

    t a k e n

    f r o m

    G o n z a

    l e z

    & W o o

    d s , D

    i g i t a l I m a g e

    P r o c e s s

    i n g

    ( 2 0 0 2 )

    41

  • 7/30/2019 ImageProcessing-SpatialFiltering

    41/56

    41of 19

    Simplified Image Enhancement

    The entire enhancement can be combinedinto a single filtering operation

    ),1(),1([),( y x f y x f y x f )1,()1,( y x f y x f

    )],(4 y x f

    f y x f y x g 2),(),(

    ),1(),1(),(5 y x f y x f y x f )1,()1,( y x f y x f

    42

  • 7/30/2019 ImageProcessing-SpatialFiltering

    42/56

    42of 19

    Simplified Image Enhancement (cont)

    This gives us a new filter which does thewhole job for us in one step

    0 -1 0

    -1 5 -1

    0 -1 0

    I m

    a g e s

    t a k e n

    f r o m

    G o n z a

    l e z

    & W o o

    d s , D

    i g i t a l I m a g e

    P r o c e s s

    i n g

    ( 2 0 0 2 )

    43

  • 7/30/2019 ImageProcessing-SpatialFiltering

    43/56

    43of 19

    Simplified Image Enhancement (cont)

    I m

    a g e s

    t a k e n

    f r o m

    G o n z a

    l e z

    & W o o

    d s , D

    i g i t a l I m a g e

    P r o c e s s

    i n g

    ( 2 0 0 2 )

  • 7/30/2019 ImageProcessing-SpatialFiltering

    44/56

    45

  • 7/30/2019 ImageProcessing-SpatialFiltering

    45/56

    45of 19

    Unsharp Mask & Highboost Filtering

    Using sequence of linear spatial filters inorder to get Sharpening effect.

    -Blur - Subtract from original image- add resulting mask to original image

    46

  • 7/30/2019 ImageProcessing-SpatialFiltering

    46/56

    46of 19

    Highboost Filtering

    47

  • 7/30/2019 ImageProcessing-SpatialFiltering

    47/56

    47of 19

    1 st Derivative Filtering

    Implementing 1 st derivative filters is difficult inpracticeFor a function f(x, y) the gradient of f at

    coordinates (x, y) is given as the columnvector:

    y f x

    f

    G

    G

    y

    xf

    48

  • 7/30/2019 ImageProcessing-SpatialFiltering

    48/56

    48of 19

    1 st Derivative Filtering (cont)

    The magnitude of this vector is given by:

    For practical reasons this can be simplified as:

    )f (mag f

    2122 y x GG2

    122

    y f

    x f

    y x GG f

    49

  • 7/30/2019 ImageProcessing-SpatialFiltering

    49/56

    49of 19

    1 st Derivative Filtering (cont)

    There is some debate as to how best tocalculate these gradients but we will use:

    which is based on these coordinates

    321987 22 z z z z z z f

    741963 22 z z z z z z

    z1 z2 z3z4 z5 z6

    z7 z8 z9

    50

  • 7/30/2019 ImageProcessing-SpatialFiltering

    50/56

    50of 19

    Sobel Operators

    Based on the previous equations we canderive the Sobel Operators

    To filter an image it is filtered using bothoperators the results of which are addedtogether

    -1 -2 -1

    0 0 0

    1 2 1

    -1 0 1

    -2 0 2

    -1 0 1

    51

  • 7/30/2019 ImageProcessing-SpatialFiltering

    51/56

    51of 19

    Sobel Example

    Sobel filters are typically used for edgedetection

    I m

    a g e s

    t a k e n

    f r o m

    G o n z a l e z

    & W o o

    d s , D

    i g i t a l I m a g e

    P r o c e s s

    i n g

    ( 2 0 0 2 ) An image of a

    contact lens whichis enhanced inorder to makedefects (at four and five oclock in

    the image) moreobvious

    52

  • 7/30/2019 ImageProcessing-SpatialFiltering

    52/56

    52of 19

    1 st & 2nd Derivatives

    Comparing the 1 st and 2 nd derivatives wecan conclude the following:

    1 st order derivatives generally produce thicker edges

    2nd order derivatives have a stronger response to fine detail e.g. thin lines

    1 st order derivatives have stronger responseto grey level step

    2nd order derivatives produce a doubleresponse at step changes in grey level

    53 Combining Spatial Enhancement

  • 7/30/2019 ImageProcessing-SpatialFiltering

    53/56

    53of 19

    Combining Spatial EnhancementMethods

    Successful imageenhancement is typically notachieved using a single

    operationRather we combine a rangeof techniques in order toachieve a final resultThis example will focus onenhancing the bone scan tothe right

    I m

    a g e s

    t a k e n

    f r o m

    G o n z a l e z

    & W o o

    d s , D

    i g i t a l I m a g e

    P r o c e s s

    i n g

    ( 2 0 0 2 )

    54 Combining Spatial Enhancement

  • 7/30/2019 ImageProcessing-SpatialFiltering

    54/56

    of 19

    Combining Spatial EnhancementMethods (cont)

    I m

    a g e s

    t a k e n

    f r o m

    G o n z a l e z

    & W o o

    d s , D

    i g i t a l I m a g e

    P r o c e s s

    i n g

    ( 2 0 0 2 )

    Laplacian filter of bone scan (a)

    Sharpened version of bone scan achievedby subtracting (a)and (b) Sobel filter of bone

    scan (a)

    (a)

    (b)

    (c)

    (d)

  • 7/30/2019 ImageProcessing-SpatialFiltering

    55/56

    56 Combining Spatial Enhancement

  • 7/30/2019 ImageProcessing-SpatialFiltering

    56/56

    of 19

    Combining Spatial EnhancementMethods (cont)

    Compare the original and final images

    a g e s

    t a k e n

    f r o m

    G o n z a l e z

    & W o o

    d s , D

    i g i t a l I m a g e

    P r o c e s s

    i n g

    ( 2 0 0 2 )