Page 1 of 1

Find the number of lines inside a specific area

Posted: Wed Sep 27, 2017 7:48 am
by Ablon
Dear All,
Does anybody know how to count lines within specific area say inside of a rectangle and the lines have specific angle say 45deg, also possible highlight the valid lines with color?

Thanks

Re: Find the number of lines inside a specific area

Posted: Wed Sep 27, 2017 10:51 pm
by kwagyeman
Here you go, you have to tune if for your application:

Code: Select all

# Counting vertical lines example script.
#
# Adjust the threshold value to max it so only really strong lines appear... A higher threshold for stronger lines.
# Adjust the theta_margin value to control the merging of lines with similar angles.
# Adjsut the rho_margin value to control the mergining of lines that are physically nearby.

# Note that you may detect two lines on either side of some object. To filter this out... make the rho margin higher.

import sensor, image

sensor.reset()
sensor.set_pixformat(sensor.GRAYSCALE)
sensor.set_framesize(sensor.VGA)
sensor.set_windowing((640, 80))
sensor.skip_frames(time = 2000)

def line_filter(line):
    # Undo negative rho...
    t = line.theta() if (line.rho() >= 0) else (line.theta() + 180) 
    # If the line is vertical it should have a theta close to 0/360
    return ((t < 10) or (t > 350)) # You'll want to play with these settings here...

while(True):
    img = sensor.snapshot()
    lines = list(filter(line_filter, img.find_lines(x_stride=1, y_stride=1,
        threshold = 1000, theta_margin = 20, rho_margin = 10))) # You'll want to play with these settings here...
    for line in lines: img.draw_line(line.line(), color=127)
    print("Lines Seen %d" % len(lines))