12.07.2015 Views

Lecture 10 Median Filtering - KTH

Lecture 10 Median Filtering - KTH

Lecture 10 Median Filtering - KTH

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

<strong>Median</strong> <strong>Filtering</strong>• You have used convolution techniques toimplement weighting kernels as a neighborhoodfunction, which represented a linear process.• There are also nonlinear neighborhood operationsthat can be performed for the purpose of noisereduction that can do a better job of preservingedges than simple smoothing filters.<strong>Median</strong> <strong>Filtering</strong>• One such method is known as median filtering.• In median filtering, the neighboring pixels are rankedaccording to brightness (intensity) and the median valuebecomes the new value for the central pixel.• <strong>Median</strong> filters can do an excellent job of rejectingcertain types of noise, in particular, “shot” or impulsenoise in which some individual pixels have extremevalues.• In the median filtering operation, the pixel values in theneighborhood window are ranked according tointensity, and the middle value (the median) becomesthe output value for the pixel under evaluation.


<strong>Median</strong> <strong>Filtering</strong>• In particular, compared to the smoothing filtersexamined thus far, median filters offer threeadvantages:– No reduction in contrast across steps, since output valuesavailable consist only of those present in the neighborhood(no averages).– <strong>Median</strong> filtering does not shift boundaries, as can happenwith conventional smoothing filters (a contrast dependentproblem).– Since the median is less sensitive than the mean to extremevalues (outliers), those extreme values are more effectivelyremoved.


<strong>Median</strong> <strong>Filtering</strong>• The median is, in a sense, a more robust “average”than the mean, as it is not affected by outliers(extreme values).• Since the output pixel value is one of the neighboringvalues, new “unrealistic” values are not created nearedges.• Since edges are minimally degraded, median filterscan be applied repeatedly, if necessary.• Considerations:<strong>Median</strong> <strong>Filtering</strong>– The median filter is more expensive to compute than asmoothing filter. Clever algorithms can save time bymaking use of repeating values as the neighborhoodwindow is slid across the image.– <strong>Median</strong> filters are nonlinear:<strong>Median</strong>[A(x)+B(x)] median[A(x)]+median[B(x)]This must be taken into account if you plan onsumming filtered images.


Hybrid <strong>Median</strong> <strong>Filtering</strong>• <strong>Median</strong> filters can tend to erase lines narrower than ½the width of the neighborhood. They can also round offcorners.• Hybrid median filters can get around these problems.• The hybrid median filter is a three step ranking processthat uses two subgroups of a 5x5 neighborhood.• These subgroups are drawn from pixels parallel to theimage frame edges, and at 45º to the edges, centered onthe reference pixel.


Original 5x5 Hybrid3x3 <strong>Median</strong> 5x5 Octagonal <strong>Median</strong>Mode Filters• A technique related to median filtering is modefiltering.• Mode filtering accomplishes noise reduction, andalso can provide for edge enhancement.• The mode of a distribution is defined as the mostlikely value.• However, for small neighborhoods, the mode ispoorly defined, so a truncated median filter can beused to approximate the mode.


Mode <strong>Filtering</strong>• In an asymmetric distribution (as would be thecase with a neighborhood near and edge) the modeis the highest point in the neighborhood histogramand the median is closer to the mode than to themean value.• In the truncated median method, values farthestfrom the mean are discarded so that the median isshifted towards the mode.• The new median becomes the output value for thecentral pixel.


Mode <strong>Filtering</strong>• One consequence of repeated application of themode filter is that the number of distinct intensityvalues is reduced (the so-called “posterization”effect).• This is also what would happen if the bit depth ofthe image were reduced.


Rank Leveling• Rank leveling is a useful technique incircumstances where the background intensityvaries widely and the features of interest arelimited in size and are smaller than the scale of thebackground variations.• Contrast between the features of interest andbackground is assumed.Rank Leveling• In rank leveling a 3x3 or 5x5 neighborhood istypically used.• As before, the center pixel in the neighborhood isevaluated.• If the background is lower in intensity than thefeature, the lowest pixel intensity in theneighborhood becomes the new center pixel valuein the output image.


