Skip to content

Update version #14

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Mar 16, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
# will have compiled files and executables
debug/
target/
venv

# Remove Cargo.lock from gitignore if creating an executable, leave it for libraries
# More information here https://doc.rust-lang.org/cargo/guide/cargo-toml-vs-cargo-lock.html
Expand Down
12 changes: 6 additions & 6 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "camera-intrinsic-calibration"
version = "0.5.6"
version = "0.6.0"
edition = "2021"
authors = ["Powei Lin <poweilin1994@gmail.com>"]
readme = "README.md"
Expand All @@ -21,13 +21,13 @@ exclude = [
]

[dependencies]
aprilgrid = "0.5.3"
aprilgrid = "0.6.0"
camera-intrinsic-model = "0.3.1"
clap = { version = "4.5.31", features = ["derive"] }
colorous = "1.0.15"
env_logger = "0.11.6"
faer = "0.20.2"
glam = "0.29.2"
faer = "0.21.5"
glam = "0.30.0"
glob = "0.3.2"
image = "0.25.5"
indicatif = { version = "0.17.11", features = ["rayon"] }
Expand All @@ -39,9 +39,9 @@ rayon = "1.10.0"
rerun = "0.17.0"
serde = { version = "1.0.218", features = ["derive"] }
serde_json = "1.0.139"
sqpnp_simple = "0.1.5"
sqpnp_simple = "0.1.6"
time = "0.3.37"
tiny-solver = "0.14.2"
tiny-solver = "0.17.0"

