Info

此问题已关闭。 请重新打开它进行编辑或回答。

Hi all...i am writing a code to calculate the PSNR of two images....but i am getting an error as 'Attempted to access A1(1,401); index out of bounds because size(A1)=[​400,400].'​..Here A1 is the R component of image.. The code is shown below

1 次查看(过去 30 天)
clc;
close all;
clear all;
A = imread('F:\EXPERIMENTION RESULTS\fruits_ORIGINAL.jpg');
A = imresize(A ,[400 400]);
A = double(A);
A1 = A(:,:,1);
A2 = A(:,:,2);
A3 = A(:,:,3);
B = imread('F:\EXPERIMENTION RESULTS\fruits_HISTEQ.jpg');
B = imresize(B , [400 400]);
B = double(B);
B1 = B(:,:,1);
B2 = B(:,:,2);
B3 = B(:,:,3);
[m n] = size(A);
[m2 n2] = size(B);
if m ~= m2 || n ~= n2
error('sizes do not match')
end
msevalue1 = 0;
for i = 1:m
for j = 1:n
msevalue1 = msevalue1 + ((A1(i,j) - B1(i,j)))^2;
end
end
msevalue1 = msevalue1/(m*n);
if msevalue1 == 0
error ('verify the image correctly')
end
signalval1 = 0;
for i = i:m
for j = 1:n
signalval1 = signalval1 + (A1(i,j))^2;
end
end
signalval1 = signalval1/(m*n);
snrval1 = signalval1/msevalue1;
snrvalue1 = 10*log10(snrval1);
psnrvalue1 = 255^2/msevalue1;
psnrvalue1 = 10*log10(psnrvalue1);
msevalue2 = 0;
for i = 1:m
for j = 1:n
msevalue1 = msevalue1 + ((A2(i,j) - B2(i,j)))^2;
end
end
msevalue2 = msevalue2/(m*n);
if msevalue2 == 0
error ('verify the image correctly')
end
signalval2 = 0;
for i = i:m
for j = 1:n
signalval2 = signalval2 + (A2(i,j))^2;
end
end
signalval2 = signalval2/(m*n);
snrval2 = signalval2/msevalue2;
snrvalue2 = 10*log10(snrval2);
psnrvalue2 = 255^2/msevalue2;
psnrvalue2 = 10*log10(psnrvalue2);
msevalue3 = 0;
for i = 1:m
for j = 1:n
msevalue3 = msevalue3 + ((A1(i,j) - B1(i,j)))^2;
end
end
msevalue3 = msevalue3/(m*n);
if msevalue3 == 0
error ('verify the image correctly')
end
signalval3 = 0;
for i = i:m
for j = 1:n
signalval3 = signalval3 + (A3(i,j))^2;
end
end
signalval3 = signalval3/(m*n);
snrval3 = signalval3/msevalue3;
snrvalue3 = 10*log10(snrval3);
psnrvalue3 = 255^2/msevalue3;
psnrvalue3 = 10*log10(psnrvalue3);
psnr=(psnrvalue1+psnrvalue2+psnrvalue3)/3

回答(2 个)

Image Analyst
Image Analyst 2014-4-29
psnr() was introduced in the R2014a Image Processing Toolbox. I suggest you use that. Or you can use my attached demo.
The reason for your error is that you can't do this:
[m n] = size(A);
[m2 n2] = size(B);
because A and B are color images. You need to do this:
[m, n, numberOfColorChannels] = size(A);
[m2, n2, numberOfColorChannels2] = size(B);

Geoff Hayes
Geoff Hayes 2014-4-29
Please edit the above code so that it readable - use the {}Code button to do so.
One thing I noticed is that for a couple of your outer for loops, you are iterating using the variable i starting from i and ending at m (which is 400). This should be fixed (unless done on purpose?).
The problem is with determining the number of rows and columns for matrices A1,A2,A3:
[m n] = size(A);
A is three dimensional and so is of size 400x400x3. In the above code, if you supply only two outputs ( m and n) for a three dimensional matrix, then m=400 (as expected) but n=1200 which is the product of the last two dimensions (400*3). This behaviour is documented in the size command help info (type help size at the command window for details). And this explains the error that is being raised as j iterates from 1 to 1200 ( n ) and so fails at j==401.
Since you are using m and n to access the elements of the A1,A2, and A3 matrices - all three of which are the same size - then just replace the above with:
[m,n] = size(A1);
The same error exists for m2 and n2.

此问题已关闭。

标签

Community Treasure Hunt

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

Start Hunting!

Translated by