• OpenMV VSCode 扩展发布了,在插件市场直接搜索OpenMV就可以安装
  • 如果有产品硬件故障问题,比如无法开机,论坛很难解决。可以直接找售后维修
  • 发帖子之前,请确认看过所有的视频教程,https://singtown.com/learn/ 和所有的上手教程http://book.openmv.cc/
  • 每一个新的提问,单独发一个新帖子
  • 帖子需要目的,你要做什么?
  • 如果涉及代码,需要报错提示全部代码文本,请注意不要贴代码图片
  • 必看:玩转星瞳论坛了解一下图片上传,代码格式等问题。
  • 由模板匹配模板弄的识别物体回传坐标,但不能返回坐标值,想问怎么样能实现模板匹配或者特征点检测画出矩形框后得出中心坐标?



    • 报错:AttributeError: 'int' object has no attribute 'x'
      代码如下
      import sensor,image,time,math,pyb,lcd
      from image import SEARCH_EX, SEARCH_DS
      from pyb import UART,LED
      import json
      import ustruct
      lcd.init()
      sensor.reset()
      sensor.set_contrast(1)
      sensor.set_gainceiling(16)
      # Max resolution for template matching with SEARCH_EX is QQVGA
      # You can set windowing to reduce the search image.
      #sensor.set_windowing(((640-80)//2, (480-60)//2, 80, 60))
      sensor.set_pixformat(sensor.GRAYSCALE)
      sensor.set_framesize(sensor.QQVGA)#QQVGA的分辨率为120*160
      sensor.skip_frames(time = 2000)
      sensor.set_auto_gain(False) # 关闭自动增益
      sensor.set_auto_whitebal(False) #关闭白平衡
      sensor.set_vflip(True)#垂直方向翻转
      
      templates = ["/1.pgm"]
      
      clock = time.clock()
      
      uart = UART(3,9600)   #定义串口3变量
      uart.init(9600, bits=8, parity=None, stop=1) # init with given parameters
      
      def sending_data(cx,cy,cw,ch):
          global uart;
          data = ustruct.pack("<bbhhhhb",      #格式为俩个字符俩个短整型(2字节)
                         0x2C,                      #帧头1
                         0x12,                      #帧头2
                         int(cx), # up sample by 4   #数据1
                         int(cy), # up sample by 4    #数据2
                         int(cw), # up sample by 4    #数据1
                         int(ch), # up sample by 4    #数据2
                         0x5B)
          uart.write(data);   #必须要传入一个字节数组
      
      led = pyb.LED(3)
          # Run template matching
      while (True):
          clock.tick()
          img = sensor.snapshot()
          led.on()
          # 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.
          for t in templates:
              template = image.Image(t)
              #对每个模板遍历进行模板匹配
              r = img.find_template(template, 0.50, 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:
                  # 如果匹配到了模板,画框、十字并记录中心坐标
             for match in r:
                 x = match.x()
                 y = match.y()
                 w = match.w()
                 h = match.h()
                 center_x = x + (w // 2)
                 center_y = y + (h // 2)
                 print(t) #打印模板名字
                 FH = bytearray([0x2C,0x12,int(center_x),int(center_y),int(w),int(h),0x5B])
                 uart.write(FH)
                 print(cx,cy,cw,ch)
                 lcd.display(img)
          else:
              FH = bytearray([0x2C,0x12,0,0,0,0,0x5B])
              uart.write(FH)
              img.draw_string(0,0,"no object",color = (120,0,0))
              lcd.display(img)
      
      
      


    • import time, sensor, image
      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)
      
      template = image.Image("/template.pgm")
      
      clock = time.clock()
      
      while (True):
          clock.tick()
          img = sensor.snapshot()
      
          r = img.find_template(template, 0.70, step=4, search=SEARCH_EX)
          if r:
              img.draw_rectangle(r)
              x = r[0]
              y = r[1]
              w = r[2]
              h = r[3]
              cx = x + w/2
              cy = y + h/2
              print(cx, cy)
          print(clock.fps())