# Find the number of lines inside a specific area

**URL:** <https://forums.openmv.io/t/find-the-number-of-lines-inside-a-specific-area/431>\
**Category:** OpenMV Boards\
**Created:** [September 27, 2017, 11:48am UTC](https://forums.openmv.io/t/find-the-number-of-lines-inside-a-specific-area/431 "2017-09-27T11:48:35Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Ablon](https://avatars.discourse-cdn.com/v4/letter/a/4491bb/32.png) [@Ablon](https://forums.openmv.io/u/Ablon)\
**Post date:** [September 27, 2017, 11:48am UTC](https://forums.openmv.io/t/find-the-number-of-lines-inside-a-specific-area/431/1 "2017-09-27T11:48:35Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![kwagyeman](https://yyz1.discourse-cdn.com/flex029/user_avatar/forums.openmv.io/kwagyeman/32/4_2.png) [@kwagyeman](https://forums.openmv.io/u/kwagyeman)\
**Post date:** [September 28, 2017, 2:51am UTC](https://forums.openmv.io/t/find-the-number-of-lines-inside-a-specific-area/431/2 "2017-09-28T02:51:24Z")

</div>

Here you go, you have to tune if for your application:

```python
# 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))

```
