本项目用于机器人竞赛场景: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.
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")识别多个小钢球