Home
Means, Medians and Images

Means, Medians and Images

6 min read
Original publication: July 08, 2016 Updated at: August 20, 2024 (added interactive images) Updated at: September 06, 2024 (content review)

Means and medians are some of the first concepts we learn in any basic statistics course. Such concepts are illustrated with several examples, such as: the mean height of the students in the class (or more sensitive information such as age or weight), or the mean/median salary of a company. It is essential to understand these concepts to better understand the world around us. In this post, however, the goal is not to explain neither the concepts, nor why they are important, but to answer the question: what does it have to do with computer science?

A straightforward application is to compute the mean execution time for a given algorithm. Which might help understanding asymptotic notation (e.g. O(1)\mathcal{O}(1), O(N)\mathcal{O}(N), O(NlogN)\mathcal{O}(NlogN)). Don’t worry if you have no idea what asymptotic notation is. I’d like to focus on another application: image processing.

Digital images

Before talking about image processing, let’s review what a digital image is. The most common way to represent an image in a computer is with a table, or matrix, of pixels. A pixel is the representation of a color, in a color image, or intensity, in a grayscale image. We will work with grayscale images for simplicity. Most grayscale images have 256 shades (no, not 50), or levels, varying from black (0) to white (255). A grayscale image is just a table with numbers between 0 and 255:

Touch the image to see the zoomed pixels.

255 255 255 255 255 251 255 243 242 247 228 221 243 235 211 148 69 187 255 201 0 19 220 255 230

Using means with images

Back to the statistics. As the image is just a table of numbers, we can compute its mean. For example, the mean of the image above is 109.26. It doesn’t say much, right?

What if we consider the mean of a smaller region of the image? Let’s see what happens when we replace each pixel by the mean of its neighbors (a total of 9 pixels: the pixel itself, and the pixels to the right, left, top, bottom, and diagonals, when they exist) with its own value, we have:

Touch the image to see the zoomed pixels.

241 222 205 208 230 246 245 245 243 240 199 205 217 229 221 119 148 189 226 207 114 152 195 232 202

In this case, the mean smoothed the difference between adjacent pixels! Visually, the borders are smoothed and the whole image is blurred and a little washed out. Let’s see what happens when we consider 25 pixels for the mean (2 pixels to each side):

Touch the image to see the zoomed pixels.

212 200 186 181 192 215 213 212 211 213 186 199 209 213 214 173 194 207 207 201 171 196 205 199 186

The process of image acquisition and storage isn’t perfect. Thus it’s common to have noise in the image. Let’s consider a simple case: an image with 4 squares, two white squares and two black squares. We’ll add random noise: some pixels that should be black were registered as white and some pixels that should be white were registered as black.

Touch the image to see the zoomed pixels.

0 0 0 0 255 0 0 0 255 255 0 0 0 255 255 255 255 255 0 0 255 255 255 0 0

Let’s repeat the process of computing the mean of the 25 neighboring pixels:

Touch the image to see the zoomed pixels.

0 41 92 143 194 41 61 102 133 163 92 92 112 122 133 143 133 133 122 112 194 163 143 112 82

As most of the pixels have the same intensity, the mean tends to get close their value. In this case, either black or white. With this, incorrect white pixels turn darker and incorrect black pixels turn brighter as most of their neighbors have the opposite color. In other words, this process can be used to reduce the noise in an image. A collateral effect is the spreading of the noise to it’s neighbors, initially with the correct color. This process of replacing a pixel by its neighbors’ mean value is known as mean filter or average filter.

The set of pixels used to compute the mean is called window. The number of pixels in the window can be larger than 25. There are no limits for neither the size or shape of the window. The following image shows the application of a mean filter with 121 pixels in the window (a 11 x 11 pixels square) on the same image with noise:

Touch the image to see the zoomed pixels.

95 105 116 129 137 105 112 118 126 131 116 118 120 124 126 126 124 122 122 120 137 131 124 122 116

Note that with this larger window size, the noise faded even further. However, a side effect is that the borders between the black and white squares is even blurrier than with the 5 x 5 window. Let’s apply the mean filter on our original image with a 11 x 11 window:

Touch the image to see the zoomed pixels.

158 160 167 172 168 161 161 167 171 166 158 158 163 166 158 153 153 156 158 151 153 154 156 157 149

In the extreme case we can use a very large window (larger than the image itself). In this case, each pixel will be replaced by the average of all the pixels in the image, in our example, 109. The result will be this image with a single grayscale intensity:

Touch the image to see the zoomed pixels.

109 109 109 109 109 109 109 109 109 109 109 109 109 109 109 109 109 109 109 109 109 109 109 109 109

Medians and images

What would happend if we simply replace the mean by the median? Let’s initially consider a property of medians. The median of the set {0,1,100}\{0, 1, 100\} is 11. It doesn’t matter how large or small the first and last numbers are. Likewise, the number 255 doesn’t affect the computation of the median of {0,0,0,0,255}\{0, 0, 0, 0, 255\}, which will still be 00, no matter what we use instead of 255255. This is not true for the mean. Let’s consider our black and white squares example once more:

Touch the image to see the zoomed pixels.

0 0 0 0 255 0 0 0 255 255 0 0 0 255 255 255 255 255 0 0 255 255 255 0 0

What do you think will happen if we replace each pixel by the median of its 5 by 5 neighbor window? Since the majority of the pixels in this image are black. The median will likely be 0. Which means the result will be:

Touch the image to see the zoomed pixels.

0 0 0 255 255 0 0 0 255 255 0 0 0 0 255 255 255 255 0 0 255 255 255 0 0

This example shows an interesting property of median filters. While a mean filter blurs the whole image, smoothing the borders, a median filter preserves borders. Now, remember our original image? Let’s sprinkle some wrong pixels on it (we picked some pixels randomly and replaced them by 255 minus their value):

Touch the image to see the zoomed pixels.

255 255 255 255 255 251 255 243 242 247 228 221 243 235 211 107 186 187 255 201 0 19 220 255 230

Let’s see what happens when we apply the median filter to the noisy image (using a window of 3 x 3 pixels):

Touch the image to see the zoomed pixels.

255 255 243 243 255 254 251 243 243 247 228 228 242 242 235 131 187 221 230 211 131 187 238 230 228

A disadvantage of using median filters instead of mean filters is its higher computational complexity. While the computation of the mean is linear (O(N)\mathcal{O}(N)), where NN is the number of pixels (we just have to sum all values and divide by NN), the computation of the median takes O(NlogN)\mathcal{O}(N\log N) (we have to sort the pixel values to return the central one). But for small window sizes (e.g. 3), it is pretty much constant.

Another way to understand the effect of mean filters on images uses the concept of Fourier transform. I find this explanation particularly beautiful, but let’s leave it for a future post.