Filtering images using a median filter
The first recipe of this chapter introduced the concept of linear filters. Non-linear filters also exist and can be advantageously used in image processing. One such filter is the median filter that we present in this recipe.
Since median filters are particularly useful in order to combat salt-and-pepper noise (or salt-only, in our case), we will use the image we created in the first recipe of Chapter 2, Manipulating Pixels, and that is reproduced here:
How to do it...
The call to the median filtering function is done in a way that is similar to the other filters:
cv::medianBlur(image,result,5); // size of the filter
The resulting image is as follows:
How it works...
Since the median filter is not a linear filter, it cannot be represented by a kernel matrix. However, it also operates on a pixel's neighborhood in order to determine the output pixel value. The pixel and its neighborhood form a set of values and, as the name suggests, the median filter will...