logo

关于返回错误'line' object isn't iterable

2023-08-03 16:37
E
Watermarker

请问我的代码运行时为什么返回如标题错误

import sensor, image, time, lcd, gc

sensor.reset()
sensor.set_pixformat(sensor.GRAYSCALE)
sensor.set_framesize(sensor.CIF)
sensor.set_windowing([0,0,136,200])
sensor.skip_frames(time = 2000)
clock = time.clock()

#==========================寻找离视野中心最近的直线函数================
def find_theline(lines):
    min_dis = 100
    for line in lines:
        if (abs(line.x1() - img.width()/2) < min_dis):
            the_line = line
            min_dis  = abs(line.x1() - img.width()/2)
    return the_line
#=================================================================

#=============================滤波参数==============================
kernel_size = 1
kernel = [-1, -1, -1,\
          -1, +8, -1,\
          -1, -1, -1]

thresholds = [(0, 36)]
#=================================================================
while(True):
    clock.tick()
    img = sensor.snapshot().lens_corr(strength = 1.0,zoom = 1)
    img.morph(kernel_size, kernel)
    img.binary(thresholds)
    img.erode(1, threshold = 2)
    lines = img.get_regression([(0,36)], robust = True)
    if (lines):
        theline = find_theline(lines)
        if(theline.magnitude() > 8):
            rho_err = abs(theline.rho())-img.width()/2
            print("rho_error:",rho_err)
            img.draw_line(theline.line())
    gc.collect()
    print("fps:",clock.fps())
E
Watermarker

问题就出在

for line in lines:

这句话上

2023-08-04 10:58
I
网红小智智 星瞳科技

img.get_regression返回的是一条线,而不是多条线。