• OpenMV VSCode 扩展发布了,在插件市场直接搜索OpenMV就可以安装
  • 如果有产品硬件故障问题,比如无法开机,论坛很难解决。可以直接找售后维修
  • 发帖子之前,请确认看过所有的视频教程,https://singtown.com/learn/ 和所有的上手教程http://book.openmv.cc/
  • 每一个新的提问,单独发一个新帖子
  • 帖子需要目的,你要做什么?
  • 如果涉及代码,需要报错提示全部代码文本,请注意不要贴代码图片
  • 必看:玩转星瞳论坛了解一下图片上传,代码格式等问题。
  • 怎么距离近的时候能识别到九宫格,距离一远就就识别的不精确,抬高镜头取阈值也不行,还有怎么给识别到的九宫格编号呢,谢谢。



    • 
      import sensor
      import image
      import time
      import math
      
      # 初始化摄像头
      sensor.reset()
      sensor.set_pixformat(sensor.RGB565)
      sensor.set_framesize(sensor.QVGA)
      sensor.skip_frames(time=2000)
      
      # 设置颜色阈值,以下是一个黄色的示例
      yellow_threshold = (30, 100, -10, 50, -30, 50)  # H: 30-100, S: 10-50, V: 30-50
      
      # 创建一个时钟对象以计算FPS
      clock = time.clock()
      
      def draw_rotated_rect(img, center, angle, width, height, color=(255, 0, 0)):
          """ 绘制旋转矩形 """
          # 计算矩形四个角的坐标
          half_width = width / 2
          half_height = height / 2
      
          # 旋转矩形的四个角
          points = [
              (-half_width, -half_height),
              (half_width, -half_height),
              (half_width, half_height),
              (-half_width, half_height)
          ]
      
          # 进行旋转
          rotated_points = []
          for point in points:
              x = point[0] * math.cos(angle) - point[1] * math.sin(angle)
              y = point[0] * math.sin(angle) + point[1] * math.cos(angle)
              rotated_points.append((x + center[0], y + center[1]))
      
          # 绘制旋转后的矩形
          img.draw_line(int(rotated_points[0][0]), int(rotated_points[0][1]), int(rotated_points[1][0]), int(rotated_points[1][1]), color=color)
          img.draw_line(int(rotated_points[1][0]), int(rotated_points[1][1]), int(rotated_points[2][0]), int(rotated_points[2][1]), color=color)
          img.draw_line(int(rotated_points[2][0]), int(rotated_points[2][1]), int(rotated_points[3][0]), int(rotated_points[3][1]), color=color)
          img.draw_line(int(rotated_points[3][0]), int(rotated_points[3][1]), int(rotated_points[0][0]), int(rotated_points[0][1]), color=color)
      
      while True:
          clock.tick()
          img = sensor.snapshot()
      
          # 查找黄色色块
          blobs = img.find_blobs([yellow_threshold], pixels_threshold=200, area_threshold=200, merge=True)
      
          # 检测到的小框的角度和中心点
          centers = []
          angles = []
          rects = []
      
          # 遍历找到的色块
          for blob in blobs:
              # 过滤掉小的噪声
              if blob.area() > 100:
                  # 获取色块的矩形框
                  rect = blob.rect()
                  rects.append(rect)
                  # 判断是否为正方形
                  if abs(rect[2] - rect[3]) < 10:  # 允许宽高差异小于10像素
                      # 计算中心点和旋转角度
                      center = (blob.cx(), blob.cy())
                      angle = math.atan2(rect[3], rect[2])  # 计算旋转角度
                      centers.append(center)
                      angles.append(angle)
      
                      # 绘制九宫格小框
                      img.draw_rectangle(rect)
      
          # 如果检测到了九个小框
          if len(centers) == 9:
              # 计算九个小框的外接矩形
              min_x = min([rect[0] for rect in rects])
              min_y = min([rect[1] for rect in rects])
              max_x = max([rect[0] + rect[2] for rect in rects])
              max_y = max([rect[1] + rect[3] for rect in rects])
      
              # 计算大矩形的中心点和角度
              center_x = (min_x + max_x) // 2
              center_y = (min_y + max_y) // 2
              avg_angle = sum(angles) / len(angles)
      
              # 计算大矩形的宽度和高度
              width = max_x - min_x
              height = max_y - min_y
      
              # 绘制旋转后的大矩形
              draw_rotated_rect(img, (center_x, center_y), avg_angle, width, height, color=(0, 255, 0))
      
          print("FPS: {}".format(clock.fps()))
      
      
      请在这里粘贴代码