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



    • import sensor, image, time, math
      from pyb import UART
      
      GRAYSCALE_THRESHOLD = [(0, 64)]
      
      ROIS = [ # [ROI, weight]
      
              (0, 100, 160, 20, 0.7), # You'll need to tweak the weights for you app
      
              (0, 050, 160, 20, 0.3), # depending on how your robot is setup.
      
              (0, 000, 160, 20, 0.1)
      
             ]
      
      
      for r in ROIS: weight_sum += r[4] # r[4] is the roi weight.
      
      #计算权值和。遍历上面的三个矩形,r[4]即每个矩形的权值。
      
      
      
      # Camera setup...
      
      sensor.reset() # Initialize the camera sensor.
      
      sensor.set_pixformat(sensor.GRAYSCALE) # use grayscale.
      
      sensor.set_framesize(sensor.QQVGA) # use QQVGA for speed.
      
      sensor.skip_frames(30) # Let new settings take affect.
      
      sensor.set_auto_gain(False) # must be turned off for color tracking
      
      sensor.set_auto_whitebal(False) # must be turned off for color tracking
      
      #关闭白平衡
      
      clock = time.clock() # Tracks FPS.
      
      uart = UART(3, 19200)
      uart.init(115200, bits=8, parity=None, stop=1)
      
      while(True):
      
          clock.tick() # Track elapsed milliseconds between snapshots().
      
          img = sensor.snapshot() # Take a picture and return the image.
      
      
      
          centroid_sum = 0
      
          #利用颜色识别分别寻找三个矩形区域内的线段
      
          for r in ROIS:
      
              blobs = img.find_blobs(GRAYSCALE_THRESHOLD, roi=r[0:4], merge=True)
      
             
             
      
              if blobs:
      
                  # Find the index of the blob with the most pixels.
      
                  most_pixels = 0
      
                  largest_blob = 0
      
                  for i in range(len(blobs)):
      
                 
      
                      if blobs[i].pixels() > most_pixels:
      
                          most_pixels = blobs[i].pixels()
      
                       
                          largest_blob = i
      
      
      
                  # Draw a rect around the blob.
      
                  img.draw_rectangle(blobs[largest_blob].rect())
      
                  img.draw_rectangle((0,0,30, 30))
      
                  #将此区域的像素数最大的颜色块画矩形和十字形标记出来
      
                  img.draw_cross(blobs[largest_blob].cx(),
      
                                 blobs[largest_blob].cy())
      
      
      
                  centroid_sum += blobs[largest_blob].cx() * r[4] # r[4] is the roi weight.
      
                  #计算centroid_sum,centroid_sum等于每个区域的最大颜色块的中心点的x坐标值乘本区域的权值
      
      
      
          center_pos = (centroid_sum / weight_sum) # Determine center of line.
      
          deflection_angle = 0
      
          #机器人应该转的角度
      
      
      
          
          deflection_angle = -math.atan((center_pos-80)/60)
      
         
      
          deflection_angle = math.degrees(deflection_angle)
      
         
          print(deflection_angle)
          uart.write(deflection_angle)
          
          time.sleep(1000)
      
         
      

      0_1562653474895_b9c94667-6ef1-440f-9ce8-207aac9914ec-image.png



    • 你的代码我无法运行。
      0_1562660166553_73b92e50-10a3-46b6-b795-d0eb7650f250-image.png