# And Measuring Noise

Using the nomenclature developed in yesterday’s post I will today also implement a method for creating salt and pepper noise in images. This noise simulates dead pixels by setting them either to the lowest or highest grey value, in our case 0 or 1.

First let’s install and load the *Raspository*, where the methods and class developed yesterday are located in.

```
if (!requireNamespace("devtools", quietly = TRUE))
install.packages("devtools")
devtools::install_github("David-J-R/Raspository")
```

```
library(Raspository)
```

Without further ado let me show you how to implement the salt and pepper noise.

```
saltAndPepperNoise <- function(object, percentage = .2){
# select the indices to set to 0 or 1 at random
indices <- sample(length(object@current), length(object@current) * percentage)
# draw zeros and ones from a binomial distribution
values <- rbinom(length(indices), 1, 0.5)
object@current[indices] <- values
return(object)
}
```

OK, our animal guest test subject today will be an owl.

```
set.seed(1)
owl <- imageBWFromJpeg("owl.jpg")
plot(owl)
```

It looks surprised, doesn’t it? It probably knows about the salt and pepper noise we will add right away.

```
owlNoise <- saltAndPepperNoise(owl)
plot(owlNoise)
```

Uhm, this looks really annoying now. The other kind of noises hadn’t been that disrupting. Just like dead pixels on your screen, they make you feel bad.

## Introducing Two Measures of Noise

But what would be nice now as well, would be to have some measure of the noise. Let’s just use the mean squared error for it. I’ll implement a function that can compare two pictures or the current and original picture in an imageBW object depending on the input parameters.

```
MSE <- function(object, other = NULL){
if(is.null(other)){
errorMatrix <- object@original - object@current
}else{
errorMatrix <- other@current - object@current
}
squaredErrorMatrix <- errorMatrix ^ 2
return(mean(squaredErrorMatrix))
}
```

Nice! Now we finally have a measure for our noise. Of course this will be especially helpful later, when we want to evaluate the performance of reconstruction methods.

So let’s test it.

```
MSE(owlNoise)
```

## [1] 0.05545031

```
MSE(owlNoise, owl)
```

## [1] 0.05545031

As expected^{1} both function calls leave us with the same result.

Let’s also implement the peak-signal-to-noise ratio, which gives us back a decibel value. The lower the value the more noise you have in the picture. You can calculate it according to the following formula, which I will also “simplify” a bit.

\(PSNR(x, y) = 10 \cdot log_{10} \left( \frac{MAX^{2}}{MSE(x, y)} \right) \

\)

I hope you know the logarithm rules. 😛

\(PSNR(x, y) = 10 \cdot log_{10}(MAX^{2}) – 10 \cdot log_{10}(MSE(x, y)) \

\) \(

PSNR(x, y) = 20 \cdot log_{10}(MAX) – 10 \cdot log_{10}(MSE(x, y))

\)

Where x and y are the two pictures to compare and MAX is the maximum value a pixel can take on.

And for MAX being one in our case we get:

\(PSNR(x, y) = – 10 \cdot log_{10}(MSE(x, y))

\)

Subsequently, it’s now time to implement this measure as well:

```
PSNR <- function(object, other = NULL){
mse <- MSE(object, other)
return(-10 * log(mse, base = 10))
}
```

That was pretty straightforward. So let’s test it!

```
PSNR(owlNoise)
```

## [1] 12.56096

It’s a relatively low value, that indicates a lot of noise. In my opinion this value is more intuitive, probably because of use of the logarithm function. Indeed the MSE value alone didn’t seem that big.

But that’s it for now. There will be probably further stuff next week. But probably not that much.

Thanks for reading and have nice remaining weekend. 🙂

## Availability Of The Code

You can access a maintained version of the code in my GitHub repository *Raspository* under R/imageNoise.R.

But as I will expand the code, it will also probably grow there.