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:
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: