-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtwo_photos_psf_estim_main.c
327 lines (269 loc) · 9.35 KB
/
two_photos_psf_estim_main.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
/*----------------------------------------------------------------------------
"Recovering the Subpixel PSF from Two Photographs at Different Distances"
Copyright 2013 mauricio delbracio (mdelbra@gmail.com)
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License as
published by the Free Software Foundation, either version 3 of the
License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Affero General Public License for more details.
You should have received a copy of the GNU Affero General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
----------------------------------------------------------------------------*/
/**
* @file two_photos_psf_estim_main.c
* @brief main for psf estimation algorithm execution.
* @author Mauricio Delbracio (mdelbra@gmail.com)
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include "io_pgm.h"
#include "image.h"
#include "two_photos_psf_estim.h"
/** @brief struct of program parameters */
typedef struct
{
int s;
int psf_nx;
int psf_ny;
char *img_zin;
char *out_psf_pgm;
char *out_intk_pgm;
char *img_zout;
char *output_psf;
char *output_intk;
char *outprefix;
int threshold;
} program_argums;
static void usage(const char* name)
{
printf("Recovering the Subpixel PSF from Two Photographs ");
printf("at Different Distances\n");
printf("Copyright M.Delbracio, A.Almansa, P.Muse. ");
printf("Version 1.0 Feb 22, 2013\n\n");
printf("Usage: %s [options] <input file 1> <input file 2> <output PSF txt> ", name);
printf("<output inter-kernel txt>\n\n");
printf("Only PGM 16/8 bits images are supported.\n\n");
printf("Options:\n");
printf(" -s <number> Super-resolution factor ");
printf("(positive integer, default 3)\n");
printf(" -k <number> PSF support size (default 13)\n");
printf(" -o <file> Estimated PSF save to a 8bits PGM image \n");
printf(" -i <file> Estimated inter-image kernel save ");
printf("to a 8bits PGM image \n");
printf(" -d <file> Save all the intermediate images in ");
printf("files with prefix <file>\n");
printf(" -t 0,1 1 - Threshold negative values to zero (0-default)\n");
}
static int parse_arguments(program_argums *param, int argc, char *argv[])
{
char *OptionString;
char OptionChar;
int i;
if(argc < 5)
{
usage(argv[0]);
return 0;
}
/* Set parameter defaults */
param->s = 3;
param->psf_nx = 13;
param->psf_ny = 13;
param->threshold = 0;
param->img_zin = NULL;
param->img_zout = NULL;
param->output_psf = NULL;
param->output_intk = NULL;
param->outprefix = NULL;
param->out_psf_pgm = NULL;
param->out_intk_pgm = NULL;
for(i = 1; i < argc;)
{
if(argv[i] && argv[i][0] == '-')
{
/*Read options that do not need extra arguments*/
if((OptionChar = argv[i][1]) == 0)
{
printf("Invalid parameter format.\n");
return 0;
}
if(argv[i][2])
OptionString = &argv[i][2];
else if(++i < argc)
OptionString = argv[i];
else
{
printf("Invalid parameter format.\n");
return 0;
}
switch(OptionChar)
{
case 's':
param->s = atoi(OptionString);
if(param->s < 1)
{
printf("Invalid superresolution factor.\n");
return 0;
}
break;
case 'k':
param->psf_nx = atoi(OptionString);
param->psf_ny = atoi(OptionString);
if(param->psf_nx <= 0)
{
printf("Invalid PSF support size.\n");
return 0;
}
break;
case 't':
param->threshold = atoi(OptionString);
if((param->threshold != 0) && (param->threshold != 1))
{
printf("Invalid Threshold option.\n");
return 0;
}
break;
case 'o':
param->out_psf_pgm = OptionString;
break;
case 'i':
param->out_intk_pgm = OptionString;
break;
case 'd':
param->outprefix = OptionString;
break;
case '-':
usage(argv[0]);
return 0;
default:
if(isprint(OptionChar))
printf("Unknown option \"-%c\".\n", OptionChar);
else
printf("Unknown option.\n");
return 0;
}
i++;
}
else
{
if(!param->img_zin)
param->img_zin = argv[i];
else if(!param->img_zout)
param->img_zout = argv[i];
else if(!param->output_psf)
param->output_psf = argv[i];
else
param->output_intk = argv[i];
i++;
}
}
if(!param->img_zin || !param->img_zout ||
!param->output_psf || !param->output_intk)
{
usage(argv[0]);
return 0;
}
printf("\n");
printf(" Loaded arguments: \n");
printf(" ----------------- \n");
printf(" Superresolution s : %dx\n",
param->s);
printf(" PSF Support k : %dx%d\n",
param->psf_nx,param->psf_ny);
printf(" PSF Output (PGM) o : %s\n",
param->out_psf_pgm?param->out_psf_pgm:"(no)");
printf(" int-k Output (PGM) i : %s\n",
param->out_intk_pgm?param->out_intk_pgm:"(no)");
printf(" Threshold Non-negative t : %s\n",
param->threshold?"(yes)":"(no)");
printf(" img1 (PGM) : %s\n",
param->img_zin);
printf(" img2 (PGM) : %s\n",
param->img_zout);
printf(" PSF Output (TXT) : %s\n",
param->output_psf);
printf(" int-kernel Output (TXT) : %s\n",
param->output_intk);
printf(" Debug Images (PGM) : %s\n\n",
param->outprefix?param->outprefix:"(no)");
return 1;
}
/**
* @brief main function call
*/
int main(int argc, char *argv[])
{
int nx_zin,ny_zin, nx_zout, ny_zout;
float *img_zin, *img_zout, *h, *k;
program_argums param;
/*Parse command-line arguments*/
if(!parse_arguments(¶m,argc,argv))
{
return EXIT_FAILURE;
}
/* read the first PGM image into data */
if (NULL == (img_zout = read_pgm_float(param.img_zout, &nx_zout,
&ny_zout))) {
fprintf(stderr,
"first image could not be properly read\n");
return EXIT_FAILURE;
}
/* read the second PGM image into data */
if (NULL ==
(img_zin = read_pgm_float(param.img_zin, &nx_zin, &ny_zin)))
{
fprintf(stderr,
"second image could not be properly read\n");
return EXIT_FAILURE;
}
h = (float*)malloc(param.psf_nx * param.psf_ny * sizeof(float));
k = (float*)malloc(param.psf_nx * param.psf_ny * sizeof(float));
/* Call psf_estimation */
two_photos_psf_estim(img_zout,
nx_zout,
ny_zout,
img_zin,
nx_zin,
ny_zin,
param.s,
param.psf_nx,
param.psf_ny,
h,
k,
param.threshold,
param.outprefix);
/*Write the estimated PSF to a text file */
write_ascii_matrix(h, param.psf_nx,
param.psf_ny, param.output_psf);
/*Write the estimated inter kernel to a text file */
write_ascii_matrix(k, param.psf_nx,
param.psf_ny, param.output_intk);
/* Write the estimated PSF to a 8bit-PGM image file if
required
*/
if(param.out_psf_pgm)
{
/* Normalize and truncate negative values */
write_pgm_normalize_float(param.out_psf_pgm,
h, param.psf_nx, param.psf_ny);
}
/* Write the estimated inter image kernel to a 8bit-PGM
image file if required
*/
if(param.out_intk_pgm)
{
/* Normalize and truncate negative values */
write_pgm_normalize_float(param.out_intk_pgm,
k, param.psf_nx, param.psf_ny);
}
/* do the cleanning */
free(h);
free(k);
free(img_zin);
free(img_zout);
return EXIT_SUCCESS;
}