-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpsf_estim_main.c
277 lines (222 loc) · 7.98 KB
/
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
/*----------------------------------------------------------------------------
"Point Spread Function Estimation from a Random Target"
Copyright 2010-2011 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 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 "psf_estim.h"
/** @brief Struct of program parameters */
typedef struct
{
int s;
int psf_nx;
int psf_ny;
char *pattern_pgm;
char *out_pgm;
char *input;
char *output;
char *detected_ppm;
int solver;
} program_argums;
/** @brief Error/Exit print a message and exit.
* @param msg
*/
static void error(char *msg)
{
fprintf(stderr, "PSF_ESTIM Error: %s\n", msg);
exit(EXIT_FAILURE);
}
static void usage(const char* name)
{
printf("Point Spread Function Estimation from a Random Target\n");
printf("Copyright M.Delbracio, P.Muse, A.Almansa. ");
printf("Version 1.3 - 26 December 2011\n\n");
printf("Usage: %s [options] <input file> <output file>\n\n"
"Only PGM 16/8 bits images are supported.\n\n",name);
printf("Options:\n");
printf(" -s <number> The super-resolution factor, positive integer");
printf(" (default 4)\n");
printf(" -k <number> PSF support size (default 4s + 1)\n");
printf(" -o <file> Estimated PSF save to a 8bits PGM image \n");
printf(" -p <file> Noise Pattern PGM image \n");
printf(" -d <file> Save the input image with detected pattern marked");
printf(" (24bits PPM)\n");
printf(" -t <0,1,2> Least Squares solver:\n");
printf(" 0 - Least Squares\n");
printf(" 1 - Least Squares + thresholding (default)\n");
printf(" 2 - Non-negative least Squares (slowest)\n");
}
static void parse_arguments(program_argums *param, int argc, char *argv[])
{
char *OptionString;
char OptionChar;
int i;
if(argc < 2)
{
usage(argv[0]);
exit(EXIT_SUCCESS);
}
/* loop to read parameters*/
for(i = 1; i < argc;)
{
if(argv[i] && argv[i][0] == '-')
{
if((OptionChar = argv[i][1]) == 0)
{
error("Invalid parameter format.\n");
}
if(argv[i][2])
OptionString = &argv[i][2];
else if(++i < argc)
OptionString = argv[i];
else
{
error("Invalid parameter format.\n");
}
switch(OptionChar)
{
case 's':
param->s = atoi(OptionString);
if(param->s < 1)
{
error("Invalid superresolution factor.\n");
}
break;
case 'k':
param->psf_nx = atoi(OptionString);
param->psf_ny = atoi(OptionString);
if(param->psf_nx <= 0)
{
error("Invalid PSF support size.\n");
}
break;
case 't':
param->solver = atoi(OptionString);
if(param->solver != 0 &&
param->solver != 1 &&
param->solver != 2)
{
error("t must be 0, 1 or 2.\n");
}
break;
case 'o':
param->out_pgm = OptionString;
break;
case 'd':
param->detected_ppm = OptionString;
break;
case 'p':
param->pattern_pgm = OptionString;
break;
case '-':
usage(argv[0]);
exit(EXIT_FAILURE);
default:
if(isprint(OptionChar))
{
fprintf(stderr, "Unknown option \"-%c\".\n",
OptionChar);
exit(EXIT_FAILURE);
} else
error("Unknown option.\n");
}
}
else
{
if(!param->input)
param->input = argv[i];
else
param->output = argv[i];
}
i++;
}
if(!param->input || !param->output)
{
usage(argv[0]);
exit(EXIT_FAILURE);
}
/* If parameters weren't set, set deafult parameters*/
param->s = param->s>0 ? param->s : 4;
param->psf_nx = param->psf_nx>0 ? param->psf_nx : 4*param->s+1;
param->psf_ny = param->psf_ny>0 ? param->psf_ny : 4*param->s+1;
param->solver = param->solver>=0 ? param->solver : 1;
param->pattern_pgm = param->pattern_pgm ?
param->pattern_pgm : "pattern_noise.pgm";
/*Display selected parameters*/
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_pgm?param->out_pgm:"(no)");
printf(" Pattern (PGM) p : %s\n",
param->pattern_pgm?param->pattern_pgm:"(no)");
printf(" Detected Pattern (PPM) d : %s\n",
param->detected_ppm?param->detected_ppm:"(no)");
printf(" LS Solver t : %d\n",param->solver);
printf(" PGM input : %s\n",param->input);
printf(" PSF Output (TXT) : %s\n\n",param->output);
}
/**
* @brief main function call
*/
int main(int argc, char *argv[])
{
int nx,ny, pat_nx,pat_ny;
float *in, *img_pattern, *x;
/*Initialize the structure param->* to -1 or null */
program_argums param = {-1, -1, -1, NULL, NULL, NULL, NULL, NULL, -1};
/*Parse command-line arguments*/
parse_arguments(¶m,argc,argv);
/* read the PGM image into data */
if (NULL == (in = read_pgm_float(param.input, &nx, &ny))) {
fprintf(stderr, "the image could not be properly read\n");
return EXIT_FAILURE;
}
/* read the PGM image pattern*/
if (NULL ==
(img_pattern = read_pgm_float(param.pattern_pgm, &pat_nx, &pat_ny)))
{
fprintf(stderr, "the pattern image could not be properly read\n");
return EXIT_FAILURE;
}
/* Call psf_estimation */
x = psf_estim(in, nx, ny, img_pattern, pat_nx, pat_ny,
param.s, param.psf_nx, param.psf_ny, param.solver,
param.detected_ppm);
/* Write the estimated PSF to a text file */
write_ascii_matrix(x, param.psf_nx, param.psf_ny, param.output);
/* Write the estimated PSF to a 8bit-PGM image file if required*/
if(param.out_pgm)
{
/* Normalize and truncate negative values */
write_pgm_normalize_float(param.out_pgm, x, param.psf_nx,
param.psf_ny);
}
/* do the cleaning */
free(x);
free(in);
free(img_pattern);
return EXIT_SUCCESS;
}