idamnjanovic@38: function [mssim, ssim_map] = ssim_index(img1, img2, K, window, L) idamnjanovic@38: idamnjanovic@38: %======================================================================== idamnjanovic@38: %SSIM Index, Version 1.0 idamnjanovic@38: %Copyright(c) 2003 Zhou Wang idamnjanovic@38: %All Rights Reserved. idamnjanovic@38: % idamnjanovic@38: %The author is with Howard Hughes Medical Institute, and Laboratory idamnjanovic@38: %for Computational Vision at Center for Neural Science and Courant idamnjanovic@38: %Institute of Mathematical Sciences, New York University. idamnjanovic@38: % idamnjanovic@38: %---------------------------------------------------------------------- idamnjanovic@38: %Permission to use, copy, or modify this software and its documentation idamnjanovic@38: %for educational and research purposes only and without fee is hereby idamnjanovic@38: %granted, provided that this copyright notice and the original authors' idamnjanovic@38: %names appear on all copies and supporting documentation. This program idamnjanovic@38: %shall not be used, rewritten, or adapted as the basis of a commercial idamnjanovic@38: %software or hardware product without first obtaining permission of the idamnjanovic@38: %authors. The authors make no representations about the suitability of idamnjanovic@38: %this software for any purpose. It is provided "as is" without express idamnjanovic@38: %or implied warranty. idamnjanovic@38: %---------------------------------------------------------------------- idamnjanovic@38: % idamnjanovic@38: %This is an implementation of the algorithm for calculating the idamnjanovic@38: %Structural SIMilarity (SSIM) index between two images. Please refer idamnjanovic@38: %to the following paper: idamnjanovic@38: % idamnjanovic@38: %Z. Wang, A. C. Bovik, H. R. Sheikh, and E. P. Simoncelli, "Image idamnjanovic@38: %quality assessment: From error measurement to structural similarity" idamnjanovic@38: %IEEE Transactios on Image Processing, vol. 13, no. 1, Jan. 2004. idamnjanovic@38: % idamnjanovic@38: %Kindly report any suggestions or corrections to zhouwang@ieee.org idamnjanovic@38: % idamnjanovic@38: %---------------------------------------------------------------------- idamnjanovic@38: % idamnjanovic@38: %Input : (1) img1: the first image being compared idamnjanovic@38: % (2) img2: the second image being compared idamnjanovic@38: % (3) K: constants in the SSIM index formula (see the above idamnjanovic@38: % reference). defualt value: K = [0.01 0.03] idamnjanovic@38: % (4) window: local window for statistics (see the above idamnjanovic@38: % reference). default widnow is Gaussian given by idamnjanovic@38: % window = fspecial('gaussian', 11, 1.5); idamnjanovic@38: % (5) L: dynamic range of the images. default: L = 255 idamnjanovic@38: % idamnjanovic@38: %Output: (1) mssim: the mean SSIM index value between 2 images. idamnjanovic@38: % If one of the images being compared is regarded as idamnjanovic@38: % perfect quality, then mssim can be considered as the idamnjanovic@38: % quality measure of the other image. idamnjanovic@38: % If img1 = img2, then mssim = 1. idamnjanovic@38: % (2) ssim_map: the SSIM index map of the test image. The map idamnjanovic@38: % has a smaller size than the input images. The actual size: idamnjanovic@38: % size(img1) - size(window) + 1. idamnjanovic@38: % idamnjanovic@38: %Default Usage: idamnjanovic@38: % Given 2 test images img1 and img2, whose dynamic range is 0-255 idamnjanovic@38: % idamnjanovic@38: % [mssim ssim_map] = ssim_index(img1, img2); idamnjanovic@38: % idamnjanovic@38: %Advanced Usage: idamnjanovic@38: % User defined parameters. For example idamnjanovic@38: % idamnjanovic@38: % K = [0.05 0.05]; idamnjanovic@38: % window = ones(8); idamnjanovic@38: % L = 100; idamnjanovic@38: % [mssim ssim_map] = ssim_index(img1, img2, K, window, L); idamnjanovic@38: % idamnjanovic@38: %See the results: idamnjanovic@38: % idamnjanovic@38: % mssim %Gives the mssim value idamnjanovic@38: % imshow(max(0, ssim_map).^4) %Shows the SSIM index map idamnjanovic@38: % idamnjanovic@38: %======================================================================== idamnjanovic@38: idamnjanovic@38: idamnjanovic@38: if (nargin < 2 || nargin > 5) idamnjanovic@38: ssim_index = -Inf; idamnjanovic@38: ssim_map = -Inf; idamnjanovic@38: return; idamnjanovic@38: end idamnjanovic@38: idamnjanovic@38: if (size(img1) ~= size(img2)) idamnjanovic@38: ssim_index = -Inf; idamnjanovic@38: ssim_map = -Inf; idamnjanovic@38: return; idamnjanovic@38: end idamnjanovic@38: idamnjanovic@38: [M N] = size(img1); idamnjanovic@38: idamnjanovic@38: if (nargin == 2) idamnjanovic@38: if ((M < 11) || (N < 11)) idamnjanovic@38: ssim_index = -Inf; idamnjanovic@38: ssim_map = -Inf; idamnjanovic@38: return idamnjanovic@38: end idamnjanovic@38: window = fspecial('gaussian', 11, 1.5); % idamnjanovic@38: K(1) = 0.01; % default settings idamnjanovic@38: K(2) = 0.03; % idamnjanovic@38: L = 255; % idamnjanovic@38: end idamnjanovic@38: idamnjanovic@38: if (nargin == 3) idamnjanovic@38: if ((M < 11) || (N < 11)) idamnjanovic@38: ssim_index = -Inf; idamnjanovic@38: ssim_map = -Inf; idamnjanovic@38: return idamnjanovic@38: end idamnjanovic@38: window = fspecial('gaussian', 11, 1.5); idamnjanovic@38: L = 255; idamnjanovic@38: if (length(K) == 2) idamnjanovic@38: if (K(1) < 0 || K(2) < 0) idamnjanovic@38: ssim_index = -Inf; idamnjanovic@38: ssim_map = -Inf; idamnjanovic@38: return; idamnjanovic@38: end idamnjanovic@38: else idamnjanovic@38: ssim_index = -Inf; idamnjanovic@38: ssim_map = -Inf; idamnjanovic@38: return; idamnjanovic@38: end idamnjanovic@38: end idamnjanovic@38: idamnjanovic@38: if (nargin == 4) idamnjanovic@38: [H W] = size(window); idamnjanovic@38: if ((H*W) < 4 || (H > M) || (W > N)) idamnjanovic@38: ssim_index = -Inf; idamnjanovic@38: ssim_map = -Inf; idamnjanovic@38: return idamnjanovic@38: end idamnjanovic@38: L = 255; idamnjanovic@38: if (length(K) == 2) idamnjanovic@38: if (K(1) < 0 || K(2) < 0) idamnjanovic@38: ssim_index = -Inf; idamnjanovic@38: ssim_map = -Inf; idamnjanovic@38: return; idamnjanovic@38: end idamnjanovic@38: else idamnjanovic@38: ssim_index = -Inf; idamnjanovic@38: ssim_map = -Inf; idamnjanovic@38: return; idamnjanovic@38: end idamnjanovic@38: end idamnjanovic@38: idamnjanovic@38: if (nargin == 5) idamnjanovic@38: [H W] = size(window); idamnjanovic@38: if ((H*W) < 4 || (H > M) || (W > N)) idamnjanovic@38: ssim_index = -Inf; idamnjanovic@38: ssim_map = -Inf; idamnjanovic@38: return idamnjanovic@38: end idamnjanovic@38: if (length(K) == 2) idamnjanovic@38: if (K(1) < 0 || K(2) < 0) idamnjanovic@38: ssim_index = -Inf; idamnjanovic@38: ssim_map = -Inf; idamnjanovic@38: return; idamnjanovic@38: end idamnjanovic@38: else idamnjanovic@38: ssim_index = -Inf; idamnjanovic@38: ssim_map = -Inf; idamnjanovic@38: return; idamnjanovic@38: end idamnjanovic@38: end idamnjanovic@38: idamnjanovic@38: C1 = (K(1)*L)^2; idamnjanovic@38: C2 = (K(2)*L)^2; idamnjanovic@38: window = window/sum(sum(window)); idamnjanovic@38: img1 = double(img1); idamnjanovic@38: img2 = double(img2); idamnjanovic@38: idamnjanovic@38: mu1 = filter2(window, img1, 'valid'); idamnjanovic@38: mu2 = filter2(window, img2, 'valid'); idamnjanovic@38: mu1_sq = mu1.*mu1; idamnjanovic@38: mu2_sq = mu2.*mu2; idamnjanovic@38: mu1_mu2 = mu1.*mu2; idamnjanovic@38: sigma1_sq = filter2(window, img1.*img1, 'valid') - mu1_sq; idamnjanovic@38: sigma2_sq = filter2(window, img2.*img2, 'valid') - mu2_sq; idamnjanovic@38: sigma12 = filter2(window, img1.*img2, 'valid') - mu1_mu2; idamnjanovic@38: idamnjanovic@38: if (C1 > 0 & C2 > 0) idamnjanovic@38: ssim_map = ((2*mu1_mu2 + C1).*(2*sigma12 + C2))./((mu1_sq + mu2_sq + C1).*(sigma1_sq + sigma2_sq + C2)); idamnjanovic@38: else idamnjanovic@38: numerator1 = 2*mu1_mu2 + C1; idamnjanovic@38: numerator2 = 2*sigma12 + C2; idamnjanovic@38: denominator1 = mu1_sq + mu2_sq + C1; idamnjanovic@38: denominator2 = sigma1_sq + sigma2_sq + C2; idamnjanovic@38: ssim_map = ones(size(mu1)); idamnjanovic@38: index = (denominator1.*denominator2 > 0); idamnjanovic@38: ssim_map(index) = (numerator1(index).*numerator2(index))./(denominator1(index).*denominator2(index)); idamnjanovic@38: index = (denominator1 ~= 0) & (denominator2 == 0); idamnjanovic@38: ssim_map(index) = numerator1(index)./denominator1(index); idamnjanovic@38: end idamnjanovic@38: idamnjanovic@38: mssim = mean2(ssim_map); idamnjanovic@38: idamnjanovic@38: return