logo

如何巡线又清楚的识别出十字路口

2019-08-10 16:33
3
RongboKang


THRESHOLD = [(0,100)]

BINARY_VISIBLE = True

import sensor, image, time, math, utime
from pyb import UART
from pyb import LED
import json
roi=(0,60,160,60)
roi_1 = [(20, 0, 120, 20),         #  北
            (20, 100, 120, 20),     # 南
            (0, 0, 20, 120),        #  西
            (140, 0, 20, 120),      #  东
            (60,40,40,40)]          #   中

#LED(1).on()
#LED(2).on()
#LED(3).on()
sensor.reset()
sensor.set_vflip(False)
sensor.set_hmirror(False)
sensor.set_pixformat(sensor.GRAYSCALE)

sensor.set_framesize(sensor.QQVGA)
sensor.set_auto_whitebal(False) # turn this off.
sensor.set_auto_gain(False)
sensor.skip_frames(time = 2000)

clock = time.clock()
buf =[0 for i in range(5)]
low_threshold = (0, 40                         )
uart = UART(3, 115200)
while(True):
    m = -1
    isten = 0
    #img = sensor.snapshot()
    sensor.set_auto_whitebal(False) # turn this off.
    sensor.set_auto_gain(False)
    utime.sleep_us(90)
    img = sensor.snapshot().lens_corr(strength = 1.8, zoom = 1.0)
    img.binary([low_threshold],invert = 1)
    for r in roi_1:
        m += 1
        blobs = img.find_blobs(THRESHOLD, roi=r[0:4],pixels_threshold=100, area_threshold=100, merge=True)
        #img.draw_rectangle(r[0:4], color=(255,0,0))
        if blobs:
            most_pixels = 0
            largest_blob = 0
            for i in range(len(blobs)):
            #目标区域找到的颜色块(线段块)可能不止一个,找到最大的一个,作为本区域内的目标直线
                if blobs[i].pixels() > most_pixels:
                   most_pixels = blobs[i].pixels()
                    #merged_blobs[i][4]是这个颜色块的像素总数,如果此颜色块像素总数大于
                   largest_blob = i
            # Draw a rect around the blob.
            img.draw_rectangle(blobs[largest_blob].rect())
            #将此区域的像素数最大的颜色块画矩形和十字形标记出来
            img.draw_cross(blobs[largest_blob].cx(),
                           blobs[largest_blob].cy())
            buf[m] = 1
        else:
            buf[m] = 0


    utime.sleep_us(90)
    sensor.set_auto_whitebal(True) # turn this off.    
    sensor.set_auto_gain(True)
    utime.sleep_us(90)
    clock.tick()
    img = sensor.snapshot().binary(THRESHOLD) if BINARY_VISIBLE else sensor.snapshot()
    line = img.get_regression([(255,255) if BINARY_VISIBLE else THRESHOLD],roi=(0,60,160,60))

    if (line):
          rho_err = abs(line.rho())-img.width()//2+256
          img.draw_line(line.line(), color = 127)

    print("theta %f, rho = %f " % (line.theta(), line.rho()) if (line) else "N/A")
    output_str="%d" %rho_err
    print(output_str)

    if buf[0]==1 and buf[1] ==1 and buf[2] ==1 and buf[3] ==1 and buf[4] ==1:
           isten = 1
    print(isten)
    out_str="[%d" %isten
    uart.write(out_str )

这是我的代码

3
RongboKang

巡线没有问题但是扫十字的结果很偶然

很少成功

2019-09-10 17:35
F
山南水北

这是车上的巡线程序?,光有一个openmv和两个电机,还是配合arduino使用的?