• OpenMV VSCode 扩展发布了,在插件市场直接搜索OpenMV就可以安装
  • 如果有产品硬件故障问题,比如无法开机,论坛很难解决。可以直接找售后维修
  • 发帖子之前,请确认看过所有的视频教程,https://singtown.com/learn/ 和所有的上手教程http://book.openmv.cc/
  • 每一个新的提问,单独发一个新帖子
  • 帖子需要目的,你要做什么?
  • 如果涉及代码,需要报错提示全部代码文本,请注意不要贴代码图片
  • 必看:玩转星瞳论坛了解一下图片上传,代码格式等问题。
  • 一个物体需要满足,是矩形和满足设置的颜色阈值这两个条件才会被识别出来,if语句应该怎么写?



    • import sensor, image, time
      
      sensor.reset()
      sensor.set_pixformat(sensor.RGB565)
      sensor.set_framesize(sensor.QQVGA)
      sensor.skip_frames(time = 2000)
      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()
      
      K=660#the value should be measured
      yellow_threshold   = (9, 62, 9, 65, -80, -7)
      def find_max(blobs):
          max_size=0
          for blob in blobs:
              if blob.pixels() > max_size:
                  max_blob=blob
                  max_size = blob.pixels()
          return max_blob
      while(True):
          clock.tick()
          img = sensor.snapshot()
          sensor.set_hmirror(False) #水平方向翻转
          sensor.set_vflip(False) #垂直方向翻转
          blobs = img.find_blobs([yellow_threshold])
      
      
          #if not blobs:
          for r in img.find_rects(threshold = 30000):
              img.draw_rectangle(r.rect(), color = (255, 0, 0))
              area = (r.x(),r.y(),r.w(),r.h())
              for p in r.corners():
                  img.draw_circle(p[0], p[1], 5, color = (0, 255, 0))
        #  for c in img.find_circles(threshold = 3500, x_margin = 10, y_margin = 10, r_margin = 10,
         #     r_min = 2, r_max = 100, r_step = 2):
              print (r)
              #area为识别到的圆的区域,即圆的外接矩形框
              statistics = img.get_statistics(roi=area)#像素颜色统计
              #print(statistics)
              #(94, 100, -128, 127, -128, 127)是红色的阈值,所以当区域内的众数(也就是最多的颜色),范围在这个阈值内,就说明是红色的圆。
              #l_mode(),a_mode(),b_mode()是L通道,A通道,B通道的众数。
              if 56<statistics.l_mode()<79 and 35<statistics.a_mode()<127 and -128<statistics.b_mode()<127:#if the circle is red
                  img.draw_rectangle(area, color = (255, 255, 255))
                  #将非红色的圆用白色的矩形框出来
          if blobs :#and len(blobs)==1 :就是这里应该怎么改!!!!!!!!!!!!!!
                      # Draw a rect around the blob.
              max_blob=find_max(blobs)
              img.draw_rectangle(max_blob.rect())
              img.draw_cross(max_blob.cx(), max_blob.cy())
              output_str="[%d,%d]\n" % (max_blob.cx(),max_blob.cy()) #方式1
                      #output_str=json.dumps([max_blob.cx(),max_blob.cy()]) #方式2
              print('you send:',output_str)
              b = blobs[0]
              img.draw_rectangle(b[0:4]) # rect
              img.draw_cross(b[5], b[6]) # cx, cy
              Lm = (b[2]+b[3])/2
              length = K/Lm
                      #print(Lm)
              print(length)
      
      

      怎么设置找到我要的颜色阈值并且该物体是矩形,就是一个物体需要满足,是矩形和是我设置的颜色阈值这两个条件才会被识别出来,所以if语句应该怎么写