导航

    • 登录
    • 搜索
    • 版块
    • 产品
    • 教程
    • 论坛
    • 淘宝
    1. 主页
    2. 53qp
    5
    • 举报资料
    • 资料
    • 关注
    • 粉丝
    • 屏蔽
    • 帖子
    • 楼层
    • 最佳
    • 群组

    53qp

    @53qp

    0
    声望
    1
    楼层
    201
    资料浏览
    0
    粉丝
    0
    关注
    注册时间 最后登录

    53qp 关注

    53qp 发布的帖子

    • 由模板匹配模板弄的识别物体回传坐标,但不能返回坐标值,想问怎么样能实现模板匹配或者特征点检测画出矩形框后得出中心坐标?
      报错: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)
      
      
      
      发布在 OpenMV Cam
      5
      53qp