Skip to content

Commit 6cdd5e9

Browse files
committed
Homography: add reference for least squares homography fit
1 parent cc6f80a commit 6cdd5e9

File tree

1 file changed

+16
-9
lines changed

1 file changed

+16
-9
lines changed

alg/gdal_homography.cpp

+16-9
Original file line numberDiff line numberDiff line change
@@ -210,10 +210,17 @@ int GDALGCPsToHomography(int nGCPCount, const GDAL_GCP *pasGCPList,
210210
return FALSE;
211211
}
212212

213-
GDALMatrix LtL(9, 9);
213+
/* -------------------------------------------------------------------- */
214+
/* Calculate the best fit homography following */
215+
/* https://www.cs.unc.edu/~ronisen/teaching/fall_2023/pdf_slides/ */
216+
/* lecture9_transformation.pdf */
217+
/* Since rank(AtA) = rank(8) = 8, append an additional equation */
218+
/* h_normalized[6] = 1 to fully define the solution. */
219+
/* -------------------------------------------------------------------- */
220+
GDALMatrix AtA(9, 9);
214221
GDALMatrix rhs(9, 1);
215222
rhs(6, 0) = 1;
216-
LtL(6, 6) = 1;
223+
AtA(6, 6) = 1;
217224

218225
for (int i = 0; i < nGCPCount; ++i)
219226
{
@@ -227,31 +234,31 @@ int GDALGCPsToHomography(int nGCPCount, const GDAL_GCP *pasGCPList,
227234
return FALSE;
228235
}
229236

230-
double Lx[] = {1, pixel, line, 0, 0,
237+
double Ax[] = {1, pixel, line, 0, 0,
231238
0, -geox, -geox * pixel, -geox * line};
232-
double Ly[] = {0, 0, 0, 1, pixel, line, -geoy, -geoy * pixel,
239+
double Ay[] = {0, 0, 0, 1, pixel, line, -geoy, -geoy * pixel,
233240
-geoy * line};
234241
int j, k;
235-
// Populate the lower triangle of symmetric LtL matrix
242+
// Populate the lower triangle of symmetric AtA matrix
236243
for (j = 0; j < 9; j++)
237244
{
238245
for (k = j; k < 9; k++)
239246
{
240-
LtL(j, k) += Lx[j] * Lx[k] + Ly[j] * Ly[k];
247+
AtA(j, k) += Ax[j] * Ax[k] + Ay[j] * Ay[k];
241248
}
242249
}
243250
}
244-
// Populate the upper triangle of symmetric LtL matrix
251+
// Populate the upper triangle of symmetric AtA matrix
245252
for (int j = 0; j < 9; j++)
246253
{
247254
for (int k = 0; k < j; k++)
248255
{
249-
LtL(j, k) = LtL(k, j);
256+
AtA(j, k) = AtA(k, j);
250257
}
251258
}
252259

253260
GDALMatrix h_normalized(9, 1);
254-
if (!GDALLinearSystemSolve(LtL, rhs, h_normalized))
261+
if (!GDALLinearSystemSolve(AtA, rhs, h_normalized))
255262
{
256263
return FALSE;
257264
}

0 commit comments

Comments
 (0)