• 免费好用的星瞳AI云服务上线!简单标注,云端训练,支持OpenMV H7和OpenMV H7 Plus。可以替代edge impulse。 https://forum.singtown.com/topic/9519
  • 我们只解决官方正版的OpenMV的问题(STM32),其他的分支有很多兼容问题,我们无法解决。
  • 如果有产品硬件故障问题,比如无法开机,论坛很难解决。可以直接找售后维修
  • 发帖子之前,请确认看过所有的视频教程,https://singtown.com/learn/ 和所有的上手教程http://book.openmv.cc/
  • 每一个新的提问,单独发一个新帖子
  • 帖子需要目的,你要做什么?
  • 如果涉及代码,需要报错提示全部代码文本,请注意不要贴代码图片
  • 必看:玩转星瞳论坛了解一下图片上传,代码格式等问题。
  • 不是为啥我模板匹配只能匹配到5,就匹配到就是5



    • 
      
      import time, sensor, image,ustruct
      from pyb import UART,LED
      from image import SEARCH_EX, SEARCH_DS
      
      sensor.reset()
      
      
      sensor.set_contrast(1)
      sensor.set_gainceiling(16)
      
      sensor.set_framesize(sensor.QQVGA)
      
      sensor.set_pixformat(sensor.GRAYSCALE)
      
      
      LED(1).on()
      LED(2).on()
      LED(3).on()
      # Load template.
      # Template should be a small (eg. 32x32 pixels) grayscale image.
      template1 = image.Image("/1.pgm")
      template2 = image.Image("/2.pgm")
      templates3 = ["/3.pgm","/3_1.pgm","/3_2.pgm","/3_3.pgm","/3_4.pgm","/3_5.pgm","/3_6.pgm","/3_7.pgm","/3_8.pgm"]
      templates4 = ["/4.pgm","/4_1.pgm","/4_2.pgm","/4_3.pgm","/4_4.pgm","/4_5.pgm","/4_6.pgm","/4_7.pgm","/4_8.pgm"]
      templates5 = ["/5.pgm","/5_1.pgm","/5_2.pgm","/5_3.pgm","/5_4.pgm","/5_5.pgm","/5_6.pgm","/5_7.pgm","/5_8.pgm"]
      templates6 = ["/6.pgm","/6_1.pgm","/6_2.pgm","/6_3.pgm","/6_4.pgm","/6_5.pgm","/6_6.pgm","/6_7.pgm","/6_8.pgm"]
      templates7 = ["/7.pgm","/7_1.pgm","/7_2.pgm","/7_3.pgm","/7_4.pgm","/7_5.pgm","/7_6.pgm","/7_7.pgm","/7_8.pgm"]
      templates8 = ["/8.pgm","/8_1.pgm","/8_2.pgm","/8_3.pgm","/8_4.pgm","/8_5.pgm","/8_6.pgm","/8_7.pgm","/8_8.pgm"]
      #加载模板图片
      
      clock = time.clock()
      uart = UART(3,115200)   #定义串口3变量
      uart.init(115200, bits=8, parity=None, stop=1) # init with given parameters
      
      def outuart(x,num):
          global uart
      
          data = ustruct.pack("<bbhhhhb",      #格式为俩个字符俩个短整型(2字节)
                         0x2C,                      #帧头1
                         0x12,                      #帧头2
                         int(x), # up sample by 4   #数据1
                         int(num), # up sample by 4    #数据2
                         int(0), # up sample by 4    #数据1
                         int(0), # up sample by 4    #数据2
                         0x5B)
          for x in range(5):
              uart.write(data)#必须要传入一个字节数组
              time.sleep_ms(1)
              print(num)
      
      # Run template matching
      while (True):
          clock.tick()
          img = sensor.snapshot()
          num=0
          # find_template(template, threshold, [roi, step, search])
          # ROI: The region of interest tuple (x, y, w, h).
          # Step: The loop step used (y+=step, x+=step) use a bigger step to make it faster.
          # Search is either image.SEARCH_EX for exhaustive search or image.SEARCH_DS for diamond search
          #
          # Note1: ROI has to be smaller than the image and bigger than the template.
          # Note2: In diamond search, step and ROI are both ignored.
          r = img.find_template(template1, 0.70, step=5, search=SEARCH_EX) #, roi=(10, 0, 60, 60))
          #find_template(template, threshold, [roi, step, search]),threshold中
      
          if r:
              print(r)
              #img.draw_rectangle(r)
              print('1')
              num=1
              outuart(0,num)
              for x in range(5):
                  LED(1).on()
                  LED(2).off()
                  LED(3).off()
                  time.sleep_ms(100)
                  LED(1).on()
                  LED(2).on()
                  LED(3).on()
                  time.sleep_ms(100)
      
          r2_0 = img.find_template(template2, 0.70, step=5, search=SEARCH_EX)
          if r2_0:
              print(r2_0)
              #img.draw_rectangle(r1_3)
              print('2')
              num=2
              outuart(0,num)
              for x in range(5):
                  LED(1).on()
                  LED(2).off()
                  LED(3).off()
                  time.sleep_ms(100)
                  LED(1).on()
                  LED(2).on()
                  LED(3).on()
                  time.sleep_ms(100)
      
          r3_0 = img.find_template(image.Image(templates3[0]), 0.70, step=5, search=SEARCH_EX)
          if r3_0:
              print(r3_0)
              #img.draw_rectangle(r1_1)
              print('3')
              num=3
              outuart(0,num)
              for x in range(5):
                  LED(1).on()
                  LED(2).off()
                  LED(3).off()
                  time.sleep_ms(100)
                  LED(1).on()
                  LED(2).on()
                  LED(3).on()
                  time.sleep_ms(100)
      
          r4_0 = img.find_template(image.Image(templates4[0]), 0.70, step=5, search=SEARCH_EX)
          if r4_0:
              print(r4_0)
              #img.draw_rectangle(r1_1)
              print('4')
              num=4
              outuart(0,num)
              for x in range(5):
                  LED(1).on()
                  LED(2).off()
                  LED(3).off()
                  time.sleep_ms(100)
                  LED(1).on()
                  LED(2).on()
                  LED(3).on()
                  time.sleep_ms(100)
          r5_0 = img.find_template(image.Image(templates5[0]), 0.70, step=5, search=SEARCH_EX)
          if r5_0:
              print(r5_0)
              #img.draw_rectangle(r1_1)
              print('5')
              num=5
              outuart(0,num)
              for x in range(5):
                  LED(1).on()
                  LED(2).off()
                  LED(3).off()
                  time.sleep_ms(100)
                  LED(1).on()
                  LED(2).on()
                  LED(3).on()
                  time.sleep_ms(100)
          r6_0 = img.find_template(image.Image(templates6[0]), 0.70, step=5, search=SEARCH_EX)
          if r6_0:
              print(r6_0)
              #img.draw_rectangle(r1_1)
              print('6')
              num=6
              outuart(0,num)
              for x in range(5):
                  LED(1).on()
                  LED(2).off()
                  LED(3).off()
                  time.sleep_ms(100)
                  LED(1).on()
                  LED(2).on()
                  LED(3).on()
                  time.sleep_ms(100)
          r7_0 = img.find_template(image.Image(templates7[0]), 0.70, step=5, search=SEARCH_EX)
          if r7_0:
              print(r7_0)
              #img.draw_rectangle(r1_1)
              print('7')
              num=7
              outuart(0,num)
              for x in range(5):
                  LED(1).on()
                  LED(2).off()
                  LED(3).off()
                  time.sleep_ms(100)
                  LED(1).on()
                  LED(2).on()
                  LED(3).on()
                  time.sleep_ms(100)
          r8_0 = img.find_template(image.Image(templates8[0]), 0.70, step=5, search=SEARCH_EX)
          if r8_0:
              print(r8_0)
              #img.draw_rectangle(r1_1)
              print('8')
              num=8
              outuart(0,num)
              for x in range(5):
                  LED(1).on()
                  LED(2).off()
                  LED(3).off()
                  time.sleep_ms(100)
                  LED(1).on()
                  LED(2).on()
                  LED(3).on()
                  time.sleep_ms(100)
      
      
      
      

      0_1721866403702_未命名图片.png