# Image distance between noisy and clean images

**URL:** <https://community.wolfram.com/t/image-distance-between-noisy-and-clean-images/21006>\
**Category:** Questions\
**Tags:** image-processing, statistics-and-probability\
**Created:** [November 7, 2022, 3:54am UTC](https://community.wolfram.com/t/image-distance-between-noisy-and-clean-images/21006 "2022-11-07T03:54:54Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![sra42](https://avatars.discourse-cdn.com/v4/letter/s/f04885/32.png) [@sra42](https://community.wolfram.com/u/sra42)\
**Post date:** [November 7, 2022, 3:54am UTC](https://community.wolfram.com/t/image-distance-between-noisy-and-clean-images/21006/1 "2022-11-07T03:54:54Z")

</div>

This is what I am trying to do:

Download MNIST images:

```
resource = ResourceObject["MNIST"];
trainingData = ResourceData[resource, "TrainingData"];
testData = ResourceData[resource, "TestData"];
SeedRandom[42];
rctraindata = RandomChoice[trainingData, 1000];
rctraindatat = 1. - Map[Flatten, Map[ImageData, rctraindata[[All, 1]]]];

```

Add Guassian noise:

```
nrctraindatat = Table[rctraindatat[[i]] + RandomVariate[NormalDistribution[0, 0.2], 784], {i, Length[rctraindatat]}];

```

Calculate the distance between clean and noisy image:

```
EuclideanDistance[rctraindatat[[1]], nrctraindatat[[1]]] (*5.82*)

```

Scale the noisy image as follows:

```
scaled = (nrctraindatat[[1]]-Min[Flatten[nrctraindatat]])/(Max[Flatten[nrctraindatat]] - 
     Min[Flatten[nrctraindatat]]);

```

The idea behind scaling is that the clean images are within (0,1) and I wanted the noisy image to be in the same range to calculate the distance between them.  
Calculate the distance again:

```
EuclideanDistance[rctraindatat[[1]], scaled] (*8.78*)

```

Which is the correct way to calculate the distance between a corrupted and a clean image and why? PSNR and SSIM are usually used as metrics. Do I need to rescale the noisy image before calculating PSNR or SSIM?

I also posted [here](https://mathematica.stackexchange.com/questions/275641/image-distance-between-noisy-and-clean-images)

---

<div class="post-metadata">

**Author:** ![rolandfranzius](https://avatars.discourse-cdn.com/v4/letter/r/5f8ce5/32.png) [@rolandfranzius](https://community.wolfram.com/u/rolandfranzius)\
**Post date:** [March 12, 2023, 6:18pm UTC](https://community.wolfram.com/t/image-distance-between-noisy-and-clean-images/21006/2 "2023-03-12T18:18:57Z")

</div>

The euclidean distance between two vectors is

```
d[x_,y_]:= Sqrt[Total[(x-y)^2] ]

```

For a disturbation W with mean 0 and standard deviation 1

```
Y = X + sigma W

```

the distance is obviously

d[X , Y] = sigma Sqrt[Total[ W ^ 2] ] = sigma Sqrt[Lenght[W]] Sqrt[Mean[W ^2]]

It follows if W is a NormalDistribution[0,1] the distance is

```
d[X , Y] = sigma Sqrt[Total[ W ^ 2] ] = sigma Sqrt[Lenght[W]] 

```

Since you are working with a sigma = 0.2 and Length[W]=784 you will find a value near 0.2 \* 28 = 5.6
