median filtering andmedian filtering and...

of 45/45
Median Filtering and Median Filtering and Morphological Filtering Yao Wang Polytechnic University, Brooklyn, NY 11201 With contribution from Zhu Liu, Onur Guleryuz, and Gonzalez/Woods, Digital Image Processing, 2ed

Post on 04-Apr-2018

227 views

Category:

Documents

2 download

Embed Size (px)

TRANSCRIPT

  • Median Filtering andMedian Filtering and Morphological Filtering

    Yao WangPolytechnic University, Brooklyn, NY 11201

    With contribution from Zhu Liu, Onur Guleryuz, andGonzalez/Woods, Digital Image Processing, 2ed

  • Lecture Outline

    Median filterRank order filter Rank order filter

    Bilevel Morphological filters Dilation and erosion Opening and closing

    Grayscale Morphological filters

    Yao Wang, NYU-Poly EL5123: Non-linear Filtering 2

  • Median Filter Problem with Averaging Filter

    Blur edges and details in an imageBlur edges and details in an image Not effective for impulse noise (Salt-and-pepper)

    Median filter: Taking the median value instead of the average or

    weighted average of pixels in the window Sort all the pixels in an increasing order, take the middle one

    The window shape does not need to be a square Special shapes can preserve line structures Special shapes can preserve line structures

    Yao Wang, NYU-Poly EL5123: Non-linear Filtering 3

  • Median Filter: 3x3 Square Window

    100 100 100 100 100

    100 200 205 203 100 Wi d100 200 205 203 100

    100 195 200 200 100

    100 200 205 195 100

    Windowshape

    100 100 100 100 100

    100 100 100 100 100100 100 100 100 100

    100 100 200 100 100

    100 200 200 200 100

    100 100 195 100 100

    100 100 100 100 100

    Yao Wang, NYU-Poly EL5123: Non-linear Filtering 4

    Matlab command: medfilt2(A,[3 3])

  • Median Filter: 3x3 Cross Window

    100 100 100 100 100

    100 200 205 203 100 Wi d100 200 205 203 100

    100 195 200 200 100

    100 200 205 195 100

    Windowshape

    100 100 100 100 100

    100 100 100 100 100100 100 100 100 100

    100 195 200 200 100

    100 200 200 200 100

    100 195 200 195 100

    100 100 100 100 100

    Yao Wang, NYU-Poly EL5123: Non-linear Filtering 5

    Note that the edges of the center square are better reserved

  • Example

    Yao Wang, NYU-Poly EL5123: Non-linear Filtering 6

  • Rank order filters

    Rank order filtersInstead of taking the mean rank all pixel Instead of taking the mean, rank all pixel values in the window, take the n-th order value.value.

    E.g. max or min or median Properties Properties

    Non-linear T(f1 + f2) T(f1) + T(f2)

    Yao Wang, NYU-Poly EL5123: Non-linear Filtering 7

  • Multi-level Median Filtering To reduce the computation, one can concatenate several

    small median filters to realize a large window operation. When the small windows are designed properly, this

    approach can also help reserve edges better.

    Median

    Yao Wang, NYU-Poly EL5123: Non-linear Filtering 8

  • Hybrid Linear/Median Filter

    One can combine median filters with linear or rank order filtersor rank order filters.

    MED1MINOr

    MED2Or

    MAXOr

    MEANMED3

    Yao Wang, NYU-Poly EL5123: Non-linear Filtering 9

  • Morphological Processing Morphological operations are originally

    developed for bilevel images for shape and structural manipulations.

    Basic functions are dilation and erosion. Concatenation of dilation and erosion in different

    orders result in more high level operations, including closing and openingincluding closing and opening.

    Morphological operations can be used for smoothing or edge detection or extraction of other features.

    Belongs to the category of spatial domain filter.

    Yao Wang, NYU-Poly EL5123: Non-linear Filtering 10

  • Morphological Filters for Bilevel Imagesg

    A binary image can be considered as a setby considering black pixels (with imageby considering black pixels (with image value 1) as elements in the set and white pixels (with value 0) as outside ( )the set.

    Morphological filters are essentially set operations

    Yao Wang, NYU-Poly EL5123: Non-linear Filtering 11

    operations

  • Basic Set Operations Let x, y, z, represent locations of 2D pixels,

    e.g. x = (x1, x2), S denote the complete set of all 1 2pixels in an image, let A, B, represent subsets of S.Union (OR) }{ BABA Union (OR) }:{ BxorAxxBA

    B OR B

    Intersection (AND) }:{ BxandAxxBA

    A A

    AB AND B

    A

    Yao Wang, NYU-Poly EL5123: Non-linear Filtering 12

  • Basic Set Operations

    Complement }:{ AxandSxxA NOT

    T l ti }{)( AAA

    NOTA

    Translation },:{)( AyxyzzA x

    ATranslation

    Reflection },:{ AxxyyA

    A x

    ReflectionAOrigin A

    Origin

    Yao Wang, NYU-Poly EL5123: Non-linear Filtering 13

  • Dilation

    Dilation of set F with a structuring elementH is represented by HF H is represented by HF

    })(:{ FHxHF xwhere represent the empty set.

    is composed of all the points HFG that when shifts its origin to these points, at least one point of is included in F.

    If the origin of H takes value 1, HFF

    Yao Wang, NYU-Poly EL5123: Non-linear Filtering 14

    HFF

  • Example of Dilation (1)

    H, 3x3, origin at the center

    H 5x3 origin

    Yao Wang, NYU-Poly EL5123: Non-linear Filtering 15

    H, 5x3, origin at the center Dilation enlarges a set.

  • Example of Dilation (2)Note that the narrow ridge is closed

    H 3x3 origin at the center

    F G

    Yao Wang, NYU-Poly EL5123: Non-linear Filtering 16

    H, 3x3, origin at the center

  • Erosion

    Erosion of set F with a structuring elementH is represented by and is definedHFH is represented by , and is defined as,

    HF

    })(:{ FHxHF

    is composed of points that

    })(:{ FHxHF x

    HFG when H is translated to these points, every point of H is contained in F.

    If the origin of H takes value of 1, FHF

    Yao Wang, NYU-Poly EL5123: Non-linear Filtering 17

  • Example of Erosion (1)

    H, 3x3, origin at the center

    H 5x3 origin

    Yao Wang, NYU-Poly EL5123: Non-linear Filtering 18

    H, 5x3, origin at the center Erosion shrinks a set

  • Example of Erosion (2)

    H 3x3 origin at the center

    F G

    Yao Wang, NYU-Poly EL5123: Non-linear Filtering 19

    H, 3x3, origin at the center

  • Structuring element

    The shape, size, and orientation of the structuring element depend on applicationstructuring element depend on application.

    A symmetrical one will enlarge or shrink the original set in all directionsthe original set in all directions.

    A vertical one, will only expand or shrink the original set in the vertical direction.

    Yao Wang, NYU-Poly EL5123: Non-linear Filtering 20

  • Properties of Dilation and Erosion OperatorsOperators

    CommunitivityABBAbutABBA ., ABBAbutABBA

    ByxAyyxBxyAyyx

    ByAyyxABxBA xx

    }:{}:{

    })(,,:{})(:{

    ABBAxBzAzzx

    BzAxzzxBzAzxzx

    ByxAyyxBxyAyyx

    xx

    })(:{},)(,:{

    },,:{},,:{

    },,:{},,:{

    Duality

    xx })({},)(,{

    ., BABABABA

    Yao Wang, NYU-Poly EL5123: Non-linear Filtering 21

  • Properties of Dilation and Erosion OperatorsOperators

    Distributivity)()()( CABACBA

    )()()(),()()(

    ),()()(

    CBCACBACABACBA

    CABACBA

    Chain rule

    ).()()( CBCACBA

    )()(),()(

    CBACBACBACBA

    ).()( CBACBA

    Yao Wang, NYU-Poly EL5123: Non-linear Filtering 22

  • Closing and Opening

    ClosingHHFHF )(

    Smooth the contour of an imageHHFHF )(

    Fill small gaps and holes Opening

    Smooth the contour of an imageHHFHF )(

    g Eliminate false touching, thin ridges and

    branches

    Yao Wang, NYU-Poly EL5123: Non-linear Filtering 23

  • Example of Closing

    F HF HHF )(

    H 3x3 origin at the center

    Yao Wang, NYU-Poly EL5123: Non-linear Filtering 24

    H, 3x3, origin at the center

  • Example of Opening

    F HF HHF )(

    H 3x3 origin at the center

    Yao Wang, NYU-Poly EL5123: Non-linear Filtering 25

    H, 3x3, origin at the center

  • Example of Opening and Closing

    Yao Wang, NYU-Poly EL5123: Non-linear Filtering 26

  • Properties of Opening and Closing OperatorsClosing Operators

    Duality

    Idempotence

    ., HFHFHFHF

    Idempotence

    .)(,)( HFHHFHFHHF

    Smoothing

    )(,)(

    Removal of small holes and narrow branches can be accomplished by concatenating opening with closing: HHFG )(

    Yao Wang, NYU-Poly EL5123: Non-linear Filtering 27

    opening with closing: HHFG )(

  • Morphological Filters for Grayscale ImagesImages

    The structure element h is a 2D grayscale image with a finite domain (D ) similar toimage with a finite domain (Dh), similar to a filterThe morphological operations can be The morphological operations can be defined for both continuous and discrete imagesimages.

    Yao Wang, NYU-Poly EL5123: Non-linear Filtering 28

  • Dilation for Grayscale Image Dilation

    }),(,),();,(),(max{),)(( fh DtysxDtstshtysxfyxhf

    Similar to linear convolution, with the max operation replacing the sums of convolution and the addition

    }),(,),();,(),(max{),)(( fh DtysxDtstshtysxfyxhf

    replacing the products of convolution. The dilation chooses the maximum value of f+h in a

    neighborhood of f defined by the domain of hneighborhood of f defined by the domain of h. If all values of h are positive, then the output image

    tends to be brighter than the input, dark details (e.g. g p ( gdark dots/lines in a white background) are either reduced or eliminated.

    Yao Wang, NYU-Poly EL5123: Non-linear Filtering 29

  • Illustration of 1-D Grayscale Dilation});()(max{});()(max{))(( bb DxsxsbxfDxxbxsfsbf

    rrDb ],[rsxrs

    b

    ],[

    A+r

    0-r rWrong result!

    Yao Wang, NYU-Poly EL5123: Non-linear Filtering 30

  • Show correct result (in Red!)

    Yao Wang, NYU-Poly EL5123: Non-linear Filtering 31

  • Erosion for Grayscale Image Erosion

    }),(,),();,(),(min{),)(( fh DtysxDtstshtysxfyxhf

    Similar to linear correlation, with the min operation replacing the sums of correlation and the subtraction

    }),(,),();,(),({),)(( fh yyfyf

    replacing the products of correlation. The erosion chooses the minimum value of f-h in a

    neighborhood of f defined by the domain of hneighborhood of f defined by the domain of h. If all values of h are positive, then the output image

    tends to be darker than the input, brighter details (e.g. p g ( gwhite dots/lines in a dark background) are either reduced or eliminated.

    Yao Wang, NYU-Poly EL5123: Non-linear Filtering 32

  • Illustration of 1-D Grayscale Erosion});()(min{))(( bDxxbxsfshf

    f(s0)-A-rWrong result!

    Yao Wang, NYU-Poly EL5123: Non-linear Filtering 33

    s0s0-r s0+r

  • Show correct result

    Yao Wang, NYU-Poly EL5123: Non-linear Filtering 34

  • Example of Grayscale Dilation and ErosionErosion

    Yao Wang, NYU-Poly EL5123: Non-linear Filtering 35

  • Opening for Grayscale Image

    Openinghhfhf )(

    Geometric interpretationhhfhf )(

    Think f as a surface where the height of each point is determined by its gray level.Thi k th t t l t h l Think the structure element has a gray scale distribution as a half sphere.Opening is the s rface formed b the highest Opening is the surface formed by the highest points reached by the sphere as it rolls over the entire surface of f from underneath

    Yao Wang, NYU-Poly EL5123: Non-linear Filtering 36

    the entire surface of f from underneath.

  • Closing for Grayscale Image

    Closinghhfhf )(

    Geometric interpretationhhfhf )(

    Think f as a surface where the height of each point is determined by its gray level.Thi k th t t l t h l Think the structure element has a gray scale distribution as a half sphere.Closing is the s rface formed b the lo est Closing is the surface formed by the lowest points reached by the sphere as it slides over the surface of f from above

    Yao Wang, NYU-Poly EL5123: Non-linear Filtering 37

    the surface of f from above.

  • Illustration of 1-D Grayscale Opening and ClosingOpening and Closing

    OpeningEliminate false touching,

    ClosingFill ll d h l

    g,thin ridges and branches

    Yao Wang, NYU-Poly EL5123: Non-linear Filtering 38

    Fill small gaps and holes

  • Example of Grayscale Opening and ClosingClosing

    Yao Wang, NYU-Poly EL5123: Non-linear Filtering 39

  • Morphological Operation for Image EnhancementEnhancement

    Morphological smoothing Opening followed by closing, hhf )( Opening followed by closing, Attenuated both bright and dark details

    hhf )(

    Yao Wang, NYU-Poly EL5123: Non-linear Filtering 40

  • Morphological Operation for Image EnhancementEnhancement

    Morphological gradient The difference between the dilated and eroded images,

    )()( hfhf The difference between the dilated and eroded images,

    Valley detection Detect dark text/lines from a gray background

    fhf etect da te t/ es o a g ay bac g ou d

    Boundary detection hff

    Yao Wang, NYU-Poly EL5123: Non-linear Filtering 41

  • Application in Face Detection Use color information to detect candidate face

    regionregion Verify the existence of face in candidate region

    OpeningInputimage

    Skin-tonecolor likelihood

    Opening processedimage

    Blob coloring

    Facedetectionresult

    Yao Wang, NYU-Poly EL5123: Non-linear Filtering 42

  • Written Assignment1. For the image A in Figure 1(b), using the structuring element B in Figure 1(a), determine the

    closing and opening of A by B.

    origin

    2. Consider a contiguous image function f and a gray scale structuring element h described by

    Figure 1. (a) Figure 1. (b)

    111441

    D i th l dil ti d i f f b h ti l

    otherwise

    yxyxh

    otherwiseyx

    yxf0

    ;1,11),(

    0;4,41

    ),(

    Yao Wang, NYU-Poly EL5123: Non-linear Filtering 43

    Derive the gray scale dilation and erosion of f by h, respectively.

  • Computer Assignment1. Write a program which can i) add salt-and-pepper noise to an image with a specified

    noise density, ii) perform median filtering with a specified window size. Consider only median filter with a square shape. Try two different noise density (0.05, 0.2) and for each density, comment on the effect of median filtering with different window sizes and experimentally determine the best window size You can use imnoise( )sizes and experimentally determine the best window size. You can use imnoise( ) to generate noise. You should write your own function for median filtering. You can ignore the boundary problem by only performing the filtering for the pixels inside the boundary.

    2. In a previous assignment you have created a program for adding Gaussian noise d filt i i filt A l th i filt d th di filt b thand filtering using average filter. Apply the averaging filter and the median filter both

    to an image with Gaussian noise (with a chosen noise variance) and with salt-and-pepper noise (with a chosen noise density). Comment on the effectiveness of each filter on each type of noise.

    3. Use the MATLAB program imdilate( ) and imerode( ) on a sample BW image. Try a p g ( ) ( ) p g ysimple 3x3 square structuring element. Comment on the effect of dilation and erosion. By concatenating the dilation and erosion operations, also generate result of closing and opening, and comment on their effects. Repeat above with a 7x7 structure element. Comment on the effect of the window size. You can generate a binary image from a grayscale one by thresholding.y g g y y g

    4. Repeat above on a gray scale image, using MATLAB gray scale dilation and erosion functions.

    5. Optional (for your own practice, will not be graded): write your own program for gray-scale dilation and erosion.

    Yao Wang, NYU-Poly EL5123: Non-linear Filtering 44

  • Reading

    R. Gonzalez, Digital Image Processing, Chapter 5 3 and Chapter 9Chapter 5.3 and Chapter 9.

    A. K. Jain, Fundamentals of Digital Image Processing Section 7 4 and Section 9 9Processing, Section 7.4 and Section 9.9.

    Yao Wang, NYU-Poly EL5123: Non-linear Filtering 45