Sabzaliev Shukur
Sabzaliev Shukur

Reputation: 381

Evaluate the correctness of In/Extrinsic Params resulting from camera calibration

I was doing camera calibration with OpenCV and manage to get all the camera parameters, but now I am not sure if I did everything correctly.
Here is the image I used enter image description here I used 6 points on the image (4 court corners and two in the middle where the net touches the court lines)
imgPts = [[577, 303], [1333, 303], [495, 517], [1422, 517], [366, 857], [1562, 857]]
Assuming the top left corner is the origin I constructed the corresponding world coordinates in meters (23.77m x 10.97m):
objPts = [[0, 0, 0], [10.97, 0, 0], [0, 11.8, 0], [10.97, 11.8, 0], [0, 23.77, 0], [10.97, 23.77, 0]]
Following is my code for obtaining the camera_matrix, rotation translation vectors:

objPts = np.array(objPts)
objPts = objPts.astype('float32')

imgPts = np.array(imgPts)
imgPts = imgPts.astype('float32')

w = frame.shape[1]
h = frame.shape[0]

size = (w,h)

camera_matrix = cv2.initCameraMatrix2D([objPts],[imgPts], size)
rms, camera_matrix, dist_coefs, rvecs, tvecs = cv2.calibrateCamera([objPts], [imgPts], size, None, None)

print(rms)

2.2659039195846487

print(camera_matrix)

[[7.29904054e+04 0.00000000e+00 7.70590422e+02]
 [0.00000000e+00 3.27820311e+03 1.05708724e+02]
 [0.00000000e+00 0.00000000e+00 1.00000000e+00]]

print(dist_coefs)

[[-4.60113019e+00  1.52353355e+03 -1.11809613e+00  7.20674734e-02
  -2.28959021e+04]]

print(rvecs[0])

[[ 0.48261931]
 [-4.87671221]
 [ 0.28501516]]

print(tvecs[0])

[[ -0.69935398]
 [ 15.30349325]
 [189.46509398]]

How can I check if these values/matrix/vectors are correct?

Upvotes: 1

Views: 1092

Answers (1)

Christoph Rackwitz
Christoph Rackwitz

Reputation: 15575

I get strange results with your numbers. The coordinates don't seem to match anything recognizable in the picture you shared.

I made my own measurements, based on the 1366x768 picture you shared. The results look very plausible.

However, with slightly different imgPts, I get vastly different results. That means you'll need a lot more measurements for accuracy.

The picture is from a match that took place in Arthur Ashe Stadium, which has a radius of ~70 meters. At ~30 meters from the center, there's a ring path, where this camera could have been.

#!/usr/bin/env python3

import os
import sys
import numpy as np
import cv2 as cv

np.set_printoptions(suppress=True)

# https://en.wikipedia.org/wiki/Tennis_court#Dimensions
court_width = 10.97 # meters
court_length = 23.77 # meters

objPts = np.float32([
    [-0.5, +0.5, 0], # far left
    [+0.5, +0.5, 0], # far right
    # center of court is 0,0,0
    [+0.5, -0.5, 0], # near right
    [-0.5, -0.5, 0], # near left
]) * np.float32([court_width, court_length, 0])

# points centered on the outside lines
# imgPts = np.float32([
#   [ 346,  245], # far left
#   [ 988,  244], # far right
#   [1188,  607], # near right
#   [ 142,  611], # near left
# ])
# points on the outsides of the outside lines (one variant)
# imgPts = np.float32([
#   [ 345,  244], # far left
#   [ 989,  243], # far right
#   [1192,  609], # near right
#   [ 139,  612], # near left
# ])
# points on the outsides of the outside lines (other variant)
imgPts = np.float32([
    [ 344,  244], # far left
    [ 989,  243], # far right
    [1192,  609], # near right
    [ 138,  613], # near left
])

#im = cv.imread("vxUZD.jpg")
#height, width = im.shape[:2]
width, height = 1366, 768

print(f"image size:\n\t{width} x {height}")

C = cv.initCameraMatrix2D([objPts], [imgPts], (width, height))
print("camera matrix:")
print(C)
fx = C[0,0]

# fx * tan(hfov/2) == width/2
hfov = np.arctan(width/2 / fx) * 2
print(f"horizontal FoV:\n\t{hfov / np.pi * 180:.2f} °")

# x? mm focal length -> 36 mm horizontal (24 vertical)?
fd = 36 / (np.tan(hfov/2) * 2)
print(f"focal length (35mm equivalent):\n\t{fd:.2f} mm")

(rv, rvec, tvec) = cv.solvePnP(objPts, imgPts, C, distCoeffs=None)
print("tvec [m]:")
print(tvec)

results:

image size:
        1366 x 768
camera matrix:
[[1850.17197043    0.          682.5       ]
 [   0.         1850.17197043  383.5       ]
 [   0.            0.            1.        ]]
horizontal FoV:
        40.52 °
focal length (35mm equivalent):
        48.76 mm
tvec [m]:
[[-0.2618669 ]
 [-0.45430541]
 [30.2741125 ]]

Here's a more fleshed out script that uses calibrateCamera and nails down various parameters. That seems to result in more stable results.

https://gist.github.com/crackwitz/0d1e401b597b435bcc5e65349cbca870

Upvotes: 2

Related Questions