# Peak Signal to Noise Ratio of Images

I am filtering a blurred image, and want to evaluate the performance of Peak Signal to Noise Ratio (PSNR) in Mathematica version 9.0. But I am facing the problem of evaluating the PSNR. Could anyone explain how to do this?

• Welcome to Mathematica SE ! Please, provide the code you are working on in order to receive constructive answers. Commented Aug 8, 2013 at 13:46
• Actually, I have successfully calculated the various parameters like MEAN INTENSITY, ENTROPY & STANDARD DEVIATION INTENSITY, etc. of the images using MMA 9.0. But, it is told me to calculate also the PSNR for my class room assignment. If possible, please provide the necessary guidelines of how to go about it Commented Aug 8, 2013 at 13:58
• I think can be written as PSNR=10Log[10,Times@@ImageDimensions[#1]/ImageDistance[#1,#2]^2]&;. Commented Apr 26, 2018 at 9:12

PSNR is a measure of the difference between two images (see for instance the article in wikipedia). Mathematica has a function for calculating the MSE (mean squared error) between two images

 ImageDistance[img1, img2]


The default measure in this calculation is the Euclidean distance, which is probably what you need to calculate the PSNR. If not, ImageDistance takes a DistanceFunction option which is quite flexible.

• ThankU soo much for Ur time and kind reply..... Commented Aug 8, 2013 at 14:27
• @user8727 - you're welcome, but the normal thing to do is to click the little up arrow if you find the answer useful. Commented Aug 8, 2013 at 14:53
• @bill s The Eucledian distance between two images is nothing but the square root of the sum over all pixels and channels of squared differences of corresponding scalars. That has nothing to do with MSE. The latter is the mean of the pixel-wise squared Euclidean distance of all the scalar or vectorial values. For vectorial values, to compute the arithmetic mean you will need to divide the sum of all these squared Euclidean distances by a product of width * height * channels.
– UDB
Commented Sep 21, 2018 at 22:55

## Normal Case

$${\mathrm{MSE}}={\frac {1}{C\times W\times H}}\sum_{\rm{pixels}} (I_1-I_2)^2$$

When using the real number in $[0,1]$, PSNR can be written as:

$$\mathrm{PSNR}=- 10 \log_{10} \mathrm{MSE}$$

For example:

target=Import["http://wpmedia.wolfram.com/uploads/sites/10/2016/08/Thumb_Wolfram.png"];
GraphicsRow[{input=ImageResize[Downsample[target,2],Scaled[2]],target},ImageSize->400]
Echo[-10Log10[Mean@Flatten@ImageData[(input-target)^2]],"PSNR: "];


## ImageDistance Case

MSE looks like "MeanSquaredEuclideanDistance", but the result is not the same as before.

ImageDistance[input,target,DistanceFunction->"MeanSquaredEuclideanDistance"];
ImageDistance[input,target]^2/Times@@Dimensions@ImageData[input//RemoveAlphaChannel]


This algorithm removes the alpha channel

This is very strange, because this is not the case in the source code:

ImageMeasurementsDumpimagedistance[img1_, img2_, "MeanSquaredEuclideanDistance"] := Divide[
ImageMeasurementsDumpimagedistance[img1, img2, SquaredEuclideanDistance],
Apply[Times, Append[ImageDimensions@img1, ImageChannels@img1]]
];


There are no special rules for the alpha channel, but I think pre-removal is more appropriate, because the alpha channel does not change in most cases, it will significantly improve the score.

## NetLoss Case

If you want to use it in deep learning, you need to write it as Layers.

Of course, you'd better remove the alpha channel when preparing the data set.