logo

颜色识别和模板识别需要相互切换,我想用中断计数但是一按中断就会和openmv断联

2025-05-29 10:30
J
sdbi
from machine import UART
from machine import Pin
from image import SEARCH_EX, SEARCH_DS
counter = 0
def callback(pin):
    global counter
    counter +=1 # 每次中断触发时加1
    print(f"中断触发: 计数={counter}")
# 颜色阈值索引,可以改变索引值来识别不同颜色此处0为红色,1为绿色,2为蓝色
pin0 = Pin("P0", Pin.IN, Pin.PULL_DOWN)
pin0.irq(callback, Pin.IRQ_FALLING)
uart = UART(3, 38400)
sergen = 0
# 设置颜色阈值,此处设置红绿蓝三种颜色的阈值
thresholds = [(36, 55, 4, 80, 17, 66),   # 红色阈值
              (10, 92, -101, -23, -128, 107),  # 绿色阈值
              (4, 72, -57, 14, -17, -65)# 蓝色阈值
              ]
# 初始化函数
templates = ["/template_num/0.pgm", "/template_num/1.pgm", "/template_num/2.pgm",
            "/template_num/3.pgm","/template_num/4.pgm"]

    #elif couter ==2:
    #    init_setup_color()
    #    main_color()


def init_setup():
    global sensor, clock                   # 设置为全局变量
    sensor.reset()                         # 初始化感光元件
    sensor.set_pixformat(sensor.RGB565)    # 设置感光元件图像色彩格式为 RGB565 (RGB565为彩图,GRAYSCALE为灰度图)
    sensor.set_framesize(sensor.QVGA)      # 设置感光元件分辨率大小为 QVGA (QVGA是320x240)
    sensor.skip_frames(time=2000)          # 等待2秒,使以上的设置生效
    sensor.set_auto_gain(False)            # 关闭自动增益。在颜色识别中,需要关闭自动增益
    sensor.set_auto_whitebal(False)        # 关闭白平衡。白平衡是默认开启的,在颜色识别中,需要关闭白平衡
    clock = time.clock()                   # 创建时钟对象

# 主函数
def main():
    while 1:# 无限循环
        clock.tick()                       # 更新FPS帧率时钟
        img = sensor.snapshot()            # 拍一张照片并返回图像
            # 循环3种颜色阈值,在图像中查看是否有对应颜色并做一些操作,[thresholds[threshold_index]]为列表格式
        for blob in img.find_blobs([thresholds[0]], pixels_threshold=200, area_threshold=200):
            img.draw_rectangle(blob.rect())        # 将目标颜色画框
            img.draw_cross(blob.cx(), blob.cy())   # 在目标颜色中心位置画个十字
            output_str_red="%d" % (1)
            uart.write(output_str_red)
        for blob in img.find_blobs([thresholds[1]], pixels_threshold=200, area_threshold=200):
            img.draw_rectangle(blob.rect())        # 将目标颜色画框
            img.draw_cross(blob.cx(), blob.cy())   # 在目标颜色中心位置画个十字
            output_str_green="%d" % (2)
            print(output_str_green)
            uart.write(output_str_green)
    
        for blob in img.find_blobs([thresholds[2]], pixels_threshold=200, area_threshold=200):
                img.draw_rectangle(blob.rect())        # 将目标颜色画框
                img.draw_cross(blob.cx(), blob.cy())   # 在目标颜色中心位置画个十字
                output_str_blue="%d" % (3)
                print(output_str_blue)
                uart.write(output_str_blue)
        
        
        img = img.to_grayscale()
        for t in templates:
            template = image.Image(t)
                        # 对每个模板遍历进行模板匹配
            r = img.find_template(template, 0.70, step=4, search=SEARCH_EX) #, roi=(10, 0, 60, 60))
                            # 在find_template(template, threshold, [roi, step, search]),threshold中
                            # 0.7是相似度阈值,roi是进行匹配的区域(左上顶点为(10,0),长80宽60的矩形),
                            # 注意roi的大小要比模板图片大,比frambuffer小。
                            # 把匹配到的图像标记出来
            if r:
                img.draw_rectangle(r)
                recognition_num = t.split('/')[2][0]
                uart.write(recognition_num)
                print('识别到的数字是 %s' % t.split('/')[2][0]) # 打印识别到的数字


# 程序入口b
if __name__ == '__main__':
    init_setup()                          # 执行初始化函数
    try:                                   # 异常处理
        main()                             # 执行主函数
    except:
        pass
请在这里粘贴代码
2025-06-11 20:11
I
网红小智智 星瞳科技

1. 不要用try catch把主程序包起来,出错你都看不到。

2. 没有import image, sensor