필터 지우기
필터 지우기

matlab function

조회 수: 1 (최근 30일)
LIU WEIHUA
LIU WEIHUA 2012년 4월 16일
I have write a code to find the first pixel that not equal to zero and store the pixel position to M and N respectively as follow:
j=1;
while (j>=1&&j<=size(I,1) )
for i=1:size(I,2)
if I(i,j) >= 0
M=j
N=i
break;
else
end
end
j=j+1;
end
where I is the image;
when the code find the first pixel that not equal to zeros,however , the loop can not stop, the break seems do not help to stop the loop;
Can you please help me on this?

채택된 답변

Walter Roberson
Walter Roberson 2012년 4월 16일
The break is breaking out of the "for", but then you are still within the "while". You need to set a flag before you break and test it after the "for" loop code, using break when the flag is set.
  댓글 수: 3
Walter Roberson
Walter Roberson 2012년 4월 16일
j=1;
breakout = false;
while (j>=1&&j<=size(I,1) )
for i=1:size(I,2)
if I(i,j) >= 0
M=j
N=i
breakout = true;
break;
else
end
end
if breakout; break; end
j=j+1;
end
Image Analyst
Image Analyst 2012년 4월 17일
I think you meant
while (j >= 1 && j <= size(I, 2) )
for i = 1 : size(I, 1)

댓글을 달려면 로그인하십시오.

추가 답변 (2개)

Richard Brown
Richard Brown 2012년 4월 16일
Why not just
[M, N] = find(I, 1, 'first')
?

Image Analyst
Image Analyst 2012년 4월 17일
Um, did you notice that your "i" variable is the first dimension (row) in the matrix but you are going to the second dimension max (the max column, not row)? Similarly for your j variable. Thus your code will bomb for anything but square matrices.
And what do you define as the "first" pixel? Starting at the upper left pixel, do you want to go across columns, then down to the next row, or go down rows then over to the next column, or do you want to find the pixel that is closest to the upper left? Those might (probably) give three different pixels as the answer.

카테고리

Help CenterFile Exchange에서 Matrix Indexing에 대해 자세히 알아보기

태그

아직 태그를 입력하지 않았습니다.

Community Treasure Hunt

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

Start Hunting!

Translated by