-
Notifications
You must be signed in to change notification settings - Fork 5
/
tc_prep.c
executable file
·555 lines (493 loc) · 17.4 KB
/
tc_prep.c
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
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
/**
* \file tc_prep.c
* \brief Preprocess with colorspace, normalization, convolution
* \author David Ray Thompson
*
* Copyright 2012, by the California Institute of Technology. ALL RIGHTS
* RESERVED. United States Government Sponsorship acknowledged. Any
* commercial use must be negotiated with the Office of Technology
* Transfer at the California Institute of Technology.
*
* This includes some code from RIT that was posted to:
* http://www.cs.rit.edu/~ncs/color/t_convert.html
*/
#include <stdlib.h>
#include <math.h>
#include <string.h>
#include <stdio.h>
#include "tc_image.h"
#include "tc_bar_fixed.h"
#include "tc_preproc.h"
#include "tc_prep.h"
const int bpbig[3]= {11,33,99};
const int bpsmall[3]= {0,3,9};
int main(int argc, char **argv)
{
float maxpx = 255.0;
int r, c, octave=0;
pixel_t tmu = 128;
tc_prep_t *tc_prep_opt, tc_prep_opt_local;
char *inname = NULL;
char *msg = malloc(sizeof(char) * MAX_STRING);
char *outname = NULL;
int arg = 0;
tc_prep_opt_local.method = TC_PREP_BANDPASS;
tc_prep_opt_local.outchans = 1;
tc_prep_opt_local.ffname = NULL;
tc_prep_opt_local.in = NULL;
tc_prep_opt_local.out = NULL;
tc_prep_opt_local.intens = NULL;
tc_prep_opt_local.scratch = NULL;
tc_prep_opt_local.scratchB = NULL;
tc_prep_opt_local.ff = NULL;
tc_prep_opt_local.bandpass_filter_small = 3;
tc_prep_opt_local.bandpass_filter_big = 11;
tc_prep_opt = &tc_prep_opt_local;
tc_bar_filter = (tc_bar_t*)malloc(sizeof(tc_bar_t));
if (tc_bar_filter == NULL)
{
tc_write_log("preproc: could not allocate memory for bar filter struct. \r\n");
free(msg);
return(-1);
}
tc_bar_filter->nscales = 3;
tc_bar_filter->norients = 8;
tc_bar_filter->support = 19;
tc_write_log("preproc: Starting.\r\n");
/* parse the commands */
arg = tc_prep_parse( tc_prep_opt, argc, argv );
if( arg == -1 )
{
free(msg);
return(-1);
}
inname = argv[arg];
outname = argv[arg+1];
/* Read each image sequentially, checking integrity as we go */
snprintf(msg,MAX_STRING,"preproc: Reading image %s\r\n",inname);
tc_write_log(msg);
tc_read_image(&tc_prep_opt->in, inname);
if (tc_prep_opt->in == NULL)
{
tc_write_log("preproc: Couldn't read image.\r\n");
free(msg);
return(-1);
}
snprintf(msg,MAX_STRING,
"preproc: Input image is %d x %d pixels x %d channels\r\n",
tc_prep_opt->in->cols, tc_prep_opt->in->rows,
tc_prep_opt->in->chans);
tc_write_log(msg);
/* Propagate the number of channels, if no processing to be done */
if (tc_prep_opt->outchans == -1)
tc_prep_opt->outchans = tc_prep_opt->in->chans;
/* Allocate the output image */
tc_alloc_image(&tc_prep_opt->out,
tc_prep_opt->in->rows, tc_prep_opt->in->cols,
tc_prep_opt->outchans);
if (tc_prep_opt->out == NULL)
{
tc_write_log("preproc: Out of memory!\r\n");
tc_free_image(tc_prep_opt->in);
free(msg);
return(-1);
}
/* Allocate the intensity image */
tc_alloc_image(&tc_prep_opt->intens,
tc_prep_opt->in->rows, tc_prep_opt->in->cols, 1);
if (tc_prep_opt->intens == NULL)
{
tc_write_log("preproc: Out of memory!\r\n");
tc_free_image(tc_prep_opt->in);
tc_free_image(tc_prep_opt->out);
free(msg);
return(-1);
}
/* convert intensity */
if (tc_intensity(tc_prep_opt->intens, tc_prep_opt->in) == ERR)
{
tc_write_log("preproc: intensity conversion failed\r\n");
tc_prep_free_intens_io( tc_prep_opt );
free(msg);
return(-1);
}
/* load bar filter info if needed */
if (tc_prep_opt->method == TC_PREP_BAR ||
tc_prep_opt->method == TC_PREP_BARHSV)
{
char* bar_fname = malloc(sizeof(char) * MAX_STRING);
FILE* fin;
int nvals = tc_bar_filter->norients * tc_bar_filter->nscales *
tc_bar_filter->support * tc_bar_filter->support;
/* Read in the bar filter info from a file */
snprintf(bar_fname, MAX_STRING,"bar_%04i.dat", tc_bar_filter->support);
fin = fopen(bar_fname, "rb");
free(bar_fname);
if (fin == NULL)
{
tc_write_log("Could not open bar filter file; check if it needs to be generated.\r\n");
free(msg);
return(-1);
}
tc_bar_filter->bar = (short*)malloc(sizeof(short*) * nvals);
if (tc_bar_filter->bar == NULL)
{
tc_write_log("preproc: could not allocate memory for bar filter. \r\n");
free(msg);
return(-1);
}
if (fread(tc_bar_filter->bar, sizeof(short), nvals, fin) != nvals)
{
tc_write_log("preproc: bar filter read failed \r\n");
free(msg);
return(-1);
}
fclose(fin);
}
switch (tc_prep_opt->method)
{
case TC_PREP_GREYWORLD:
tc_write_log("preproc: normalizing intensity tc_prep_opt->in each channel.\r\n");
tc_greyworld(tc_prep_opt->out, tc_prep_opt->in, tmu);
break;
case TC_PREP_BANDPASS:
tc_write_log("preproc: bandpass filter\r\n");
tc_bandpass_image(tc_prep_opt->out, tc_prep_opt->intens,
tc_prep_opt->bandpass_filter_big, tc_prep_opt->bandpass_filter_small, tmu);
break;
case TC_PREP_BANDPASS_OCTAVES:
tc_alloc_image(&tc_prep_opt->scratch, tc_prep_opt->in->rows,
tc_prep_opt->in->cols, 1);
if (tc_prep_opt->scratch == NULL)
{
tc_write_log("preproc: Out of memory!\r\n");
tc_prep_free_intens_io( tc_prep_opt );
free(msg);
return(-1);
}
for (octave=0; octave<3; octave++)
{
if (tc_bandpass_image(tc_prep_opt->scratch, tc_prep_opt->intens, bpbig[octave],
bpsmall[octave],tmu) == ERR)
{
tc_write_log("preproc: intensity conversion failed\r\n");
tc_free_image(tc_prep_opt->scratch);
tc_prep_free_intens_io( tc_prep_opt );
free(msg);
return(-1);
}
/* Copy the result into the appropriate output channel */
for (r=0; r<tc_prep_opt->in->rows; r++)
{
for (c=0; c<tc_prep_opt->in->cols; c++)
{
tc_set(tc_prep_opt->out,r,c,octave,tc_get(tc_prep_opt->scratch,r,c,0));
}
}
}
tc_free_image(tc_prep_opt->scratch);
break;
case TC_PREP_HSV:
if (tc_rgbhsv(tc_prep_opt->out, tc_prep_opt->in, maxpx) == ERR)
{
tc_write_log("preproc: hsv conversion failed\r\n");
tc_prep_free_intens_io( tc_prep_opt );
free(msg);
return(-1);
}
break;
case TC_PREP_TEXTURECAM:
if (tc_read_image(&tc_prep_opt->ff, tc_prep_opt->ffname) == ERR);
{
tc_write_log("preproc: could not find flat field image.\r\n");
tc_prep_free_intens_io( tc_prep_opt );
free(msg);
return(-1);
}
if (tc_flatfield_image(tc_prep_opt->scratch, tc_prep_opt->in,
tc_prep_opt->ff) == ERR)
for (r=0; r<tc_prep_opt->in->rows; r++)
{
for (c=0; c<tc_prep_opt->in->cols; c++)
{
tc_set(tc_prep_opt->out,r,c,0,tc_get(tc_prep_opt->scratch,r,c,0));
tc_set(tc_prep_opt->out,r,c,1,tc_get(tc_prep_opt->scratch,r,c,1));
tc_set(tc_prep_opt->out,r,c,2,tc_get(tc_prep_opt->scratch,r,c,2));
}
}
tc_free_image(tc_prep_opt->ff);
break;
case TC_PREP_INTENSITY:
if (tc_greyworld(tc_prep_opt->out, tc_prep_opt->intens, tmu) == ERR)
{
tc_write_log("preproc: intensity conversion failed\r\n");
tc_prep_free_intens_io( tc_prep_opt );
free(msg);
return(-1);
}
break;
case TC_PREP_IPEX:
tc_alloc_image(&tc_prep_opt->scratch, tc_prep_opt->in->rows,
tc_prep_opt->in->cols, 1);
if (tc_prep_opt->scratch == NULL)
{
tc_write_log("preproc: Out of memory!\r\n");
tc_prep_free_intens_io( tc_prep_opt );
free(msg);
return(-1);
}
/* local highpass filter */
if (tc_bandpass_image(tc_prep_opt->scratch, tc_prep_opt->intens, 11, 0,
tmu) == ERR)
{
tc_write_log("preproc: bandpass failed\r\n");
tc_free_image(tc_prep_opt->scratch);
tc_prep_free_intens_io( tc_prep_opt );
free(msg);
return(-1);
}
/* Copy the result into the appropriate output channel */
for (r=0; r<tc_prep_opt->in->rows; r++)
{
for (c=0; c<tc_prep_opt->in->cols; c++)
{
tc_set(tc_prep_opt->out,r,c,0,tc_get(tc_prep_opt->scratch,r,c,0));
}
}
tc_free_image(tc_prep_opt->scratch);
tc_alloc_image(&tc_prep_opt->scratchB, tc_prep_opt->in->rows,
tc_prep_opt->in->cols, 3);
if (tc_prep_opt->scratchB == NULL)
{
tc_write_log("preproc: Out of memory!\r\n");
tc_prep_free_intens_io( tc_prep_opt );
free(msg);
return(-1);
}
if (tc_rgbhsv(tc_prep_opt->scratchB, tc_prep_opt->in, maxpx) == ERR)
{
tc_write_log("preproc: hsv conversion failed\r\n");
tc_free_image(tc_prep_opt->scratchB);
tc_prep_free_intens_io( tc_prep_opt );
free(msg);
return(-1);
}
for (r=0; r<tc_prep_opt->in->rows; r++)
{
for (c=0; c<tc_prep_opt->in->cols; c++)
{
tc_set(tc_prep_opt->out,r,c,1,tc_get(tc_prep_opt->scratchB,r,c,0));
tc_set(tc_prep_opt->out,r,c,2,tc_get(tc_prep_opt->scratchB,r,c,2));
}
}
tc_free_image(tc_prep_opt->scratchB);
break;
/* only callable internally, not from command prompt */
case TC_PREP_GREY2RGB:
tc_write_log("preproc: greyrgb conversion\r\n");
if (tc_greyrgb(tc_prep_opt->out, tc_prep_opt->in) == ERR)
{
tc_write_log("preproc: greyrgb conversion failed\r\n");
tc_prep_free_intens_io( tc_prep_opt );
free(msg);
return(-1);
}
break;
case TC_PREP_BAR:
if (tc_bar(tc_prep_opt->out, tc_prep_opt->intens) == ERR)
{
tc_write_log("preproc: oriented bars failed\r\n");
tc_prep_free_intens_io( tc_prep_opt );
free(msg);
return(-1);
}
break;
case TC_PREP_NONE:
/* just copy data into output buffer */
memcpy( tc_prep_opt->out->data, tc_prep_opt->in->data,
tc_prep_opt->in->rows * tc_prep_opt->in->cols * tc_prep_opt->in->chans);
break;
case TC_PREP_BARHSV:
/* Allocate scratch space */
tc_alloc_image(&tc_prep_opt->scratch, tc_prep_opt->in->rows,
tc_prep_opt->in->cols, 1);
if (tc_prep_opt->scratch == NULL)
{
tc_write_log("preproc: Out of memory!\r\n");
tc_prep_free_intens_io( tc_prep_opt );
free(msg);
return(-1);
}
/* Oriented bar filter */
if (tc_bar(tc_prep_opt->scratch, tc_prep_opt->intens) == ERR)
{
tc_write_log("preproc: oriented bars failed\r\n");
tc_free_image(tc_prep_opt->scratch);
tc_prep_free_intens_io( tc_prep_opt );
free(msg);
return(-1);
}
/* Copy the result into the appropriate output channel */
for (r=0; r<tc_prep_opt->in->rows; r++)
{
for (c=0; c<tc_prep_opt->in->cols; c++)
{
tc_set(tc_prep_opt->out,r,c,0,tc_get(tc_prep_opt->scratch,r,c,0));
}
}
tc_free_image(tc_prep_opt->scratch);
/* Allocate a new scratch space image */
tc_alloc_image(&tc_prep_opt->scratchB, tc_prep_opt->in->rows,
tc_prep_opt->in->cols, 3);
if (tc_prep_opt->scratchB == NULL)
{
tc_write_log("preproc: Out of memory!\r\n");
tc_prep_free_intens_io( tc_prep_opt );
free(msg);
return(-1);
}
/* Now convert HSV */
if (tc_rgbhsv(tc_prep_opt->scratchB, tc_prep_opt->in, maxpx) == ERR)
{
tc_write_log("preproc: hsv conversion failed\r\n");
tc_free_image(tc_prep_opt->scratchB);
tc_prep_free_intens_io( tc_prep_opt );
free(msg);
return(-1);
}
/* Copy the result into the appropriate output channel */
for (r=0; r<tc_prep_opt->in->rows; r++)
{
for (c=0; c<tc_prep_opt->in->cols; c++)
{
tc_set(tc_prep_opt->out,r,c,1,tc_get(tc_prep_opt->scratchB,r,c,0));
tc_set(tc_prep_opt->out,r,c,2,tc_get(tc_prep_opt->scratchB,r,c,1));
tc_set(tc_prep_opt->out,r,c,3,tc_get(tc_prep_opt->scratchB,r,c,2));
}
}
tc_free_image(tc_prep_opt->scratchB);
break;
default:
break;
}
/* Write output */
snprintf(msg,MAX_STRING,"preproc: Writing output image %s\r\n",outname);
tc_write_log(msg);
tc_write_image(tc_prep_opt->out, outname);
/* Clean up */
tc_write_log("preproc: Cleanup (intens).\r\n");
tc_prep_free_intens_io( tc_prep_opt );
free(msg);
tc_write_log("preproc: Exiting.\r\n");
/* normal return */
return 0;
}
int tc_prep_parse( tc_prep_t *tc_prep_opt, int argc, char **argv )
{
int arg=1, help=0, additional_args=0;
/* Parse the command line */
while (arg < argc && argv[arg][0] == '-')
{
switch (argv[arg][1])
{
case 'n':
tc_prep_opt->method = TC_PREP_INTENSITY;
tc_prep_opt->outchans = 1;
break;
case 'c':
tc_prep_opt->method = TC_PREP_HSV;
tc_prep_opt->outchans = 3;
break;
case 'z':
tc_prep_opt->method = TC_PREP_NONE;
tc_prep_opt->outchans = -1;
break;
case 'B':
tc_prep_opt->method = TC_PREP_BAR;
tc_prep_opt->outchans = 1;
if ((arg+1)>=argc)
{
help=1;
break;
}
tc_bar_filter->support = atoi(argv[arg+1]);
arg = arg+1;
break;
case 'a':
tc_prep_opt->method = TC_PREP_BARHSV;
tc_prep_opt->outchans = 4;
if ((arg+1)>=argc)
{
help=1;
break;
}
tc_bar_filter->support = atoi(argv[arg+1]);
arg = arg+1;
break;
case 'b':
tc_prep_opt->method = TC_PREP_BANDPASS;
if ((arg+2)>=argc)
{
help=1;
break;
}
tc_prep_opt->bandpass_filter_small = atoi(argv[arg+1]);
tc_prep_opt->bandpass_filter_big = atoi(argv[arg+2]);
tc_prep_opt->outchans = 1;
arg = arg+2;
break;
case 'o':
tc_prep_opt->method = TC_PREP_BANDPASS_OCTAVES;
tc_prep_opt->outchans = 3;
break;
case 'g':
tc_prep_opt->method = TC_PREP_GREYWORLD;
tc_prep_opt->outchans = 3;
break;
case 'i':
tc_prep_opt->method = TC_PREP_IPEX;
tc_prep_opt->outchans = 3;
break;
case 't':
tc_prep_opt->method = TC_PREP_TEXTURECAM;
if ((arg+1)>=argc)
{
help=1;
break;
}
tc_prep_opt->ffname = argv[arg+1];
tc_prep_opt->outchans = 3;
arg = arg+1;
break;
default:
help = 1;
break;
}
arg++;
}
additional_args = 2;
if ((arg+additional_args)>argc || help || argc==1)
{
tc_write_log("Usage is tcprep [OPTIONS] <input.pgm> <output.pgm>\r\n");
tc_write_log(" OPTIONS must be one of the following:\r\n");
tc_write_log(" -c switch colorspace to hsv\r\n");
tc_write_log(" -g greyworld color constancy\r\n");
tc_write_log(" -n normalized intensity channel\r\n");
tc_write_log(" -b <small> <big> bandpass filter w/ given radii\r\n");
tc_write_log(" -o three-channel bandpass suite\r\n");
tc_write_log(" -i IPEX hsv & bandpass suite\r\n");
tc_write_log(" -B <support> oriented bar filter w/given support\r\n");
tc_write_log(" -a <support> oriented bar filter and HSV \r\n");
tc_write_log(" -t <flatfield.ppm> TextureCam/Hitachi preprocessing\r\n");
tc_write_log(" -z no pre processing\r\n");
tc_write_log(" -h help!\r\n");
return(-1);
}
return arg;
}
void tc_prep_free_intens_io( tc_prep_t *tc_prep_opt )
{
tc_free_image(tc_prep_opt->intens);
tc_free_image(tc_prep_opt->in);
tc_free_image(tc_prep_opt->out);
}