The Flutter Shutter Code Optimizer
 All Files Functions Macros Pages
demo_fluttercode.cpp
Go to the documentation of this file.
1 /*demo_fluttercode.cpp*/
2 /*
3 * Copyright 2014 IPOL Image Processing On Line http://www.ipol.im/
4 *
5 * This program is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation, either version 3 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
17 */
18 
19 
34 #include <stdio.h>
35 #include <stdlib.h>
36 #include <iostream>
37 #include <math.h>
38 #include <fstream>
39 
40 
41 #ifndef ABS
42 
45 #define ABS(x) (((x) > 0) ? (x) : (-(x)))
46 #endif
47 
48 
49 #ifndef M_PI
50 
53 #define M_PI 3.14159265358979323846
54 #endif
55 
58 #include "gain_evaluation.h"
59 #include "optimal_snapshot.h"
60 using namespace std;
61 
79 int main(int argc, char **argv)
80 {
81 //Check arguments : IN OUT;
82  if (argc < 8)
83  {
84  std::cerr << " ***************************************** " << std::endl
85  << " ********** Flutter shutter code calculator ****** " << std::endl
86  << " EXAMPLE: ./flutter_optimizer 0 0.25 52 5 code.txt " << std::endl
87  << " TF_code.txt rendement.txt mean_sdt.txt " << std::endl
88  << " Usage: input : motion _model and parameter******** " << std::endl
89  << " Code_length, Exposure time factor multiplication*** " << std::endl
90  << " code_length exposure_time_factor code_file_name**** " << std::endl
91  << "Fourier_transform_file_name efficency_file_name***** " << std::endl
92  << " efficency_rist_file_name*************************** " << std::endl
93  << " *************************************************** " << std::endl
94  << " ************* Yohann Tendero, 2014 ************** " << std::endl
95  << " *************************************************** " << std::endl;
96  return 1;
97  }
98 
99 
115 
120  int flag_motion_model=atoi(argv[1]);
122  double motion_model_parameter=ABS(atof(argv[2]));
126  int code_length=atoi(argv[3]); // length of the code L
127  double exposure_factor=atof(argv[4]);// tune the support of the
128  // flutter shutter function as a factor of the optimal snapshot
129  // exposure time.
130  double* code = new double[code_length]; //to store the flutter shutter code
131  const int num_plot_points=300; // number of points for plots
132  double T_star; // to store the optimal exposure time (std camera)
133  double velocity_max=motion_model_parameter;// maximum velocity.
134  if (flag_motion_model==0) velocity_max=4*motion_model_parameter;
135 
136  // storing files names for output.
137  char* code_file_name=argv[5];
138  char* TF_code_file_name=argv[6];
139  char* SNR_comparison_file_name=argv[7];
140  char* mean_sdt_file_name=argv[8];
141 
142 
147 
148  T_star=optimal_snapshot(flag_motion_model,
149  motion_model_parameter);
150  double deltat=exposure_factor*T_star/code_length;
151 
152 
153  if (deltat*velocity_max>1)
154  {
155  std::cerr << " Deltat too big! convergence not guaranteed"
156  << std::endl;
157  std::cerr << " Increase the code length L." << std::endl;
158  }
159 
164 
165 
166  if (flag_motion_model==0) //Gaussian motion model
167  {
168  gaussian_optimiser(code,motion_model_parameter,code_length,deltat);
169  }
170  if (flag_motion_model==1) //Uniform motion model
171  {
172  uniform_optimiser(code,motion_model_parameter,code_length,deltat);
173  }
174 
175  //Recall that at this point the code is normalized so that \int\alpha(t)dt=1
176 
177 
178 
179 
185 
186 
188  ofstream file_code(code_file_name, ios::out | ios::trunc);
189  // opening the file (erase if previous exists)
190  if (file_code)
191  {
192  for (int k=0; k<code_length; k++)
193  {
194  file_code << code[k] <<";" << endl;
195  }
196 
197  file_code.close();
198  }
199  else
200  cerr << "Unable to open file !" << endl;
201 
207 
208 
209 
210 
213  double w_0=0;
214  if (flag_motion_model==0) w_0=gaussian_w(motion_model_parameter,0);
215  if (flag_motion_model==1) w_0=uniform_w(motion_model_parameter,0);
216  // at this point w_0 contains \W(0).
217 
218 
219  ofstream file_TFcode(TF_code_file_name, ios::out | ios::trunc);
220  if (file_TFcode)
221  {
222  if (flag_motion_model==0) // Gaussian motion model
223  for (double xi=-2*velocity_max*M_PI; xi<=2*velocity_max*M_PI;
224  xi=xi+4*velocity_max*M_PI/num_plot_points)
225  {
226  file_TFcode << xi
227  << " "
228  << pow(gaussian_w(motion_model_parameter, xi)/w_0,0.25)
229  <<" "
230  << abs_hat_alpha(code, code_length, xi,deltat) << endl;
231  }
232  if (flag_motion_model==1) // Uniform motion model
233  for (double xi=-2*velocity_max*M_PI; xi<=2*velocity_max*M_PI;
234  xi=xi+4*velocity_max*M_PI/num_plot_points)
235  {
236  file_TFcode << xi
237  << " "
238  << pow(uniform_w(motion_model_parameter, xi)/w_0,0.25)
239  <<" "
240  << abs_hat_alpha(code, code_length, xi,deltat) << endl;
241  }
242 
243 
244  file_TFcode.close();
245  }
246  else
247  cerr << "Unable to open file !" << endl;
248 
253  // Compute the relative efficiency, the relative efficiency on average,
254  // the std_dev_gain_sigma and store them.
255 
256  int i=0;
257  double* average_gain_RMSE = new double[num_plot_points];
258  double* average_gain_mu = new double[1];
259  double* std_dev_gain_sigma = new double[1];
260  if (flag_motion_model==0) velocity_max=4*motion_model_parameter;
261 
262  average_gain_in_terms_of_RMSE( code, code_length, T_star,
263  flag_motion_model, motion_model_parameter,
264  deltat,num_plot_points,
265  average_gain_RMSE,
266  average_gain_mu);
267 
268  std_dev_gain_in_terms_of_RMSE(flag_motion_model,
269  motion_model_parameter,
270  num_plot_points,
271  average_gain_RMSE,
272  average_gain_mu,
273  std_dev_gain_sigma);
274 
275  ofstream file_comparison_file_name(SNR_comparison_file_name,
276  ios::out | ios::trunc);
277 // opening the file (erase if previous exists)
278  if (file_comparison_file_name)
279  {
280  double velocity=-velocity_max;
281  for (i=0; i<num_plot_points; i++)
282  {
283  file_comparison_file_name << velocity
284  << " "
285  << average_gain_RMSE[i]
286  << " "
287  << average_gain_mu[0]
288  << " "
289  << probability_density(velocity, flag_motion_model,
290  motion_model_parameter)
291  << " "
292  << "1"
293  << endl;
294  velocity=velocity+2*velocity_max/num_plot_points;
295 
296  }
297 
298  file_comparison_file_name.close();
299  }
300  else
301  cerr << "Unable to open file !" << endl;
302 
303  printf("Maximum blur support for optimal snapshot \
304 (\\Delta t^* |velocity_{max}|) in pixel(s): %f\n",
305  T_star*velocity_max);
306 
307 
308 
309 
310  ofstream file_mean_sdt_file_name(mean_sdt_file_name, ios::out | ios::trunc);
311 // opening the file (erase if previous exists)
312  if (file_mean_sdt_file_name)
313  {
314 
315  file_mean_sdt_file_name
316  << "Gain with respect to the optimal snapshot \
317 in terms of RMSE, summary" << std::endl
318  << "*average gain: " <<
319  average_gain_mu[0] << std::endl
320  <<"*Standard deviation: " << std_dev_gain_sigma[0] << endl;
321  file_mean_sdt_file_name.close();
322  }
323  else
324  cerr << "Unable to open file !" << endl;
325 
326  delete [] code;
327  delete [] average_gain_RMSE;
328  delete [] average_gain_mu;
329  delete [] std_dev_gain_sigma;
330  return 0;
331 }
332 
333 
334 
335 
void average_gain_in_terms_of_RMSE(double *code, int code_length, double tilde_deltat_star, int flag_motion_model, double motion_model_parameter, double deltat, int num_plot_points, double *G_of_v, double *average_gain_mu)
This function implements equation (24): := G(v) (v) dv.
double optimal_snapshot(int flag_motion_model, double motion_model_parameter)
Given a motion model compute the optimal exposure time for a snaphot. The minimization is done by sca...
void gaussian_optimiser(double *code, double sigma, int code_length, double deltat)
Computes the a_k coefficients of the code; gaussian motion model.
double gaussian_w(double sigma, double xi)
Computes the W() function; gaussian motion model. This is equation (15): W():={}^{4{ N}} ({{-v^2}{2{ ...
void uniform_optimiser(double *code, double velocity_max, int code_length, double deltat)
Functions for the uniform motion model:
double probability_density(double velocity, int flag_motion_model, double motion_model_parameter)
Given a velocity velocity, the motion model and its parameters the function computes the probability...
#define ABS(x)
double abs_hat_alpha(double *code, int code_length, double xi, double deltat)
Given a code compute the modulus of the Fourier transform of the Flutter-Shutter function, see equation (2): ()= t {sinc}({ t}{2}) e^{-{i t}{2}} {k=0}^{L-1} e^{-ik t}.
#define M_PI
double uniform_w(double velocity_max, double xi)
Compute the weight function () for a uniform velocity.
void std_dev_gain_in_terms_of_RMSE(int flag_motion_model, double motion_model_parameter, int num_plot_points, double *G_of_v, double *average_gain_mu, double *std_dev_gain_sigma)
This function implements equation (25): :={ |G(v)- G(u) (u) du|^2 (v) dv}.
int main(int argc, char **argv)
main function call