导航

    • 登录
    • 搜索
    • 版块
    • 产品
    • 教程
    • 论坛
    • 淘宝
    1. 主页
    2. eo5s
    E
    • 举报资料
    • 资料
    • 关注
    • 粉丝
    • 屏蔽
    • 帖子
    • 楼层
    • 最佳
    • 群组

    eo5s

    @eo5s

    0
    声望
    1
    楼层
    29
    资料浏览
    0
    粉丝
    0
    关注
    注册时间 最后登录

    eo5s 关注

    eo5s 发布的帖子

    • 第一段代码没报错,第二段出现NoneType object has no attribute cx,叹号处报错
      from pyb import UART
      import sensor
      import time
      import image
      def init_sensor():
          sensor.reset()
          sensor.set_pixformat(sensor.RGB565)#设置图像传感器采集图像时的像素格式
          sensor.set_framesize(sensor.QVGA)#功能是让图像传感器跳过若干帧图像,跳过一段时间内的图像帧,可以避免采集到不稳定环境下的图像,从而提高后续图像处理的准确性
          sensor.skip_frames(time=2000)#功能是让图像传感器跳过若干帧图像,跳过一段时间内的图像帧,可以避免采集到不稳定环境下的图像,从而提高后续图像处理的准确性
          sensor.set_auto_gain(False)#自动增益功能会根据图像的整体亮度自动调整传感器的增益,以确保图像具有合适的亮度和对比度
          sensor.set_auto_whitebal(False)#自动白平衡功能的作用是自动调整图像的颜色平衡,以消除不同光源颜色对图像颜色的影响
      def init_uart():#初始化串口用于与其他设备进行通信
          return UART(3, 115200)
      # 查找最大的色块
      def find_max(blobs):
          max_blob = None#用于存储当前找到的最大色块对应的 blob 对象,。将 max_blob 初始化为 None,代表当前不存在最大的色块。None 是 Python 里的一个特殊值,用于表示变量没有值或者对象不存在。
          max_size = 0#用于记录当前找到的最大色块所包含的像素数量。初始化为 0 能确保在首次比较时,任何非空的 blob 对象的像素数量都会大于 max_size,从而使比较过程得以正常开展
          for blob in blobs:
              if blob.pixels() > max_size:#若当前 blob 对象的像素数量大于已记录的最大像素数量 max_size,就更新 max_blob 为当前 blob 对象
                  max_size = blob.pixels()
                  max_blob = blob
          return max_blob
      # 标记最大色块
      def mark_max_blob(img, max_blob):
          blob_cx = max_blob.cx()#用于获取该色块的中心 x 坐标
          blob_cy = max_blob.cy()
          img.draw_cross(blob_cx, blob_cy)#画十字架
          img.draw_rectangle(max_blob.rect())#画矩形
          return blob_cx, blob_cy
      #发送命令到串口
      def send_control_commands(uart, x_offset, y_offset):
          # 开启异常处理,捕获并处理可能出现的异常
          try:
              # 定义较大的偏移量阈值,用于判断是否需要较大幅度的调整
              LARGE_OFFSET = 20
              # 定义较小的偏移量阈值,用于判断是否需要较小幅度的调整
              SMALL_OFFSET = 10
      
              # 根据水平偏移量 x_offset 的大小,确定水平方向的控制指令
              if x_offset < -LARGE_OFFSET:
                  # 当水平偏移量小于负的较大阈值时,设定控制指令为 'a'
                  command = 'a'
              elif x_offset > LARGE_OFFSET:
                  # 当水平偏移量大于较大阈值时,设定控制指令为 'c'
                  command = 'c'
              elif -LARGE_OFFSET <= x_offset < -SMALL_OFFSET:
                  # 当水平偏移量在负的较大阈值和负的较小阈值之间时,设定控制指令为 'b'
                  command = 'b'
              elif SMALL_OFFSET < x_offset <= LARGE_OFFSET:
                  # 当水平偏移量在较小阈值和较大阈值之间时,设定控制指令为 'd'
                  command = 'd'
              else:
                  # 当水平偏移量在较小阈值范围内时,不设置水平方向的控制指令
                  command = None
      
              # 根据垂直偏移量 y_offset 的大小,确定垂直方向的控制指令
              if y_offset < -LARGE_OFFSET:
                  # 当垂直偏移量小于负的较大阈值时,设定控制指令为 'w'
                  vertical_command = 'w'
              elif y_offset > LARGE_OFFSET:
                  # 当垂直偏移量大于较大阈值时,设定控制指令为 'x'
                  vertical_command = 'x'
              elif -LARGE_OFFSET <= y_offset < -SMALL_OFFSET:
                  # 当垂直偏移量在负的较大阈值和负的较小阈值之间时,设定控制指令为 'y'
                  vertical_command = 'y'
              elif SMALL_OFFSET < y_offset <= LARGE_OFFSET:
                  # 当垂直偏移量在较小阈值和较大阈值之间时,设定控制指令为 'z'
                  vertical_command = 'z'
              else:
                  # 当垂直偏移量在较小阈值范围内时,设定控制指令为 'o'
                  vertical_command = 'o'
      
              # 确定最终要发送的控制指令
              # 如果水平方向有有效的控制指令,则优先使用水平方向的指令
              # 否则,使用垂直方向的指令
              final_command = command if command is not None else vertical_command
      
              # 通过串口 uart 发送最终确定的控制指令
              uart.write(final_command)
      
          # 捕获并处理可能出现的异常
          except Exception as e:
              # 打印错误信息,方便调试
              print(f"Error sending commands: {e}")
      def main():
          init_sensor()
          uart = init_uart()
          red_threshold = [
             (75, 43, 56, 30, -2, 28),
             (70, 40, 64, 38, -19, 14),
          ]#设置颜色的阈值
          clock = time.clock()#可以记录每次循环开始和结束的时间,通过时间差来计算帧率
          while True:
              clock.tick()
              img = sensor.snapshot()
              blobs = img.find_blobs(red_threshold)
              fps = clock.fps()
              print(f"FPS: {fps}")
              if blobs:
                  max_blob = find_max(blobs)
                  blob_cx, blob_cy = mark_max_blob(img, max_blob)
                  img_center_x = img.width() // 2
                  img_center_y = img.height() // 2
                  #分别计算当前图像在水平方向和垂直方向上的中心坐标。
                  #值为 320 // 2 = 160;通过 img_height // 2 计算出图像垂直方向的中心坐标 img_center_y,其值为 240 // 2 = 120。
                  #240//2=120
                  x_offset = blob_cx - img_center_x
                  y_offset = blob_cy - img_center_y
                  #接着用最大色块的中心横坐标 blob_cx 减去图像水平中心坐标 img_center_x,
                  #得到水平偏移量 x_offset = 180 - 160 = 20;用最大色块的中心纵坐标 blob_cy 减去图像垂直中心坐标 img_center_y,
                  #得到垂直偏移量 y_offset = 150 - 120 = 30。
                  send_control_commands(uart, x_offset, y_offset)
              else:
                  send_control_commands(uart, 0, 0)
              time.sleep(50 / 1000)
      if __name__ == "__main__":
          main()
      

      请在这里粘贴代码

      # This work is licensed under the MIT license.
      # Copyright (c) 2013-2023 OpenMV LLC. All rights reserved.
      # https://github.com/openmv/openmv/blob/master/LICENSE
      #
      # Hello World Example
      #
      # Welcome to the OpenMV IDE! Click on the green run arrow button below to run the script!
      
      from pyb import UART
      import sensor
      import time
      import image
      from machine import LED
      
      led1 = LED("LED_BLUE")
      led2 = LED("LED_RED")
      led3 = LED("LED_GREEN")
      
      def init_sensor():
          sensor.reset()
          sensor.set_pixformat(sensor.RGB565)#设置图像传感器采集图像时的像素格式
          sensor.set_framesize(sensor.QVGA)#功能是让图像传感器跳过若干帧图像,跳过一段时间内的图像帧,可以避免采集到不稳定环境下的图像,从而提高后续图像处理的准确性
          sensor.skip_frames(time=2000)#功能是让图像传感器跳过若干帧图像,跳过一段时间内的图像帧,可以避免采集到不稳定环境下的图像,从而提高后续图像处理的准确性
          sensor.set_auto_gain(False)#自动增益功能会根据图像的整体亮度自动调整传感器的增益,以确保图像具有合适的亮度和对比度
          sensor.set_auto_whitebal(False)#自动白平衡功能的作用是自动调整图像的颜色平衡,以消除不同光源颜色对图像颜色的影响
      def init_uart():#初始化串口用于与其他设备进行通信
          return UART(3, 115200)
      # 查找最大的色块
      def find_redmax(blobs):
          redmax_blob = None#用于存储当前找到的最大色块对应的 blob 对象,。将 max_blob 初始化为 None,代表当前不存在最大的色块。None 是 Python 里的一个特殊值,用于表示变量没有值或者对象不存在。
          redmax_size = 0#用于记录当前找到的最大色块所包含的像素数量。初始化为 0 能确保在首次比较时,任何非空的 blob 对象的像素数量都会大于 max_size,从而使比较过程得以正常开展
          for blob in blobs:
              if (blob.pixels() > redmax_size and (blob.code==1 or blob.code==2)):#若当前 blob 对象的像素数量大于已记录的最大像素数量 max_size,就更新 max_blob 为当前 blob 对象
                  redmax_size = blob.pixels()
                  redmax_blob = blob
          return redmax_blob
      # 标记最大色块
      def find_greenmax(blobs):
          greenmax_blob = None#用于存储当前找到的最大色块对应的 blob 对象,。将 max_blob 初始化为 None,代表当前不存在最大的色块。None 是 Python 里的一个特殊值,用于表示变量没有值或者对象不存在。
          greenmax_size = 0#用于记录当前找到的最大色块所包含的像素数量。初始化为 0 能确保在首次比较时,任何非空的 blob 对象的像素数量都会大于 max_size,从而使比较过程得以正常开展
          for blob in blobs:
              if (blob.pixels() > greenmax_size and blob.code==4 ):#若当前 blob 对象的像素数量大于已记录的最大像素数量 max_size,就更新 max_blob 为当前 blob 对象
                  greenmax_size = blob.pixels()
                  greenmax_blob = blob
          return greenmax_blob
      # 标记最大色块
      def mark_redmax_blob(img, redmax_blob):
          blob_cx = redmax_blob.cx()#用于获取该色块的中心 x 坐标    !!!!!!!!!!
          blob_cy = redmax_blob.cy()
          img.draw_cross(blob_cx, blob_cy)#画十字架
          img.draw_rectangle(redmax_blob.rect())#画矩形
          return blob_cx, blob_cy
      def mark_greenmax_blob(img, greenmax_blob):
          blob_cx = greenmax_blob.cx()#用于获取该色块的中心 x 坐标
          blob_cy = greenmax_blob.cy()
          img.draw_cross(blob_cx, blob_cy)#画十字架
          img.draw_rectangle(greenmax_blob.rect())#画矩形
          return blob_cx, blob_cy
      #发送命令到串口
      
      def send_control_commands(uart, x_offset, y_offset):
          # 开启异常处理,捕获并处理可能出现的异常
          try:
              # 定义较大的偏移量阈值,用于判断是否需要较大幅度的调整
              LARGE_OFFSET = 20
              # 定义较小的偏移量阈值,用于判断是否需要较小幅度的调整
              SMALL_OFFSET = 10
      
              # 根据水平偏移量 x_offset 的大小,确定水平方向的控制指令
              if x_offset < -LARGE_OFFSET:
                  # 当水平偏移量小于负的较大阈值时,设定控制指令为 'a'
                  command = 'a'
                  led1.on()
                  time.sleep_ms(200)
                  led1.off()
                  time.sleep_ms(200)
              elif x_offset > LARGE_OFFSET:
                  # 当水平偏移量大于较大阈值时,设定控制指令为 'c'
                  command = 'c'
                  led2.on()
                  time.sleep_ms(200)
                  led2.off()
                  time.sleep_ms(200)
              elif -LARGE_OFFSET <= x_offset < -SMALL_OFFSET:
                  # 当水平偏移量在负的较大阈值和负的较小阈值之间时,设定控制指令为 'b'
                  command = 'b'
              elif SMALL_OFFSET < x_offset <= LARGE_OFFSET:
                  # 当水平偏移量在较小阈值和较大阈值之间时,设定控制指令为 'd'
                  command = 'd'
              else:
                  # 当水平偏移量在较小阈值范围内时,不设置水平方向的控制指令
                  command = None
      
              # 根据垂直偏移量 y_offset 的大小,确定垂直方向的控制指令
              if y_offset < -LARGE_OFFSET:
                  # 当垂直偏移量小于负的较大阈值时,设定控制指令为 'w'
                  vertical_command = 'w'
              elif y_offset > LARGE_OFFSET:
                  # 当垂直偏移量大于较大阈值时,设定控制指令为 'x'
                  vertical_command = 'x'
              elif -LARGE_OFFSET <= y_offset < -SMALL_OFFSET:
                  # 当垂直偏移量在负的较大阈值和负的较小阈值之间时,设定控制指令为 'y'
                  vertical_command = 'y'
              elif SMALL_OFFSET < y_offset <= LARGE_OFFSET:
                  # 当垂直偏移量在较小阈值和较大阈值之间时,设定控制指令为 'z'
                  vertical_command = 'z'
              else:
                  # 当垂直偏移量在较小阈值范围内时,设定控制指令为 'o'
                  vertical_command = 'o'
      
              # 确定最终要发送的控制指令
              # 如果水平方向有有效的控制指令,则优先使用水平方向的指令
              # 否则,使用垂直方向的指令
              final_command = command if command is not None else vertical_command
      
              # 通过串口 uart 发送最终确定的控制指令
              uart.write(final_command)
      
          # 捕获并处理可能出现的异常
          except Exception as e:
              # 打印错误信息,方便调试
              print(f"Error sending commands: {e}")
      def main():
          init_sensor()
          uart = init_uart()
          color_threshold = [
             (75, 43, 56, 30, -2, 28),
             (70, 40, 64, 38, -19, 14),
          (20,80,-60,-20,-30,30)]#设置颜色的阈值
          clock = time.clock()#可以记录每次循环开始和结束的时间,通过时间差来计算帧率
          while True:
              clock.tick()
              img = sensor.snapshot()
              blobs = img.find_blobs(color_threshold)
              fps = clock.fps()
              print(f"FPS: {fps}")
              if blobs:
                  redmax_blob = find_redmax(blobs)
                  greenmax_blob = find_greenmax(blobs)
                  blob_cxred, blob_cyred = mark_redmax_blob(img, redmax_blob)
                  blob_cxgreen, blob_cygreen = mark_redmax_blob(img, greenmax_blob)
                  img_center_x = img.width() // 2
                  img_center_y = img.height() // 2
                  #分别计算当前图像在水平方向和垂直方向上的中心坐标。
                  #值为 320 // 2 = 160;通过 img_height // 2 计算出图像垂直方向的中心坐标 img_center_y,其值为 240 // 2 = 120。
                  #240//2=120
                  x_offset = blob_cxred - blob_cxgreen
                  y_offset = blob_cyred - blob_cygreen
                  #接着用最大色块的中心横坐标 blob_cx 减去图像水平中心坐标 img_center_x,
                  #得到水平偏移量 x_offset = 180 - 160 = 20;用最大色块的中心纵坐标 blob_cy 减去图像垂直中心坐标 img_center_y,
                  #得到垂直偏移量 y_offset = 150 - 120 = 30。
                  send_control_commands(uart, x_offset, y_offset)
              else:
                  send_control_commands(uart, 0, 0)
              time.sleep(50 / 1000)
      if __name__ == "__main__":
          main()
      
      
      发布在 OpenMV Cam
      E
      eo5s