• OpenMV VSCode 扩展发布了,在插件市场直接搜索OpenMV就可以安装
  • 如果有产品硬件故障问题,比如无法开机,论坛很难解决。可以直接找售后维修
  • 发帖子之前,请确认看过所有的视频教程,https://singtown.com/learn/ 和所有的上手教程http://book.openmv.cc/
  • 每一个新的提问,单独发一个新帖子
  • 帖子需要目的,你要做什么?
  • 如果涉及代码,需要报错提示全部代码文本,请注意不要贴代码图片
  • 必看:玩转星瞳论坛了解一下图片上传,代码格式等问题。
  • 请问这个程序采用了自适应阈值巡线(黑线),找不到黑线(屏幕全白)是什么原因



    • import sensor, image, math
      from pyb import UART,LED
      
      LED(3).on()
      
      uart = UART(3, 115200)
      uart.init(115200, bits=8, parity=None, stop=1)
      def send_data(data1,data2,data3):
          global uart
          data = bytearray([0xb3,0xb3,data1,data2,data3,0x5b])
          uart.write(data)
      
      
      ROIS = [
            (0, 0, 128, 40, 0.2),
            (0, 40, 128, 40, 0.4),
            (0, 80, 128, 40, 0.6)
           ]
      range_stop = [400,100,70]
      range_wait = 500
      
      weight_sum = 0
      for r in ROIS: weight_sum += r[4]
      
      sensor.reset()
      sensor.set_contrast(1)
      sensor.set_pixformat(sensor.RGB565)
      sensor.set_framesize(sensor.QQVGA2)
      sensor.set_brightness(10)
      sensor.skip_frames(30)
      sensor.set_auto_whitebal(False)
      sensor.set_vflip(False)
      sensor.set_hmirror(False)
      
      def find_max(blobs):
          max_size=[0,0]
          max_ID=[-1,-1]
          for i in range(len(blobs)):
              if blobs[i].pixels()>max_size[0]:
                  max_ID[1]=max_ID[0]
                  max_size[1]=max_size[0]
                  max_ID[0]=i
                  max_size[0]=blobs[i].pixels()
              elif blobs[i].pixels()>max_size[1]:
                  max_ID[1]=i
                  max_size[1]=blobs[i].pixels()
          return max_ID
      
      def degrees(radians):
          return (180 * radians) / math.pi
      
      #def stop1():
      
      
      def car_run():
          centroid_sum = [0,0]
          left_center=[-1,-1,-1]
          right_center=[-1,-1,-1]
          flag_cross=0
          flag_wait = [0,0]
          flag_stop = [0,0]
      
          for r in range(3):
              blobs = img.find_blobs([(0, l), (-128, a),(-128, b)], roi=ROIS[r][0:4],merge=True,area_threshold=200,margin=3,pixel_threshold=200)
              if blobs:
                  stop_flag = flag_stop[0]
                  max_ID=[-1,-1]
                  max_ID=find_max(blobs)
                  img.draw_rectangle(blobs[max_ID[0]].rect(),50)
                  img.draw_cross(blobs[max_ID[0]].cx(),blobs[max_ID[0]].cy(),50)
                  if max_ID[1]!=-1:
                      flag_cross=1 #有岔路口
                      img.draw_rectangle(blobs[max_ID[1]].rect(),50)
                      img.draw_cross(blobs[max_ID[1]].cx(),blobs[max_ID[1]].cy(),50)
                      if blobs[max_ID[0]].cx()<blobs[max_ID[1]].cx():
                          left_center[r]=blobs[max_ID[0]].cx()
                          right_center[r]=blobs[max_ID[1]].cx()
                      else:
                          left_center[r]=blobs[max_ID[1]].cx()
                          right_center[r]=blobs[max_ID[0]].cx()
                  else:
                      flag_cross=0
                      if blobs[max_ID[0]].pixels()>range_wait:
                          flag_wait[0]+=1
                      elif blobs[max_ID[0]].pixels()>range_stop[r]:
                          flag_stop[0]=r+1
                      left_center[r]=right_center[r]=blobs[max_ID[0]].cx()
                  centroid_sum[0] += left_center[r] * ROIS[r][4]
                  centroid_sum[1] += right_center[r] * ROIS[r][4]
                  if stop_flag == 3 and flag_stop[0] == 2:
                      flag_stop[1] = 1
                  else:
                       flag_stop[1] = 0
      
          center_pos =[0,0]
          center_pos[0] = (centroid_sum[0] / weight_sum)
          center_pos[1] = (centroid_sum[1] / weight_sum)
          deflection_angle = [0,0]
          deflection_angle[0] = -math.atan((center_pos[0]-64)/80)
          deflection_angle[1] = -math.atan((center_pos[1]-64)/80)
          deflection_angle[0] = math.degrees(deflection_angle[0])
          deflection_angle[1] = math.degrees(deflection_angle[1])
          if flag_wait[0] >= 2:
              flag_wait[1] = 1
          else:
              flag_wait[1] = 0
          if center_pos[0]==center_pos[1]==0:
              deflection_angle[1]=deflection_angle[0]=0
          A=[int(deflection_angle[0]),int(deflection_angle[1]),flag_stop[1],flag_wait[1]] #1等待
          return A
      
      
      while(True):
          cross = 1
          flag = 1
          if(uart.any()):
              flag = uart.readchar()
              if flag == 1:
                  cross = 1
              if flag == 2:
                  cross = 1
              if flag == 3:
                  cross = 1
              if flag == 6:
                  cross = 1
              if flag == 7:
                  cross = 0
              if flag == 4:
                  cross = 1
          else:
              cross = 1
      
          img = sensor.snapshot().lens_corr(strength = 0.5, zoom = 1.0)
          img.bilateral(3,color_sigma=0.1,space_sigma=0.1)#采用双边滤波
          img.gaussian(3, unsharp=True,)#图像消除锐化
          histogram = img.get_histogram()
          THRESHOLD = histogram.get_threshold()
          l  = THRESHOLD .l_value()
          a = THRESHOLD .a_value()
          b = THRESHOLD .b_value()
          print(THRESHOLD)
          img.binary([(0, l), (-128, a),(-128, b)])
      
          row_data=[0,0,0,0]
          row_data[0],row_data[1],row_data[2],row_data[3]=car_run()
          if cross == 0:
              if row_data[0] < -4:
                  send_data(-4,row_data[2],row_data[3])
              if -4<= row_data[0] < 0:
                  send_data(row_data[0],row_data[2],row_data[3])
              if 0 <= row_data[0] <= 3:
                  send_data(0,row_data[2],row_data[3])
              if row_data[0] >3:
                  send_data(row_data[0],row_data[2],row_data[3])
          if cross == 1:
              if row_data[1] < -5:
                  send_data(-5,row_data[2],row_data[3])
              if -5<= row_data[1] < 0:
                  send_data(row_data[1],row_data[2],row_data[3])
              if 0 <= row_data[1] <= 3:
                  send_data(0,row_data[2],row_data[3])
              if row_data[1] >3:
                  send_data(row_data[1],row_data[2],row_data[3])
      


    • 我感觉是RGB模式下结果有问题,可以暂时用灰度图:

      import sensor, image, time
      
      sensor.reset()
      sensor.set_pixformat(sensor.GRAYSCALE)
      sensor.set_framesize(sensor.QVGA)
      sensor.skip_frames(time = 2000)
      
      clock = time.clock()
      
      while(True):
          img = sensor.snapshot()
          histogram = img.get_histogram()
          THRESHOLD = histogram.get_threshold()
          print(THRESHOLD)
          img.binary([(0, THRESHOLD.value())])
      

      跟踪issue:
      https://github.com/openmv/openmv/issues/1794