calibrate.c
Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034 #include <stdio.h>
00035
00036 #include "calibrate.h"
00037
00038 extern int flag_debug;
00039
00040 int perform_calibration(calibration *cal) {
00041 int j;
00042 float n, x, y, x2, y2, xy, z, zx, zy;
00043 float det, a, b, c, e, f, i;
00044 float scaling = 65536.0;
00045
00046
00047 n = x = y = x2 = y2 = xy = 0;
00048 for(j=0;j<5;j++) {
00049 n += 1.0;
00050 x += (float)cal->x[j];
00051 y += (float)cal->y[j];
00052 x2 += (float)(cal->x[j]*cal->x[j]);
00053 y2 += (float)(cal->y[j]*cal->y[j]);
00054 xy += (float)(cal->x[j]*cal->y[j]);
00055 }
00056
00057
00058 det = n*(x2*y2 - xy*xy) + x*(xy*y - x*y2) + y*(x*xy - y*x2);
00059 if(det < 0.1 && det > -0.1) {
00060 fprintf(stderr, "ts_calibrate: determinant is too small -- %f\n",det);
00061 return 0;
00062 }
00063
00064
00065 a = (x2*y2 - xy*xy)/det;
00066 b = (xy*y - x*y2)/det;
00067 c = (x*xy - y*x2)/det;
00068 e = (n*y2 - y*y)/det;
00069 f = (x*y - n*xy)/det;
00070 i = (n*x2 - x*x)/det;
00071
00072
00073 z = zx = zy = 0;
00074 for(j=0;j<5;j++) {
00075 z += (float)cal->xfb[j];
00076 zx += (float)(cal->xfb[j]*cal->x[j]);
00077 zy += (float)(cal->xfb[j]*cal->y[j]);
00078 }
00079
00080
00081 cal->a[0] = (int)((a*z + b*zx + c*zy)*(scaling));
00082 cal->a[1] = (int)((b*z + e*zx + f*zy)*(scaling));
00083 cal->a[2] = (int)((c*z + f*zx + i*zy)*(scaling));
00084
00085 if (flag_debug)
00086 fprintf(stderr, "%f %f %f\n",(a*z + b*zx + c*zy),
00087 (b*z + e*zx + f*zy),
00088 (c*z + f*zx + i*zy));
00089
00090
00091 z = zx = zy = 0;
00092 for(j=0;j<5;j++) {
00093 z += (float)cal->yfb[j];
00094 zx += (float)(cal->yfb[j]*cal->x[j]);
00095 zy += (float)(cal->yfb[j]*cal->y[j]);
00096 }
00097
00098
00099 cal->a[3] = (int)((a*z + b*zx + c*zy)*(scaling));
00100 cal->a[4] = (int)((b*z + e*zx + f*zy)*(scaling));
00101 cal->a[5] = (int)((c*z + f*zx + i*zy)*(scaling));
00102
00103 if (flag_debug)
00104 fprintf(stderr, "%f %f %f\n",(a*z + b*zx + c*zy),
00105 (b*z + e*zx + f*zy),
00106 (c*z + f*zx + i*zy));
00107
00108
00109 cal->a[6] = (int)scaling;
00110 return 1;
00111
00112
00113
00114
00115
00116
00117
00118
00119
00120 }