Finding 3 consecutive zero values row by row in an image.

Hi,
I would like to be able to go through every row in an image looking for when there are 3 consecutive pixels with a zero value. What I need is it to print the row(X) and column(Y) for the pixel before those 3 pixels. And then continue to move through the rest of the rows. So in the end I would have a list for the Xs and Ys. Can anyone help me make this loop?
Thanks

댓글 수: 6

What if the three consecutive pixels occur int he first three columns? If there are four consecutive zeros, does this count? If so, which column(s) should be returned? A small example would be best.
And is the image grayscale or color?
color
No it wouldn't count and I don't think it will happen. For an example think of a jagged line down the middle of the image. One side has color the other side is dark(zeros). Does that help?
Care to post the image (so we can see it), and let us know why you want/need to find locations of exactly 3 (no more and no less) black pixels? What are you going to do once you know the location of the pixels before the black line starts?
So what I am trying to do is get the coordinates of that line. So it doesn't have to be exactly 3. It could be more but going less I thought might be an issue. Once I have a list of the Xs and Ys then I might doing some curve fitting. That's why I need the Xs and Ys loction.

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

답변 (2개)

Andrei Bobrov
Andrei Bobrov 2012년 4월 10일
x1 = all(img==0,3);
rst = conv2(x1+0,[1 1 1],'same');
[ii,jj] = find(rst == 3);
jj = bsxfun(@plus,jj,-1:1);
Geoff
Geoff 2012년 4월 9일
Simplest (and rather inefficient) approach:
for y = 1:size(img,1)
blacks = 0;
for x = 1:size(img,2)
if all(squeeze(img(y,x,:)) == 0)
blacks = blacks + 1;
if blacks == 3
% BTW, what do you do if first three pixels in the row are black??
disp( [x-blacks, y] );
break; % Stop processing row
end
else
blacks = 0;
end
end
end

카테고리

도움말 센터File Exchange에서 Graphics Performance에 대해 자세히 알아보기

질문:

2012년 4월 9일

Community Treasure Hunt

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

Start Hunting!

Translated by