How to resolve the "Index exceeds matrix dimensions" error ?

2 次查看(过去 30 天)
Hi, I did some coding for lossless image compression and I want to calculate the entropy value. the error "Index exceeds matrix dimensions" keeps appearing at line _FreqPixInt(PixPos(x,y)+1)=FreqPixInt(PixPos(x,y)+1)+1. I don't know how to solve. Please help me. Below are my coding ;
clc
clear
x1= imread('barbara.bmp');
im=double(x1);
En1 = entropy(x1);
% Start from the Haar wavelet and get the
% corresponding lifting scheme.
lshaar = liftwave('haar');
% Add a primal ELS to the lifting scheme.
els = {'p',[-0.125 0.125],0};
lshaarInt = liftwave('haar','int2int');
lsnewInt = addlift(lshaarInt,els);
[cAint,cHint,cVint,cDint] = lwt2(im,lsnewInt);
x2=double(cAint);
[cAint2,cHint2,cVint2,cDint2] = lwt2(x2,lsnewInt);
test=[cAint2,cHint2;cVint2,cDint2];
xRecInt2 = ilwt2(cAint2,cHint2,cVint2,cDint2,lsnewInt);
errInt2 = max(max(abs(x2-xRecInt2)))
one(1:128,1:128)=int8(cAint2);
one(129:256,1:128)=int8(cHint2);
one(1:128,129:256)=int8(cVint2);
one(129:256,129:256)=int8(cDint2);
level2=[cAint2,cHint2;cVint2,cDint2];
two=[level2,cHint;cVint,cDint];
test1=[level2,cHint;cVint,cDint];
figure(3);
imshow(int8(two));
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%entropy%%%%%%%%%%%%%%%%%%%%%%%%%%
%convert to positive integer - kalau tak convert tak boleh kira
for y=1:512;
for x=1:512;
if test1(x,y)==0;
PixPos(x,y)=1;
else if test1(x,y)>0;
PixPos(x,y)=(test1(x,y)*2)+1;
else
PixPos(x,y)=abs(test1(x,y))*2;
end
end
end
end
%calculate frequency occurence of symbols
FreqPixInt(1:512)=0;
for y=1:512 %all pixel values in rows
for x=1:512 %all pixel values in columns
FreqPixInt(PixPos(x,y)+1)=FreqPixInt(PixPos(x,y)+1)+1;
end
end
FreqPixInt;
%calculate entropy of symbols(positive integer)
En2=0;% initialize value entropy to 0
for i=1:512 % all pixels value in first rows
if FreqPixInt(i)==0
En2=En2; %dummy
else
En2=En2+(FreqPixInt(i)/262144)*log2(FreqPixInt(i)/262144); %512x512=262144 total pixels
end
end
En2=En2*(-1)
  2 个评论
Bob Thompson
Bob Thompson 2018-2-26
FreqPixInt is a 1x512 array, but when you call PixPos(x,y)+1 at x = 512 or y = 512 you're trying to look at the 513 element of FreqPixInt.
nur aqila
nur aqila 2018-2-28
so what should I change? this coding only works with certain image, some image gives the same error. I only used 512x512 image for this coding

请先登录,再进行评论。

采纳的回答

Cam Salzberger
Cam Salzberger 2018-2-26
Hello Nur,
There's a lot to go through there, and a lot of loop iterations as well. I suggest that you run this command in the Command Window:
dbstop if error
then rerun the code. Now when the error occurs, it will stop in debug mode at that point. You can see what are the values of x, y, PixPos(x, y), test1(x, y), and so on. You should be able to see that the value is (probably) exceeding 512, and then you can work backwards to figure out why that is and whether something should be calculated or handled differently.
Also, it doesn't really matter since your matrices are square, but the first index is row, and the second index is column. You seem to have conflicting comments and possibly code.
Also it should be using "elseif" not "else if". That results in some misaligned indentation and an extra "end" statement, though it won't actually have an impact on the output in this case.
-Cam
  2 个评论
nur aqila
nur aqila 2018-2-28
Thanks for the suggestion. I already tried. my coding only works with the certain image. Some image gives the same error. I only used 512x512 image for this coding. Do you have any suggestion where should I change ?
Cam Salzberger
Cam Salzberger 2018-2-28
As per Bob's suggestion, it may help to determine if you are attempting to index into the 513th pixel at any point, and where. Then you should go through the logic you used to write the code and determine if the code is correct. If you do need to access the "i+1" pixel, then maybe the loop should be limited to n-1. Or maybe you are calculating the index incorrectly. It's not really for us to tell you how to code, since only you know what your code should be doing.
-Cam

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Images 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by