Example: 05-Feature-Detection/find_lines.py
# 本作品采用MIT许可证授权。
# 版权所有 (c) 2013-2023 OpenMV LLC。保留所有权利。
# https://github.com/openmv/openmv/blob/master/LICENSE
#
# 直线检测示例
#
# 本示例展示如何识别图像中的直线。每个检测到的直线对象
# 将返回包含该直线旋转角度的数据结构。
# 注:直线检测基于霍夫变换实现:
# http://zh.wikipedia.org/wiki/霍夫变换
# 关于参数`theta`和`rho`的具体含义,请参阅上述链接中的原理说明。
# find_lines()用于检测无限长直线,find_line_segments()则用于检测有限线段。
import sensor
import time
ENABLE_LENS_CORR = False # 开启以获得更直的线条...
sensor.reset()
sensor.set_pixformat(sensor.RGB565) # 灰度处理速度更快
sensor.set_framesize(sensor.QQVGA)
sensor.skip_frames(time=2000)
clock = time.clock()
# 所有直线对象都可通过`theta()`方法获取其旋转角度(度数制)。
# 可根据直线的旋转角度进行筛选。
min_degree = 0
max_degree = 179
# 所有线条还提供`x1()`、`y1()`、`x2()`和`y2()`方法以获取其端点
# 以及`line()`方法将所有上述值作为一个四元组返回,供`draw_line()`使用。
while True:
clock.tick()
img = sensor.snapshot()
if ENABLE_LENS_CORR:
img.lens_corr(1.8) # 适用于2.8mm镜头...
# `threshold`参数控制图像中检测到的线条数量。仅当
# 边缘差异幅值总和大于`threshold`时,线条才会被检测到...
# 关于`threshold`的更多信息 - 图像中每个像素都会为线条贡献一个幅值
# 所有贡献值的总和即为该线条的幅值。随后
# 当线条合并时,它们的幅值会相加。请注意,`threshold`
# 会在合并前过滤掉低幅值的线条。若要查看某线条的幅值
# 未合并的线条将 `theta_margin` 和 `rho_margin` 设置为 0...
# `theta_margin` 和 `rho_margin` 控制合并相似线条。如果两条线的
# theta 和 rho 值的差异小于边距,则它们将被合并。
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))
# 打印(l)
print("FPS %f" % clock.fps())
# 关于负 rho 值:
#
# 一个 [theta+0:-rho] 元组与 [theta+180:+rho] 相同。