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 255 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 51 102 153 204 51 82 112 143 173 102 112 122 133 143 153 143 133 122 112 204 173 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.