# Median filter problems about the output image

An image has an isolated cluster of dark pixels on a light background. The area of the cluster is $(n - 1)/2$ pixels where $n$ is an odd positive integer. What happens to the cluster when it is filtered with a median filter of size of $n \times n$? Explain why this happens.

I guess the output picture will be blurred since there are dark pixels.

• i will retype this problems when there is a good answer,since the answer now is a picture Commented Dec 20, 2016 at 3:02

## 2 Answers

To give you a hint, here is how you can do this calculation with a PC, to check your assumptions:

import cv2
import numpy as np
import matplotlib.pyplot as plt
N = 8
n = 5
n12 = (n-1)/2

I = np.ones((N,N), dtype=np.uint8)
I[2:(2+n12),2:(2+n12)] = 0

I2 = cv2.medianBlur(I, n)

print "Input image:"
print I
print
print "Output image"
print I2


The program output is:

Input image:
[[1 1 1 1 1 1 1 1]
[1 1 1 1 1 1 1 1]
[1 1 0 0 1 1 1 1]
[1 1 0 0 1 1 1 1]
[1 1 1 1 1 1 1 1]
[1 1 1 1 1 1 1 1]
[1 1 1 1 1 1 1 1]
[1 1 1 1 1 1 1 1]]

Output image
[[1 1 1 1 1 1 1 1]
[1 1 1 1 1 1 1 1]
[1 1 1 1 1 1 1 1]
[1 1 1 1 1 1 1 1]
[1 1 1 1 1 1 1 1]
[1 1 1 1 1 1 1 1]
[1 1 1 1 1 1 1 1]
[1 1 1 1 1 1 1 1]]


So, the median filter completely removes the dark area. Now, it's up to you to understand how the median filter works and why its completely removing the dark area. You can ask specific questions, on what is your problem now.

• an inspirational answer！good Commented Dec 20, 2016 at 14:44

This is a good question, I may use this exercise in the future.

Answering a blurring would be too vague, for two main reasons: - first, a blur is often associated with linear filtering, and median filtering is not linear - second, the hypotheses are very precise; this is an hint that more precise reasoning is possible.

So: you have a cluster of pixels. Cluster is not a very precise term, but we can at least suppose that the pixels are connected (either with the 4- or 8-type connectivity, Von Neumann or Moore neighborhood). Since the area is $(n−1)/2$, the cluster cannot be more than $(n−1)/2$ pixel wide or tall. The extreme cases are horizontal (or vertical) segments of length $(n−1)/2$. Here is an example with $n=15$:

1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
1 1 1 1 0 0 0 0 0 0 0 1 1 1 1 1 1 1
1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1


So all in all, the cluster is totally enclosed into a little square $s$ of size $(n−1)/2\times(n−1)/2$. Since the cluster is isolated, this means that outside this square, there are only light values.

Now, by a simple counting argument, visualize the little square $s$ inside any bigger square $S$ of size $n\times n$. Since the little square has sides strictly smaller than "half of $n$", the median of the big square cannot be a value taken from small square $s$. Hence each pixel from the cluster, when median filtered, will be replaced be a value taken in the neighborhood on this pixel, and a value that is not in the cluster.

So the result is a "dark cluster" eraser, or a "background filler". If dark means pure black, and light pure white, the result will be a full white image.

Except possibly at the borders, without specific hypotheses on the pixels "outside the image".