[[bin]]
name = "ccrs"
Expand Down
10 changes: 9 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,12 +1,16 @@
# camera-intrinsic-calibration
[![crate](https://img.shields.io/crates/v/camera-intrinsic-calibration.svg)](https://crates.io/crates/camera-intrinsic-calibration)
[![PyPI - Version](https://img.shields.io/pypi/v/camera-intrinsic-calibration.svg)](https://pypi.org/project/camera-intrinsic-calibration)
[![PyPI - Python Version](https://img.shields.io/pypi/pyversions/camera-intrinsic-calibration.svg)](https://pypi.org/project/camera-intrinsic-calibration)

A pure rust camera intrinsic calibration library.

## Installation
```sh
# install cli
# cargo install cli
cargo install camera-intrinsic-calibration
# pip install cli
pip install camera-intrinsic-calibration
```
Or download from the latest [release](https://github.com/powei-lin/camera-intrinsic-calibration-rs/releases).

Expand All @@ -26,7 +30,11 @@ ccrs dataset-calib-cam1_1024_16 --model eucm
```
### Visualize details after calibration
```sh
# use cargo to install rerun
cargo install rerun-cli --version 0.17.0
# or use pip to install rerun
pip install rerun-sdk==0.17.0
# visualize result
rerun results/20YYMMDD_HH_MM_SS/logging.rrd
```
<img src="data/rerun_logs.jpg" width="800" alt="example detection">
Expand Down
26 changes: 26 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
[build-system]
requires = ["maturin>=1.4,<2.0"]
build-backend = "maturin"

[project]
name = "camera-intrinsic-calibration"
requires-python = ">=3.8"
classifiers = [
"Programming Language :: Rust",
"Programming Language :: Python :: Implementation :: CPython",
"Programming Language :: Python :: Implementation :: PyPy",
"Programming Language :: Python :: 3.8",
"Programming Language :: Python :: 3.9",
"Programming Language :: Python :: 3.10",
"Programming Language :: Python :: 3.11",
"Programming Language :: Python :: 3.12",
]
authors = [
{ name = "Powei Lin", email = "poweilin1994@gmail.com" },
]
dynamic = ["version"]

[tool.maturin]
bindings = "bin"
module-name = "ccrs"
strip = true
3 changes: 2 additions & 1 deletion scripts/build_macos.sh
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,5 @@ cargo build -r
cp ./target/release/ccrs ./ccrs
tar czvf ccrs-aarch64-apple-darwin.tar.gz ./ccrs
shasum -a 256 ccrs-aarch64-apple-darwin.tar.gz > ccrs-aarch64-apple-darwin.tar.gz.sha256
rm ./ccrs
rm ./ccrs
maturin build -r
3 changes: 2 additions & 1 deletion scripts/cross_build_linux_aarch64.sh
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,5 @@ cargo zigbuild --release --target=aarch64-unknown-linux-gnu
cp ./target/aarch64-unknown-linux-gnu/release/ccrs ./ccrs
tar czvf ccrs-aarch64-unknown-linux-gnu.tar.gz ./ccrs
shasum -a 256 ccrs-aarch64-unknown-linux-gnu.tar.gz > ccrs-aarch64-unknown-linux-gnu.tar.gz.sha256
rm ./ccrs
rm ./ccrs
maturin build -r --target aarch64-unknown-linux-gnu --zig
3 changes: 2 additions & 1 deletion scripts/cross_build_linux_x86_64.sh
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,5 @@ cargo zigbuild --release --target=x86_64-unknown-linux-gnu
cp ./target/x86_64-unknown-linux-gnu/release/ccrs ./ccrs
tar czvf ccrs-x86_64-unknown-linux-gnu.tar.gz ./ccrs
shasum -a 256 ccrs-x86_64-unknown-linux-gnu.tar.gz > ccrs-x86_64-unknown-linux-gnu.tar.gz.sha256
rm ./ccrs
rm ./ccrs
maturin build -r --target x86_64-unknown-linux-gnu --zig
3 changes: 2 additions & 1 deletion scripts/cross_build_windows_x86_64.sh
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,5 @@ cargo zigbuild --release --target=x86_64-pc-windows-gnu
cp ./target/x86_64-pc-windows-gnu/release/ccrs.exe ./ccrs.exe
zip ccrs-x86_64-pc-windows-gnu.zip ./ccrs.exe
shasum -a 256 ccrs-x86_64-pc-windows-gnu.zip > ccrs-x86_64-pc-windows-gnu.zip.sha256
rm ./ccrs.exe
rm ./ccrs.exe
maturin build -r --target x86_64-pc-windows-gnu --zig
1 change: 1 addition & 0 deletions scripts/setup_cross_compile.sh
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
rustup target add aarch64-unknown-linux-gnu
rustup target add x86_64-unknown-linux-gnu
rustup target add x86_64-pc-windows-gnu
cargo install cargo-zigbuild
brew install zig
4 changes: 2 additions & 2 deletions src/data_loader.rs
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ pub fn load_euroc(
let mut sorted_path: Vec<std::path::PathBuf> =
img_paths.into_iter().filter_map(img_filter).collect();

sorted_path.sort_by(|a, b| a.cmp(b));
sorted_path.sort();
let new_paths: Vec<_> = sorted_path.iter().skip(start_idx).step_by(step).collect();
let mut time_frame: Vec<_> = new_paths
.iter()
Expand Down Expand Up @@ -137,7 +137,7 @@ pub fn load_others(
let mut sorted_path: Vec<std::path::PathBuf> =
img_paths.into_iter().filter_map(img_filter).collect();

sorted_path.sort_by(|a, b| a.cmp(b));
sorted_path.sort();
let new_paths: Vec<_> = sorted_path
.iter()
.skip(start_idx)
Expand Down
58 changes: 26 additions & 32 deletions src/optimization/homography.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use faer::prelude::SpSolverLstsq;
use faer::linalg::solvers::SolveLstsqCore;
use log::debug;
use nalgebra as na;
use rand::seq::SliceRandom;
Expand All @@ -13,18 +13,18 @@ fn h6_l1l2_solver(six_pt_pairs: &[(glam::Vec2, glam::Vec2)]) -> Option<(f32, na:
let x_p = pt1.x;
let y_p = pt1.y;
unsafe {
m1.write_unchecked(r, 0, -x * y_p);
m1.write_unchecked(r, 1, -y * y_p);
m1.write_unchecked(r, 2, -y_p);
m1.write_unchecked(r, 3, x * x_p);
m1.write_unchecked(r, 4, x_p * y);
m1.write_unchecked(r, 5, x_p);
m1.write_unchecked(r, 6, -x * x * y_p - y * y * y_p);
m1.write_unchecked(r, 7, x * x * x_p + x_p * y * y);
*m1.get_mut_unchecked(r, 0) = -x * y_p;
*m1.get_mut_unchecked(r, 1) = -y * y_p;
*m1.get_mut_unchecked(r, 2) = -y_p;
*m1.get_mut_unchecked(r, 3) = x * x_p;
*m1.get_mut_unchecked(r, 4) = x_p * y;
*m1.get_mut_unchecked(r, 5) = x_p;
*m1.get_mut_unchecked(r, 6) = -x * x * y_p - y * y * y_p;
*m1.get_mut_unchecked(r, 7) = x * x * x_p + x_p * y * y;
}
}
// let q_mat = mm1.transpose().qr().q();
let q_mat = m1.transpose().qr().compute_q();
let q_mat = m1.transpose().qr().compute_Q();
let q_mat_t = q_mat.transpose();
let n = q_mat_t.subrows(6, 2);
let n02 = *n.get(0, 2);
Expand Down Expand Up @@ -62,7 +62,7 @@ fn h6_l1l2_solver(six_pt_pairs: &[(glam::Vec2, glam::Vec2)]) -> Option<(f32, na:
for which_gamma in 0..2 {
let gamma = g_result[which_gamma];
let l = -(gamma * n06 + n16) / (-gamma * n02 - n12);
let v1 = gamma * n.row(0) + n.row(1);
let v1 = faer::Scale(gamma) * n.row(0) + n.row(1);
temp_h[which_gamma][(0, 0)] = *v1.get(0);
temp_h[which_gamma][(0, 1)] = *v1.get(1);
temp_h[which_gamma][(0, 2)] = *v1.get(2);
Expand All @@ -79,12 +79,10 @@ fn h6_l1l2_solver(six_pt_pairs: &[(glam::Vec2, glam::Vec2)]) -> Option<(f32, na:
let x_p = pt1.x;
let y_p = pt1.y;
unsafe {
eq10a.write_unchecked(row, 0, -x * x_p);
eq10a.write_unchecked(row, 1, -x_p * y);
eq10a.write_unchecked(row, 2, -l * x * x * x_p - l * x_p * y * y - x_p);
eq10a.write_unchecked(
row,
3,
*eq10a.get_mut_unchecked(row, 0) = -x * x_p;
*eq10a.get_mut_unchecked(row, 1) = -x_p * y;
*eq10a.get_mut_unchecked(row, 2) = -l * x * x * x_p - l * x_p * y * y - x_p;
*eq10a.get_mut_unchecked(row, 3) =
l * x * x * x_p * x_p * temp_h[which_gamma][(0, 2)]
+ l * x * x * y_p * y_p * temp_h[which_gamma][(0, 2)]
+ l * x_p * x_p * y * y * temp_h[which_gamma][(0, 2)]
Expand All @@ -94,25 +92,21 @@ fn h6_l1l2_solver(six_pt_pairs: &[(glam::Vec2, glam::Vec2)]) -> Option<(f32, na:
+ x_p * x_p * y * temp_h[which_gamma][(0, 1)]
+ x_p * x_p * temp_h[which_gamma][(0, 2)]
+ y * y_p * y_p * temp_h[which_gamma][(0, 1)]
+ y_p * y_p * temp_h[which_gamma][(0, 2)],
);
+ y_p * y_p * temp_h[which_gamma][(0, 2)];

eq10b.write_unchecked(
row,
0,
-l * x * x * temp_h[which_gamma][(0, 2)]
- l * y * y * temp_h[which_gamma][(0, 2)]
- x * temp_h[which_gamma][(0, 0)]
- y * temp_h[which_gamma][(0, 1)]
- temp_h[which_gamma][(0, 2)],
);
*eq10b.get_mut_unchecked(row, 0) = -l * x * x * temp_h[which_gamma][(0, 2)]
- l * y * y * temp_h[which_gamma][(0, 2)]
- x * temp_h[which_gamma][(0, 0)]
- y * temp_h[which_gamma][(0, 1)]
- temp_h[which_gamma][(0, 2)];
}
}
// std::cout << "svd\n";
let eq10x = eq10a.qr().solve_lstsq(eq10b);
// Eigen::JacobiSVD<Eigen::Matrix<float, 6, 4>> svd(
// eq10A, Eigen::ComputeFullU | Eigen::ComputeFullV);
// Eigen::Matrix<float, 4, 1> eq10x = svd.solve(eq10b);
let mut eq10x = eq10b;
eq10a
.qr()
.solve_lstsq_in_place_with_conj(faer::Conj::No, eq10x.as_mut());

temp_h[which_gamma][(2, 0)] = *eq10x.get(0, 0);
temp_h[which_gamma][(2, 1)] = *eq10x.get(1, 0);
temp_h[which_gamma][(2, 2)] = *eq10x.get(2, 0);
Expand Down
12 changes: 6 additions & 6 deletions src/util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -241,7 +241,7 @@ pub fn convert_model(
HashMap::<String, na::DVector<f64>>::from([("params".to_string(), target_params_init)]);

// initialize optimizer
let optimizer = tiny_solver::LevenbergMarquardtOptimizer::default();
let optimizer = tiny_solver::GaussNewtonOptimizer::default();

// distortion parameter bound
set_problem_parameter_bound("params", &mut problem, target_model, false);
Expand Down Expand Up @@ -311,7 +311,7 @@ pub fn init_ucm(
]);

// initialize optimizer
let optimizer = tiny_solver::LevenbergMarquardtOptimizer::default();
let optimizer = tiny_solver::GaussNewtonOptimizer::default();
if fixed_focal {
init_focal_alpha_problem.fix_variable("params", 0);
}
Expand Down Expand Up @@ -346,7 +346,7 @@ pub fn init_ucm(
0,
fixed_focal,
)
.unwrap()
.expect("The initial UCM model fitting failed. Might be wrong board configuration.")
.0,
)
} else {
Expand Down Expand Up @@ -406,7 +406,7 @@ pub fn calib_camera(
}
}

let optimizer = tiny_solver::LevenbergMarquardtOptimizer::default();
let optimizer = tiny_solver::GaussNewtonOptimizer::default();
// let initial_values = optimizer.optimize(&problem, &initial_values, None);

set_problem_parameter_bound("params", &mut problem, generic_camera, xy_same_focal);
Expand Down Expand Up @@ -512,7 +512,7 @@ pub fn init_camera_extrinsic(cam_rtvecs: &[HashMap<usize, RvecTvec>]) -> Vec<Rve
("tvec".to_string(), tvec),
]);

let optimizer = tiny_solver::LevenbergMarquardtOptimizer::default();
let optimizer = tiny_solver::GaussNewtonOptimizer::default();
let result = optimizer.optimize(&problem, &initial_values, None).unwrap();
println!("extrinsic cam{} cam0", cam_i);
println!("rvec: {}", result["rvec"]);
Expand Down Expand Up @@ -623,7 +623,7 @@ pub fn calib_all_camera_with_extrinsics(
println!("set focal");
problem.fix_variable("params0", 0);
}
let optimizer = tiny_solver::LevenbergMarquardtOptimizer::default();
let optimizer = tiny_solver::GaussNewtonOptimizer::default();

let result_option = optimizer.optimize(&problem, &initial_values, None);
if let Some(mut result) = result_option {
Expand Down