A PyQt6-based desktop application for monocular camera calibration using custom (non-chessboard) patterns. The tool integrates a Genetic Algorithm (GA) optimizer that automatically selects the optimal subset of calibration images and OpenCV calibration flags, minimizing the reprojection error.
- 3 calibration modes in a unified GUI:
- 📷 Real-Time: draw ROI live on the camera feed, capture frames and calibrate
- 🖼️ Pattern Offline + Real-Time Photos: load a pattern image from disk, capture frames with the camera
- 📁 Full Offline: load pattern + a set of images entirely from disk, no camera needed
- Automatic GA optimization after every calibration: no manual step required
- Custom pattern support via OpenCV
ccalib(cv2.ccalib.CustomPattern) - Results saved as
.xmlfiles (OpenCVFileStorageformat) - Per-mode GA log panel embedded in the UI, showing live epoch progress
After the standard cv2.calibrateCamera() call completes, the app automatically
launches a GAWorker thread that runs a Genetic Algorithm on the full set of
collected calibration frames.
Each chromosome encodes:
- A binary mask over the collected frames (which images to include)
- A set of OpenCV calibration flags (
CALIB_FIX_K3,CALIB_RATIONAL_MODEL, etc.)
The fitness function runs cv2.calibrateCamera() on the active subset and
computes the mean reprojection error. The GA evolves the population over multiple
epochs using crossover, mutation, and a cataclysm mechanism to preserve diversity.
The best result is saved to out/ga_best_calibration.xml, containing:
cameraMatrix,newCameraMatrix,distCoeffsERR_REPROJ,N_IMAGES,ACTIVE_INDICES,FLAGS
├── ccalib_ga.py # Main entry point, MainWindow (PyQt6)
├── calibration_worker.py # All 3 calibration mode widgets + workers (QThread)
├── ga_worker.py # GAWorker QThread (wraps GAEngine)
├── ga_engine.py # Genetic Algorithm core logic
├── ga_chromosome.py # Chromosome representation (image subset + flags)
├── ga_constants.py # GA hyperparameters
├── camera_calibrator.py # CameraCalibrator: wraps cv2.calibrateCamera
├── pattern_manager.py # PatternManager: wraps cv2.ccalib.CustomPattern
├── utils.py # ChessboardValidator and utilities
└── res/
└── camera_calib_tool_icon.png
| Library | Purpose |
|---|---|
opencv-python + opencv-contrib-python |
Core computer vision, ccalib module |
PyQt6 |
GUI framework |
numpy |
Numerical operations |
Install standard dependencies:
pip install PyQt6 numpyThe cv2.ccalib module is not included in the standard opencv-python pip
package. To use it, OpenCV must be compiled from source with the
opencv_contrib modules enabled.
Steps used to integrate ccalib on Windows 11 + Python 3.14.3:
- Download OpenCV and opencv_contrib sources
- Use CMake to configure the build, enabling only the required modules:
core,calib3d,ccalib,features2d,imgproc,highgui
- Build with Visual Studio (or MSVC)
- Place the compiled
.pydfile — rename it tocv2.pyd— and all required.dllfiles into the Pythonsite-packagesdirectory:
C:\Users\<user>\AppData\Local\Programs\Python\Python314\Lib\site-packages\
- Add or update
__init__.pyin that folder to expose thecv2module correctly
After this, import cv2 and from cv2 import ccalib work normally in any
Python script or virtual environment pointing to that interpreter.
python ccalib_ga.py- Click ▶ Start Camera
- Draw the ROI rectangle around the calibration pattern in the video feed
- Click ✅ Confirm ROI — the pattern is created
- Click 📸 Capture Frame multiple times (≥ 10 recommended) from different angles
- Click ⚙️ Perform Calibration + GA — standard calibration runs first, then the GA optimizer starts automatically in background
- Click 📂 Load Pattern — select your pattern image
- Click 🗂️ Load Calibration Images — select a batch of photos
- Click ⚙️ Run Calibration + GA — the app processes all images,
runs
calibrateCamera, then launches the GA automatically
Results are saved to the out/ folder.
Tunable in ga_constants.py:
| Parameter | Default | Description |
|---|---|---|
GA_EPOCHS |
25 | Number of evolution generations |
GA_POOL_SIZE |
50 | Population size |
GA_P_MUTATION |
0.25 | Mutation probability |
GA_P_CROSSOVER |
0.75 | Crossover probability |
GA_T_CATACLYSM |
0.01 | Diversity threshold triggering cataclysm |
GA_P_CATACLYSM |
0.50 | Fraction of population replaced on cataclysm |
MIN_GENES |
5 | Minimum active images per chromosome |
OPTIMIZE_FLAGS |
True | Whether to also optimize calibration flags |
PARALLEL |
True | Run fitness evaluations in parallel threads |
-
GA calibration image selection: ga_stereocalib by Alexandru-Ion Marinescu and A. Sergiu - IJCAI 2021 AI4AD Workshop by Alexandru-Ion Marinescu, Adrian-Sergiu Darabant and Tudor-Alexandru Ileni - genetic algorithm for stereo camera calibration image subset selection. This project adapts and extends that approach to monocular calibration with custom patterns.
-
Custom Pattern Calibration: OpenCV ccalib tutorial (YouTube) and the official OpenCV ccalib module (
cv2.ccalib.CustomPattern), part ofopencv_contrib.
This project is intended for research and educational use. (MIT)