aboutsummaryrefslogtreecommitdiff
path: root/src/argaze/utils/camera_calibrate.py
diff options
context:
space:
mode:
Diffstat (limited to 'src/argaze/utils/camera_calibrate.py')
-rw-r--r--src/argaze/utils/camera_calibrate.py45
1 files changed, 26 insertions, 19 deletions
diff --git a/src/argaze/utils/camera_calibrate.py b/src/argaze/utils/camera_calibrate.py
index b590767..c42b721 100644
--- a/src/argaze/utils/camera_calibrate.py
+++ b/src/argaze/utils/camera_calibrate.py
@@ -11,23 +11,23 @@ import argparse
import os
import time
-from argaze.ArUcoMarkers import ArUcoMarkersDictionary, ArUcoBoard, ArUcoDetector, ArUcoCamera
+from argaze.ArUcoMarkers import ArUcoMarkersDictionary, ArUcoBoard, ArUcoDetector, ArUcoOpticCalibrator
import cv2
def main():
"""
- Captures board pictures and finally outputs camera calibration data into a calibration.json file.
+ Captures board pictures and finally outputs optic parameter into a calibration.json file.
- Export and print a calibration board using aruco_calibration_board_export.py script.
- Place the calibration board face to the camera.
- Move the calibration board in a manner to view it entirely on screen in various configurations (orientation and distance):
The script will automatically take pictures. Do this step with a good lighting and a clear background.
- - Once enough pictures have been captured (~20), press Esc key then, wait for the camera calibration processing.
+ - Once enough pictures have been captured (~20), press Esc key then, wait for optic parameters processing.
- Finally, check rms parameter: it should be between 0. and 1. if the calibration succeeded (lower is better).
### Reference:
- - [Camera calibration using ArUco marker tutorial](https://automaticaddison.com/how-to-perform-camera-calibration-using-opencv/)
+ - [Optic calibration using ArUco marker tutorial](https://automaticaddison.com/how-to-perform-camera-calibration-using-opencv/)
"""
# Manage arguments
@@ -48,8 +48,8 @@ def main():
frame_width = int(video_capture.get(cv2.CAP_PROP_FRAME_WIDTH))
frame_height = int(video_capture.get(cv2.CAP_PROP_FRAME_HEIGHT))
- # Create aruco camera
- aruco_camera = ArUcoCamera.ArUcoCamera(dimensions=(frame_width, frame_height))
+ # Create aruco optic calibrator
+ aruco_optic_calibrator = ArUcoOpticCalibrator.ArUcoOpticCalibrator()
# Create aruco board
aruco_board = ArUcoBoard.ArUcoBoard(args.columns, args.rows, args.square_size, args.marker_size, args.dictionary)
@@ -57,7 +57,7 @@ def main():
# Create aruco detector
aruco_detector = ArUcoDetector.ArUcoDetector(dictionary=args.dictionary, marker_size=args.marker_size)
- print(f'{aruco_camera.dimensions[0]}x{aruco_camera.dimensions[1]} pixels camera calibration starts')
+ print(f'{frame_width}x{frame_height} pixels camera calibration starts')
print("Waiting for calibration board...")
expected_markers_number = aruco_board.markers_number
@@ -80,8 +80,8 @@ def main():
aruco_detector.draw_detected_markers(video_frame)
# Draw current calibration data count
- cv2.putText(video_frame, f'Capture: {aruco_camera.calibration_data_count}', (50, 50), cv2.FONT_HERSHEY_SIMPLEX, 1, (255, 255, 255), 2, cv2.LINE_AA)
- cv2.imshow('Camera Calibration', video_frame)
+ cv2.putText(video_frame, f'Capture: {aruco_optic_calibrator.calibration_data_count}', (50, 50), cv2.FONT_HERSHEY_SIMPLEX, 1, (255, 255, 255), 2, cv2.LINE_AA)
+ cv2.imshow('Optic Calibration', video_frame)
# If all board corners are detected
if aruco_detector.board_corners_number == expected_corners_number:
@@ -90,9 +90,9 @@ def main():
aruco_detector.draw_board(video_frame)
# Append calibration data
- aruco_camera.store_calibration_data(aruco_detector.board_corners, aruco_detector.board_corners_identifier)
+ aruco_optic_calibrator.store_calibration_data(aruco_detector.board_corners, aruco_detector.board_corners_identifier)
- cv2.imshow('Camera Calibration', video_frame)
+ cv2.imshow('Optic Calibration', video_frame)
# Stop calibration by pressing 'Esc' key
if cv2.waitKey(1) == 27:
@@ -106,17 +106,24 @@ def main():
cv2.destroyAllWindows()
print('\nCalibrating camera...')
- aruco_camera.calibrate(aruco_board)
+ optic_parameters = aruco_optic_calibrator.calibrate(aruco_board, dimensions=(frame_width, frame_height))
- print('\nCalibration succeeded!')
- print(f'\nRMS:\n{aruco_camera.rms}')
- print(f'\nDimensions:\n{frame_width}x{frame_height}')
- print(f'\nCamera matrix:\n{aruco_camera.K}')
- print(f'\nDistortion coefficients:\n{aruco_camera.D}')
+ if optic_parameters:
- aruco_camera.to_json(f'{args.output}/calibration.json')
+ print('\nCalibration succeeded!')
- print(f'\ncalibration.json file exported into {args.output} folder')
+ print(f'\nRMS:\n{optic_parameters.rms}')
+ print(f'\nDimensions:\n{optic_parameters.dimensions[0]}x{optic_parameters.dimensions[1]}')
+ print(f'\nCamera matrix:\n{optic_parameters.K}')
+ print(f'\nDistortion coefficients:\n{optic_parameters.D}')
+
+ optic_parameters.to_json(f'{args.output}/optic_parameters.json')
+
+ print(f'\ncalibration.json file exported into {args.output} folder')
+
+ else:
+
+ print('\nCalibration error.')
if __name__ == '__main__':