0

当我执行以下代码以查找不同计算机 (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);
4

1 回答 1

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 );
于 2013-07-23T05:25:55.717 回答