comparison util/ssim_index.m @ 38:84b7c44e54c4

(none)
author idamnjanovic
date Mon, 14 Mar 2011 15:33:18 +0000
parents
children
comparison
equal deleted inserted replaced
37:d80c103d9876 38:84b7c44e54c4
1 function [mssim, ssim_map] = ssim_index(img1, img2, K, window, L)
2
3 %========================================================================
4 %SSIM Index, Version 1.0
5 %Copyright(c) 2003 Zhou Wang
6 %All Rights Reserved.
7 %
8 %The author is with Howard Hughes Medical Institute, and Laboratory
9 %for Computational Vision at Center for Neural Science and Courant
10 %Institute of Mathematical Sciences, New York University.
11 %
12 %----------------------------------------------------------------------
13 %Permission to use, copy, or modify this software and its documentation
14 %for educational and research purposes only and without fee is hereby
15 %granted, provided that this copyright notice and the original authors'
16 %names appear on all copies and supporting documentation. This program
17 %shall not be used, rewritten, or adapted as the basis of a commercial
18 %software or hardware product without first obtaining permission of the
19 %authors. The authors make no representations about the suitability of
20 %this software for any purpose. It is provided "as is" without express
21 %or implied warranty.
22 %----------------------------------------------------------------------
23 %
24 %This is an implementation of the algorithm for calculating the
25 %Structural SIMilarity (SSIM) index between two images. Please refer
26 %to the following paper:
27 %
28 %Z. Wang, A. C. Bovik, H. R. Sheikh, and E. P. Simoncelli, "Image
29 %quality assessment: From error measurement to structural similarity"
30 %IEEE Transactios on Image Processing, vol. 13, no. 1, Jan. 2004.
31 %
32 %Kindly report any suggestions or corrections to zhouwang@ieee.org
33 %
34 %----------------------------------------------------------------------
35 %
36 %Input : (1) img1: the first image being compared
37 % (2) img2: the second image being compared
38 % (3) K: constants in the SSIM index formula (see the above
39 % reference). defualt value: K = [0.01 0.03]
40 % (4) window: local window for statistics (see the above
41 % reference). default widnow is Gaussian given by
42 % window = fspecial('gaussian', 11, 1.5);
43 % (5) L: dynamic range of the images. default: L = 255
44 %
45 %Output: (1) mssim: the mean SSIM index value between 2 images.
46 % If one of the images being compared is regarded as
47 % perfect quality, then mssim can be considered as the
48 % quality measure of the other image.
49 % If img1 = img2, then mssim = 1.
50 % (2) ssim_map: the SSIM index map of the test image. The map
51 % has a smaller size than the input images. The actual size:
52 % size(img1) - size(window) + 1.
53 %
54 %Default Usage:
55 % Given 2 test images img1 and img2, whose dynamic range is 0-255
56 %
57 % [mssim ssim_map] = ssim_index(img1, img2);
58 %
59 %Advanced Usage:
60 % User defined parameters. For example
61 %
62 % K = [0.05 0.05];
63 % window = ones(8);
64 % L = 100;
65 % [mssim ssim_map] = ssim_index(img1, img2, K, window, L);
66 %
67 %See the results:
68 %
69 % mssim %Gives the mssim value
70 % imshow(max(0, ssim_map).^4) %Shows the SSIM index map
71 %
72 %========================================================================
73
74
75 if (nargin < 2 || nargin > 5)
76 ssim_index = -Inf;
77 ssim_map = -Inf;
78 return;
79 end
80
81 if (size(img1) ~= size(img2))
82 ssim_index = -Inf;
83 ssim_map = -Inf;
84 return;
85 end
86
87 [M N] = size(img1);
88
89 if (nargin == 2)
90 if ((M < 11) || (N < 11))
91 ssim_index = -Inf;
92 ssim_map = -Inf;
93 return
94 end
95 window = fspecial('gaussian', 11, 1.5); %
96 K(1) = 0.01; % default settings
97 K(2) = 0.03; %
98 L = 255; %
99 end
100
101 if (nargin == 3)
102 if ((M < 11) || (N < 11))
103 ssim_index = -Inf;
104 ssim_map = -Inf;
105 return
106 end
107 window = fspecial('gaussian', 11, 1.5);
108 L = 255;
109 if (length(K) == 2)
110 if (K(1) < 0 || K(2) < 0)
111 ssim_index = -Inf;
112 ssim_map = -Inf;
113 return;
114 end
115 else
116 ssim_index = -Inf;
117 ssim_map = -Inf;
118 return;
119 end
120 end
121
122 if (nargin == 4)
123 [H W] = size(window);
124 if ((H*W) < 4 || (H > M) || (W > N))
125 ssim_index = -Inf;
126 ssim_map = -Inf;
127 return
128 end
129 L = 255;
130 if (length(K) == 2)
131 if (K(1) < 0 || K(2) < 0)
132 ssim_index = -Inf;
133 ssim_map = -Inf;
134 return;
135 end
136 else
137 ssim_index = -Inf;
138 ssim_map = -Inf;
139 return;
140 end
141 end
142
143 if (nargin == 5)
144 [H W] = size(window);
145 if ((H*W) < 4 || (H > M) || (W > N))
146 ssim_index = -Inf;
147 ssim_map = -Inf;
148 return
149 end
150 if (length(K) == 2)
151 if (K(1) < 0 || K(2) < 0)
152 ssim_index = -Inf;
153 ssim_map = -Inf;
154 return;
155 end
156 else
157 ssim_index = -Inf;
158 ssim_map = -Inf;
159 return;
160 end
161 end
162
163 C1 = (K(1)*L)^2;
164 C2 = (K(2)*L)^2;
165 window = window/sum(sum(window));
166 img1 = double(img1);
167 img2 = double(img2);
168
169 mu1 = filter2(window, img1, 'valid');
170 mu2 = filter2(window, img2, 'valid');
171 mu1_sq = mu1.*mu1;
172 mu2_sq = mu2.*mu2;
173 mu1_mu2 = mu1.*mu2;
174 sigma1_sq = filter2(window, img1.*img1, 'valid') - mu1_sq;
175 sigma2_sq = filter2(window, img2.*img2, 'valid') - mu2_sq;
176 sigma12 = filter2(window, img1.*img2, 'valid') - mu1_mu2;
177
178 if (C1 > 0 & C2 > 0)
179 ssim_map = ((2*mu1_mu2 + C1).*(2*sigma12 + C2))./((mu1_sq + mu2_sq + C1).*(sigma1_sq + sigma2_sq + C2));
180 else
181 numerator1 = 2*mu1_mu2 + C1;
182 numerator2 = 2*sigma12 + C2;
183 denominator1 = mu1_sq + mu2_sq + C1;
184 denominator2 = sigma1_sq + sigma2_sq + C2;
185 ssim_map = ones(size(mu1));
186 index = (denominator1.*denominator2 > 0);
187 ssim_map(index) = (numerator1(index).*numerator2(index))./(denominator1(index).*denominator2(index));
188 index = (denominator1 ~= 0) & (denominator2 == 0);
189 ssim_map(index) = numerator1(index)./denominator1(index);
190 end
191
192 mssim = mean2(ssim_map);
193
194 return