logo

为什么当标志位变化时,准备运行下一个if语句时,openmv就会自动断开连接?

2022-05-12 17:55
0
pwtg
import sensor, image, time
from pyb import UART
from image import SEARCH_EX, SEARCH_DS
thresholds = [(30, 100, 15, 127, 15, 127)]  #red
sensor.reset()                      # Reset and initialize the sensor.
sensor.set_pixformat(sensor.RGB565) # Set pixel format to RGB565 (or GRAYSCALE)
sensor.set_framesize(sensor.QQVGA)   # Set frame size to QVGA (320x240)
sensor.skip_frames(time = 2000)     # Wait for settings take effect.
sensor.set_auto_gain(False)
sensor.set_auto_whitebal(False)     #关掉白平衡和自动增益
uart = UART(3, 9600)
templates1 = ["/11.pgm","/12.pgm","/13.pgm"]
templates2 = ["/21.pgm","/22.pgm","/23.pgm"] #保存多个模板
templates3 = ["/31.pgm","/32.pgm","/33.pgm"]
templates4 = ["/41.pgm","/42.pgm","/43.pgm"]
templates5 = ["/51.pgm","/52.pgm","/53.pgm"]
templates6 = ["/61.pgm","/62.pgm","/63.pgm"]
templates7 = ["/71.pgm","/72.pgm","/73.pgm"]
templates8 = ["/81.pgm","/82.pgm","/83.pgm"]
clock = time.clock()
while(True):
    p1=p2=p3=0
    flag=1
    clock.tick()                    # Update the FPS clock.
    img = sensor.snapshot()         # Take a picture and return the image.
   #img.draw_rectangle((150,100, 50,50), color=(0,0,255))  #中
    img.draw_rectangle((70,30,30,35), color=(0,0,255))  #上
    img.draw_rectangle((40,50,30,40), color=(0,0,255))  #左
    img.draw_rectangle((100,50,30,40), color=(0,0,255))  #右
   #img.draw_rectangle((150,160, 50,50), color=(0,0,255))  #下
   #上 左 右(73,55,20,15)  (50,70,25,15)  (91,70,25,15)
    if flag==1:
        for blob in img.find_blobs([thresholds[0]],roi=(70,30,30,35),pixels_threshold=15,area_threshold=15):
            img.draw_cross(blob.cx(),blob.cy())
            p1=1
        for blob in img.find_blobs([thresholds[0]],roi=(40,50,30,40),pixels_threshold=15,area_threshold=15):
            img.draw_cross(blob.cx(),blob.cy())
            p2=1
        for blob in img.find_blobs([thresholds[0]],roi=(100,50,30,40),pixels_threshold=15,area_threshold=15):
            img.draw_cross(blob.cx(),blob.cy())
            p3=1
        if p1==1 and p2==0 and p3==0 :
           uart.write("A") #前进
        elif p1==1 and p2==1 and p3==0:
           uart.write("B") #左转
        elif p1==0 and p2==1 and p3==0:
           uart.write("BB") #左转
        elif p1==1 and p2==0 and p3==1:
           uart.write("C") #右转
        elif p1==0 and p2==0 and p3==1:
           uart.write("CC") #右转
        elif p1==1 and p2==1 and p3==1:
           uart.write("D")   #路口1,2
           flag=2
           break
        elif p1==0 and p2==1 and p3==1:
           uart.write("E")   #路口3
           flag=2
           break
        else:
           uart.write("E")

    elif flag==2:
        img = img.to_grayscale() #转化为灰度图
        for t in templates1:
            img = sensor.snapshot()
            template = image.Image(t)
            r1 = img.find_template(template, 0.70, step=4, search=SEARCH_EX)
        for t in templates2:
            img = sensor.snapshot()
            template = image.Image(t)
            r2 = img.find_template(template, 0.70, step=4, search=SEARCH_EX)
        for t in templates3:
            img = sensor.snapshot()
            template = image.Image(t)
            r3 = img.find_template(template, 0.70, step=4, search=SEARCH_EX)
        for t in templates4:
            img = sensor.snapshot()
            template = image.Image(t)
            r4 = img.find_template(template, 0.70, step=4, search=SEARCH_EX)
        for t in templates5:
            img = sensor.snapshot()
            template = image.Image(t)
            r5 = img.find_template(template, 0.70, step=4, search=SEARCH_EX)
        for t in templates6:
            img = sensor.snapshot()
            template = image.Image(t)
            r6 = img.find_template(template, 0.70, step=4, search=SEARCH_EX)
        for t in templates7:
            img = sensor.snapshot()
            template = image.Image(t)
            r7 = img.find_template(template, 0.70, step=4, search=SEARCH_EX)
        for t in templates8:
            img = sensor.snapshot()
            template = image.Image(t)
            r8 = img.find_template(template, 0.70, step=4, search=SEARCH_EX)
            if r1:
               uart.write("1")
            if r2:
               uart.write("2")
            if r3:
               uart.write("3")
            if r4:
               uart.write("4")
            if r5:
               uart.write("5")
            if r6:
               uart.write("6")
            if r7:
               uart.write("7")
            if r8:
               uart.write("8")
            flag=1
2022-05-12 21:01
I
网红小智智 星瞳科技

第66行,70行,74行,78行,82行,86行,90行,94行,重新采集了,img又变成RGB

你用的是什么硬件?是OpenMV4 Plus吗?

2022-05-13 10:26
0
pwtg

@kidswong999 openmv-h7 R1

2022-05-13 10:36
0
pwtg

@kidswong999 那如果想寻找色块和模板匹配一起使用,应该怎么弄(模板匹配需要每一个采集一次,不然延迟很高)?图像的img应该怎么弄?

2022-05-13 18:31
I
网红小智智 星瞳科技

@pwtg 第66行,70行,74行,78行,82行,86行,90行,94行下面都添加img.to_grayscale()

I
网红小智智 星瞳科技

另外你最好用OpenMV4 h7 plus,否则你这么多模板,内存容易不够。