LESSON

# Monadic Functions

#### Share

#### Transcript

Let’s talk in detail about some of these monadic functions.

First, changing the pixel type—quite useful frequently to convert an unsigned integer image into a double image. So what we are going to do is take input pixels in the range 0 through 255, and these are integers so they have got value 0, 1, 2, 3 and so on, and we are going to map them into the range of real numbers between 0 and 1. And we can represent that mapping by a graph, which is shown there on the right.

Conversely, we can convert double precision images into uint8 images. So what we want to do now is to take pixels in the range 0 to 1 and map them into integer values, in the range of 0 to 255, and again we can represent this mapping graphically. Note that in both cases, the mapping is a straight line and it passes through the origin with a gradient of 1.

We could also change the brightness of the image and there are a couple of ways that we can do that. One way is simply to add a constant positive value to all of the grey values within the scene.

So f(x) is the input value x, plus the value of 1/4. And if we represent that graphically we see this shape here, and what happens is that some of the pixels are going to exceed the value of 1. So we apply what we call a saturation: we don’t allow them to be greater than 1, so our line has got a kink in it. If we apply this transformation to the Mona Lisa image we can see that indeed the image that has become brighter.

Another way to increase the brightness of the image, often referred to actually as increasing the contrast of the image, is to multiply all the grey values by a constant, and in this case we are going to multiply all the grey values by the value of 2. We can see if we represent this graphically again, the slope of the line now is steeper than it was before. This line now has got a steepness, a gradient of, 2, and the saturation problem is more pronounced now.

So there are a lot of values which if we apply the function to x would have a value much greater than 1, and we have had to limit those—or saturate those—to the maximum value of 1, which we have when we represent images in double precision numbers.

What we can see now when we look at the image, we can see that it is indeed much more contrasty, but we can also see some areas where the pixels have become saturated.

A simple function like this—1-x—produces a negative image. So this is it graphically: a line with a slope of -1. And what happens here are bright input pixels become dark in the output image and vice versa.

I mentioned earlier the technique called posterization, which you can often find in pop art. And what we do is we limit the number of possible output brightness values that there can be. We quantise it if you like, so in the image shown here what we have allowed are only four possible output grey values, so this staircase mapping converts a continuous range of input grey levels to one of four possible output values.

#### Code

Let’s look at some simple monadic functions such as type conversion, brightness and contrast adjustment, inversion and posterisation and the effect they have on an image.

### Skill level

MATLAB experience

This content assumes an understanding of high school level mathematics; for example, trigonometry, algebra, calculus, physics (optics) and experience with MATLAB command line and programming, for example workspace, variables, arrays, types, functions and classes.