Info

This question is closed. Reopen it to edit or answer.

how to eliminate Index out of bound error in this code?

1 view (last 30 days)
I am working on shape analysis and classification on the basis of extent. But I am getting error as index out of bound. Please help me on this code. I am attaching paper with algorithm for this program and my code.
% Step 1: Read image Read in I = imread('shapes.png'); figure, imshow(I) title('Original Image'); % Step 2: Convert image from rgb to gray shape1 = rgb2gray(I); figure, imshow(shape1) title('Gray Image') % Step 3: Threshold the image Convert the image to black and white in order % to prepare for boundary tracing using bwboundaries. level = graythresh(shape1); shape2 = im2bw(shape1,0.8); figure, imshow(shape2) title('Binary Image'); % Step 4: Invert the Binary Image shape3 = imcomplement(shape2); figure, imshow(shape3) title('Inverted Binary Image'); % Step 5: Find the boundaries Concentrate only on the exterior boundaries. % Option 'noholes' will accelerate the processing by preventing B = bwboundaries(shape3); figure, imshow(shape3) % step 6: labelling objects labeled = bwlabel(shape3,8); % Step 7: Determine objects properties st = regionprops(labeled,'all'); imshow(shape3) hold on for k = 1:numel(st) c = st(k).Centroid; text(c(1), c(2), sprintf('%d', k), ... 'HorizontalAlignment', 'center', ... 'VerticalAlignment', 'middle'); end hold off % step 8: Adding red coloured BoundingBox to objects for k=1:length(st); st(k) thisBB = st(k).BoundingBox; rectangle('Position', [thisBB(1),thisBB(2),thisBB(3),thisBB(4)],... 'EdgeColor','r','LineWidth',2 ) thisex = st(k).Extent; if thisex(k) == 0.7826 display('circle'); elseif thisex(k) == 0.53 display('triangle'); elseif thisex(k) ==1 display('rectangle') end end
  1 Comment
Geoff Hayes
Geoff Hayes on 16 Oct 2014
Jesisha - please format your code so that it is readable. Highlight the code portion and press the {}Code button. Else, attach the m file (which includes this code) to your question by using the paperclip.
When an error is generated, it usually provides the line of code that generated the error. It is always helpful to include this information when trying to debug what is wrong. The index out of bounds error could be from one of your two for loops at steps 7 and 8.
Try the following. Before running your code, type the following in the Command Window
dbstop if error
Now run your code. When the error occurs, the debugger will pause at the line of code that generated the error. Check the local variables at this line. What is k, what is numel(st) or length(st)? Is st(k).Centroid valid for k, or should you be accessing st.Centroid and treating that result as a 2D array?

Answers (0)

Community Treasure Hunt

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

Start Hunting!