You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何用OpenCV Python校正已知尺寸曲棍球场的畸变图像

基于已知曲棍球场尺寸的OpenCV畸变校正方案

由于你已掌握球场精确尺寸(91.4m×55m),无需依赖棋盘格标定板,直接利用球场自身特征点即可完成畸变校正,具体实施步骤如下:

1. 标记图像与真实世界的对应点

  • 在畸变图像中标记至少6对非共线的特征点(数量越多,校正精度越高),优先选择球场的清晰交点:比如四个外角、中线与边线的交点、禁区角点等。
  • 设定真实世界坐标系:以球场左下角为原点(0,0,0),所有点都在z=0的平面上(球场是平面),对应坐标按实际尺寸填写。示例如下:
    • 图像点(需实际在图中标记坐标):
      image_points = np.array([[x1,y1], [x2,y2], [x3,y3], [x4,y4], [x5,y5], [x6,y6]], dtype=np.float32)
      
    • 对应真实世界点(单位:米):
      world_points = np.array([[0,0,0], [91.4,0,0], [91.4,55,0], [0,55,0], [45.7,0,0], [45.7,55,0]], dtype=np.float32)
      

2. 求解相机内参与畸变参数

利用OpenCV的相机标定函数,结合标记的点对求解畸变系数和优化后的内参矩阵:

import cv2
import numpy as np

# 替换为你的图像实际尺寸
img_size = (1280, 720)
# 初始化内参矩阵(基于图像尺寸估算,后续会被优化)
K = np.array([[img_size[0], 0, img_size[0]/2],
              [0, img_size[0], img_size[1]/2],
              [0, 0, 1]], dtype=np.float32)
# 初始畸变系数设为0
dist_coeffs = np.zeros((5,1))

# 执行标定,得到优化后的内参和畸变系数
ret, K_opt, dist_opt, rvecs, tvecs = cv2.calibrateCamera(
    [world_points], [image_points], img_size, K, dist_coeffs
)

若图像存在严重鱼眼畸变,改用鱼眼标定模块:cv2.fisheye.calibrate,参数格式需对应调整,且点对数量建议不少于10对。

3. 校正图像畸变

使用求解得到的参数对原始图像进行畸变校正:

# 读取原始畸变图像
original_img = cv2.imread("hockey_field.jpg")

# 方法1:直接校正
undistorted_img = cv2.undistort(original_img, K_opt, dist_opt, None, K_opt)

# 方法2:生成映射表(适合批量处理,效率更高)
mapx, mapy = cv2.initUndistortRectifyMap(K_opt, dist_opt, None, K_opt, img_size, cv2.CV_32FC1)
undistorted_img = cv2.remap(original_img, mapx, mapy, cv2.INTER_LINEAR)

# 保存校正后的图像
cv2.imwrite("undistorted_hockey_field.jpg", undistorted_img)

4. (可选)透视变换为标准平面视图

如果需要将校正后的球场转换为严格的俯视标准尺寸视图,可通过透视变换实现:

# 在校正后的图像中标记球场四个外角
corrected_corners = np.array([[x1', y1'], [x2', y2'], [x3', y3'], [x4', y4']], dtype=np.float32)
# 设定目标尺寸(按比例,比如914x550像素对应91.4m×55m)
target_corners = np.array([[0,0], [914,0], [914,550], [0,550]], dtype=np.float32)

# 计算透视变换矩阵
M = cv2.findHomography(corrected_corners, target_corners)[0]
# 执行变换
top_down_img = cv2.warpPerspective(undistorted_img, M, (914, 550))

cv2.imwrite("top_down_hockey_field.jpg", top_down_img)

关键注意事项

  • 标记点的精度直接决定校正效果,尽量放大图像标记像素级准确的交点。
  • 若单幅图像标定结果不够理想,可从视频中提取多帧不同角度的球场图像,重复上述步骤,用多帧点对参与标定,进一步提升精度。

内容的提问来源于stack exchange,提问作者Adrien Kinart

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.15 02:10:33