logo

OpenMV4 H7 Plus识别1cm钢球并定位方位(机器人竞赛应用)

2026-07-23 15:05
R
rrkpz1ks8x6z

OpenMV4 H7 Plus识别1cm钢球并定位方位(机器人竞赛应用)

📌 项目背景与目标

本项目用于机器人竞赛场景:OpenMV4 H7 Plus mounted on a mobile robot car, tasked with detecting a ~1cm diameter metallic steel ball in an indoor venue, estimating its relative position (azimuth angle & distance approximation), and sending coordinate/heading commands to MSPM0G3507 microcontroller for real-time car turning and head alignment.

🔍 视觉识别核心思路

  • 利用钢球高反光特性,在可控室内光照下采用阈值分割(如LAB色彩空间L通道+自适应二值化)提取亮斑区域
  • 结合形态学滤波(开运算去噪、闭运算补洞)提升圆形候选区域鲁棒性
  • 使用find_circles()或find_blobs() + 圆形度筛选(roundness ≈ 1.0)精确定位钢球中心坐标(x, y)
  • 通过像素直径→实际直径标定关系(需在5m距离下实测像素直径),估算相对距离;结合图像中心偏移量计算水平方位角
  • 输出结构化数据(如JSON或简单字节流):{"x":162,"y":120,"r":8,"valid":1} → 经UART/串口发送至MSPM0G3507

⚙️ 关键代码方向提示

import sensor, image, time, math
sensor.reset()
sensor.set_pixformat(sensor.RGB565)
sensor.set_framesize(sensor.QVGA)
sensor.skip_frames(time = 2000)

# 标定参数(示例,需实测)
PIXEL_DIAMETER_AT_5M = 12  # 单位:像素
FOV_H_DEG = 70.8  # OpenMV4 H7 Plus QVGA 水平视场角

while(True):
    img = sensor.snapshot()
    # 转LAB提取高亮区域(钢球反光)
    l, a, b = img.to_lab()
    blobs = l.binary([(200, 255)], invert=False, zero=True)
    blobs = blobs.dilate(2).erode(1)
    
    circles = img.find_circles(threshold = 2000, x_margin = 10, y_margin = 10, r_margin = 5,
                                r_min = 5, r_max = 20, r_step = 2)
    if circles:
        c = max(circles, key=lambda x: x.r())  # 取最大圆(最可能为钢球)
        # 计算方位角(以图像中心为参考)
        cx, cy = img.width()//2, img.height()//2
        dx = c.x() - cx
        angle_deg = (dx / cx) * (FOV_H_DEG / 2)
        
        # 发送数据(示例格式)
        uart.write(f"{c.x()},{c.y()},{c.r()},1\n")

⚠️ 注意事项

  • 室内光照需稳定——避免强直射光造成过曝或镜面眩光干扰;建议加装柔光罩或使用环形LED补光
  • 5m距离下1cm钢球仅约10–15像素直径,需确保镜头对焦准确(推荐使用M12定焦镜头,f=6mm或8mm)
  • MSPM0G3507需配置对应波特率(如115200)、解析协议,并做防丢包校验(如添加起始符/校验和)
  • 首次部署务必进行实地标定:在5m处放置钢球,测量其在QVGA图像中的平均像素直径,更新代码中PIXEL_DIAMETER_AT_5M参数
  • 可选增强:加入颜色过滤(金属灰度+轻微蓝调)、多帧平均抑制噪声、或用find_blobs()+min enclosing circle提高小目标鲁棒性
2026-07-27 20:37
I
icxw9xr95w33

识别多个小钢球