I've been working with a grayscale image that has a lot of Salt and Pepper noise and came to know that MedianBlur is very useful. So I used the Python version of Opencv(cv2.medianBlur()). It worked but didn't work in the way I wanted. So I was looking for the actual algorithm it worked on and got the following explanation:
If you have a kernel size(k) of 5, then for every 5(row count)x5(column count) square window, the central pixel of this window will get replaced by the Median value of all the elements in it. So, for instance, consider this window:
[[11, 4, 17, 1, 5],
[ 6, 14, 0, 12, 16],
[24, 19, 13, 18, 23],
[ 7, 11, 11, 10, 5],
[10, 13, 23, 3, 0]]
Here, the central element, 13 will get replaced by the Median of all these elements, i.e. 11. Is this correct? If yes, what happens to the first 2 rows, since there can't be elements in the first two rows which are the central element for ANY window? As per my observation(comparing the original and processed image), the first two rows change too.
According to the medianBlur() documentation, it uses BORDER_REPLICATE internally to process border pixels.
BORDER_REPLICATE
Python: cv.BORDER_REPLICATE
aaaaaa|abcdefgh|hhhhhhh
So it repeats the bordering pixels until all pixels are the mid pixel of a window.
EDIT: To apply a kernel with a size of 5x5, the first pixel should be at the third row and column of the image. It means replicate the the border by two pixels. So your image becomes this internally:
[[11, 11, 11, 4, 17, 1, 5, 5, 5],
[11, 11, 11, 4, 17, 1, 5, 5, 5],
[11, 11, 11, 4, 17, 1, 5, 5, 5],
[ 6, 6, 6, 14, 0, 12, 16, 16, 16],
[24, 24, 24, 19, 13, 18, 23, 23, 23],
[ 7, 7, 7, 11, 11, 10, 5, 5, 5],
[10, 10, 10, 13, 23, 3, 0, 0, 0],
[10, 10, 10, 13, 23, 3, 0, 0, 0],
[10, 10, 10, 13, 23, 3, 0, 0, 0]]
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With