星瞳实验室APP,快速收到回复
  • 我们只解决官方正版的OpenMV的问题(STM32),其他的分支有很多兼容问题,我们无法解决。
  • 如果有产品硬件故障问题,比如无法开机,论坛很难解决。可以直接找售后维修
  • 发帖子之前,请确认看过所有的视频教程,https://singtown.com/learn/ 和所有的上手教程http://book.openmv.cc/
  • 每一个新的提问,单独发一个新帖子
  • 帖子需要目的,你要做什么?
  • 如果涉及代码,需要报错提示全部代码文本,请注意不要贴代码图片
  • 必看:玩转星瞳论坛了解一下图片上传,代码格式等问题。
  • 二值化后如何查找色块,阈值如何设置设置LAB还是其他?



    • 0_1565075630056_b0beca1b-8b64-4ea5-8772-f3fed0fa1986-image.png

      # Find Rects Example
      #
      # This example shows off how to find rectangles in the image using the quad threshold
      # detection code from our April Tags code. The quad threshold detection algorithm
      # detects rectangles in an extremely robust way and is much better than Hough
      # Transform based methods. For example, it can still detect rectangles even when lens
      # distortion causes those rectangles to look bent. Rounded rectangles are no problem!
      # (But, given this the code will also detect small radius circles too)...
      
      import sensor, image, time, math, pyb
      from pyb import UART
      import lcd
      
      sensor.reset()
      sensor.set_contrast(1)
      sensor.set_brightness(1)
      sensor.set_saturation(1)
      sensor.set_gainceiling(16)
      #sensor.set_pixformat(sensor.GRAYSCALE)
      sensor.set_pixformat(sensor.RGB565) # grayscale is faster (160x120 max on OpenMV-M7)
      sensor.set_framesize(sensor.QQVGA)
      #sensor.set_windowing((340, 340))  #数据映射
      sensor.skip_frames(time = 2000)
      clock = time.clock()
      
      uart = UART(3, 115200)
      
      lcd.init()
      led = pyb.LED(1)
      
      #计算左右两边直线长度
      def cal_line(corners):
          x1 = corners[0][0]
          y1 = corners[0][1]
          x2 = corners[1][0]
          y2 = corners[1][1]
          x3 = corners[2][0]
          y3 = corners[2][1]
          x4 = corners[3][0]
          y4 = corners[3][1]
          #计算长度
          yl1 = math.sqrt(math.pow((x1-x4),2) + math.pow((y1-y4),2))
          yl2 = math.sqrt(math.pow((x2-x3),2) + math.pow((y2-y3),2))
          return yl1,yl2
      
      def FindMaxBlobs(BlobList):
          #寻找最大色块
          most_pixels = 0
          largest_blob = 0
          if BlobList:
              for i in range(len(BlobList)):#range()创建一个整数列表
                  if BlobList[i].pixels() > most_pixels:
                      most_pixels = BlobList[i].pixels()
                      largest_blob = i
              return BlobList[largest_blob]
          return None
      
      #发送uart数据
      def send_uart(Type,x,y,s):
          output_str="%d,%d,%d,%d" % (Type,x,y,s)
          uart.write(output_str+'\r\n')
          print(output_str)
      
      blank_thresholds = [(3, 35, -52, 47, -128, 126)]
      red_threshold = [(19, 100, 25, 74, -128, 127)]
      #blank_thresholds=[(140, 255)]
      while(True):
          Type = 255
          P0 = -1
          P1 = -1
          P2 = -1
          PL1 = -1
          LP2 = -1
          clock.tick()
          img = sensor.snapshot().lens_corr(1.8)
          img.binary(red_threshold)
          #img.erode(3)
          #img.dilate(1)
          #img.erode(3)
          #img.dilate(1)
          if img.find_blobs(red_threshold,merge=True):
              led.on()
              blobs = img.find_blobs(red_threshold,pixels_threshold=3,merge=True)
              r = FindMaxBlobs(blobs)
              #for r in img.find_blobs(blank_thresholds,pixels_threshold=10,merge=True):
              img.draw_rectangle(r.rect(), color = (0, 0, 0))
              for p in r.corners(): img.draw_circle(p[0], p[1], 5, color = (0, 255, 0))
              if r.w() * r.h()>200:
                  Type = 1
                  P0 = r.x() + r.w()//2
                  P1 = r.y() + r.h()//2
                  P2 = r.w() * r.h()//100
                  (PL1,PL2) = cal_line(r.corners())
                  #print(PL1,PL2)
                  send_uart(Type,P0,P1,P2)
              else:
                  Type = 255
                  send_uart(255,-1,-1,-1)
          if Type == 1:
                  led.on()
          else:
              led.off()
          lcd.display(img)
      
      
      

      使用以上代码对图片先进行二值化然后查找红色色块,二值化阈值与查找色块阈值为同一红色阈值,一开始会检测到并框出,但是莫名就不能检测了,求教😁



    • 二值化之后就是黑白的了,阈值就不是红色了。是白色