Skip to content
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

Implement kornia3d::pose for homography and affine #194

Merged
merged 5 commits into from
Dec 25, 2024
Merged
Changes from 1 commit
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
Prev Previous commit
Next Next commit
small fixes
edgarriba committed Dec 25, 2024

Verified

This commit was created on GitHub.com and signed with GitHub’s verified signature. The key has expired.
commit ab5bb282379bef1733582c1e04ccb0f5539b1405
21 changes: 11 additions & 10 deletions crates/kornia-3d/src/pose/affine.rs
Original file line number Diff line number Diff line change
@@ -9,22 +9,23 @@ use faer::solvers::SpSolverLstsq;
/// # Returns
///
/// The output 2D affine transformation matrix with shape (2, 3).
pub fn affine_4pt2d(x1: &[[f64; 2]; 4], x2: &[[f64; 2]; 4], affine: &mut [[f64; 3]; 3]) {
pub fn affine_4pt2d(x1: &[[f64; 2]; 4], x2: &[[f64; 2]; 4], affine: &mut [[f64; 3]; 2]) {
// construct matrix A
let mut mat_a = faer::Mat::<f64>::zeros(8, 6);
let mut mat_b = faer::Mat::<f64>::zeros(8, 1);

for i in 0..4 {
let (x1_i, x2_i) = (x1[i], x2[i]);
let (x1_0, x1_1) = (x1[i][0], x1[i][1]);
let (x2_0, x2_1) = (x2[i][0], x2[i][1]);
unsafe {
mat_a.write_unchecked(2 * i, 0, x1_i[0]);
mat_a.write_unchecked(2 * i, 1, x1_i[1]);
mat_a.write_unchecked(2 * i, 0, x1_0);
mat_a.write_unchecked(2 * i, 1, x1_1);
mat_a.write_unchecked(2 * i, 2, 1.0);
mat_a.write_unchecked(2 * i + 1, 3, x1_i[0]);
mat_a.write_unchecked(2 * i + 1, 4, x1_i[1]);
mat_a.write_unchecked(2 * i + 1, 3, x1_0);
mat_a.write_unchecked(2 * i + 1, 4, x1_1);
mat_a.write_unchecked(2 * i + 1, 5, 1.0);
mat_b.write_unchecked(2 * i, 0, x2_i[0]);
mat_b.write_unchecked(2 * i + 1, 0, x2_i[1]);
mat_b.write_unchecked(2 * i, 0, x2_0);
mat_b.write_unchecked(2 * i + 1, 0, x2_1);
}
}

@@ -46,7 +47,7 @@ mod tests {
let x1 = [[0.0, 0.0], [1.0, 0.0], [0.0, 1.0], [1.0, 1.0]];
let x2 = [[0.0, 0.0], [1.0, 0.0], [0.0, 1.0], [1.0, 1.0]];
let expected = [[1.0, 0.0, 0.0], [0.0, 1.0, 0.0]];
let mut affine = [[0.0; 3]; 3];
let mut affine = [[0.0; 3]; 2];
affine_4pt2d(&x1, &x2, &mut affine);

for i in 0..2 {
@@ -61,7 +62,7 @@ mod tests {
let x1 = [[0.0, 0.0], [1.0, 0.0], [0.0, 1.0], [1.0, 1.0]];
let x2 = [[1.0, 1.0], [2.0, 1.0], [1.0, 2.0], [2.0, 2.0]];
let expected = [[1.0, 0.0, 1.0], [0.0, 1.0, 1.0]];
let mut affine = [[0.0; 3]; 3];
let mut affine = [[0.0; 3]; 2];
affine_4pt2d(&x1, &x2, &mut affine);

for i in 0..2 {
29 changes: 15 additions & 14 deletions crates/kornia-3d/src/pose/homography.rs
Original file line number Diff line number Diff line change
@@ -99,21 +99,22 @@ pub fn homography_4pt3d(

let mut m_mat = faer::Mat::<f64>::zeros(8, 9);
for i in 0..4 {
let (x1_i, x2_i) = (x1[i], x2[i]);
let (x1_0, x1_1, x1_2) = (x1[i][0], x1[i][1], x1[i][2]);
let (x2_0, x2_1, x2_2) = (x2[i][0], x2[i][1], x2[i][2]);
unsafe {
m_mat.write_unchecked(2 * i, 0, x2_i[2] * x1_i[0]);
m_mat.write_unchecked(2 * i, 1, x2_i[2] * x1_i[1]);
m_mat.write_unchecked(2 * i, 2, x2_i[2] * x1_i[2]);
m_mat.write_unchecked(2 * i, 6, -x2_i[0] * x1_i[0]);
m_mat.write_unchecked(2 * i, 7, -x2_i[0] * x1_i[1]);
m_mat.write_unchecked(2 * i, 8, -x2_i[0] * x1_i[2]);

m_mat.write_unchecked(2 * i + 1, 3, x2_i[2] * x1_i[0]);
m_mat.write_unchecked(2 * i + 1, 4, x2_i[2] * x1_i[1]);
m_mat.write_unchecked(2 * i + 1, 5, x2_i[2] * x1_i[2]);
m_mat.write_unchecked(2 * i + 1, 6, -x2_i[1] * x1_i[0]);
m_mat.write_unchecked(2 * i + 1, 7, -x2_i[1] * x1_i[1]);
m_mat.write_unchecked(2 * i + 1, 8, -x2_i[1] * x1_i[2]);
m_mat.write_unchecked(2 * i, 0, x2_2 * x1_0);
m_mat.write_unchecked(2 * i, 1, x2_2 * x1_1);
m_mat.write_unchecked(2 * i, 2, x2_2 * x1_2);
m_mat.write_unchecked(2 * i, 6, -x2_0 * x1_0);
m_mat.write_unchecked(2 * i, 7, -x2_0 * x1_1);
m_mat.write_unchecked(2 * i, 8, -x2_0 * x1_2);

m_mat.write_unchecked(2 * i + 1, 3, x2_2 * x1_0);
m_mat.write_unchecked(2 * i + 1, 4, x2_2 * x1_1);
m_mat.write_unchecked(2 * i + 1, 5, x2_2 * x1_2);
m_mat.write_unchecked(2 * i + 1, 6, -x2_1 * x1_0);
m_mat.write_unchecked(2 * i + 1, 7, -x2_1 * x1_1);
m_mat.write_unchecked(2 * i + 1, 8, -x2_1 * x1_2);
}
}