Error: Index in position 1 exceeds array bounds

2 views (last 30 days)
Hi,
I would like my code to stop at the last value but it is exceeding 1 point more and giving me an error, can anyone help
Code:
I=imread('frame14.png'); %Read the Image
[C,h] = imcontour(I,[-1000 30]); % store the C from the contour
Contours_matrix = C' %transpose the rows into columns
Y_Coordinates = Contours_matrix(:,2) %extract the second column (Y-values)
Current_Rowindex=1
counter=0
while Current_Rowindex < size(Y_Coordinates,1)
counter = counter+1
Rowindices(counter) = Current_Rowindex
Current_Rowindex = Current_Rowindex+Y_Coordinates(Current_Rowindex,1)+1
end
Rowindices_1=Rowindices'
for i= 1:length(Rowindices_1)
co_ordinates(i,1) = Y_Coordinates(Rowindices_1(i,1)+1:Rowindices_1(i+1)-1,1)
end
Error:
Index in position 1 exceeds array bounds

Answers (1)

Bjorn Gustavsson
Bjorn Gustavsson on 24 Nov 2020
I think you would benefit from using the fact that there is information about the number of elements in a contour-segment in C. The first column of C are the column-vector with contour-level and number of elements. That is you get the level and number of elements of the first contour-segment like this:
C_level(1) = C(1,1);
N_elements(1) = C(1,1);
Then you have the same information about the next contour-segment at C(:,2+N_elements(1)). That way you can count off the contour-segments and make sure that you stop before the end of C - you will have to check that 2+N_elements(end) is smaller than size(C,1). (These are before your transposing).
HTH

Categories

Find more on Read, Write, and Modify Image in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!