Skip to content

Commit 1cda043

Browse files
committed
Add useful std output for response calibration.
1 parent 62f76a4 commit 1cda043

File tree

1 file changed

+49
-25
lines changed

1 file changed

+49
-25
lines changed

modules/photometric_calib/src/ResponseCalib.cpp

Lines changed: 49 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ Vec2d ResponseCalib::rmse(const double *G, const double *E, const std::vector<do
4141
}
4242
}
4343

44-
//return Vec2d((double)(1e5 * sqrtl((e/num))), (double)num);
44+
//return Eigen::Vector2d(1e5*sqrtl((e/num)), (double)num);
4545
return Vec2d((double)(1e5 *sqrt((e/num))), (double)num);
4646
}
4747

@@ -89,8 +89,10 @@ void ResponseCalib::plotE(const double* E, int w, int h, const std::string &save
8989

9090
if(!saveTo.empty())
9191
{
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;
9496
}
9597
}
9698

@@ -120,6 +122,7 @@ void ResponseCalib::plotG(const double* G, const std::string &saveTo)
120122
std::cout<<"Inv. Response "<<min<<" - "<<max<<std::endl;
121123
cv::imshow("G", GImg);
122124
if(!saveTo.empty()) cv::imwrite(saveTo, GImg*255);
125+
std::cout<<"Saved: "<<saveTo<<std::endl;
123126
}
124127

125128
void ResponseCalib::calib()
@@ -135,23 +138,33 @@ void ResponseCalib::calib()
135138
cv::Mat img = imageReader->getImage(i);
136139
if(img.rows==0 || img.cols==0) continue;
137140
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+
}
138151
w = img.cols;
139152
h = img.rows;
140153

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);
143156
dataVec.push_back(data);
144157
exposureDurationVec.push_back((double)(imageReader->getExposureDuration(i)));
145-
unsigned char* data2 = new unsigned char[w*h];
146158

159+
unsigned char* data2 = new unsigned char[w * h];
147160
for (int j = 0; j < _leakPadding; ++j)
148161
{
149-
memcpy(data2, data, w*h);
162+
memcpy(data2, data, w * h);
150163
for (int y = 1; y < h - 1; ++y)
151164
{
152165
for (int x = 1; x < w - 1; ++x)
153166
{
154-
if(data[x+y*w] == 255)
167+
if(data[x + y * w] == 255)
155168
{
156169
data2[x+1 + w*(y+1)] = 255;
157170
data2[x+1 + w*(y )] = 255;
@@ -167,11 +180,13 @@ void ResponseCalib::calib()
167180
}
168181
}
169182
}
170-
memcpy(data, data2, w*h);
183+
memcpy(data, data2, w * h);
171184
}
172185
delete[] data2;
173186
}
174187
n = dataVec.size();
188+
std::cout<<"Loaded "<<n<<" images!"<<std::endl;
189+
std::cout<<"Response calibration begin!"<<std::endl;
175190

176191
double* E = new double[w*h]; // scene irradiance
177192
double* En = new double[w*h]; // scene irradiance
@@ -194,21 +209,27 @@ void ResponseCalib::calib()
194209
for(int k=0;k<w*h;k++)
195210
E[k] = E[k]/En[k];
196211

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;
198218

199219
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);
201221
logFile.precision(15);
202222

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");
205225
cv::waitKey(100);
206226

207227
bool optE = true;
208228
bool optG = true;
209229

210230
for(int it=0;it<_nIts;it++)
211231
{
232+
std::cout<<"Iteration "<<it+1<<"..."<<std::endl;
212233
if(optG)
213234
{
214235
// optimize log inverse response function.
@@ -235,14 +256,10 @@ void ResponseCalib::calib()
235256
delete[] GNum;
236257
printf("optG RMSE = %f! \t", rmse(G, E, exposureDurationVec, dataVec, w*h )[0]);
237258

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);
239260
plotG(G, buf);
240261
}
241262

242-
243-
244-
245-
246263
if(optE)
247264
{
248265
// optimize scene irradiance function.
@@ -270,20 +287,20 @@ void ResponseCalib::calib()
270287
delete[] ESum;
271288
printf("OptE RMSE = %f! \t", rmse(G, E, exposureDurationVec, dataVec, w*h )[0]);
272289

273-
char buf[1000]; snprintf(buf, 1000, "photoCalibResult/E-%d", it+1);
290+
char buf[1000]; snprintf(buf, 1000, "photoCalibResult/E-%02d", it+1);
274291
plotE(E,w,h, buf);
275292
}
276293

277-
278294
// rescale such that maximum response is 255 (fairly arbitrary choice).
279295
double rescaleFactor=255.0 / G[255];
280296
for(int i=0;i<w*h;i++)
281297
{
282298
E[i] *= rescaleFactor;
283299
if(i<256) G[i] *= rescaleFactor;
284300
}
301+
//Eigen::Vector2d err = rmse(G, E, exposureVec, dataVec, w*h );
285302
Vec2d err = rmse(G, E, exposureDurationVec, dataVec, w*h );
286-
printf("resc RMSE = %f! \trescale with %f!\n", err[0], rescaleFactor);
303+
printf("Rescaled RMSE = %f! \trescale with %f!\n", err[0], rescaleFactor);
287304

288305
logFile << it << " " << n << " " << err[1] << " " << err[0] << "\n";
289306

@@ -294,19 +311,26 @@ void ResponseCalib::calib()
294311
logFile.close();
295312

296313
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\ngamma: [";
298316
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] << ']';
301320
lg << "\n";
302321

303322
lg.flush();
304323
lg.close();
305324

325+
std::cout<<"pcalib file has been saved to: photoCalibResult/pcalib.yaml"<<std::endl;
326+
306327
delete[] E;
307328
delete[] En;
308329
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;
310334
}
311335

312336
}} // namespace photometric_calib, cv

0 commit comments

Comments
 (0)