@@ -41,7 +41,7 @@ Vec2d ResponseCalib::rmse(const double *G, const double *E, const std::vector<do
41
41
}
42
42
}
43
43
44
- // return Vec2d((double)( 1e5 * sqrtl((e/num) )), (double)num);
44
+ // return Eigen::Vector2d( 1e5* sqrtl((e/num)), (double)num);
45
45
return Vec2d ((double )(1e5 *sqrt ((e/num))), (double )num);
46
46
}
47
47
@@ -89,8 +89,10 @@ void ResponseCalib::plotE(const double* E, int w, int h, const std::string &save
89
89
90
90
if (!saveTo.empty ())
91
91
{
92
- cv::imwrite (saveTo + " .png" , EImg);
93
- cv::imwrite (saveTo + " 16.png" , EImg16);
92
+ imwrite (saveTo + " .png" , EImg);
93
+ std::cout<<" Saved: " <<saveTo + " .png" <<std::endl;
94
+ imwrite (saveTo + " -16.png" , EImg16);
95
+ std::cout<<" Saved: " <<saveTo + " -16.png" <<std::endl;
94
96
}
95
97
}
96
98
@@ -120,6 +122,7 @@ void ResponseCalib::plotG(const double* G, const std::string &saveTo)
120
122
std::cout<<" Inv. Response " <<min<<" - " <<max<<std::endl;
121
123
cv::imshow (" G" , GImg);
122
124
if (!saveTo.empty ()) cv::imwrite (saveTo, GImg*255 );
125
+ std::cout<<" Saved: " <<saveTo<<std::endl;
123
126
}
124
127
125
128
void ResponseCalib::calib ()
@@ -135,23 +138,33 @@ void ResponseCalib::calib()
135
138
cv::Mat img = imageReader->getImage (i);
136
139
if (img.rows ==0 || img.cols ==0 ) continue ;
137
140
CV_Assert (img.type () == CV_8U);
141
+
142
+ if ((w!=0 && w != img.cols ) || img.cols ==0 )
143
+ {
144
+ std::cout<<" Width mismatch!" <<std::endl;
145
+ exit (1 );
146
+ }
147
+ if ((h!=0 && h != img.rows ) || img.rows ==0 )
148
+ { std::cout<<" Height mismatch!" <<std::endl;
149
+ exit (1 );
150
+ }
138
151
w = img.cols ;
139
152
h = img.rows ;
140
153
141
- uchar *data = new uchar[w* h];
142
- memcpy (data, img.data , w* h);
154
+ uchar *data = new uchar[w * h];
155
+ memcpy (data, img.data , w * h);
143
156
dataVec.push_back (data);
144
157
exposureDurationVec.push_back ((double )(imageReader->getExposureDuration (i)));
145
- unsigned char * data2 = new unsigned char [w*h];
146
158
159
+ unsigned char * data2 = new unsigned char [w * h];
147
160
for (int j = 0 ; j < _leakPadding; ++j)
148
161
{
149
- memcpy (data2, data, w* h);
162
+ memcpy (data2, data, w * h);
150
163
for (int y = 1 ; y < h - 1 ; ++y)
151
164
{
152
165
for (int x = 1 ; x < w - 1 ; ++x)
153
166
{
154
- if (data[x+y* w] == 255 )
167
+ if (data[x + y * w] == 255 )
155
168
{
156
169
data2[x+1 + w*(y+1 )] = 255 ;
157
170
data2[x+1 + w*(y )] = 255 ;
@@ -167,11 +180,13 @@ void ResponseCalib::calib()
167
180
}
168
181
}
169
182
}
170
- memcpy (data, data2, w* h);
183
+ memcpy (data, data2, w * h);
171
184
}
172
185
delete[] data2;
173
186
}
174
187
n = dataVec.size ();
188
+ std::cout<<" Loaded " <<n<<" images!" <<std::endl;
189
+ std::cout<<" Response calibration begin!" <<std::endl;
175
190
176
191
double * E = new double [w*h]; // scene irradiance
177
192
double * En = new double [w*h]; // scene irradiance
@@ -194,21 +209,27 @@ void ResponseCalib::calib()
194
209
for (int k=0 ;k<w*h;k++)
195
210
E[k] = E[k]/En[k];
196
211
197
- // CV_Assert(system("rm -rf photoCalibResult") != -1 && system("mkdir photoCalibResult") != -1);
212
+ // TODO: System independent folder creating
213
+ // Only on Linux for now.
214
+ if (-1 == system (" rm -rf photoCalibResult" ))
215
+ std::cout<<" could not delete old photoCalibResult folder!" <<std::endl;
216
+ if (-1 == system (" mkdir photoCalibResult" ))
217
+ std::cout<<" could not create photoCalibResult folder!" <<std::endl;
198
218
199
219
std::ofstream logFile;
200
- logFile.open (" log.txt" , std::ios::trunc | std::ios::out);
220
+ logFile.open (" photoCalibResult/ log.txt" , std::ios::trunc | std::ios::out);
201
221
logFile.precision (15 );
202
222
203
- std::cout<<" Initial RMSE = " <<rmse (G, E, exposureDurationVec, dataVec, w*h)[0 ]<<std::endl;
204
- plotE (E,w, h, " E-0" );
223
+ std::cout<<" Initial RMSE = " <<rmse (G, E, exposureDurationVec, dataVec, w*h)[0 ] << " ! " <<std::endl;
224
+ plotE (E, w, h, " photoCalibResult/ E-0" );
205
225
cv::waitKey (100 );
206
226
207
227
bool optE = true ;
208
228
bool optG = true ;
209
229
210
230
for (int it=0 ;it<_nIts;it++)
211
231
{
232
+ std::cout<<" Iteration " <<it+1 <<" ..." <<std::endl;
212
233
if (optG)
213
234
{
214
235
// optimize log inverse response function.
@@ -235,14 +256,10 @@ void ResponseCalib::calib()
235
256
delete[] GNum;
236
257
printf (" optG RMSE = %f! \t " , rmse (G, E, exposureDurationVec, dataVec, w*h )[0 ]);
237
258
238
- char buf[1000 ]; snprintf (buf, 1000 , " G-%d .png" , it+1 );
259
+ char buf[1000 ]; snprintf (buf, 1000 , " photoCalibResult/ G-%02d .png" , it+1 );
239
260
plotG (G, buf);
240
261
}
241
262
242
-
243
-
244
-
245
-
246
263
if (optE)
247
264
{
248
265
// optimize scene irradiance function.
@@ -270,20 +287,20 @@ void ResponseCalib::calib()
270
287
delete[] ESum;
271
288
printf (" OptE RMSE = %f! \t " , rmse (G, E, exposureDurationVec, dataVec, w*h )[0 ]);
272
289
273
- char buf[1000 ]; snprintf (buf, 1000 , " photoCalibResult/E-%d " , it+1 );
290
+ char buf[1000 ]; snprintf (buf, 1000 , " photoCalibResult/E-%02d " , it+1 );
274
291
plotE (E,w,h, buf);
275
292
}
276
293
277
-
278
294
// rescale such that maximum response is 255 (fairly arbitrary choice).
279
295
double rescaleFactor=255.0 / G[255 ];
280
296
for (int i=0 ;i<w*h;i++)
281
297
{
282
298
E[i] *= rescaleFactor;
283
299
if (i<256 ) G[i] *= rescaleFactor;
284
300
}
301
+ // Eigen::Vector2d err = rmse(G, E, exposureVec, dataVec, w*h );
285
302
Vec2d err = rmse (G, E, exposureDurationVec, dataVec, w*h );
286
- printf (" resc RMSE = %f! \t rescale with %f!\n " , err[0 ], rescaleFactor);
303
+ printf (" Rescaled RMSE = %f! \t rescale with %f!\n " , err[0 ], rescaleFactor);
287
304
288
305
logFile << it << " " << n << " " << err[1 ] << " " << err[0 ] << " \n " ;
289
306
@@ -294,19 +311,26 @@ void ResponseCalib::calib()
294
311
logFile.close ();
295
312
296
313
std::ofstream lg;
297
- lg.open (" photoCalibResult/pcalib.txt" , std::ios::trunc | std::ios::out);
314
+ lg.open (" photoCalibResult/pcalib.yaml" , std::ios::trunc | std::ios::out);
315
+ lg << " %YAML:1.0\n gamma: [" ;
298
316
lg.precision (15 );
299
- for (int i=0 ;i<256 ;i++)
300
- lg << G[i] << " " ;
317
+ for (int i=0 ;i<255 ;i++)
318
+ lg << G[i] << " , " ;
319
+ lg << G[255 ] << ' ]' ;
301
320
lg << " \n " ;
302
321
303
322
lg.flush ();
304
323
lg.close ();
305
324
325
+ std::cout<<" pcalib file has been saved to: photoCalibResult/pcalib.yaml" <<std::endl;
326
+
306
327
delete[] E;
307
328
delete[] En;
308
329
delete[] G;
309
- for (size_t i=0 ;i<n;i++) delete[] dataVec[i];
330
+ for (size_t i=0 ;i<n;i++)
331
+ delete[] dataVec[i];
332
+
333
+ std::cout<<" Camera response function calibration finished!" <<std::endl;
310
334
}
311
335
312
336
}} // namespace photometric_calib, cv
0 commit comments