Ansel 0.0
A darktable fork - bloat + design vision
Loading...
Searching...
No Matches
homography.c
Go to the documentation of this file.
1/*
2 * This file is part of darktable,
3 * Copyright (C) 2016 johannes hanika.
4 * Copyright (C) 2016, 2020 Tobias Ellinghaus.
5 * Copyright (C) 2020 Pascal Obry.
6 * Copyright (C) 2021 Sakari Kapanen.
7 * Copyright (C) 2022 Martin Baƙinka.
8 *
9 * darktable is free software: you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation, either version 3 of the License, or
12 * (at your option) any later version.
13 *
14 * darktable is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
18 *
19 * You should have received a copy of the GNU General Public License
20 * along with darktable. If not, see <http://www.gnu.org/licenses/>.
21 */
22
23
24#include "math/homography.h"
26
27// using SVD to solve the system with h[8] also being 0 would be better, but this seems to be good enough
28int get_homography(const point_t *source, const point_t *target, float *h)
29{
30 // Use double precision internally when solving for the homography
31 // to avoid numerical instabilities.
32 const double x1 = source[0].x;
33 const double y1 = source[0].y;
34 const double x2 = source[1].x;
35 const double y2 = source[1].y;
36 const double x3 = source[2].x;
37 const double y3 = source[2].y;
38 const double x4 = source[3].x;
39 const double y4 = source[3].y;
40
41 const double x_1 = target[0].x;
42 const double y_1 = target[0].y;
43 const double x_2 = target[1].x;
44 const double y_2 = target[1].y;
45 const double x_3 = target[2].x;
46 const double y_3 = target[2].y;
47 const double x_4 = target[3].x;
48 const double y_4 = target[3].y;
49
50 double P[9*9] = { -x1, -y1, -1.0, 0.0, 0.0, 0.0, x1 * x_1, y1 * x_1, x_1,
51 0.0, 0.0, 0.0, -x1, -y1, -1.0, x1 * y_1, y1 * y_1, y_1,
52 -x2, -y2, -1.0, 0.0, 0.0, 0.0, x2 * x_2, y2 * x_2, x_2,
53 0.0, 0.0, 0.0, -x2, -y2, -1.0, x2 * y_2, y2 * y_2, y_2,
54 -x3, -y3, -1.0, 0.0, 0.0, 0.0, x3 * x_3, y3 * x_3, x_3,
55 0.0, 0.0, 0.0, -x3, -y3, -1.0, x3 * y_3, y3 * y_3, y_3,
56 -x4, -y4, -1.0, 0.0, 0.0, 0.0, x4 * x_4, y4 * x_4, x_4,
57 0.0, 0.0, 0.0, -x4, -y4, -1.0, x4 * y_4, y4 * y_4, y_4,
58 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 1.0};
59
60 double h_tmp[9];
61 for(int i = 0; i < 8; i++) h_tmp[i] = 0.0;
62 h_tmp[8] = 1.0;
63
64 int err_code = gauss_solve(P, h_tmp, 9);
65 if(err_code) for(int i = 0; i < 9; i++) h[i] = h_tmp[i];
66 return err_code;
67}
68
70{
71 const float s = p.x * h[2 * 3 + 0] + p.y * h[2 * 3 + 1] + h[2 * 3 + 2];
72 const float x = (p.x * h[0 * 3 + 0] + p.y * h[0 * 3 + 1] + h[0 * 3 + 2]) / s;
73 const float y = (p.x * h[1 * 3 + 0] + p.y * h[1 * 3 + 1] + h[1 * 3 + 2]) / s;
74
75 const point_t result = {.x=x, .y=y};
76
77 return result;
78}
79
80float apply_homography_scaling(point_t p, const float *h)
81{
82 // The local scaling of areas by the homography mapping is given by
83 // the absolute value of its Jacobian determinant at point p.
84 const float x = p.x * h[0 * 3 + 0] + p.y * h[0 * 3 + 1] + h[0 * 3 + 2];
85 const float y = p.x * h[1 * 3 + 0] + p.y * h[1 * 3 + 1] + h[1 * 3 + 2];
86 const float s = p.x * h[2 * 3 + 0] + p.y * h[2 * 3 + 1] + h[2 * 3 + 2];
87
88 // Components of the Jacobian matrix, without division by s^2, which is factored
89 // out and done for the whole determinant.
90 const float J00 = h[0 * 3 + 0] * s - h[2 * 3 + 0] * x;
91 const float J01 = h[0 * 3 + 1] * s - h[2 * 3 + 1] * x;
92 const float J10 = h[1 * 3 + 0] * s - h[2 * 3 + 0] * y;
93 const float J11 = h[1 * 3 + 1] * s - h[2 * 3 + 1] * y;
94 const float s2 = s * s;
95 return fabsf(J00 * J11 - J01 * J10) / (s2 * s2);
96}
97
98// modified;
99// clang-format off
100// modelines: These editor modelines have been set for all relevant files by tools/update_modelines.py
101// vim: shiftwidth=2 expandtab tabstop=2 cindent
102// kate: tab-indents: off; indent-width 2; replace-tabs on; indent-mode cstyle; remove-trailing-spaces modified;
103// clang-format on
104
static const float x
#define P(V, params)
static int gauss_solve(double *A, double *b, int n)
point_t apply_homography(point_t p, const float *h)
Definition homography.c:69
float apply_homography_scaling(point_t p, const float *h)
Definition homography.c:80
int get_homography(const point_t *source, const point_t *target, float *h)
Definition homography.c:28
size_t y
Definition censorize.c:78
size_t x
Definition censorize.c:78