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.