Asked by Shah Bano
on 13 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.

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

Shah Bano
on 14 Jun 2019

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.

Shah Bano
on 14 Jun 2019

Thank You Per Isakson,

You had helped me alot. The error is still there but thanks for your time.

per isakson
on 14 Jun 2019

"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

Sign in to comment.

Opportunities for recent engineering grads.

Apply Today
## 1 Comment

## Walter Roberson (view profile)

## Direct link to this comment

https://au.mathworks.com/matlabcentral/answers/466927-index-exceeds-matrix-dimensions#comment_714547

Sign in to comment.