The Flutter Shutter Code Optimizer
 All Files Functions Macros Pages
optimal_snapshot.cpp
Go to the documentation of this file.
1 /*optimal_snapshot.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 */
30 #include <math.h>
31 #include "optimal_snapshot.h"
32 
33 
34 #define eps 0.001//epsilon definition, for integral evaluation.
35 
36 
37 #ifndef ABS
38 
41 #define ABS(x) (((x) > 0) ? (x) : (-(x)))
42 #endif
43 
44 #ifndef M_PI
45 
48 #define M_PI 3.14159265358979323846
49 #endif
50 
51 
52 
53 
54 
55 
61 
78 double optimal_snapshot(int flag_motion_model,
79  double motion_model_parameter)
80 {
82  //initialization
83  double T_star=0; //$T^*$ in the paper.
84  double optimal_Energy=optimal_snapshot_energy(0, flag_motion_model,
85  motion_model_parameter);
86  double current_Energy=optimal_Energy;
87  double velocity_max=motion_model_parameter;
89  if (flag_motion_model==0) velocity_max=4*motion_model_parameter;
90 
91 
92  // main loop for enegy minimization : scanning on \Deltat values
93  // see algo 1, step 1. (Note that velocity_max is positive.)
94  for (double T=(2/velocity_max)/100; //start : \frac{0.02}{v_{max}}
95  T<1.999/velocity_max; //stop \frac{1.999}{v_{max}}
96  T=T+(2/velocity_max)/100)// step \frac{0.02}{v_{max}}
97  {
98  current_Energy=optimal_snapshot_energy(T,
99  flag_motion_model,
100  motion_model_parameter);
101  if (current_Energy<optimal_Energy)
102  {
103  optimal_Energy=current_Energy;
104  T_star=T;
105  }
106  }
107  return(T_star);
108 
109 }
110 
111 
112 
113 
118 
129 double optimal_snapshot_energy(double T, int flag_motion_model,
130  double motion_model_parameter)
131 {
133 
134  double velocity_max=motion_model_parameter;
135  if (flag_motion_model==0) velocity_max=4*motion_model_parameter;
136  // flag_motion_model=0 if the motion model is (truncated) Gaussian.
137 
140 
142  double a=0;// Using parity.
143  double b=velocity_max;
144  double h=(b-a)/2;
145  double s1=optimal_snapshot_integrand(a,T)*
146  proba_motion_model(a,motion_model_parameter,
147  flag_motion_model)+
149  proba_motion_model(b,motion_model_parameter,
150  flag_motion_model);
151  double s2=0;
152  double s4=optimal_snapshot_integrand(a+h,T)*
153  proba_motion_model(a+h,motion_model_parameter,
154  flag_motion_model);
155  double tn=h*(s1+4*s4)/3;
156  double ta=tn+eps*tn;
157  int zh=2;
158  int j;
159 
160 
162  while (ABS(ta-tn)>eps*ABS(tn))
163  {
164  ta=tn;
165  zh=2*zh;
166  h=h/2;
167  s2=s2+s4;
168  s4=0;
169  j=1;
170  while (j<=zh)
171  {
172  s4=s4+optimal_snapshot_integrand(a+j*h,T)*
173  proba_motion_model(a+j*h,motion_model_parameter,
174  flag_motion_model);
175  j=j+2;
176  }
177  tn = h*(s1+2*s2+4*s4)/3;
178  }
179  return(tn);
180 
181 }
182 
183 
189 
200 double optimal_snapshot_integrand(double velocity, double T)
201 {
202 
205  double a=0;
206  double b=M_PI;
207  double h=(b-a)/2;
208  double s1=optimal_snapshot_integrand_part2(a,velocity,T)+
209  optimal_snapshot_integrand_part2(b,velocity,T);
210  double s2=0;
211  double s4=optimal_snapshot_integrand_part2(a+h,velocity,T);
212  double tn=h*(s1+4*s4)/3;
213  double ta=tn+eps*tn;
214  int zh=2;
215  int j;
216 
218  while (ABS(ta-tn)>eps*ABS(tn))
219  {
220  ta=tn;
221  zh=2*zh;
222  h=h/2;
223  s2=s2+s4;
224  s4=0;
225  j=1;
226  while (j<=zh)
227  {
228  s4=s4+optimal_snapshot_integrand_part2(a+j*h,velocity,T);
229  j=j+2;
230  }
231  tn = h*(s1+2*s2+4*s4)/3;
232  }
233  return(2*tn);
234 }
235 
236 
237 
243 
253 double optimal_snapshot_integrand_part2(double xi, double velocity,
254  double T)
255 {
256  double xivT_2=xi*velocity*T/2;
257  double sinc=1;
258  if (xi*velocity!=0)
259  {
260  sinc=sin(xivT_2)/xivT_2;
261  }
262  if (T!=0 && sinc!=0)
263  {
264  return(1/(T*sinc*sinc));
265  }
266  else
267  {
268  return(99999); //Dummy case, called if deltat=0 (the energy is infinite)
269  }
270 
271 }
272 
278 
290 double proba_motion_model(double velocity, double motion_model_parameter,
291  int flag_motion_model)
292 {
293 
294  if (flag_motion_model==0)
295  return(exp(-velocity*velocity/
296  (2*motion_model_parameter*motion_model_parameter)));
299  else
300  return(1/(2*motion_model_parameter));
301 }
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...
double optimal_snapshot_energy(double T, int flag_motion_model, double motion_model_parameter)
Given a motion model computes the energy of Equation (20) for a given Tt.
#define ABS(x)
double proba_motion_model(double velocity, double motion_model_parameter, int flag_motion_model)
Given a motion model compute the probability of the velocity velocity ((velocity)) ...
#define eps
double optimal_snapshot_integrand(double velocity, double T)
Given a velocity v and T this function returns the integral in of equation (20), i...
double optimal_snapshot_integrand_part2(double xi, double velocity, double T)
This function implements a part of the integrand in (20).
#define M_PI