[Python] Single-Image Camera Calibration Using 4+ Known Points from a Broadcast Football Match

Discussão em 'Python' iniciado por Stack, Julho 3, 2025.

  1. Stack

    Stack Membro Participativo

    I'm attempting to calibrate a camera from a single image captured during a broadcast football match.

    My goal is to estimate the camera's intrinsic parameters using 4 known points using this function:

    cv2.calibrateCamera(real_image_points, image_points, image.shape[:2], None, None)


    but I'm encountering an error in OpenCV:


    OpenCV(4.7.0) D:\a\opencv-python\opencv-python\opencv\modules\calib3d\src\calibration.cpp:1576: error: (-5:Bad argument) For non-planar calibration rigs, the initial intrinsic matrix must be specified in function 'cvCalibrateCamera2Internal'.
    ​

    Is it possible to calibrate a camera from a single image captured during a broadcast football match?

    Here is the full code used:

    import cv2
    import numpy as np



    clicked_points = []


    def click_event(event, x, y, flags, param):
    if event == cv2.EVENT_LBUTTONDOWN:
    clicked_points.append((x, y))
    print(f"Clicked pixel at (x={x}, y={y})")


    image=cv2.imread('image0.png')

    cv2.imshow("Image", image)
    cv2.setMouseCallback("Image", click_event)

    while True:
    key = cv2.waitKey(1) & 0xFF
    if key == ord('q'):
    break

    cv2.destroyAllWindows()


    print("Clicked points:")
    clicked_points=np.array(clicked_points,dtype=np.float32)
    for point in clicked_points:
    print(point)
    clicked_points=[clicked_points]
    scale_x=image.shape[1]/105
    scale_y=image.shape[0]/68

    first_point_x=5.83*3*scale_x
    second_point_x=5.83*4*scale_x
    third_point_x=5.83*5*scale_x
    fourth_point_x=5.83*6*scale_x
    #all 4 points have y value equal to 0 in 3d world coordinate frame
    distance_z_camera=77.


    real_image_points=[np.array([(first_point_x,0,distance_z_camera),(second_point_x,0,distance_z_camera),(third_point_x,0,distance_z_camera),(fourth_point_x,0,distance_z_camera)],dtype=np.float32)]

    retval, K, distortion_coefficients, _, _ = cv2.calibrateCamera(real_image_points, clicked_points, image.shape[:2], None, None)

    Continue reading...

Compartilhe esta Página