MATLAB Answers

0

index exceeds matrix dimensions

Asked by Shah Bano on 13 Jun 2019
Latest activity Edited by per isakson
on 14 Jun 2019
I am tring to code the parallelogram haar like features and while calculating the sum of intensities in the parallelogram region I get this error.
labeledImage = bwlabel(TP1);
measurements = regionprops(labeledImage, 'BoundingBox');
allBB = zeros(length(measurements), 4);
for k = 1 : length(measurements)
thisBB = round(measurements(k).BoundingBox);
allBB(k, :) = thisBB;
[x, y, w1, h1] = deal(thisBB(1),thisBB(2),thisBB(3),thisBB(4));
end
SP1 = TP1(y+h1-1,-(x+w1-h1)) + TP1(-(x-h1),y+h1-1) - TP1(x+w1,y-1) - TP1(x,y-1); % (ERROR LINE)
if any one is getting my point than kindly help me.

  1 Comment

When you put in a breakpoint at the assignment to SP1, then what shows up as the values of x, y, w1, h1, and what shows up as size(TP1) ?
Note that you are overwriting x, y, w1, h1 at each iteration of the for k loop, so your assignment to SP1 uses the final values that x y w1 h1 happened to get set to.

Sign in to comment.

1 Answer

Answer by per isakson
on 14 Jun 2019
Edited by per isakson
on 14 Jun 2019
 Accepted Answer

Your profile shows that you over the last few months have posed ten questions in this forum and not received a single answer. Are we that bad in answering?
My comments on this particular question
  • The topic is a very common one. There are many questions here at Answer and I encounter this error message and variants all too often.
  • It's mostly difficult to spot the cause by inspection of the code. But with the help of the debugging tools it's easy. However, that requires it's possible to run the code up til the error.
  • The error messages on "index" have been improved over the years. (What release do you use?)
  • The code given in the question cannot be run, too many items are missing. My first shot returned "Undefined function or variable 'TP1'". At this point I could have posted a comment asking for "missing items".
Debugging session
  • based on an example in the documentation I modified the code
a = imread('circlesBrightDark.png');
TP1 = a < 100;
measurements = regionprops('struct',TP1,'BoundingBox');
for k = 1 : length(measurements)
thisBB = round(measurements(k).BoundingBox);
[x, y, w1, h1] = deal(thisBB(1),thisBB(2),thisBB(3),thisBB(4));
end
SP1 = TP1(y+h1-1,-(x+w1-h1)) + TP1(-(x-h1),y+h1-1) - TP1(x+w1,y-1) - TP1(x,y-1); % (ERROR LINE)
  • set Pause on Errors
  • run the script. Execution halted before the error was thrown (littler green arrow). I selected the indicies of TP1 (blue background). Right click and Evaluate Selection.
  • and got this output
K>> y+h1-1,-(x+w1-h1)
ans =
290
ans =
-400
K>>
The negative value, -400, caused the error since, "Array indices must be positive integers or logical values."
See

  3 Comments

I = input image;
x=256;y=256;
I_TP1 =double( rgb2gray(imresize(I, [x+y,y])));
TP1=zeros(x+y,y);
% calculation of TP1
for j = 2:y
for i = 2:x+y-j
TP1(i,j) = TP1(i-1,j)+TP1(i+1,j-1)-TP1(i,j-1)+I_TP1(i,j);
end
end
Image = bwlabel(TP1);
measurements = regionprops(Image, 'BoundingBox');
allBB = zeros(length(measurements), 4);
for k = 1 : length(measurements)
thisBB = round(measurements(k).BoundingBox);
allBB(k, :) = thisBB;
[x, y, w1, h1] = deal(thisBB(1),thisBB(2),thisBB(3),thisBB(4));
end
SP1 = TP1(-(x+w1-h1)+1,y+h1-1) + TP1(-(x-h1),y+h1-1) - TP1(x+w1,y-1) - TP1(x,y-1); % ERROR
this is the whole thing and I debug the program but it is showing me the same error at the last line. I don't know how to solve this issue. Kindly help.
Thank You Per Isakson,
You had helped me alot. The error is still there but thanks for your time.
"I don't know how to solve this issue." I assume you have written this script. What exactly is it supposed to do? There are logical errors. Walter told you that [x, y, w1, h1] are overwritten for each iteration. I assume you didn't intend to do that. You are working with a coordinate system with (0,0) inside the image. Matlab doesn't allow negative indicies. Use pen and paper, make skethes, write comments in the code. You done changes to the line, which throws the error, why exactly these changes? I'm afraid you have to make the code work.
Google found this for me: Demo illustrating HAAR features
Search File Exchange for Haar features . It will find three submissions.

Sign in to comment.