georef_tps.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488
  1. /****************************************************************************
  2. *
  3. * MODULE: imagery library
  4. * AUTHOR(S): Markus Metz
  5. *
  6. * PURPOSE: Image processing library
  7. * COPYRIGHT: (C) 2013 by the GRASS Development Team
  8. *
  9. * This program is free software under the GNU General Public
  10. * License (>=v2). Read the file COPYING that comes with GRASS
  11. * for details.
  12. *
  13. *****************************************************************************/
  14. #include <stdlib.h>
  15. #include <math.h>
  16. #include <grass/gis.h>
  17. #include <grass/imagery.h>
  18. #include <grass/glocale.h>
  19. #include <signal.h>
  20. /* STRUCTURE FOR USE INTERNALLY WITH THESE FUNCTIONS. THESE FUNCTIONS EXPECT
  21. SQUARE MATRICES SO ONLY ONE VARIABLE IS GIVEN (N) FOR THE MATRIX SIZE */
  22. struct MATRIX
  23. {
  24. int n; /* SIZE OF THIS MATRIX (N x N) */
  25. double *v;
  26. };
  27. /* CALCULATE OFFSET INTO ARRAY BASED ON R/C */
  28. #define M(row,col) m->v[(((row)-1)*(m->n))+(col)-1]
  29. #define MSUCCESS 1 /* SUCCESS */
  30. #define MNPTERR 0 /* NOT ENOUGH POINTS */
  31. #define MUNSOLVABLE -1 /* NOT SOLVABLE */
  32. #define MMEMERR -2 /* NOT ENOUGH MEMORY */
  33. #define MPARMERR -3 /* PARAMETER ERROR */
  34. #define MINTERR -4 /* INTERNAL ERROR */
  35. #define MAXORDER 3 /* HIGHEST SUPPORTED ORDER OF TRANSFORMATION */
  36. /***********************************************************************
  37. FUNCTION PROTOTYPES FOR STATIC (INTERNAL) FUNCTIONS
  38. ************************************************************************/
  39. static int calccoef(struct Control_Points *, double **, double **);
  40. static int calcls(struct Control_Points *, struct MATRIX *, double *,
  41. double *, double *, double *);
  42. static double tps_base_func(const double x1, const double y1,
  43. const double x2, const double y2);
  44. static int solvemat(struct MATRIX *, double *, double *, double *, double *);
  45. /***********************************************************************
  46. TRANSFORM A SINGLE COORDINATE PAIR.
  47. ************************************************************************/
  48. int I_georef_tps(double e1, /* EASTING TO BE TRANSFORMED */
  49. double n1, /* NORTHING TO BE TRANSFORMED */
  50. double *e, /* EASTING, TRANSFORMED */
  51. double *n, /* NORTHING, TRANSFORMED */
  52. double *E, /* EASTING COEFFICIENTS */
  53. double *N, /* NORTHING COEFFICIENTS */
  54. struct Control_Points *cp,
  55. int fwd
  56. )
  57. {
  58. int i, j;
  59. double dist, *pe, *pn;
  60. if (fwd) {
  61. pe = cp->e1;
  62. pn = cp->n1;
  63. }
  64. else {
  65. pe = cp->e2;
  66. pn = cp->n2;
  67. }
  68. /* global affine (1st order poly) */
  69. *e = E[0] + e1 * E[1] + n1 * E[2];
  70. *n = N[0] + e1 * N[1] + n1 * N[2];
  71. for (i = 0, j = 0; i < cp->count; i++) {
  72. if (cp->status[i] > 0) {
  73. dist = tps_base_func(e1, n1, pe[i], pn[i]);
  74. *e += E[j + 3] * dist;
  75. *n += N[j + 3] * dist;
  76. j++;
  77. }
  78. }
  79. return MSUCCESS;
  80. }
  81. /***********************************************************************
  82. COMPUTE THE FORWARD AND BACKWARD GEOREFFERENCING COEFFICIENTS
  83. BASED ON A SET OF CONTROL POINTS
  84. ************************************************************************/
  85. int I_compute_georef_equations_tps(struct Control_Points *cp,
  86. double **E12tps, double **N12tps,
  87. double **E21tps, double **N21tps)
  88. {
  89. double *tempptr;
  90. int numactive; /* NUMBER OF ACTIVE CONTROL POINTS */
  91. int status, i;
  92. double xmax, xmin, ymax, ymin;
  93. double delx, dely;
  94. double xx, yy;
  95. double sumx, sumy, sumx2, sumy2, sumxy;
  96. double SSxx, SSyy, SSxy;
  97. /* CALCULATE THE NUMBER OF VALID CONTROL POINTS */
  98. for (i = numactive = 0; i < cp->count; i++) {
  99. if (cp->status[i] > 0)
  100. numactive++;
  101. }
  102. if (numactive < 3)
  103. return MNPTERR;
  104. if (numactive > 100000) /* arbitrary, admittedly */
  105. return MNPTERR;
  106. xmin = xmax = cp->e1[0];
  107. ymin = ymax = cp->n1[0];
  108. sumx = sumy = sumx2 = sumy2 = sumxy = 0.0;
  109. for (i = 0; i < cp->count; i++ ) {
  110. if (cp->status[i] > 0) {
  111. xx = cp->e1[i];
  112. yy = cp->n1[i];
  113. xmax = MAX(xmax, xx);
  114. xmin = MIN(xmin, xx);
  115. ymax = MAX(ymax, yy);
  116. ymin = MIN(ymin, yy);
  117. sumx += xx;
  118. sumx2 += xx * xx;
  119. sumy += yy;
  120. sumy2 += yy * yy;
  121. sumxy += xx * yy;
  122. }
  123. }
  124. delx = xmax - xmin;
  125. dely = ymax - ymin;
  126. SSxx = sumx2 - sumx * sumx / numactive;
  127. SSyy = sumy2 - sumy * sumy / numactive;
  128. SSxy = sumxy - sumx * sumy / numactive;
  129. if (delx < 0.001 * dely || dely < 0.001 * delx ||
  130. fabs(SSxy * SSxy / (SSxx * SSyy)) > 0.99) {
  131. /* points are colinear */
  132. return MUNSOLVABLE;
  133. }
  134. xmin = xmax = cp->e2[0];
  135. ymin = ymax = cp->n2[0];
  136. sumx = sumy = sumx2 = sumy2 = sumxy = 0.0;
  137. for (i = 0; i < cp->count; i++ ) {
  138. if (cp->status[i] > 0) {
  139. xx = cp->e2[i];
  140. yy = cp->n2[i];
  141. xmax = MAX(xmax, xx);
  142. xmin = MIN(xmin, xx);
  143. ymax = MAX(ymax, yy);
  144. ymin = MIN(ymin, yy);
  145. sumx += xx;
  146. sumx2 += xx * xx;
  147. sumy += yy;
  148. sumy2 += yy * yy;
  149. sumxy += xx * yy;
  150. }
  151. }
  152. delx = xmax - xmin;
  153. dely = ymax - ymin;
  154. SSxx = sumx2 - sumx * sumx / numactive;
  155. SSyy = sumy2 - sumy * sumy / numactive;
  156. SSxy = sumxy - sumx * sumy / numactive;
  157. if (delx < 0.001 * dely || dely < 0.001 * delx ||
  158. fabs(SSxy * SSxy / (SSxx * SSyy)) > 0.99) {
  159. /* points are colinear */
  160. return MUNSOLVABLE;
  161. }
  162. /* CALCULATE THE FORWARD TRANSFORMATION COEFFICIENTS */
  163. G_message(_("Calculating forward transformation coefficients"));
  164. status = calccoef(cp, E12tps, N12tps);
  165. if (status != MSUCCESS)
  166. return status;
  167. /* SWITCH THE 1 AND 2 EASTING AND NORTHING ARRAYS */
  168. tempptr = cp->e1;
  169. cp->e1 = cp->e2;
  170. cp->e2 = tempptr;
  171. tempptr = cp->n1;
  172. cp->n1 = cp->n2;
  173. cp->n2 = tempptr;
  174. /* CALCULATE THE BACKWARD TRANSFORMATION COEFFICIENTS */
  175. G_message(_("Calculating backward transformation coefficients"));
  176. status = calccoef(cp, E21tps, N21tps);
  177. /* SWITCH THE 1 AND 2 EASTING AND NORTHING ARRAYS BACK */
  178. tempptr = cp->e1;
  179. cp->e1 = cp->e2;
  180. cp->e2 = tempptr;
  181. tempptr = cp->n1;
  182. cp->n1 = cp->n2;
  183. cp->n2 = tempptr;
  184. return status;
  185. }
  186. /***********************************************************************
  187. COMPUTE THE GEOREFFERENCING COEFFICIENTS
  188. BASED ON A SET OF CONTROL POINTS
  189. ************************************************************************/
  190. static int calccoef(struct Control_Points *cp, double **E, double **N)
  191. {
  192. struct MATRIX m;
  193. double *a;
  194. double *b;
  195. int numactive; /* NUMBER OF ACTIVE CONTROL POINTS */
  196. int status, i;
  197. /* CALCULATE THE NUMBER OF VALID CONTROL POINTS */
  198. for (i = numactive = 0; i < cp->count; i++) {
  199. if (cp->status[i] > 0)
  200. numactive++;
  201. }
  202. /* INITIALIZE MATRIX */
  203. m.n = numactive + 3;
  204. m.v = G_calloc(m.n * m.n, sizeof(double));
  205. if (m.v == NULL)
  206. G_fatal_error(_("%s: out of memory"), "I_compute_georef_equations_tps()");
  207. a = G_calloc(m.n, sizeof(double));
  208. if (a == NULL)
  209. G_fatal_error(_("%s: out of memory"), "I_compute_georef_equations_tps()");
  210. b = G_calloc(m.n, sizeof(double));
  211. if (b == NULL)
  212. G_fatal_error(_("%s: out of memory"), "I_compute_georef_equations_tps()");
  213. /* equation coefficients */
  214. *E = G_calloc(m.n, sizeof(double));
  215. if (*E == NULL)
  216. G_fatal_error(_("%s: out of memory"), "I_compute_georef_equations_tps()");
  217. *N = G_calloc(m.n, sizeof(double));
  218. if (*N == NULL)
  219. G_fatal_error(_("%s: out of memory"), "I_compute_georef_equations_tps()");
  220. status = calcls(cp, &m, a, b, *E, *N);
  221. G_free(m.v);
  222. G_free(a);
  223. G_free(b);
  224. return status;
  225. }
  226. /***********************************************************************
  227. CALCULATE THE TRANSFORMATION COEFFICIENTS FOR THIN PLATE SPLINE
  228. INTERPOLATION.
  229. THIS ROUTINE USES THE LEAST SQUARES METHOD TO COMPUTE THE COEFFICIENTS.
  230. ************************************************************************/
  231. static int calcls(struct Control_Points *cp, struct MATRIX *m,
  232. double a[], double b[],
  233. double E[], /* EASTING COEFFICIENTS */
  234. double N[] /* NORTHING COEFFICIENTS */
  235. )
  236. {
  237. int i, j, n, o, numactive = 0;
  238. double dist = 0.0, dx, dy, regularization;
  239. /* INITIALIZE THE MATRIX AND THE TWO COLUMN VECTORS */
  240. for (i = 1; i <= m->n; i++) {
  241. for (j = i; j <= m->n; j++) {
  242. M(i, j) = 0.0;
  243. if (i != j)
  244. M(j, i) = 0.0;
  245. }
  246. a[i - 1] = b[i - 1] = 0.0;
  247. }
  248. /* SUM THE UPPER HALF OF THE MATRIX AND THE COLUMN VECTORS ACCORDING TO
  249. THE LEAST SQUARES METHOD OF SOLVING OVER DETERMINED SYSTEMS */
  250. for (n = 0; n < cp->count; n++) {
  251. if (cp->status[n] > 0) {
  252. a[numactive + 3] = cp->e2[n];
  253. b[numactive + 3] = cp->n2[n];
  254. numactive++;
  255. M(1, numactive + 3) = 1.0;
  256. M(2, numactive + 3) = cp->e1[n];
  257. M(3, numactive + 3) = cp->n1[n];
  258. M(numactive + 3, 1) = 1.0;
  259. M(numactive + 3, 2) = cp->e1[n];
  260. M(numactive + 3, 3) = cp->n1[n];
  261. }
  262. }
  263. if (numactive < m->n - 3)
  264. return MINTERR;
  265. i = 0;
  266. for (n = 0; n < cp->count; n++) {
  267. if (cp->status[n] > 0) {
  268. i++;
  269. j = 0;
  270. for (o = 0; o <= n; o++) {
  271. if (cp->status[o] > 0) {
  272. j++;
  273. M(i + 3, j + 3) = tps_base_func(cp->e1[n], cp->n1[n],
  274. cp->e1[o], cp->n1[o]);
  275. if (i != j)
  276. M(j + 3, i + 3) = M(i + 3, j + 3);
  277. dx = cp->e1[n] - cp->e1[o];
  278. dy = cp->n1[n] - cp->n1[o];
  279. dist += sqrt(dx * dx + dy * dy);
  280. }
  281. }
  282. }
  283. }
  284. /* regularization */
  285. dist /= (numactive * numactive);
  286. regularization = 0.01 * dist * dist;
  287. /* set diagonal to regularization, but not the first 3x3 (global affine) */
  288. return solvemat(m, a, b, E, N);
  289. }
  290. /***********************************************************************
  291. SOLVE FOR THE 'E' AND 'N' COEFFICIENTS BY USING A SOMEWHAT MODIFIED
  292. GAUSSIAN ELIMINATION METHOD.
  293. | M11 M12 ... M1n | | E0 | | a0 |
  294. | M21 M22 ... M2n | | E1 | = | a1 |
  295. | . . . . | | . | | . |
  296. | Mn1 Mn2 ... Mnn | | En-1 | | an-1 |
  297. and
  298. | M11 M12 ... M1n | | N0 | | b0 |
  299. | M21 M22 ... M2n | | N1 | = | b1 |
  300. | . . . . | | . | | . |
  301. | Mn1 Mn2 ... Mnn | | Nn-1 | | bn-1 |
  302. ************************************************************************/
  303. static int solvemat(struct MATRIX *m, double a[], double b[], double E[],
  304. double N[])
  305. {
  306. int i, j, i2, j2, imark;
  307. double factor, temp;
  308. double pivot; /* ACTUAL VALUE OF THE LARGEST PIVOT CANDIDATE */
  309. for (i = 1; i <= m->n; i++) {
  310. G_percent(i - 1, m->n, 4);
  311. j = i;
  312. /* find row with largest magnitude value for pivot value */
  313. pivot = M(i, j);
  314. imark = i;
  315. for (i2 = i + 1; i2 <= m->n; i2++) {
  316. temp = fabs(M(i2, j));
  317. if (temp > fabs(pivot)) {
  318. pivot = M(i2, j);
  319. imark = i2;
  320. }
  321. }
  322. /* if the pivot is very small then the points are nearly co-linear */
  323. /* co-linear points result in an undefined matrix, and nearly */
  324. /* co-linear points results in a solution with rounding error */
  325. if (pivot == 0.0)
  326. return MUNSOLVABLE;
  327. /* if row with highest pivot is not the current row, switch them */
  328. if (imark != i) {
  329. for (j2 = 1; j2 <= m->n; j2++) {
  330. temp = M(imark, j2);
  331. M(imark, j2) = M(i, j2);
  332. M(i, j2) = temp;
  333. }
  334. temp = a[imark - 1];
  335. a[imark - 1] = a[i - 1];
  336. a[i - 1] = temp;
  337. temp = b[imark - 1];
  338. b[imark - 1] = b[i - 1];
  339. b[i - 1] = temp;
  340. }
  341. /* compute zeros above and below the pivot, and compute
  342. values for the rest of the row as well */
  343. for (i2 = 1; i2 <= m->n; i2++) {
  344. if (i2 != i) {
  345. factor = M(i2, j) / pivot;
  346. for (j2 = j; j2 <= m->n; j2++)
  347. M(i2, j2) -= factor * M(i, j2);
  348. a[i2 - 1] -= factor * a[i - 1];
  349. b[i2 - 1] -= factor * b[i - 1];
  350. }
  351. }
  352. }
  353. G_percent(1, 1, 1);
  354. /* SINCE ALL OTHER VALUES IN THE MATRIX ARE ZERO NOW, CALCULATE THE
  355. COEFFICIENTS BY DIVIDING THE COLUMN VECTORS BY THE DIAGONAL VALUES. */
  356. for (i = 1; i <= m->n; i++) {
  357. E[i - 1] = a[i - 1] / M(i, i);
  358. N[i - 1] = b[i - 1] / M(i, i);
  359. }
  360. return MSUCCESS;
  361. }
  362. static double tps_base_func(const double x1, const double y1,
  363. const double x2, const double y2)
  364. {
  365. /* official: r * r * log(r) */
  366. double dist;
  367. if ((x1 == x2) && (y1 == y2))
  368. return 0.0;
  369. dist = (x2 - x1) * (x2 - x1) + (y2 - y1) * (y2 - y1);
  370. return dist * log(dist) * 0.5;
  371. }