Median filtering
Median filtering is an image processing methodology for removing high frequency noise from images.
The methodology works by moving a window across an image a data point at a time. In our case the image is stored in a 2D array so that's what we move across. The window is large enough to encompass a neighbourhood of data cells. Each data cell is adjusted so that it is the median value of its neighbourhood (its current value is included in the neighbourhood).
Plainly as we're moving across an array, if we adjusted the array with the data in it we'd be altering points that later went to be neighbours of other points, so, infact, a second array is generated containing the altered results.
Take the following example:
For the first cell "2", we collect together all the neighbouring cells into an array, sort the array in order, and work out what the middle value is. In this case we have an odd number of cells in the neighbourhood - 9 - so the median is the 5th number (if we had an even number, we'd take the mean of the two middle numbers). The 5th number in this case is "10", so we keep this in the results array. We then move to the next cell: the "35" cell. The median of its neighbourhood is "17", so we keep this. And so on.
We would repeat this for all cells in the array, or some internal subset, depending on how we want to resolve the boundary issues (see lecture 3). The resulting array is then our filtered dataset.
The key thing here is how to sort the array containing the neighbourhood values. Sorting routines are a major branch of programming science as efficient sorting makes for efficient data access and searching. You can find out more about the popular sorting routines on the sorting algorithm Wikipedia page.
Here we are using a "Moore Neighbourhood" - that is, a square neighbourhood around the chosen cell, however, there are other types of neighbourhood. Here the Moore Neighbourhood has a radius of one, that is, there is one cell taken on either side of the central, chosen, cell.