Error: Index in position 1 exceeds array bounds

조회 수: 1 (최근 30일)
muhammad choudhry
muhammad choudhry 2020년 11월 24일
답변: Bjorn Gustavsson 2020년 11월 24일
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

답변 (1개)

Bjorn Gustavsson
Bjorn Gustavsson 2020년 11월 24일
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

카테고리

Help CenterFile Exchange에서 Computer Vision with Simulink에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by