I am trying to calculate the MSE between two images.
In Matlab the immse
function contains an unexpected formula which is (norm(x(:)-y(:),2).^2)/numel(x)
.
My teacher instead is using mean((x(:)-y(:)).^2)
.
I saw that the results of the method with the mean is more or less half of the other's method.
According to Wikipedia, my teacher's version seems to be the correct one.
What's the right version to use? Why are there these differences?
They are different forms of computing the same thing, and they do give the same answer. We can test this is the same using an example
rng(0);
x = rand(3,3);
y = rand(3,3);
immse( x, y ); % = 0.20527
mean((x(:)-y(:)).^2) % = 0.20527
This is why...
According to the norm documentation, norm(v,2)
is equivalent to norm(v)
or the Euclidean norm, defined as the square root of the sum of squares
Your own Wikipedia link defines MSE as
We can combine these (using consistent lettering for n
=N
an k
=i
from the two sources)
Definition of norm(v,2)
and this expression squared:
Let:
And substitute into the MSE equation:
Which is the calculation done within immse
: (norm(x(:)-y(:),2).^2)/numel(x)
So the definitions given by Wikipedia and MATLAB's immse
are the same. The introductory sentence for the Wiki article even mentions the norm
As it is derived from the square of Euclidean distance [...]
This is equivalent to your teacher's definition. They are using mean
to perform the summation over i=1..n
and division by n
, and computing the square of the delta which is the same as the squared Euclidean norm.