Example: 05-Feature-Detection/linear_regression_fast.py
# 本作品采用MIT许可证授权。
# 版权所有 (c) 2013-2023 OpenMV LLC。保留所有权利。
# https://github.com/openmv/openmv/blob/master/LICENSE
#
# 快速线性回归示例
#
# 这个示例展示了如何在您的OpenMV Cam上使用get_regression()方法
# 来获取感兴趣区域的线性回归。使用此方法,您可以轻松构建
# 一个能够跟踪指向同一方向的线条的机器人
# 但这些线条实际上并未连接。对于连接良好的线条,使用find_blobs()
# 以获得更好的过滤选项和控制。
#
# 这被称为快速线性回归,因为我们使用最小二乘法
# 来拟合线条。然而,这种方法对于任何包含大量(或任何)异常点的图像都不适用
# 这些异常点会破坏线条的拟合...
import sensor
import time
THRESHOLD = (0, 100) # 用于暗色物体的灰度阈值。
BINARY_VISIBLE = True # 首先进行二值化处理,以查看线性回归的运行情况。
sensor.reset()
sensor.set_pixformat(sensor.GRAYSCALE)
sensor.set_framesize(sensor.QQVGA)
sensor.skip_frames(time=2000)
clock = time.clock()
while True:
clock.tick()
img = sensor.snapshot().binary([THRESHOLD]) if BINARY_VISIBLE else sensor.snapshot()
# 返回一个类似于find_lines()和find_line_segments()返回的线条对象
# find_line_segments()。您可以使用x1()、y1()、x2()、y2()、length()、
# theta()(旋转角度,单位为度)、rho()和magnitude()。
#
# magnitude()表示线性回归的效果。其取值范围为
# (0, INF],其中0表示圆形。场景越接近线性,
# magnitude()的值就越高。
line = img.get_regression([(255, 255) if BINARY_VISIBLE else THRESHOLD])
if line:
img.draw_line(line.line(), color=127)
print(
"FPS %f, mag = %s" % (clock.fps(), str(line.magnitude()) if (line) else "N/A")
)
# 关于负 rho 值:
#
# 一个 [theta+0:-rho] 元组与 [theta+180:+rho] 相同。