• OpenMV VSCode 扩展发布了,在插件市场直接搜索OpenMV就可以安装
  • 如果有产品硬件故障问题,比如无法开机,论坛很难解决。可以直接找售后维修
  • 发帖子之前,请确认看过所有的视频教程,https://singtown.com/learn/ 和所有的上手教程http://book.openmv.cc/
  • 每一个新的提问,单独发一个新帖子
  • 帖子需要目的,你要做什么?
  • 如果涉及代码,需要报错提示全部代码文本,请注意不要贴代码图片
  • 必看:玩转星瞳论坛了解一下图片上传,代码格式等问题。
  • 颜色识别和模板识别需要相互切换,我想用中断计数但是一按中断就会和openmv断联



    • 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
      请在这里粘贴代码