計算matlab中影象的PSNR和SSIM
阿新 • • 發佈:2018-12-14
網上找了很多關於PSNR和SSIM的計算,很多結果算出來都不一樣,公式都是普遍的,如下:
現在總結下造成結果差異的原因。
PSNR的差異:
1.灰度影象:灰度影象比較好計算,只有一個灰度值。
2.彩色影象:
(a)可以將分別計算R,G,B三個通道總和,最後MSE直接在原公式上多除以3就行(opencv官方代
(b)將R,G,B格式轉換為YCbCr,只計算Y分量(亮度分量),結果會比直接計算要高几個dB。
貼程式碼,這裡是將圖片格式轉成YCbCr(只計算Y分量):
- function [PSNR, MSE] = psnr(X, Y)
- %%%%%%%%%%%%%%%%%%%%%%%%%%%
- %
- % 計算峰值信噪比PSNR
- % 將RGB轉成YCbCr格式進行計算
- % 如果直接計算會比轉後計算值要小2dB左右(當然是個別測試)
- %
- %%%%%%%%%%%%%%%%%%%%%%%%%%%
- if size(X,3)~=1 %判斷影象時不是彩色圖,如果是,結果為3,否則為1
- org=rgb2ycbcr(X);
- test=rgb2ycbcr(Y);
- Y1=org(:,:,1);
- Y2=test(:,:,1);
- Y1=double(Y1); %計算平方時候需要轉成double型別,否則uchar型別會丟失資料
- Y2=double(Y2);
- else %灰度影象,不用轉換
- Y1=double(X);
- Y2=double(Y);
- end
- if nargin<2
- D = Y1;
- else
- if any(size(Y1)~=size(Y2))
- error('The input size is not equal to each other!');
- end
- D = Y1 - Y2;
- end
- MSE = sum(D(:).*D(:)) / numel(Y1);
- PSNR = 10*log10(255^2 / MSE);
控制檯輸入下面三條語句:
- >> X= imread('C:\Users\Administrator\Desktop\noise_image.jpg'
); - >> Y= imread('C:\Users\Administrator\Desktop\actruel_image.jpg');
- >> psnr(X, Y)
SSIM的差異:同上,如果直接不轉換成YCbCr格式,結果會偏高很多(matlab中,SSIM提出者【1】,程式碼)。opencv裡面是分別計算了R,G,B三個分量的SSIM值(官方程式碼)。最後我將3個值取了個平均(這個值比matlab裡面低很多)。
以下程式碼主要是參考原作者修改的,原始碼是直接沒有進行格式轉換,直接RGB格式,下面我是將他轉換成YCbCr計算圖片的SSIM
- function [mssim, ssim_map] = ssim(img1, img2, K, window, L)
- %========================================================================
- %SSIM Index, Version 1.0
- %Copyright(c) 2003 Zhou Wang
- %All Rights Reserved.
- %
- %The author is with Howard Hughes Medical Institute, and Laboratory
- %for Computational Vision at Center for Neural Science and Courant
- %Institute of Mathematical Sciences, New York University.
- %
- %----------------------------------------------------------------------
- %Permission to use, copy, or modify this software and its documentation
- %for educational and research purposes only and without fee is hereby
- %granted, provided that this copyright notice and the original authors'
- %names ap pearon all copies and supporting documentation. This program
- %shall not be used, rewritten, or adapted as the basis of a commercial
- %software or hardware product without first obtaining permission of the
- %authors. The authors make no representations about the suitability of
- %this software for any purpose. It is provided "as is" without express
- %or implied warranty.
- %----------------------------------------------------------------------
- %
- %This is an implementation of the algorithm for calculating the
- %Structural SIMilarity (SSIM) index between two images. Please refer
- %to the following paper:
- %
- %Z. Wang, A. C. Bovik, H. R. Sheikh, and E. P. Simoncelli, "Image
- %quality assessment: From error visibility to structural similarity"
- %IEEE Transactios on Image Processing, vol. 13, no. 4, pp.600-612,
- %Apr. 2004.
- %
- %Kindly report any suggestions or corrections to [email protected]
- %
- %----------------------------------------------------------------------
- %
- %Input : (1) img1: the first image being compared
- % (2) img2: the second image being compared
- % (3) K: constants in the SSIM index formula (see the above
- % reference). defualt value: K = [0.01 0.03]
- % (4) window: local window for statistics (see the above
- % reference). default widnow is Gaussian given by
- % window = fspecial('gaussian', 11, 1.5);
- % (5) L: dynamic range of the images. default: L = 255
- %
- %Output: (1) mssim: the mean SSIM index value between 2 images.
- % If one of the images being compared is regarded as
- % perfect quality, then mssim can be considered as the
- % quality measure of the other image.
- % If img1 = img2, then mssim = 1.
- % (2) ssim_map: the SSIM index map of the test image. The map
- % has a smaller size than the input images. The actual size:
- % size(img1) - size(window) + 1.
- %
- %Default Usage:
- % Given 2 test images img1 and img2, whose dynamic range is 0-255
- %
- % [mssim ssim_map] = ssim_index(img1, img2);
- %
- %Advanced Usage:
- % User defined parameters. For example
- %
- % K = [0.05 0.05];
- % window = ones(8);
- % L = 100;
- % [mssim ssim_map] = ssim_index(img1, img2, K, window, L);
- %
- %See the results:
- %
- % mssim %Gives the mssim value
- % imshow(max(0, ssim_map).^4) %Shows the SSIM index map
- %
- %========================================================================
- if (nargin < 2 | nargin > 5)
- ssim_index = -Inf;
- ssim_map = -Inf;
- return;
- end
- if (size(img1) ~= size(img2))
- ssim_index = -Inf;
- ssim_map = -Inf;
- return;
- end
- [M N] = size(img1);
- if (nargin == 2)
- if ((M < 11) | (N < 11)) % 影象大小過小,則沒有意義。
- ssim_index = -Inf;
- ssim_map = -Inf;
- return
- end
- window = fspecial('gaussian', 11, 1.5); % 引數一個標準偏差1.5,11*11的高斯低通濾波。
- K(1) = 0.01; % default settings
- K(2) = 0.03;
- L = 255;
- end
- if (nargin == 3)
- if ((M < 11) | (N < 11))
- ssim_index = -Inf;
- ssim_map = -Inf;
- return
- end
- window = fspecial('gaussian', 11, 1.5);
- L = 255;
- if (length(K) == 2)
- if (K(1) < 0 | K(2) < 0)
- ssim_index = -Inf;
- ssim_map = -Inf;
- return;
- end
- else
- ssim_index = -Inf;
- ssim_map = -Inf;
- return;
- end
- end
- if (nargin == 4)
- [H W] = size(window);
- if ((H*W) < 4 | (H > M) | (W > N))
- ssim_index = -Inf;
- ssim_map = -Inf;
- return
- end
- L = 255;
- if (length(K) == 2)
- if (K(1) < 0 | K(2) < 0)
- ssim_index = -Inf;
- ssim_map = -Inf;
- return;
- end
- else
- ssim_index = -Inf;
- ssim_map = -Inf;
- return;
- end
- end
- if (nargin == 5)
- [H W] = size(window);
- if ((H*W) < 4 | (H > M) | (W > N))
- ssim_index = -Inf;
- ssim_map = -Inf;
- return
- end
- if (length(K) == 2)
- if (K(1) < 0 | K(2) < 0)
- ssim_index = -Inf;
- ssim_map = -Inf;
- return;
- end
- else
- ssim_index = -Inf;
- ssim_map = -Inf;
- return;
- end
- end
- if size(img1,3)~=1 %判斷影象時不是彩色圖,如果是,結果為3,否則為1
- org=rgb2ycbcr(img1);
- test=rgb2ycbcr(img2);
- y1=org(:,:,1);
- y2=test(:,:,1);
- y1=double(y1);
- y2=double(y2);
- else
- y1=double(img1);
- y2=double(img2);
- end
- img1 = double(y1);
- img2 = double(y2);
- % automatic downsampling
- %f = max(1,round(min(M,N)/256));
- %downsampling by f
- %use a simple low-pass filter
- % if(f>1)
- % lpf = ones(f,f);
- % lpf = lpf/sum(lpf(:));
- % img1 = imfilter(img1,lpf,'symmetric','same');
- % img2 = imfilter(img2,lpf,'symmetric','same');
- % img1 = img1(1:f:end,1:f:end);
- % img2 = img2(1:f:end,1:f:end);
- % end
- C1 = (K(1)*L)^2; % 計算C1引數,給亮度L(x,y)用。 C1=6.502500
- C2 = (K(2)*L)^2; % 計算C2引數,給對比度C(x,y)用。 C2=58.522500
- window = window/sum(sum(window)); %濾波器歸一化操作。
- mu1 = filter2(window, img1, 'valid'); % 對影象進行濾波因子加權 valid改成same結果會低一丟丟
- mu2 = filter2(window, img2, 'valid'); % 對影象進行濾波因子加權
- mu1_sq = mu1.*mu1; % 計算出Ux平方值。
- mu2_sq = mu2.*mu2; % 計算出Uy平方值。
- mu1_mu2 = mu1.*mu2; % 計算Ux*Uy值。
- sigma1_sq = filter2(window, img1.*img1, 'valid') - mu1_sq; % 計算sigmax (標準差)
- sigma2_sq = filter2(window, img2.*img2, 'valid') - mu2_sq; % 計算sigmay (標準差)
- sigma12 = filter2(window, img1.*img2, 'valid') - mu1_mu2; % 計算sigmaxy(標準差)
- if (C1 > 0 & C2 > 0)
- ssim_map = ((2*mu1_mu2 + C1).*(2*sigma12 + C2))./((mu1_sq + mu2_sq + C1).*(sigma1_sq + sigma2_sq + C2));
- else
- numerator1 = 2*mu1_mu2 + C1;
- numerator2 = 2*sigma12 + C2;
- denominator1 = mu1_sq + mu2_sq + C1;
- denominator2 = sigma1_sq + sigma2_sq + C2;
- ssim_map = ones(size(mu1));
- index = (denominator1.*denominator2 > 0);
- ssim_map(index) = (numerator1(index).*numerator2(index))./(denominator1(index).*denominator2(index));
- index = (denominator1 ~= 0) & (denominator2 == 0);
- ssim_map(index) = numerator1(index)./denominator1(index);
- end
- mssim = mean2(ssim_map);
- return
控制檯輸入以下程式碼:
- >> img1= imread('C:\Users\Administrator\Desktop\noise_image.jpg');
- >> img2= imread('C:\Users\Administrator\Desktop\actruel_image.jpg');
- >> ssim(img1,img2)
最後說一句,不管是結果如何,只要對比實驗用的同一種評價程式碼工具,無所謂結果和原論文一不一樣,問題是很多論文實驗都搞不出來滴
參考文獻
【1】Wang Z, Bovik A C, Sheikh H R, et al. Image quality assessment: from error visibility to structural similarity[J]. IEEE Transactions on Image Processing, 2004, 13(4):600-612.