Attempt to reconstruct the original author's stuff
I was curious enough to have a look into the Matlab source code ssim.m provided by the original authors Wang et al. 2004. I have tried my best to exactly re-implement this code with Wolfram Language using MMA 9 (have checked it under MMA 11.3 as well and have re-arrangend the listing below for better readability):
MyMSSIM[
img1raw_Image,
img2raw_Image,
window_: GaussianMatrix[{Table[(11 - 1)/2, {2}], 1.5},
Method -> "Gaussian"],
k1_: 0.01, k2_: 0.03] :=
Module[{
f = Max[1, Round[Min[Sequence @@ ImageDimensions[img1raw]]/256]],
img1in = ColorConvert[Image[img1raw, "Real"], "Grayscale"],
img2in = ColorConvert[Image[img2raw, "Real"], "Grayscale"],
mssimmap, mssim},
If[f > 1,(*if subsampling needs to be done*)
{img1in, img2in} =
ImageCorrelate[
ImagePad[#,(*to mimic the Matlab imfilter edge treatment*)
NestList[Reverse, {Floor[(f - 1)/2], Ceiling[(f - 1)/2]}, 1],
"Reversed"],
ConstantArray[1./Times[f, f], {f, f}],
Padding -> None] & /@ {img1in, img2in};
{img1in, img2in} =
ImageTake[#, {1, -1, f}, {1, -1, f}] & /@ {img1in, img2in};
];
mssimmap =
Function[{img1, img2},
Function[{mu1, mu2},
Function[{mu1mu1, mu2mu2, mu1mu2},
ImageApply[#1/#2 &,
{ImageMultiply[
ImageAdd[
ImageMultiply[mu1mu2, 2.],
k1^2],
ImageAdd[
ImageMultiply[
ImageSubtract[
ImageCorrelate[
ImageMultiply[img1, img2],
window, Padding -> None],
mu1mu2],
2.],
k2^2]
],
ImageMultiply[
ImageAdd[
ImageAdd[mu1mu1, mu2mu2],
k1^2],
ImageAdd[
ImageAdd[
ImageSubtract[
ImageCorrelate[
ImageMultiply[img1, img1],
window, Padding -> None],
mu1mu1],
ImageSubtract[
ImageCorrelate[
ImageMultiply[img2, img2],
window, Padding -> None],
mu2mu2]],
k2^2]
]
}
]
][
ImageMultiply[mu1, mu1],
ImageMultiply[mu2, mu2],
ImageMultiply[mu1, mu2]
]
][
ImageCorrelate[img1, window, Padding -> None],
ImageCorrelate[img2, window, Padding -> None]
]
][img1in, img2in
];
mssim = ImageMeasurements[mssimmap, "Mean"];
Return@mssim;
];
I more or less consequently was using the built-in routines to handle Image
data.
I have checked the examples given under http://www.cns.nyu.edu/~lcv/ssim:
urlbase = "http://www.cns.nyu.edu/~lcv/ssim/";
imagelist = {"einstein.gif", "meanshift.gif", "contrast.gif", "impulse.gif", "blur.gif", "jpg.gif"};
Function[{name1, name2},
Column[{Last@StringSplit[name2, "/"], Show@#2,
"Mean SSIM: " <> ToString@MyMSSIM[#1, #2]}, Center] &[
Import@name1, Import@name2]][urlbase <> First@imagelist,
urlbase <> #] & /@ imagelist
Please re-check and compare by your own. I also compared a plenty of mean SSIM values for image pairs one-by-one with the output of the Matlab code and found no differences within the precision of the displayed mantissa.
What I skipped in the original Matlab code was any check of minimum image sizes and something like that, and I also did not cover the unlikely case that someone will operate with either k1
or k2
set to 0
. Their presets (0.01 and 0.03, resp.) as well as parameters for the Gaussian smoothing kernel window
were also taken from that code. As you can see in my code, I also mimic the strange smoothing using a box filter in case of a sub-sampling of images larger than 383 pixels at their short edge. And I generally did exactly the same edge handling using certain Padding
settings.
Please look at two basic examples (part of Wang's demo as the other examples given above):
MyMSSIM[Import@"http://www.cns.nyu.edu/~lcv/ssim/einstein.gif",
Import@"http://www.cns.nyu.edu/~lcv/ssim/meanshift.gif"]
gives
0.988359
for
and 
while
MyMSSIM[Import@"http://www.cns.nyu.edu/~lcv/ssim/einstein.gif",
Import@"http://www.cns.nyu.edu/~lcv/ssim/jpg.gif"]
gives
0.662363
for
and 
There is a very small change I made, as the original Matlab code is intended for scalar images and cannot handle RGB images, in case someone inserts RGB images here, I convert them to real valued luminance images. As values now are in the interval [0.,1.], I could cancel the parameter L in the original code, indicating the maximum pixel value. BTW, output range is between -1 and 1, while for typical image pairs the result will be positive.
What I did not consider so far was any multi-scale implementation of the Structural SIMilarity (SSIM), called MS-SSIM msssim.m