Rank Leveling• If the background is brighter, then the highestpixel intensity is used.• This process is sometimes referred to as grayscaleerosion or dilation.• The result of this process is that the size of thefeatures is reduced by the size of the neighborhoodradius along with extending the local intensityvalues into the area previously covered by thefeatures.


Rank Leveling• This process can be continued until the features ofinterest are eliminated/separated altogether.• The number of repetitions required to removefeatures is based on the relative size of theneighborhood to the features, e.g. a feature lessthan or equal to four times the width of theneighborhood will be removed in four iterations.Rank Leveling• In the absence of precise feature dimensions, thehistogram can be used with each iteration to determinewhen the features have been removed.• The result is an image with the large scale variations ofthe original background present, but no features.• Subtraction of the leveled image from the originalproduces an image with clearly defined features whichcan be extracted by thresholding.


Rank Leveling• The subtracted image may be processed by any ofa number of mapping methods (e.g. polynomialexpansion) to increase the contrast of the features,allowing for the discrimination of very smallfeatures.• This is important in histological analysis of tissuesamples.


Axons (TEM)Background(5 pixel radius)Axons/BackgroundPolynomial ContrastExpansionMATLAB Syntax• <strong>Median</strong> filtering is accomplished using the medfilt2function:B = medfilt2(A,[m n]);Where B is the output image, A is the input image, and mx n is the size of the neighborhood to be used for mediandetermination.• If m=n the neighborhood will be square.


MATLAB Syntax• The ordfilt2 function allows additional flexibilityin implementing neighborhood rank filteringfunctions. The syntax is:B = ordfilt2(A,order,domain);• B is the output image, A is the input image,domain specifies the neighborhood, and orderrepresents the rank within the neighborhoodintensity values to be used as the output pixelvalue.• For example:MATLAB SyntaxB = ordfilt2(A,5,ones[3 3]);implements a median filter using a 3x3neighborhood.• In the same case, if you wanted to use themaximum of the neighborhood:B = ordfilt2(A,9,ones[3 3]);


MATLAB Syntax• As you would expect, the corresponding minimumfilter would be:B = ordfilt2(A,1,ones[3 3]);• Remember that the for the order index, 1represents the minimum value.• Non-square neighborhoods may also be specified.MATLAB Syntax• In the neighborhood (domain) specification thearray is used as a binary table.• That is, array elements with value = 1 are used indefining the neighborhood, and array elementswith value = 0 are ignored.• Thus it is possible to define neighborhoods likethose discussed for hybrid median filtering.


MATLAB Syntax• In the examples just shown, the ones function wasused to define a square neighborhood in which allof the values were to be used in the rankingprocess.• To produce neighborhoods consisting of a subsetof a square matrix, simply set the unwanted arrayvalues to zero.MATLAB Syntax• If you want to produce a maximum filter with aneighborhood using only pixels that are notdiagonals of a 3x3 neighborhood:B = ordfilt2(A,4,[0 1 0;1 0 1;0 1 0]);• The corresponding minimum filter would be:B = ordfilt2(A,1,[0 1 0;1 0 1;0 1 0]);


MATLAB Syntax• For more elaborate configurations based on largerarrays, it is possible to define a named binaryarray and call it by name rather than specifying itexplicitly in the ordfilt2 function.• So if you wanted to use a neighborhood using onlythe diagonals of a 5x5 square for a minimum filter,define the array (C) and use:B = ordfilt2(A,1,C);• Where:MATLAB Syntax<strong>10</strong>C 00<strong>10</strong><strong>10</strong><strong>10</strong>000000<strong>10</strong><strong>10</strong><strong>10</strong>001

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!