The calibration tool is designed to calculate camera intrinsic parameters and distortion coefficients using a chessboard pattern. This process is essential for correcting lens distortion and enabling accurate object detection.
- A chessboard pattern printed on a rigid, flat surface
- Camera or set of calibration images
- The compiled calibration tool
On Linux:
./calibration/build_calibration.shOn Windows:
# First ensure you have OpenCV and ONNX Runtime installed
# Then generate the Visual Studio solution
generate_vs_solution.bat
# Open the solution in Visual Studio and build the project
# Or build from command line:
cd build
cmake --build . --config ReleaseUsing a Live Camera:
Linux:
./build/calibrate_camera --input 0 --pattern_size 9x6 --square_size 20 --output camera_calibration.yamlWindows:
build\Release\calibrate_camera.exe --input 0 --pattern_size 9x6 --square_size 20 --output camera_calibration.yamlUsing a Directory of Images:
Linux:
./build/calibrate_camera --input /path/to/images --pattern_size 9x6 --square_size 20 --output camera_calibration.yamlWindows:
build\Release\calibrate_camera.exe --input C:/path/to/images --pattern_size 9x6 --square_size 20 --output camera_calibration.yamlParameters Explained:
--input: Camera index (e.g., 0 for default camera) or directory path containing calibration images--pattern_size: Dimensions of the internal corners of the chessboard (Width x Height)--square_size: Physical size of each square in millimeters--output: Path to save the calibration file--min_images: Minimum number of images required for calibration (default: 10)--skip_frames: Number of frames to skip between captures in camera mode (default: 20)
When using a camera:
- Press
SpaceorCto capture the current frame when a chessboard is detected - Press
Ato toggle auto-capture mode - Press
Enterto start calibration once enough images are captured - Press
ESCto exit
For dual-camera setups, you can use the master-slave calibration script (Linux only):
./calibration/run_master_slave_calibration.sh --master=/path/to/master/images --slave=/path/to/slave/images --pattern=4x4 --square=100For Windows, use the individual calibration tool for each camera and save the results to separate files.
Linux:
./src/detection_test_app --help
./src/detection_test_app --video=your_video.mp4 --model=best.onnx --classes=classes.txt --calibration=your_calibration.yamlWindows:
build\Release\detection_test_app.exe --video=your_video.mp4 --model=best.onnx --classes=classes.txt --calibration=your_calibration.yaml- Undistortion Process: The calibration parameters are used to correct lens distortion in the input frames
- Coordinate Transformation: Bounding box coordinates are properly transformed from model space to the undistorted image space
┌───────────────┐ ┌───────────────┐ ┌────────────────┐ ┌──────────────┐
│ Input Image │──────►│ Undistortion │──────►│ Letterboxing │──────►│ ONNX Model │
└───────────────┘ └───────────────┘ └────────────────┘ └──────────────┘
│ │
Uses camera │
calibration │
parameters │
▼
┌────────────────┐ ┌────────────────┐ ┌────────────────┐ ┌──────────────┐
│ Final Output │◄─────│ Draw Detections │◄─────│ Coordinate │◄────│ Raw Model │
└────────────────┘ └────────────────┘ │ Transformation │ │ Predictions │
└────────────────┘ └──────────────┘
| Problem | Possible Causes | Solution |
|---|---|---|
| Chessboard not detected | Poor lighting, blurry image, wrong pattern size | Improve lighting, ensure pattern is in focus, verify pattern dimensions |
| High reprojection error | Inaccurate corner detection, moving chessboard | Use a rigid chessboard, capture from different angles, ensure pattern is stationary |
| Insufficient calibration images | Too few successful detections | Capture more images, position chessboard at different angles and distances |
| Distortion still visible after calibration | Incorrect calibration parameters, extreme lens distortion | Recalibrate with more images, verify calibration file is loaded correctly |
| Windows path issues | Backslashes in paths | Use forward slashes in paths ('/') even on Windows, or use raw strings with double backslashes |
| Problem | Possible Causes | Solution |
|---|---|---|
| Poor detection accuracy with calibration | Incorrect coordinate transformation | Ensure the enhanced detector is being used with letterboxing |
| No detections with calibration enabled | Incompatible calibration file, extreme undistortion | Verify calibration file format, check calibration quality |
| Detection coordinates misaligned | Improper scaling or transformation | Use the enhanced detector with proper letterboxing |
| Slowed performance with calibration | Computationally expensive undistortion | Pre-compute undistortion maps, optimize preprocessing pipeline |
| DLL not found errors (Windows) | Missing path to ONNX Runtime or OpenCV | Add library directories to PATH or copy DLLs to executable directory |
- Use a flat, rigid surface to print or mount your chessboard
- Ensure the pattern has precise dimensions
- Use a matte finish to avoid glare
- Capture the chessboard from various angles and distances
- Cover the entire field of view, especially the edges
- Maintain consistent lighting conditions
- Keep the chessboard stationary during captures
- Collect at least 15-20 images for reliable calibration
- Reprojection Error: Lower is better, aim for < 0.5 pixels
- Visual Verification: Use
test_calibration_image.shto visually inspect undistortion - Grid Test: Straight lines in the undistorted image should remain straight
The enhanced detector implements several key improvements over basic implementations:
Standard resizing can distort aspect ratios, leading to inaccurate detections. Our implementation:
- Maintains the original aspect ratio during resizing
- Adds padding to reach the target dimensions
- Prevents distortion of object shapes
// Letterboxing implementation
double ratio = std::min(target_size / (double)input.cols, target_size / (double)input.rows);
int new_width = static_cast<int>(input.cols * ratio);
int new_height = static_cast<int>(input.rows * ratio);
int pad_left = (target_size - new_width) / 2;
int pad_top = (target_size - new_height) / 2;Proper bounding box coordinates require careful scaling:
- Model outputs coordinates in its input space (e.g., 640x640)
- These must be transformed back to the original image space
- Our implementation accounts for both letterboxing and undistortion
When combined with camera calibration:
- First, lens distortion is corrected using calibration parameters
- The undistorted image is letterboxed to maintain aspect ratio
- Detection is performed on the properly processed image
- Resulting bounding box coordinates are transformed back to original image space
This comprehensive approach significantly improves detection accuracy, especially for:
- Wide-angle camera lenses with significant distortion
- Applications requiring precise object localization
- Cases where objects appear near the edges of the frame
Linux:
./test/test_calibration_image.sh --calibration calibration_results/master_calibration.yaml --input /path/to/image.jpgWindows:
build\Release\test_calibration_image.exe --calibration calibration_results/master_calibration.yaml --input C:/path/to/image.jpgLinux:
./test_detectors.sh --calibration calibration_results/master_calibration.yaml --video /path/to/video.aviWindows:
build\Release\detection_test_app.exe --video=C:/path/to/video.avi --model=best.onnx --classes=classes.txt --calibration=calibration_results/master_calibration.yamlThese tests help verify that your calibration is working correctly and improving detection accuracy.
When working on Windows, consider these path handling tips:
- Always use forward slashes in paths, even on Windows:
C:/path/to/fileinstead ofC:\path\to\file - If you must use backslashes, double them in strings:
C:\\path\\to\\file - For command-line arguments, enclose paths with spaces in quotes:
"C:/Program Files/My App/data" - The toolkit has been designed to handle all these cases properly
On Windows, you may need to set environment variables to help the system find required libraries:
set PATH=%PATH%;C:\opencv\build\x64\vc16\bin;C:\path\to\onnxruntime-win-x64-1.21.0\libAlternatively, you can copy required DLLs to the same directory as your executables.