星瞳实验室APP,快速收到回复
  • 我们只解决官方正版的OpenMV的问题(STM32),其他的分支有很多兼容问题,我们无法解决。
  • 发帖子之前,请确认看过所有的视频教程,https://singtown.com/learn/ 和所有的上手教程http://book.openmv.cc/
  • 每一个新的提问,单独发一个新帖子
  • 帖子需要目的,你要做什么?
  • 如果涉及代码,需要报错提示全部代码文本,请注意不要贴代码图片
  • 必看:玩转星瞳论坛了解一下图片上传,代码格式等问题。
  • 关于分别模板匹配每个roi区域共5个区域并输出结果的程序,代码错在哪里?



    • import sensor, image, time
      from image import SEARCH_EX, SEARCH_DS

      sensor.reset()
      sensor.set_pixformat(sensor.GRAYSCALE)
      sensor.set_framesize(sensor.QQVGA)
      sensor.skip_frames(time = 2000)
      clock = time.clock()

      img.draw_rectangle((5,50,25,35),color = (0, 0, 0))
      img.draw_rectangle((30,50,25,35),color = (0, 0, 0))
      img.draw_rectangle((55,50,25,35),color = (0, 0, 0))
      img.draw_rectangle((80,50,25,35),color = (0, 0, 0))
      img.draw_rectangle((105,50,25,35),color = (0, 0, 0))

      templates = ["/0.pgm", "/1.pgm", ] #保存多个模板

      while(True):
      clock.tick()
      img = sensor.snapshot()
      a=0 #依次对每个共5个roi区域进行模板匹配,得出结果
      if a==0:
      for t in templates:
      template = image.Image(t)
      #对每个模板遍历进行模板匹配
      r = img.find_template(template, 0.70, step=4, search=SEARCH_EX) , roi=(5, 50, 25, 35))
      if r:
      img.draw_rectangle(r, color=0)
      print(t)
      a+=1
      elif a==1:
      for t in templates:
      template = image.Image(t)
      r = img.find_template(template, 0.70, step=4, search=SEARCH_EX) , roi=(30, 50, 25, 35))
      if r:
      img.draw_rectangle(r, color=0)
      print(t)
      a+=1
      elif a==2:
      for t in templates:
      template = image.Image(t)
      r = img.find_template(template, 0.70, step=4, search=SEARCH_EX) , roi=(55, 50, 25, 35))
      if r:
      img.draw_rectangle(r, color=0)
      print(t)
      a+=1
      elif a==3:
      for t in templates:
      template = image.Image(t)
      r = img.find_template(template, 0.70, step=4, search=SEARCH_EX) , roi=(80, 50, 25, 35))
      if r:
      img.draw_rectangle(r, color=0)
      print(t)
      a+=1
      else a==4:
      for t in templates:
      template = image.Image(t)
      r = img.find_template(template, 0.70, step=4, search=SEARCH_EX) , roi=(105, 50, 25, 35))
      if r:
      img.draw_rectangle(r, color=0)
      print(t)
      a=0
      #print(clock.fps())
      0_1556813099470_666.png



    • 代码出现什么错误?



    • This post is deleted!


    • @kidswong999

      import sensor, image, time
      from image import SEARCH_EX, SEARCH_DS
      
      sensor.reset()
      sensor.set_pixformat(sensor.GRAYSCALE)
      sensor.set_framesize(sensor.QQVGA)
      sensor.skip_frames(time = 2000)
      clock = time.clock()
      
      
      
      templates = ["/0.pgm", "/1.pgm", ] #保存多个模板
      
      while(True):
          clock.tick()
          img = sensor.snapshot()
          img.draw_rectangle((5,50,25,35),color = (0, 0, 0))
          img.draw_rectangle((30,50,25,35),color = (0, 0, 0))
          img.draw_rectangle((55,50,25,35),color = (0, 0, 0))
          img.draw_rectangle((80,50,25,35),color = (0, 0, 0))
          img.draw_rectangle((105,50,25,35),color = (0, 0, 0))
          a=0  #依次对每个共5个roi区域进行模板匹配,得出结果
          if a==0:
              for t in templates:
                 template = image.Image(t)
                  #对每个模板遍历进行模板匹配
                 r = img.find_template(template, 0.70, step=4, search=SEARCH_EX, roi=(5, 50, 25, 35))
                 if r:
                      img.draw_rectangle(r)
                      print(t)
                      a+=1
          elif a==1:
              for t in templates:
                 template = image.Image(t)
                 r = img.find_template(template, 0.70, step=4, search=SEARCH_EX, roi=(30, 50, 25, 35))
                 if r:
                    img.draw_rectangle(r)
                    print(t)
                    a+=1
          elif a==2:
               for t in templates:
                 template = image.Image(t)
                 r = img.find_template(template, 0.70, step=4, search=SEARCH_EX, roi=(55, 50, 25, 35))
                 if r:
                      img.draw_rectangle(r)
                      print(t)
                      a+=1
          elif a==3:
               for t in templates:
                   template = image.Image(t)
                   r = img.find_template(template, 0.70, step=4, search=SEARCH_EX, roi=(80, 50, 25, 35))
                   if r:
                       img.draw_rectangle(r)
                       print(t)
                       a+=1
          else:
               for t in templates:
                   template = image.Image(t)
                   r = img.find_template(template, 0.70, step=4, search=SEARCH_EX, roi=(105, 50, 25, 35))
                   if r:
                      img.draw_rectangle(r)
                      print(t)
                      a=0
      


    • @kidswong999 程序只能识别第一位的0,是逻辑错误吗



    • 你的逻辑很奇怪,遍历区域直接for循环就行。那个a加来加去没意义。

      rois = [(5, 50, 25, 35),(30, 50, 25, 35),(55, 50, 25, 35),(80, 50, 25, 35),(105, 50, 25, 35)]
      for roi in rois:
          for t in templates:
               template = image.Image(t)
               r = img.find_template(template, 0.70, step=4, search=SEARCH_EX, roi=roi)
               if r:
                   img.draw_rectangle(r)
                   print(t)