# Find Lines Example
#
# This example shows off how to find lines in the image. For each line object
# found in the image a line object is returned which includes the line's rotation.
# Note: Line detection is done by using the Hough Transform:
# http://en.wikipedia.org/wiki/Hough_transform
# Please read about it above for more information on what `theta` and `rho` are.
# find_lines() finds infinite length lines. Use find_line_segments() to find non-infinite lines.
enable_lens_corr = False # turn on for straighter lines...
import sensor, image, time,pyb
from pyb import LED
sensor.reset()
sensor.set_pixformat(sensor.RGB565) # grayscale is faster
sensor.set_framesize(sensor.QQVGA)
sensor.skip_frames(time = 2000)
clock = time.clock()
# All line objects have a `theta()` method to get their rotation angle in degrees.
# You can filter lines based on their rotation angle.
red_led = LED(1)
green_led = LED(2)
blue_led = LED(3)
ir_led = LED(4)
def led_control(x):
if (x&1)==0: red_led.off()
elif (x&1)==1: red_led.on()
if (x&2)==0: green_led.off()
elif (x&2)==2: green_led.on()
if (x&4)==0: blue_led.off()
elif (x&4)==4: blue_led.on()
if (x&8)==0: ir_led.off()
elif (x&8)==8: ir_led.on()
min_degree = 80
max_degree = 100
# All lines also have `x1()`, `y1()`, `x2()`, and `y2()` methods to get their end-points
# and a `line()` method to get all the above as one 4 value tuple for `draw_line()`.
while(True):
clock.tick()
img = sensor.snapshot()
if enable_lens_corr: img.lens_corr(1.8) # for 2.8mm lens...
# `threshold` controls how many lines in the image are found. Only lines with
# edge difference magnitude sums greater than `threshold` are detected...
# More about `threshold` - each pixel in the image contributes a magnitude value
# to a line. The sum of all contributions is the magintude for that line. Then
# when lines are merged their magnitudes are added togheter. Note that `threshold`
# filters out lines with low magnitudes before merging. To see the magnitude of
# un-merged lines set `theta_margin` and `rho_margin` to 0...
# `theta_margin` and `rho_margin` control merging similar lines. If two lines
# theta and rho value differences are less than the margins then they are merged.
for l in img.find_lines(threshold = 1000, theta_margin = 25, rho_margin = 25):
if (min_degree <= l.theta()) and (l.theta() <= max_degree):
img.draw_line(l.line(), color = (255, 0, 0))
# print(l)
if (min_degree <= l.theta()) and (l.theta() <= max_degree):
led_control(3)
p = pyb.Pin("P0", pyb.Pin.OUT_PP)
p.high() # or p.value(1) to make the pin high (3.3V)
time.sleep(500);
p.low();
else:
led_control(1);
p = pyb.Pin("P0", pyb.Pin.OUT_PP)
p.low() # or p.value(1) to make the pin high (3.3V)
# Red LED = 1, Green LED = 2, Blue LED = 3, IR LEDs = 4.
print("FPS %f" % clock.fps())
# About negative rho values:
#
# A [theta+0:-rho] tuple is the same as [theta+180:+rho].
评论1