Created on Sunday, April 21, 2024
Author: Tung Nguyen - Handsome
Reference: Camera Calibration by OpenCV (OpenCV Tutorial)
This Python script provides a class, CameraCalibration
, to perform camera calibration based on a set of image points and their corresponding object points / undistort image or frame from camera
This script includes functionalities to:
- Calculate camera calibration data based on a set of images.
- Save and load the camera calibration data.
- Undistort images or points using the calibration data.
Using this command to install the packages (or clone this repository):
pip install TH-camera-calibration
- Python 3.x
- OpenCV
- Numpy
- PyYAML (For working with YAML files) To set up the environment, install all the required libraries by using this command:
pip install -r requirements.txt
-
Import the
CameraCalibration
class fromcamera_calibration.py
.from camera_calibration import CameraCalibration
-
Create an instance of
CameraCalibration
.camera_calibrator = CameraCalibration()
-
Calculate calibration data using a set of images with known chessboard pattern.
camera_calibrator.calculate_calibration_data( run=True, chessboardSize=(9, 6), size_of_chessboard_squares_mm=25, framesize=(1280, 720), calibrationDir=None, #path of calibration dir savepath="", saveformat="pkl", show_process_img=True, show_calibration_data=True, )
-
Read calibration data. (run when have calib data already and do not want to calculate calib data from scratch)
calibrator.read_calibration_data(r"calibration.pkl", "pkl", True)
-
Undistort an image. (If not calculate calib data from scratch, require to read calib data first)
undistorted_image = camera_calibrator.undistortion_img( img, method="default", img_size=(1280, 720), verbose=False )
or
undistorted_image = camera_calibrator.undistortion_img( img, method="Remapping", img_size=(1280, 720), verbose=False )
- My Gmail: quangtung.work73@gmail.com
- My Github: Tung Nguyen - Handsome - Github
- My Youtube channel: Tung Nguyen - Handsome - Youtube
- My Linkedin: Tung Nguyen - Handsome - Linkedin