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



    • 为什么我采集三角形经常会让其他圆形或矩形来识别,总会混叠



    • 代码发来我帮你找找问题



    • # Untitled - By: A - 周六 10月 10 2020
      
      import sensor
      import image
      import 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()
      enable_lens_corr = False  # turn on for straighter lines...打开以获得更直的线条…
      
      
      thresholds = [(30, 100, 15, 127, 15, 127),  # 红
                    (68, 81, -90, -24, 19, 90),  # 绿
                    (28, 72, -7, 49, -103, -50)]  # 蓝
      
      # generic_red_thresholds -> index is 0 so code == (1 << 0)
      red = (30, 100, 15, 127, 15, 127)
      green = (0, 100, -94, -28, 22, 90)
      blue = (28, 72, -7, 49, -103, -50)
      
      if (sensor.get_id() == sensor.OV7725):
          sensor.__write_reg(0xAC, 0xDF)
          sensor.__write_reg(0x8F, 0xFF)
      
      
      min_degree = 0
      max_degree = 179
      
      
      def find_max(blobs):
          max_size = 0
          for blob in blobs:
              if blob[2]*blob[3] > max_size:
                  max_blob = blob
                  max_size = blob[2]*blob[3]
          return max_blob
      
      
      while(True):
          clock.tick()
          img = sensor.snapshot().lens_corr(1.8)
          img.gaussian(1, unsharp=True)
      
          # 下面的`threshold`应设置为足够高的值,以滤除在图像中检测到的具有
      
          # 低边缘幅度的噪声矩形。最适用与背景形成鲜明对比的矩形。
      
          # 检测正方形
          flag = 0    # 摄像头是否检测到物体
          for r in img.find_rects(threshold=1000):
      
              area = (r.x(), r.y(), r.h(), r.w())
              if r.x()<0 or r.y()<0:
                          continue
              statistics = img.get_statistics (roi = area)  # 像素颜色统计
              m =(1/2)*r.w()
              n = (1/2)*r.h()
              # print(statistics)
      
              print(m,n)
      
              # (0,100,0,120,0,120)是红色的阈值,所以当区域内的众数(也就是最多的颜色),范围在这个阈值内,就说明是红色的矩形。
      
              # l_mode(),a_mode(),b_mode()是L通道,A通道,B通道的众数。
      
              if 20 < statistics.l_mode() < 80 and -12 < statistics.a_mode() < 90 and 2 < statistics.b_mode() < 93:  # if the rectangle is red
                  img.draw_rectangle(r.rect(), color=(255, 0, 0))  # 识别到的红色矩形用红色的矩框出来
                  print('这是一个红色矩形')
                  m = int(m)
                  n = int(n)
                  img.draw_cross(r.x()+m, r.y() + n, size=5, color=(255,255,255))
                  flag = 1
      
              if 5 < statistics.l_mode() < 81 and -73 < statistics.a_mode() < 16 and 9 < statistics.b_mode() < 75:  # if the rectangle is red
                  img.draw_rectangle(r.rect(), color=(0, 255, 0))  # 识别到的绿色矩形用绿色的矩框出来
                  m = int(m)
                  n = int(n)
                  img.draw_cross(r.x()+m, r.y() + n, size=5, color=(255,255,255))
                  print('这是一个绿色矩形')
                  flag = 1
      
              if 28 < statistics.l_mode() < 72 and -7 < statistics.a_mode() < 49 and -103 < statistics.b_mode() < -50:  # if the rectangle is red
                  img.draw_rectangle(r.rect(), color=(0, 0, 255))  # 识别到的蓝色矩形用蓝色的矩框出来
                  m = int(m)
                  n = int(n)
                  img.draw_cross(r.x()+m, r.y() + n, size=5, color=(255,255,255))
                  print('这是一个蓝色矩形')
                  flag = 1
              # print(r)
      
          # 检测圆
          if flag == 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):
      
                  area = (c.x()-c.r(), c.y()-c.r(), 2*c.r(), 2*c.r())
      
                  # area为识别到的圆的区域,即圆的外接矩形框
      
                  statistics = img.get_statistics(roi=area)  # 像素颜色统计
      
                  # print(statistics)
      
                  # (0,100,0,120,0,120)是红色的阈值,所以当区域内的众数(也就是最多的颜色),范围在这个阈值内,就说明是红色的圆。
      
                  # l_mode(),a_mode(),b_mode()是L通道,A通道,B通道的众数。
      
                  if 30 < statistics.l_mode() < 100 and 15 < statistics.a_mode() < 127 and 15 < statistics.b_mode() < 127:  # if the circle is red
      
                      img.draw_circle(c.x(), c.y(), c.r() + 5, color=(
                          255, 0, 0))  # 识别到的红色圆形用红色的圆框出来
                      img.draw_cross(c.x(), c.y(),size = 5 ,color = (255,255,255))
                      print('这是一个红色的圆')
                      flag = 1
      
                  if 68 < statistics.l_mode() < 81 and -90 < statistics.a_mode() < -24 and 19 < statistics.b_mode() < 90:  # if the circle is red
      
                      img.draw_circle(c.x(), c.y(), c.r() + 5, color=(
                          0, 255, 0))  # 识别到的绿色圆形用绿色的圆框出来
                      img.draw_cross(c.x(), c.y(),size = 5 ,color = (255,255,255))
                      print('这是一个绿色的圆')
                      flag = 1
      
                  if 28 < statistics.l_mode() < 72 and -7 < statistics.a_mode() < 49 and -103 < statistics.b_mode() < -50:  # if the circle is red
      
                      img.draw_circle(c.x(), c.y(), c.r() + 5, color=(
                          0, 0, 255))  # 识别到的蓝色圆形用蓝色的圆框出来
                      img.draw_cross(c.x(), c.y(),size = 5 ,color = (255,255,255))
                      print('这是一个蓝色的圆')
                      flag = 1
      
              #print("FPS %f" % clock.fps())
          # 检测三角形
          if flag == 0:
          
              if img.find_blobs([red]):
                  max_blob = find_max(img.find_blobs([red]))
                  kernel_size = 1 # kernel width = (size*2)+1, kernel height = (size*2)+1
                  kernel = [-1, -1, -1,\
                            -1, +8, -1,\
                            -1, -1, -1]
                  # 这个一个高通滤波器。见这里有更多的kernel
                  # http://www.fmwconcepts.com/imagemagick/digital_image_filtering.pdf
                  thresholds = [(100, 255)] #
                  img.morph(kernel_size, kernel)
                  img.binary(thresholds)
                  img.erode(1, threshold = 1)
                  line_segs = img.find_lines(
                      [max_blob.x(), max_blob.y(), max_blob.w(), max_blob.h()])
                  if len(line_segs) == 3:
                      for line_seg in line_segs:
                          img.draw_line(line_seg.x1(), line_seg.y1(),
                                        line_seg.x2(), line_seg.y2())
                          print(line_seg)
              
                      vertex_Ax = (line_segs[0].x1()+line_segs[1].x1())//2
                      vertex_Ay = (line_segs[0].y1()+line_segs[1].y1())//2
                      vertex_Dx = (line_segs[2].x1()+line_segs[2].x2())//2
                      vertex_Dy = (line_segs[2].y1()+line_segs[2].y2())//2
              
                      center_x = vertex_Ax-(vertex_Ax-vertex_Dx)*2//3
                      center_y = vertex_Ay-(vertex_Ay-vertex_Dy)*2//3
                      img.draw_cross(center_x, center_y, 5)
                      # print(center_x,center_y)
                      # print(center_x,center_y)
              
                      print("这是红色的正三角形")
              
              if img.find_blobs([green]):
                  max_blob = find_max(img.find_blobs([green]))
                 
                  kernel_size = 1 # kernel width = (size*2)+1, kernel height = (size*2)+1
                  kernel = [-1, -1, -1,\
                            -1, +8, -1,\
                            -1, -1, -1]
                  # 这个一个高通滤波器。见这里有更多的kernel
                  # http://www.fmwconcepts.com/imagemagick/digital_image_filtering.pdf
                  thresholds = [(100, 255)] #
                  img.morph(kernel_size, kernel)
                  img.binary(thresholds)
                  img.erode(1, threshold = 1)
                  line_segs = img.find_lines(
                      [max_blob.x(), max_blob.y(), max_blob.w(), max_blob.h()])
                  if len(line_segs) == 3:
                      for line_seg in line_segs:
                          img.draw_line(line_seg.x1(), line_seg.y1(),
                                        line_seg.x2(), line_seg.y2())
                          print(line_seg)
              
                          vertex_Ax = (line_segs[0].x1()+line_segs[1].x1())//2
                          vertex_Ay = (line_segs[0].y1()+line_segs[1].y1())//2
                          vertex_Dx = (line_segs[2].x1()+line_segs[2].x2())//2
                          vertex_Dy = (line_segs[2].y1()+line_segs[2].y2())//2
              
                          center_x = vertex_Ax-(vertex_Ax-vertex_Dx)*2//3
                          center_y = vertex_Ay-(vertex_Ay-vertex_Dy)*2//3
                          img.draw_cross(center_x, center_y, 5)
                          # print(center_x,center_y)
                          # print(center_x,center_y)
              
                          print("这是绿色的正三角形")
              
              if img.find_blobs([blue]):
                  max_blob = find_max(img.find_blobs([blue]))
                  
                  kernel_size = 1 # kernel width = (size*2)+1, kernel height = (size*2)+1
                  kernel = [-1, -1, -1,\
                            -1, +8, -1,\
                            -1, -1, -1]
                  # 这个一个高通滤波器。见这里有更多的kernel
                  # http://www.fmwconcepts.com/imagemagick/digital_image_filtering.pdf
                  thresholds = [(100, 255)] #
                  img.morph(kernel_size, kernel)
                  img.binary(thresholds)
                  img.erode(1, threshold = 1)
                  line_segs = img.find_lines(
                      [max_blob.x(), max_blob.y(), max_blob.w(), max_blob.h()])
                  if len(line_segs) == 3:
                      for line_seg in line_segs:
                          img.draw_line(line_seg.x1(), line_seg.y1(),
                                        line_seg.x2(), line_seg.y2())
                          print(line_seg)
              
                          vertex_Ax = (line_segs[0].x1()+line_segs[1].x1())//2
                          vertex_Ay = (line_segs[0].y1()+line_segs[1].y1())//2
                          vertex_Dx = (line_segs[2].x1()+line_segs[2].x2())//2
                          vertex_Dy = (line_segs[2].y1()+line_segs[2].y2())//2
              
                          center_x = vertex_Ax-(vertex_Ax-vertex_Dx)*2//3
                          center_y = vertex_Ay-(vertex_Ay-vertex_Dy)*2//3
                          img.draw_cross(center_x, center_y, 5)
                          # print(center_x,center_y)
                          # print(center_x,center_y)
              
                          print("这是蓝色的正三角形")
      

      就这些,谢谢哈