# Or Let’s Make Some Noise!!

As I wrote in one of my last posts there will be some content about image processing and computer vision. And following my second lecture of it, here we go! Fortunately R has some capabilities of handling pictures, of which I will take use.

For now the topic will just be, how to add noise to images, which can be useful to you, e.g. if you want to prove the performance of an algorithm for handling noise. So we’re simulating noise. Natural noise in images can come from different sourcesâ€¦ The most common of it would be due to high ISO values or/and long exposure times. So in general in low light conditions.

However I’ll only work with black and white pictures for today. Maybe color comes at some point in the future. đź™‚

## Loading Pictures Into

First I’ll show you how to load pictures into R and how to print them again. It’s quite easy tbh:

```
library(jpeg)
goose <- readJPEG("goose.jpg")
class(goose)
```

## [1] "array"

```
dim(goose)
```

## [1] 600 897 3

As the picture is black and white we only need one dimension of it. And we can just plot it by transforming it into a raster object:

```
gooseBW <- goose[,,1]
plot(as.raster(gooseBW))
```

Cute this goose, isn’t it? đź™‚ Now let’s destroy this picture with noise! Nyehehehehe^{1}.!

## Adding The Noise

To begin with I will add some uniform distributed noise. As the values in the matrix are between 0 and 1, I will add values between -1 and 1

```
set.seed(1)
gooseUniformNoise <- gooseBW + runif(length(gooseBW), min = -1, max = 1)
```

But we can’t plot this right, as the matrix now also contains values above 1 and below 0. I’ll fix this by cropping the boundaries and then plot.

```
gooseUniformNoise[gooseUniformNoise > 1] <- 1
gooseUniformNoise[gooseUniformNoise < 0] <- 0
plot(as.raster(gooseUniformNoise))
```

Interesting how you can still make out the goose in spite of the noise? Now let’s do the same with normal distributed noise. I’ll take the standard deviation of the black and white picture.

```
gooseNormalNoise <- gooseBW + rnorm(length(gooseBW), sd = sd(gooseBW))
gooseNormalNoise[gooseNormalNoise > 1] <- 1
gooseNormalNoise[gooseNormalNoise < 0] <- 0
plot(as.raster(gooseNormalNoise))
```

In the lecture we also did multiplicative and impulse noise. However I won’t do them in this post, maybe another time.

Anyway, do you already see, why I like R so much? đź™‚ If you do Linear Algebra and Statistics stuff it’s just super comfy!

I’ll show you probably even more reasons why, when I demonstrate you, how to denoise picture again. But that’s it for today.

Have a nice weekend!

## One comment