均方误差(MSE)问题

时间:2013-07-23 04:29:29

标签: matlab

当我执行以下代码以在不同的计算机(MATLAB)上找到均方误差时,我会收到不同的答案。有没有人知道为什么会这样?

function [mse,psnr] = mse_psnr(im1,im2)

N = size(im1);
acc = 0;

for k1=1:N(1)
  for k2=1:N(2)
     acc = acc+ ( im1(k1,k2) - im2(k1,k2) )^2;   
  end
end

mse  = acc/(N(1)*N(2));
psnr = 10*log10((255^2)/mse);

1 个答案:

答案 0 :(得分:0)

请尝试对代码进行矢量化以删除嵌套循环:

function [mse psnr] = mse_psnr( im1, im2 )
% make sure input data type is double
im1 = double(im1);
im2 = double(im2);
acc = sum( (im1(:)-im2(:)).^2 ); % instead of nested loop...
mse = acc / numel(im1);
psnr = 10*log10( (255^2)/mse );