{ "nbformat": 3, "nbformat_minor": 0, "worksheets": [ { "cells": [ { "cell_type": "code", "language": "python", "outputs": [], "collapsed": false, "input": [ "%matplotlib inline" ], "metadata": {} }, { "source": "
Rank filters are non-linear filters using the local gray-level ordering to\ncompute the filtered value. This ensemble of filters share a common base: the\nlocal gray-level histogram is computed on the neighborhood of a pixel (defined\nby a 2-D structuring element). If the filtered value is taken as the middle\nvalue of the histogram, we get the classical median filter.
\nRank filters can be used for several purposes such as:
\nSome well known filters are specific cases of rank filters [1] e.g.\nmorphological dilation, morphological erosion, median filters.
\nIn this example, we will see how to filter a gray-level image using some of the\nlinear and non-linear filters available in skimage. We use the camera image\nfrom skimage.data for all comparisons.
\n[1] | Pierre Soille, On morphological operators based on rank filters, Pattern\nRecognition 35 (2002) 527-535. |
Some noise is added to the image, 1% of pixels are randomly set to 255, 1% are\nrandomly set to 0. The median filter is applied to remove the noise.
\nThe added noise is efficiently removed, as the image defaults are small (1\npixel wide), a small filter radius is sufficient. As the radius is increasing,\nobjects with bigger sizes are filtered as well, such as the camera tripod. The\nmedian filter is often used for noise removal because borders are preserved and\ne.g. salt and pepper noise typically does not distort the gray-level.
\nThe example hereunder shows how a local mean filter smooths the camera man\nimage.
\nOne may be interested in smoothing an image while preserving important borders\n(median filters already achieved this), here we use the bilateral filter\nthat restricts the local neighborhood to pixel having a gray-level similar to\nthe central one.
\nNote
\nA different implementation is available for color images in\nskimage.filter.denoise_bilateral.
\nOne can see that the large continuous part of the image (e.g. sky) is smoothed\nwhereas other details are preserved.
\nWe compare here how the global histogram equalization is applied locally.
\nThe equalized image [2] has a roughly linear cumulative distribution function\nfor each pixel neighborhood. The local version [3] of the histogram\nequalization emphasizes every local gray-level variations.
\n[2] | http://en.wikipedia.org/wiki/Histogram_equalization |
[3] | http://en.wikipedia.org/wiki/Adaptive_histogram_equalization |
Another way to maximize the number of gray-levels used for an image is to apply\na local auto-leveling, i.e. the gray-value of a pixel is proportionally\nremapped between local minimum and local maximum.
\nThe following example shows how local auto-level enhances the camara man\npicture.
\nThis filter is very sensitive to local outliers, see the little white spot in\nthe left part of the sky. This is due to a local maximum which is very high\ncomparing to the rest of the neighborhood. One can moderate this using the\npercentile version of the auto-level filter which uses given percentiles (one\ninferior, one superior) in place of local minimum and maximum. The example\nbelow illustrates how the percentile parameters influence the local auto-level\nresult.
\nThe morphological contrast enhancement filter replaces the central pixel by the\nlocal maximum if the original pixel value is closest to local maximum,\notherwise by the minimum local.
\nThe percentile version of the local morphological contrast enhancement uses\npercentile p0 and p1 instead of the local minimum and maximum.
\nThe Otsu threshold [1]_ method can be applied locally using the local gray-\nlevel distribution. In the example below, for each pixel, an "optimal"\nthreshold is determined by maximizing the variance between two classes of\npixels of the local neighborhood defined by a structuring element.
\nThe example compares the local threshold with the global threshold\nskimage.filter.threshold_otsu.
\nNote
\nLocal is much slower than global thresholding. A function for global Otsu\nthresholding can be found in : skimage.filter.threshold_otsu.
\n[4] | http://en.wikipedia.org/wiki/Otsu's_method |
The following example shows how local Otsu thresholding handles a global level\nshift applied to a synthetic image.
\nLocal maximum and local minimum are the base operators for gray-level\nmorphology.
\nNote
\nskimage.dilate and skimage.erode are equivalent filters (see below for\ncomparison).
\nHere is an example of the classical morphological gray-level filters: opening,\nclosing and morphological gradient.
\nLocal histograms can be exploited to compute local entropy, which is related to\nthe local image complexity. Entropy is computed using base 2 logarithm i.e. the\nfilter returns the minimum number of bits needed to encode local gray-level\ndistribution.
\nskimage.rank.entropy returns the local entropy on a given structuring\nelement. The following example shows applies this filter on 8- and 16-bit\nimages.
\nNote
\nto better use the available image bit, the function returns 10x entropy for\n8-bit images and 1000x entropy for 16-bit images.
\nThe central part of the skimage.rank filters is build on a sliding window\nthat updates the local gray-level histogram. This approach limits the algorithm\ncomplexity to O(n) where n is the number of image pixels. The complexity is\nalso limited with respect to the structuring element size.
\nIn the following we compare the performance of different implementations\navailable in skimage.
\nComparison between
\non increasing structuring element size:
\nand increasing image size:
\nComparison between:
\non increasing structuring element size:
\nComparison of outcome of the three methods:
\nand increasing image size:
\n