© Bueno Systems, Inc. • TSL1401-DB (2009.10.01)
Page 38 of 52
•
Third, there’s a chance that the first edge won’t have been found. But we didn’t check for that;
we just plowed ahead, looking for the second edge. But remember the way find works: if it
doesn’t find something once, it won’t find anything on subsequent
FNDNXT
s either, until the
Left and Right limits are reset to new values. So we’re safe there.
•
Fourth, we’re only checking for the presence of the right edge in the
IF
statement to see if the
entire object is present. Again, that’s because if the left edge wasn’t found, the right edge is
automatically not found either. So that’s all we need to check.
•
And finally, what’s with the “
– 1
” in the first
DEBUG
statement? Well, here are the two edges we
would have found with the above image:
… 0000111000 1111111100000000
1
11111
0
000111111
1111100000 …
31 32 64 65
The second edge is located one pixel beyond what we consider to be the right edge of the object so, to
point to the right edn of the object we need to subtract one. On the other hand, if we’re interested in the
size
of the object, we can just subtract
lft_edge
from
rgt_edge
.
Now, suppose the central portion of above image represents a backlit bagel. In this situation, the bagel
consists of two dark, silhouetted areas separated by a bright hole. What we’re after here is the diameter
of the bagel; we don’t care about the hole. This is where a backward search comes into play. First we
locate the first bright-to-dark edge scanning forward. Next we locate the first bright-to-dark edge
scanning backward. These will be the extreme left and right edges of the bagel, from which we can
compute its diameter. Here’s the code:
lft_edge VAR Byte
rgt_edge VAR Byte
OWOUT owio, 0, [SETEXP, 60, SETBIN, 100, 3, 0, ACQBIN]
GOSUB Ready
OWOUT owio, 0, [FNDNEW|FWD|DRKEDG, 32, 64]
GOSUB Ready
OWOUT owio, 0, [FNDNXT|BKWD|DRKEDG]
GOSUB Ready
OWOUT owio, 0, [DUMPADR, R 5]
OWIN owio, 2, [lft_edge, rgt_edge]
IF (rgt_edge) THEN
DEBUG "Bagel found with diameter ", DEC rgt_edge - lf 1
ELSE
DEBUG "No bagel found."
ENDIF
Okay, everything looks as expected, except for that “
+ 1
”. What’s up with that? Here are the edges that
the program found:
… 0000111000 11111111
0
0000000111111000
0
111111
1111100000 …
31 32 64 65
When scanning backward for an edge,
FNDNXT
looks for the first bright pixel, which is at location 64,
then, moving right to left, the first dark pixel after that, which is the one highlighted above and which is
part of the bagel itself. Hence, the necessary addition to get the diameter.
Of course, all this assumes that we’re looking at the largest part of the bagel, which would be a real
coincidence. In a subsequent section, we’ll see how to parlay this into an application that inspects bagels
moving past on a conveyor and that finds their actual diameters.