Skip to content

Commit

Permalink
add calibration routine to connector + test + assets.
Browse files Browse the repository at this point in the history
  • Loading branch information
mgineer85 committed Dec 2, 2024
1 parent d3d262e commit 36c193c
Show file tree
Hide file tree
Showing 29 changed files with 411 additions and 0 deletions.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
97 changes: 97 additions & 0 deletions tests/tests/test_connector_calibrator.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
import glob
import logging

import cv2
import numpy as np

from wigglecam.connector.calibrator import CalibrationDataExtrinsics, CalibrationDataIntrinsics, Calibrator, ExtrinsicPair, Intrinsic

logger = logging.getLogger(name=None)


def test_calibrate_intrinsic(tmp_path):
# calibrator = Calibrator()
intrinsic = Intrinsic("cam0")

images = list(sorted(glob.glob("tests/assets/tutorial_stereo_images_chessboard7x6/left*.png"))) # read a series of images
assert images

(cal_data, objpoints, imgpoints) = intrinsic.calibrate(images)

logger.info(cal_data.err)

cal_data.save(tmp_path / "data.pickle")
assert (tmp_path / "data.pickle").is_file()


def test_calibrate_undistort(tmp_path):
test_index = 5
intrinsic = Intrinsic("cam0")

images = list(sorted(glob.glob("tests/assets/tutorial_stereo_images_chessboard7x6/left*.png"))) # read a series of images
assert images[test_index]

(cal_data, objpoints, imgpoints) = intrinsic.calibrate(images)

logger.info(cal_data.err)

frame = cv2.imread(images[test_index])
undistorted_frame = intrinsic.undistort(frame)

cv2.imwrite(tmp_path / "test_image_original.jpg", frame)
cv2.imwrite(tmp_path / "test_image_undistorted.jpg", undistorted_frame)

# show image if desired
# Image.fromarray(cv2.cvtColor(undistorted_frame, cv2.COLOR_BGR2RGB)).show()


def test_calibrate_stereo(tmp_path):
calibrator = Calibrator()
intrinsic_l = Intrinsic("cam_l")
intrinsic_r = Intrinsic("cam_r")
extrinsic_pair = ExtrinsicPair("cam_l+cam_r")

images_l = list(sorted(glob.glob("tests/assets/tutorial_stereo_images_chessboard7x6/left*.png"))) # read a series of images
images_r = list(sorted(glob.glob("tests/assets/tutorial_stereo_images_chessboard7x6/right*.png"))) # read a series of images
assert images_l, images_r

(cal_data_l, objpoints_l, imgpoints_l) = intrinsic_l.calibrate(images_l)
(cal_data_r, objpoints_r, imgpoints_r) = intrinsic_r.calibrate(images_r)

(cal_data_stereo, objpoints, imgpoints_l, imgpoints_r) = extrinsic_pair.calibrate(images_l, cal_data_l, images_r, cal_data_r)

logger.info(cal_data_stereo)

# homography works on 2d only - cannot use for universal detection of corresponding points, only valid for the scene tested.
# ref: https://stackoverflow.com/a/46802181
# src_pts = np.vstack(imgpoints_l)
# dst_pts = np.vstack(imgpoints_r)
# H, _ = cv2.findHomography(src_pts, dst_pts, cv2.RANSAC, 5.0) # needs at least 4 points.


def test_save_load_intrinsicdata(tmp_path):
test_data = CalibrationDataIntrinsics(np.zeros((np.prod((2, 2))), np.float32), 2, 3, 4, 5, 6, 7, "date")
test_data.save(tmp_path / "test.pickle")

loaded_data = CalibrationDataIntrinsics.from_file(tmp_path / "test.pickle")

print(test_data)
print(loaded_data)

assert test_data is not loaded_data
np.testing.assert_equal(test_data.mtx, loaded_data.mtx)
assert test_data.calibration_datetime == loaded_data.calibration_datetime


def test_save_load_extrinsicdata(tmp_path):
test_data = CalibrationDataExtrinsics(1.0, np.zeros((np.prod((2, 2))), np.float32), 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, "date")
test_data.save(tmp_path / "test.pickle")

loaded_data = CalibrationDataExtrinsics.from_file(tmp_path / "test.pickle")

print(test_data)
print(loaded_data)

assert test_data is not loaded_data
np.testing.assert_equal(test_data.Kl, loaded_data.Kl)
assert test_data.calibration_datetime == loaded_data.calibration_datetime
Loading

0 comments on commit 36c193c

Please sign in to comment.