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?
2 Answers
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.
-
$\begingroup$ ThankU soo much for Ur time and kind reply..... $\endgroup$– user8727Commented 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 sCommented 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$– UDBCommented 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:
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
.
Of course, you'd better remove the alpha channel when preparing the data set.
PSNR=10Log[10,Times@@ImageDimensions[#1]/ImageDistance[#1,#2]^2]&;
. $\endgroup$