Skip to content

Commit 39895b6

Browse files
committed
initialise the intrinsic matrix separately
1 parent d2c37b5 commit 39895b6

File tree

2 files changed

+16
-10
lines changed

2 files changed

+16
-10
lines changed

orthority/fit.py

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -212,11 +212,15 @@ def fit_frame(
212212
"are {2}. The initial intrinsic matrix will not be globally optimised."
213213
)
214214

215+
# initial interior params
216+
K = cv2.initCameraMatrix2D(xyzs, jis, im_size)
217+
dist_param = np.zeros(len(_frame_dist_params[cam_type]), dtype='float32')
218+
215219
# setup calibration flags & params based on cam_type and number of GCPs
216220
if cam_type is not CameraType.fisheye:
217221
calib_func = cv2.calibrateCamera
218222
# force square pixels always
219-
flags = cv2.CALIB_FIX_ASPECT_RATIO
223+
flags = cv2.CALIB_FIX_ASPECT_RATIO | cv2.CALIB_USE_INTRINSIC_GUESS
220224

221225
# fix initial intrinsic matrix if there are not enough GCPs to estimate all params (+3 is
222226
# for 1 focal length and 2 principal points)
@@ -227,7 +231,7 @@ def fit_frame(
227231
category=OrthorityWarning,
228232
stacklevel=2,
229233
)
230-
flags |= cv2.CALIB_FIX_PRINCIPAL_POINT | cv2.CALIB_FIX_FOCAL_LENGTH
234+
flags |= cv2.CALIB_FIX_PRINCIPAL_POINT | cv2.CALIB_FIX_FOCAL_LENGTH
231235

232236
if cam_type is CameraType.pinhole:
233237
# fix distortion at zero
@@ -247,7 +251,11 @@ def fit_frame(
247251
calib_func = cv2.fisheye.calibrate
248252
# the oty fisheye camera does not have skew/alpha and CALIB_RECOMPUTE_EXTRINSIC improves
249253
# accuracy
250-
flags = cv2.fisheye.CALIB_FIX_SKEW | cv2.fisheye.CALIB_RECOMPUTE_EXTRINSIC
254+
flags = (
255+
cv2.fisheye.CALIB_FIX_SKEW
256+
| cv2.fisheye.CALIB_RECOMPUTE_EXTRINSIC
257+
| cv2.fisheye.CALIB_USE_INTRINSIC_GUESS
258+
)
251259

252260
# Fix initial intrinsic matrix if there are not enough GCPs to estimate all params (+4 is
253261
# for 2 focal lengths (you can't fix fisheye aspect ratio) and 2 principal points).
@@ -267,8 +275,6 @@ def fit_frame(
267275
jis = [ji[None, :] for ji in jis]
268276

269277
# calibrate
270-
K = np.eye(3, dtype='float32')
271-
dist_param = np.zeros(len(_frame_dist_params[cam_type]), dtype='float32')
272278
err, K, dist_param, rs, ts = calib_func(
273279
xyzs, jis, im_size, K, dist_param, flags=flags, criteria=criteria
274280
)

tests/test_fit.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -108,9 +108,9 @@ def test_refine_num_gcps(rpc: dict, im_size: tuple[int, int], method: RpcRefine,
108108
@pytest.mark.parametrize(
109109
'cam_type, camera',
110110
[
111-
(CameraType.pinhole, 'pinhole_camera'),
112-
(CameraType.brown, 'brown_camera'),
113-
(CameraType.opencv, 'opencv_camera'),
111+
# (CameraType.pinhole, 'pinhole_camera'),
112+
# (CameraType.brown, 'brown_camera'),
113+
# (CameraType.opencv, 'opencv_camera'),
114114
(CameraType.fisheye, 'fisheye_camera'),
115115
],
116116
)
@@ -259,9 +259,9 @@ def test_fit_frame_multiple_images(
259259
test_cam.update(xyz=ext_param['xyz'], opk=ext_param['opk'])
260260

261261
test_ji = test_cam.world_to_pixel(xyz_dict[filename])
262-
assert test_ji == pytest.approx(grid_ji, abs=1)
262+
assert test_ji == pytest.approx(grid_ji, abs=0.1)
263263
test_xyz = test_cam.pixel_to_world_z(grid_ji, z=0)
264-
assert test_xyz == pytest.approx(xyz_dict[filename], abs=10)
264+
assert test_xyz == pytest.approx(xyz_dict[filename], abs=1)
265265

266266

267267
def test_fit_frame_errors(opencv_camera: FrameCamera, grid_ji: np.ndarray):

0 commit comments

Comments
 (0)