-
Notifications
You must be signed in to change notification settings - Fork 0
/
LinearSystem.cpp
486 lines (388 loc) · 10.5 KB
/
LinearSystem.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
// LinearSystem.cpp: implementation of the LinearSystem class.
//
//////////////////////////////////////////////////////////////////////
/*
#define DEBUG
*/
#ifdef HAVE_CONFIG_H
#include <config.h>
#endif /*HAVE_CONFIG_H*/
#include <cstdlib>
#include <iostream>
#include <math.h>
#include <string.h>
#include <vips/vips8>
#include "LinearSystem.h"
#include "svd.h"
#include "nrutil.h"
#include "computepoly.h"
#include "writeptm.h"
using namespace vips;
//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////
LinearSystem::LinearSystem(Basis_e b, bool c, int crop_left, int crop_top,
int crop_width, int crop_height)
{
basis_m = b;
cache = c;
crop_left_m = crop_left;
crop_top_m = crop_top;
crop_width_m = crop_width;
crop_height_m = crop_height;
colors = 0;
Samples_m = 0;
}
void
readLine (FILE * fp, char *buf, int limit)
{
int c, i;
for (i = 0; i < limit - 1 && (c = getc (fp)) != EOF && c != '\n'; i++)
buf[i] = c;
if (c == '\n')
{
buf[i] = c;
i++;
}
if (c != '\n' && c != EOF)
{
printf ("File reading warning:\n");
printf ("attempted to read line and stopped before newline\n");
printf ("Please use a shorter path or check for newlines\n");
}
buf[i] = '\0';
}
// load the files from the filenames that were found in the .lp file
int
LinearSystem::LoadFiles ()
{
int stat = 1;
int i;
/* Output images. high images has r,g,b = A,B,C repectively */
/* low images have r,g,b = D,E,F */
printf ("Reading images:");
for (i = 0; i < Images_m; i++)
{
char buf[5];
sprintf (buf, "%3i", i + 1);
if (i)
printf ("\b\b\b");
printf ("%s", buf);
fflush (stdout);
char *basename = g_path_get_basename(Samples_m[i].filename);
VImage im = VImage::new_from_file(basename,
VImage::option()->set("access", VIPS_ACCESS_SEQUENTIAL));
g_free( basename );
int left = crop_left_m / 1000.0 * im.width();
int top = crop_top_m / 1000.0 * im.height();
int width = crop_width_m / 1000.0 * im.width();
int height = crop_height_m / 1000.0 * im.height();
im = im.extract_area (left, top, width, height);
Samples_m[i].im = im;
Samples_m[i].xsize = im.width();
Samples_m[i].ysize = im.height();
if (Samples_m[0].xsize != im.width()
|| Samples_m[0].ysize != im.height())
{
fprintf (stderr, "images differ in size\n");
return -1;
}
}
printf ("\n");
return stat;
}
int
LinearSystem::InitFiles (char *lpfile)
{
char filedesc[250];
float vecmag;
int readargs;
FILE *infofp;
infofp = fopen (lpfile, "r");
if (infofp == NULL)
{
fprintf (stderr, "Light Position file: %s, not found.\n", lpfile);
return -1;
}
if (fscanf (infofp, "%i\n", &Images_m) != 1)
{
fprintf (stderr, "Light Position file: first line wrong\n");
return -1;
}
Samples_m = new RGB_Image[Images_m];
for (int i = 0; i < Images_m; i++)
{
char inputLine[STRSIZE];
readLine (infofp, inputLine, STRSIZE);
readargs = sscanf (inputLine, "%s %f %f %f", (char *) &filedesc,
&(Samples_m[i].x), &(Samples_m[i].y),
&(Samples_m[i].z));
if (readargs != 4)
{
printf
("Caution, couldn't find required values: filename x y z in lp file.\n");
printf ("For line: %s\n", inputLine);
}
vecmag = (float) sqrt (Samples_m[i].x * Samples_m[i].x +
Samples_m[i].y * Samples_m[i].y +
Samples_m[i].z * Samples_m[i].z);
if (vecmag > 0)
{
// normalize
vecmag = 1.0f / vecmag;
Samples_m[i].x *= vecmag;
Samples_m[i].y *= vecmag;
Samples_m[i].z *= vecmag;
}
else
{
printf ("Caution, length zero vector found in lp file: %f %f %f\n",
Samples_m[i].x, Samples_m[i].y, Samples_m[i].z);
}
Samples_m[i].filename = new char[strlen (filedesc) + 1];
strcpy (Samples_m[i].filename, filedesc);
}
fclose (infofp);
return 0;
}
int
LinearSystem::FitPTM (char *lpfile)
{
int stat = 1;
if (InitFiles (lpfile) == -1)
return -1;
if (LoadFiles () == -1)
return -1;
// here we do the set up for solving the PTM;
// here we first set up a matrix and the right hand side
// then we call the solver to obtain the result
// Afterwards scale and bias have to be computed and the PTM file has to
// be written
double **M;
stat = BuildMatrix (M);
if (stat == -1)
return stat;
stat = ComputePolynomials (M);
if (stat == -1)
return stat;
std::vector<VImage> in;
for (int i = 0; i < Images_m; i++)
in.push_back(Samples_m[i].im);
VImage::call("compute_polys", VImage::option()->
set( "in", in )->
set( "out", &coeffs )->
set( "M", vipsM ) );
/* With cache enabled, write to a huge memory buffer.
*/
if( cache )
coeffs = coeffs.write(VImage::new_memory());
ComputeScaleAndBias ();
printf ("computation done!\n");
// if we're not caching, we'll need to scan again for write
if( !cache ) {
// we open our files in streaming mode, so after the pass where we
// calculate scale/bias, we need to reopen for the ptm write and
// regen coeffs
LoadFiles ();
std::vector<VImage> in2;
for (int i = 0; i < Images_m; i++)
in2.push_back(Samples_m[i].im);
VImage::call("compute_polys", VImage::option()->
set( "in", in2 )->
set( "out", &coeffs )->
set( "M", vipsM ) );
}
int coldim;
if (basis_m == QUADRATIC_BIVARIATE)
coldim = 6;
else if (basis_m == QUADRATIC_UNIVARIATE)
coldim = 3;
else
coldim = 6; // use QUADRATIC_BIVARIATE as default
free_dmatrix (M, 1, Images_m, 1, coldim);
return stat;
}
int
LinearSystem::BuildMatrix (double **&M)
{
int coldim;
int stat = 1;
if (basis_m == QUADRATIC_BIVARIATE)
coldim = 6;
else if (basis_m == QUADRATIC_UNIVARIATE)
coldim = 3;
else
coldim = 6; // use QUADRATIC_BIVARIATE as default
if (Images_m < coldim)
{
printf ("Error: Not enough samples for fitting a PTM\n");
if (coldim == 6)
printf ("A Bivariate PTM requires 6 or more images\n");
else if (coldim == 3)
printf ("A Univariate PTM requires 3 or more images\n");
stat = -1;
}
M = dmatrix (1, Images_m, 1, coldim);
if (basis_m == QUADRATIC_BIVARIATE)
{
for (int k = 1; k <= Images_m; k++)
{
M[k][1] = 1.0;
M[k][2] = Samples_m[k - 1].y;
M[k][3] = Samples_m[k - 1].x;
M[k][4] = M[k][2] * M[k][3];
M[k][5] = M[k][2] * M[k][2];
M[k][6] = M[k][3] * M[k][3];
}
}
else if (basis_m == QUADRATIC_UNIVARIATE)
{
for (int k = 1; k <= Images_m; k++)
{
M[k][1] = 1.0;
M[k][2] = Samples_m[k - 1].x;
M[k][3] = M[k][2] * M[k][2];
}
}
else
{
printf ("Basis not implemented\n");
stat = -1;
}
return stat;
}
int
LinearSystem::ComputePolynomials (double **M)
{
// R is the right hand side and hence is crucial
// FILE *fp = fopen("Matrix","w");
// fclose(fp);
int i, j, k, l;
int coldim;
if (basis_m == QUADRATIC_BIVARIATE)
coldim = 6;
else if (basis_m == QUADRATIC_UNIVARIATE)
coldim = 3;
else
coldim = 6; // use QUADRATIC_BIVARIATE as default
double *Diag = dvector (1, Images_m);
double *R = dvector (1, Images_m);
double **V = dmatrix (1, Images_m, 1, Images_m);
svdcmp (M, Images_m, coldim, Diag, V);
for (k = 1; k <= coldim; k++)
if (fabs (Diag[k]) <= 1.0e-10)
{
printf ("System can not be solved, not enough info to "
"compute coefficients!\n");
printf ("Most likely cause: sample locations are redundant; "
"e.g. are colinear\n");
free_dvector (Diag, 1, Images_m);
free_dvector (R, 1, Images_m);
free_dmatrix (V, 1, Images_m, 1, Images_m);
free_dmatrix (M, 1, Images_m, 1, coldim);
return -1;
}
double **UT = MatrixStyleTranspose (M, Images_m, coldim);
for (k = 1; k <= coldim; k++)
for (l = 1; l <= Images_m; l++)
UT[k][l] = UT[k][l] / Diag[k];
double **InverseMatrix =
MatrixStyleMult (V, coldim, coldim, UT, coldim, Images_m);
// make the matrix image
vipsM = VImage::new_matrix(Images_m, coldim);
for (j = 0; j < coldim; j++)
for (i = 0; i < Images_m; i++)
*VIPS_MATRIX( vipsM.get_image(), i, j ) = InverseMatrix[j + 1][i + 1];
free_dvector (Diag, 1, Images_m);
free_dvector (R, 1, Images_m);
free_dmatrix (V, 1, Images_m, 1, Images_m);
free_dmatrix (UT, 1, coldim, 1, Images_m);
free_dmatrix (InverseMatrix, 1, coldim, 1, Images_m);
return 1;
}
void
LinearSystem::ComputeScaleAndBias ()
{
// First compute the minimum and maximum of each coefficient for each channel
int i;
int basedim;
printf ("computing scale and bias ... \n");
if (basis_m == QUADRATIC_BIVARIATE)
basedim = 6;
else if (basis_m == QUADRATIC_UNIVARIATE)
basedim = 3;
else
basedim = 6;
VImage stats = coeffs.stats ();
#ifdef DEBUG
std::cout << stats;
#endif /*DEBUG*/
// the first three channels are RGB, the subsequent 3 or 6 are the poly
// coeffs
double lummin[6], lummax[6];
for (i = 0; i < basedim; i++)
{
lummin[i] = *VIPS_MATRIX( stats.get_image(), 0, i + 4);
lummax[i] = *VIPS_MATRIX( stats.get_image(), 1, i + 4);
}
#ifdef DEBUG
printf( "min: " );
for( i = 0; i < basedim; i++ )
printf( "%g ", lummin[i] );
printf( "\n" );
printf( "max: " );
for( i = 0; i < basedim; i++ )
printf( "%g ", lummax[i] );
printf( "\n" );
#endif /*DEBUG*/
int lumscale[6];
for (i = 0; i < basedim; i++)
{
// check what the next highest 2 power is
frexp (lummax[i] - lummin[i], &(lumscale[i]));
//256 or 8 is what we can deal with, higher power requires scaling
lumscale[i] = lumscale[i] - 8;
}
for (i = 0; i < basedim; i++)
scale[i] = (float) pow (2, lumscale[i]);
for (i = 0; i < basedim; i++)
{
if (lummin[i] < 0)
bias[i] = (int) (0.0 - lummin[i] / scale[i] + 1);
while (bias[i] > 255)
{
scale[i] = scale[i] * 2;
bias[i] = (int) (0.0 - lummin[i] / scale[i] + 1);
}
if (lummin[i] > 0)
{
while (lummax[i] / scale[i] > 255)
scale[i] = scale[i] * 2;
bias[i] = 0;
}
}
#ifdef DEBUG
for (i = 0; i < basedim; i++)
printf ("SCALE AND BIAS: %lf %d\n", scale[i], bias[i]);
#endif /*DEBUG*/
}
void
LinearSystem::WriteFileVersion1_2 (char *fname)
{
if( writeptm( coeffs.get_image (), fname, scale, bias ) )
{
std::cerr << "Error writing file\n";
}
}
LinearSystem::~LinearSystem ()
{
// clean up the samples as well
if (Samples_m)
{
for (int i = 0; i < Images_m; i++)
{
delete Samples_m[i].filename;
}
}
delete[]Samples_m;
}