Calculation of psnr value in RGB image. I have calculated psnr using this code. But the result obtained is a complex number. What should be the error??

2 次查看(过去 30 天)
if true
function PSNR = psnrgb(I,W)
[m,n,p] = size(I); [h,w,q] = size(W); if m ~= h n ~= w p~=q error('Two images must have the same size.') end for k=1:p for i=1:m for j=1:n delta=sum(I(i,j,k)-W(i,j,k).^2); end end end
delta = 1/(m*n*p) * delta ; PSNR = 10 * log10( 255^2/delta );
end end

回答(1 个)

DGM
DGM 2024-9-13
There are plenty of problems.
function PSNR = psnrgb(I,W)
[m,n,p] = size(I);
[h,w,q] = size(W);
% obviously, this test expression is invalid as written
if m ~= h n ~= w p ~= q
error('Two images must have the same size.')
end
% this entire set of loops does nothing but calculate
% the squared difference of the last pixel pair alone.
% while the rest of the entire image is ignored,
% the inputs are presumed to be uint8, so it's a 50% chance
% that this answer for that one single pixel is still completely wrong.
for k=1:p
for i=1:m
for j=1:n
delta = sum(I(i,j,k)-W(i,j,k).^2);
end
end
end
% convert the sum to a mean, but we don't have a sum of anything
delta = 1/(m*n*p) * delta ;
% this presumes the input is uint8
PSNR = 10 * log10( 255^2/delta );
end
We don't need to try fixing that. IPT already has psnr(). If you want to write your own, you can:
% the inputs
A = imread('peppers.png'); % the reference
B = imnoise(A,'gaussian',0,0.1); % the image to test
% compare the example against IPT psnr()
psnrrgb(B,A)
ans = 11.8650
psnr(B,A)
ans = 11.8650
function P = psnrrgb(inpict,ref)
% synopsis goes here
% test the inputs
if ~isequal(size(inpict),size(ref))
error('Images must both have the same size')
end
if ~strcmp(class(inpict),class(ref))
error('Images must both have the same class')
end
% calculate the output
err = double(inpict)-double(ref); % error
mse = mean(err(:).^2); % mean-square error
dr = diff(double(getrangefromclass(inpict))); % dynamic range for this numeric class
P = 10*log10(dr^2/mse);
end

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by