2
$\begingroup$

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?

$\endgroup$
3
  • 1
    $\begingroup$ Welcome to Mathematica SE ! Please, provide the code you are working on in order to receive constructive answers. $\endgroup$
    – Sektor
    Commented Aug 8, 2013 at 13:46
  • $\begingroup$ 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 $\endgroup$
    – user8727
    Commented Aug 8, 2013 at 13:58
  • $\begingroup$ I think can be written as PSNR=10Log[10,Times@@ImageDimensions[#1]/ImageDistance[#1,#2]^2]&;. $\endgroup$
    – Aster
    Commented Apr 26, 2018 at 9:12

2 Answers 2

5
$\begingroup$

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.

$\endgroup$
3
  • $\begingroup$ ThankU soo much for Ur time and kind reply..... $\endgroup$
    – user8727
    Commented Aug 8, 2013 at 14:27
  • $\begingroup$ @user8727 - you're welcome, but the normal thing to do is to click the little up arrow if you find the answer useful. $\endgroup$
    – bill s
    Commented Aug 8, 2013 at 14:53
  • $\begingroup$ @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. $\endgroup$
    – UDB
    Commented Sep 21, 2018 at 22:55
5
$\begingroup$

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: "];

enter image description here

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:

Image`MeasurementsDump`imagedistance[img1_, img2_, "MeanSquaredEuclideanDistance"] := Divide[
    Image`MeasurementsDump`imagedistance[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.

enter image description here

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

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.