-
Notifications
You must be signed in to change notification settings - Fork 16
/
main.cpp
667 lines (591 loc) · 25.7 KB
/
main.cpp
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
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
// modified OpenCV example
#include "opencv2/calib3d/calib3d.hpp"
#include "opencv2/highgui/highgui.hpp"
#include "opencv2/imgproc/imgproc.hpp"
#include <vector>
#include <string>
#include <algorithm>
#include <iostream>
#include <iomanip>
#include <iterator>
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
using namespace cv;
using namespace std;
static int print_help()
{
cout <<
" Given a list of chessboard images, the number of corners (nx, ny) \n"
" on the chessboards, and a flag: useCalibrated with possible values: \n"
" 0 : calibrated or \n"
" 1 : uncalibrated, use cvStereoCalibrate() or \n"
" 2 : uncalibrated, compute fundamental matrix separately \n"
" Calibrate the cameras and display the rectified results along with the \n"
" computed disparity images. \n\n"
" Parameters: \n"
" -w <chesboard_width> : chessboard width (needed) \n"
" -h <chesboard_height> : chessboard height (needed) \n"
" --norect : if used, don't compute and display rectification \n"
" --webcam <w1> <w2> : use input images from webcams, with interactive \n"
" program to get the images when the chessboard pattern is found. \n"
" with w1: the number of the first webcam to use \n"
" w2: the number of the second webcam to use \n"
" --image-list <file>: the XML/YML file containing the list of images, \n"
" if we don't use webcams \n"
" --input-dir <dir>: the directory containing the images\n"
" (they will be read in alphabetical order)\n"
" --save: save the images used for calibration in a folder\n" << endl;
return 0;
}
enum INPUT_MODE {
NONE, // program will fail
WEBCAM, // use webcam to get images
IMAGE_LIST, // use XML/YML image list file
INPUT_DIR // a directory that contains images
};
class Settings
{
public:
Size boardSize; // the size of the chessboard
bool showRectified = true; // display rectification ?
bool useCalibrated = true;
INPUT_MODE inputMode = NONE; // input mode for images
string imageListFilename; // only if input mode is an image list file (--image-list)
string inputDir; // directory to read images from, only if input mode is --input-dir
int webcam[2]; // the number of the webcams (only if input mode is webcam)
VideoCapture capture[2]; // the VideoCapture to access the webcam streams
int imageNumber = 10; // number of images to get (for each cam) if input mode is webcam
bool save = false; // save the calibration pictures
string images_output_folder = "images_calib"; // folder where to save the pictures
// WARNING: images will not be saved if the images_output_folder doesn't exist,
// it must be created by the user
};
static bool readStringList( const string& filename, vector<string>& l );
static void getImagesFromWebcams(Settings settings, vector<Mat>& images);
static void getImagesFromImageList(Settings settings, vector<Mat>& images);
static void getImagesFromInputDir(Settings settings, vector<Mat>& images);
static void StereoCalib(const vector<Mat>& images, Settings settings);
int main(int argc, char** argv)
{
Settings settings;
// we parse the arguments to get board size and image list XML/YML file
for( int i = 1; i < argc; i++ )
{
// chessboard width parameter
if( string(argv[i]) == "-w" )
{
if ((i+1) == argc) {
cout << "missing board width after -w" << endl;
return print_help();
}
if( sscanf(argv[++i], "%d", &settings.boardSize.width) != 1 || settings.boardSize.width <= 0 )
{
cout << "invalid board width" << endl;
return print_help();
}
}
// chessboard height parameter
else if( string(argv[i]) == "-h" )
{
if ((i+1) == argc) {
cout << "missing board height after -h" << endl;
return print_help();
}
else if( sscanf(argv[++i], "%d", &settings.boardSize.height) != 1 || settings.boardSize.height <= 0 )
{
cout << "invalid board height" << endl;
return print_help();
}
}
// display rectification ?
else if( string(argv[i]) == "--norect" )
settings.showRectified = false;
// input mode: webcam or image list
else if (string(argv[i]) == "--image-list" ) {
if ((i+1) >= argc) {
cout << "missing image list filename after --image-list" << endl;
return print_help();
}
settings.imageListFilename = argv[++i];
settings.inputMode = IMAGE_LIST;
}
else if (string(argv[i]) == "--webcam" ) {
if ((i+2) >= argc) {
cout << "missing webcam numbers after --webcam" << endl;
return print_help();
}
else if( sscanf(argv[++i], "%d", &settings.webcam[0]) != 1 || settings.webcam[0] < 0 ||
sscanf(argv[++i], "%d", &settings.webcam[1]) != 1 || settings.webcam[1] < 0)
{
cout << "invalid webcam number" << endl;
return print_help();
}
settings.inputMode = WEBCAM;
}
else if (string(argv[i]) == "--input-dir" ) {
if ((i+1) >= argc) {
cout << "missing input directory path after --input-dir" << endl;
return print_help();
}
settings.inputDir = argv[++i];
settings.inputMode = INPUT_DIR;
}
else if (string(argv[i]) == "--save" ) {
settings.save = true;
}
// invalid options or parameters
else if( string(argv[i]) == "-h" || string(argv[i]) == "--help" )
return print_help();
else if( argv[i][0] == '-' )
{
cout << "invalid option: " << argv[i] << endl;
return 0;
}
else
{
cout << "unknown parameter: " << argv[i] << endl;
return 0;
}
}
// check that an input mode was selected
if (settings.inputMode == NONE) {
cout << "ERROR: please precise an input mode (--webcam or --image-list options)\n" << endl;
return print_help();
}
// check that the chessboard size was entered by the user
if( settings.boardSize.width <= 0 || settings.boardSize.height <= 0 )
{
cout << "ERROR: please precise chessboard width and height (-w and -h options)\n" << endl;
return print_help();
}
vector<Mat> images; // the images
// we get the calibration input images, depending on the mode selected by the user
if (settings.inputMode == IMAGE_LIST)
getImagesFromImageList(settings, images);
else if (settings.inputMode == INPUT_DIR)
getImagesFromInputDir(settings, images);
else if (settings.inputMode == WEBCAM)
getImagesFromWebcams(settings, images);
// we save the calibration pictures, if requested by the user
if (settings.save) {
cout << "saving images..." << endl;
for (size_t i=0; i<images.size(); i++) {
ostringstream ss;
// append '0', to make sure images will be in alphabetical order
ss << setw(2) << setfill('0') << i;
string filename = settings.images_output_folder + ss.str() + ".png";
imwrite(filename, images.at(i));
}
}
// we finally start the calibration process
StereoCalib(images, settings);
return 0;
}
static void StereoCalib(const vector<Mat>& images, Settings settings)
{
if( images.size() % 2 != 0 )
{
cout << "Error: the image list contains odd (non-even) number of elements\n" << endl;
exit(1);
}
bool displayCorners = true; // display GUI with the chessboard corners drawn on the window
const int maxScale = 2;
const float squareSize = 1.f; // Set this to your actual square size
// ARRAY AND VECTOR STORAGE:
vector<vector<Point2f> > imagePoints[2];
vector<vector<Point3f> > objectPoints;
// we get the size of the first image, and for each image we will check that it is the same size
Size imageSize = images[0].size();
int i, j, k, nimages = (int)images.size()/2;
imagePoints[0].resize(nimages);
imagePoints[1].resize(nimages);
vector<Mat> goodImages;
// loop on all the pictures
// (outer loop iterates only on the pictures from one of the two cameras)
for( i = j = 0; i < nimages; i++ )
{
// for the picture of the left and the right camera
for( k = 0; k < 2; k++ )
{
// we get a new image
Mat img = images[i*2+k];
// we check that the image is not empty and that it has the same size than other images
if(img.empty())
break;
else if( img.size() != imageSize )
{
cout << "The image number " << i*2+k << " has the size different "
"from the first image size. Skipping the pair\n";
break;
}
bool found = false;
vector<Point2f>& corners = imagePoints[k][j];
for( int scale = 1; scale <= maxScale; scale++ )
{
Mat timg;
// we resize the picture if necessary
if( scale == 1 )
timg = img;
else
resize(img, timg, Size(), scale, scale);
// we search for the chessboard corners
found = findChessboardCorners(timg, settings.boardSize, corners,
CV_CALIB_CB_ADAPTIVE_THRESH | CV_CALIB_CB_NORMALIZE_IMAGE);
if( found )
{
// if the picture was scaled, we scale back the corner coordinates
if( scale > 1 )
{
Mat cornersMat(corners);
cornersMat *= 1./scale;
}
break;
}
}
// if user want to display the corners in a GUI
if( displayCorners )
{
Mat cimg, cimg1;
cvtColor(img, cimg, COLOR_GRAY2BGR);
drawChessboardCorners(cimg, settings.boardSize, corners, found);
double sf = 640./MAX(img.rows, img.cols); // scale factor stuff
resize(cimg, cimg1, Size(), sf, sf); // scale factor stuff
imshow("corners", cimg1);
char c = (char)waitKey(500);
if( c == 27 || c == 'q' || c == 'Q' ) //Allow ESC to quit
exit(-1);
}
else
putchar('.');
if( !found )
break;
// if we found the corners, we refine the corner locations
cornerSubPix(img, corners, Size(11,11), Size(-1,-1),
TermCriteria(CV_TERMCRIT_ITER+CV_TERMCRIT_EPS, 30, 0.01));
}
// if we found the corners in each of the two pictures,
// we add them to the good images list
if( k == 2 )
{
goodImages.push_back(images[i*2]);
goodImages.push_back(images[i*2+1]);
j++;
}
} // end of the loop on all the pictures
cout << j << " pairs have been successfully detected.\n";
nimages = j;
if( nimages < 2 )
{
cout << "Error: too little pairs to run the calibration\n";
return;
}
// we remove unitialized image points
// (which appear when there is a picture on which we don't find the chessboard pattern)
imagePoints[0].resize(nimages);
imagePoints[1].resize(nimages);
// we create a list containing the positions of the chessboard corner points in the real world
// in a coordinate system where the chessboard plane is in the plane z = 0
objectPoints.resize(nimages);
for( i = 0; i < nimages; i++ )
for( j = 0; j < settings.boardSize.width; j++ )
for( k = 0; k < settings.boardSize.height; k++ )
objectPoints[i].push_back(Point3f(j*squareSize, k*squareSize, 0));
cout << "Running stereo calibration ...\n";
Mat cameraMatrix[2], distCoeffs[2];
cameraMatrix[0] = Mat::eye(3, 3, CV_64F);
cameraMatrix[1] = Mat::eye(3, 3, CV_64F);
Mat R, T, E, F;
double rms = stereoCalibrate(objectPoints, imagePoints[0], imagePoints[1],
cameraMatrix[0], distCoeffs[0],
cameraMatrix[1], distCoeffs[1],
imageSize, R, T, E, F,
TermCriteria(CV_TERMCRIT_ITER+CV_TERMCRIT_EPS, 100, 1e-5),
CV_CALIB_FIX_ASPECT_RATIO +
CV_CALIB_ZERO_TANGENT_DIST +
CV_CALIB_SAME_FOCAL_LENGTH +
CV_CALIB_RATIONAL_MODEL +
CV_CALIB_FIX_K3 + CV_CALIB_FIX_K4 + CV_CALIB_FIX_K5);
cout << "done with RMS error=" << rms << endl; // RMS = Root Mean Square error
// CALIBRATION QUALITY CHECK
// because the output fundamental matrix implicitly
// includes all the output information,
// we can check the quality of calibration using the
// epipolar geometry constraint: m2^t*F*m1=0
double err = 0;
int npoints = 0;
vector<Vec3f> lines[2];
for( i = 0; i < nimages; i++ )
{
int npt = (int)imagePoints[0][i].size();
Mat imgpt[2];
for( k = 0; k < 2; k++ )
{
imgpt[k] = Mat(imagePoints[k][i]);
undistortPoints(imgpt[k], imgpt[k], cameraMatrix[k], distCoeffs[k], Mat(), cameraMatrix[k]);
computeCorrespondEpilines(imgpt[k], k+1, F, lines[k]);
}
for( j = 0; j < npt; j++ )
{
double errij = fabs(imagePoints[0][i][j].x*lines[1][j][0] +
imagePoints[0][i][j].y*lines[1][j][1] + lines[1][j][2]) +
fabs(imagePoints[1][i][j].x*lines[0][j][0] +
imagePoints[1][i][j].y*lines[0][j][1] + lines[0][j][2]);
err += errij;
}
npoints += npt;
}
cout << "average reprojection err = " << err/npoints << endl;
// save intrinsic parameters
FileStorage fs("intrinsics.yml", CV_STORAGE_WRITE);
if( fs.isOpened() )
{
fs << "M1" << cameraMatrix[0] << "D1" << distCoeffs[0] <<
"M2" << cameraMatrix[1] << "D2" << distCoeffs[1];
fs.release();
}
else
cout << "Error: can not save the intrinsic parameters\n";
// stereo rectification
Mat R1, R2, P1, P2, Q;
Rect validRoi[2]; // rectangles inside the rectified images where all the pixels are valid
// (smaller than full frame)
stereoRectify(cameraMatrix[0], distCoeffs[0],
cameraMatrix[1], distCoeffs[1],
imageSize, R, T, R1, R2, P1, P2, Q,
CALIB_ZERO_DISPARITY, 1, imageSize, &validRoi[0], &validRoi[1]);
// save extrinsic parameters
fs.open("extrinsics.yml", CV_STORAGE_WRITE);
if( fs.isOpened() )
{
fs << "R" << R << "T" << T << "R1" << R1 << "R2" << R2 << "P1" << P1 << "P2" << P2 << "Q" << Q;
fs.release();
}
else
cout << "Error: can not save the extrinsic parameters\n";
// OpenCV can handle left-right
// or up-down camera arrangements
bool isVerticalStereo = fabs(P2.at<double>(1, 3)) > fabs(P2.at<double>(0, 3));
// COMPUTE AND DISPLAY RECTIFICATION
if( !settings.showRectified )
return;
Mat rmap[2][2]; // rectify maps
// IF BY CALIBRATED (BOUGUET'S METHOD)
if( settings.useCalibrated )
{
// we already computed everything
}
// OR ELSE HARTLEY'S METHOD
// use intrinsic parameters of each camera, but
// compute the rectification transformation directly
// from the fundamental matrix
else
{
vector<Point2f> allimgpt[2];
for( k = 0; k < 2; k++ )
{
for( i = 0; i < nimages; i++ )
std::copy(imagePoints[k][i].begin(), imagePoints[k][i].end(), back_inserter(allimgpt[k]));
}
F = findFundamentalMat(Mat(allimgpt[0]), Mat(allimgpt[1]), FM_8POINT, 0, 0);
Mat H1, H2;
stereoRectifyUncalibrated(Mat(allimgpt[0]), Mat(allimgpt[1]), F, imageSize, H1, H2, 3);
R1 = cameraMatrix[0].inv()*H1*cameraMatrix[0];
R2 = cameraMatrix[1].inv()*H2*cameraMatrix[1];
P1 = cameraMatrix[0];
P2 = cameraMatrix[1];
}
//Precompute maps for cv::remap()
initUndistortRectifyMap(cameraMatrix[0], distCoeffs[0], R1, P1, imageSize, CV_16SC2, rmap[0][0], rmap[0][1]);
initUndistortRectifyMap(cameraMatrix[1], distCoeffs[1], R2, P2, imageSize, CV_16SC2, rmap[1][0], rmap[1][1]);
Mat canvas;
double sf; // some ratio, to set canvas to pre-determined size
int w, h;
if( !isVerticalStereo )
{
// if the width is bigger than the height (which is most likely)
// the canvas width for an image will be 600 and the height will be scaled acordingly
// to keep the original ratip, and in the canvas we will put two images,
// one for each camera (hence the w*2)
sf = 600./MAX(imageSize.width, imageSize.height);
w = cvRound(imageSize.width*sf);
h = cvRound(imageSize.height*sf);
canvas.create(h, w*2, CV_8UC3);
}
else
{
// same principle than above, but we limit the size to 300 instead of 600
sf = 300./MAX(imageSize.width, imageSize.height);
w = cvRound(imageSize.width*sf);
h = cvRound(imageSize.height*sf);
canvas.create(h*2, w, CV_8UC3);
}
for( i = 0; i < nimages; i++ )
{
for( k = 0; k < 2; k++ )
{
Mat img = goodImages[i*2+k], rimg, cimg; // rimg = rectified img
// we remap the images with the rectify maps found by initUndistortRectifyMap
remap(img, rimg, rmap[k][0], rmap[k][1], CV_INTER_LINEAR);
// we make a copy of the rectified img in grey shades
cvtColor(rimg, cimg, COLOR_GRAY2BGR);
// we get the part of canvas which contains the picture related to the current camera
Mat canvasPart = !isVerticalStereo ? canvas(Rect(w*k, 0, w, h)) : canvas(Rect(0, h*k, w, h));
// we resize the grey img to fill in the canvasPart
resize(cimg, canvasPart, canvasPart.size(), 0, 0, CV_INTER_AREA);
if( settings.useCalibrated )
{
// we draw the rectangle of the valid ROI on the canvasPart
Rect vroi(cvRound(validRoi[k].x*sf), cvRound(validRoi[k].y*sf),
cvRound(validRoi[k].width*sf), cvRound(validRoi[k].height*sf));
rectangle(canvasPart, vroi, Scalar(0,0,255), 3, 8);
}
}
if( !isVerticalStereo )
for( j = 0; j < canvas.rows; j += 16 )
// horizontal line every 16 pixels
line(canvas, Point(0, j), Point(canvas.cols, j), Scalar(0, 255, 0), 1, 8);
else
for( j = 0; j < canvas.cols; j += 16 )
// vertical line every 16 pixels
line(canvas, Point(j, 0), Point(j, canvas.rows), Scalar(0, 255, 0), 1, 8);
imshow("rectified", canvas);
char c = (char)waitKey();
if( c == 27 || c == 'q' || c == 'Q' )
break;
} // end loop in each image
}
// get images from the webcams
// @param settings -> the settings
// @param images -> the output list of images
static void getImagesFromWebcams(Settings settings, vector<Mat>& images) {
// we initialize the webcams
settings.capture[0] = VideoCapture(settings.webcam[0]);
settings.capture[1] = VideoCapture(settings.webcam[1]);
if (!settings.capture[0].isOpened() || !settings.capture[1].isOpened()) {
cerr << "Error: can't access webcam stream" << endl;
exit(1);
}
vector<Point2f> corners1; // content unused, but needed as argument for findChessboardCorners
vector<Point2f> corners2; // content unused, but needed as argument for findChessboardCorners
bool detectionMode = false; // press 'd' to start detection mode (take a new image)
// this boolean is set to false again each time a new picture on which the pattern is found is taken.
// this allows for a better quality of results, because:
// 1. you can control when you want to start taking a new picture, so you have time to change
// position of the chessboard in the webcam pictures. Having pictures with the chessboard
// located in different positions results in better calibration results.
// 2. we keep the picture only when the chessboard if found, if not, we try again to find it
// in the next picture
cout << "press 'd' to take a new picture" << endl;
Mat frameCam1, frameCam2;
while (images.size()/2 < settings.imageNumber) {
// we get new frames
settings.capture[0].grab();
settings.capture[1].grab();
settings.capture[0].retrieve(frameCam1);
settings.capture[1].retrieve(frameCam2);
if (detectionMode) {
bool found1 = findChessboardCorners(frameCam1, settings.boardSize, corners1,
CV_CALIB_CB_ADAPTIVE_THRESH | CV_CALIB_CB_NORMALIZE_IMAGE);
bool found2 = findChessboardCorners(frameCam2, settings.boardSize, corners2,
CV_CALIB_CB_ADAPTIVE_THRESH | CV_CALIB_CB_NORMALIZE_IMAGE);
if (found1 && found2) {
// we keep those images
Mat frameCam1_save, frameCam2_save;
cvtColor(frameCam1, frameCam1_save, CV_BGR2GRAY);
cvtColor(frameCam2, frameCam2_save, CV_BGR2GRAY);
images.push_back(frameCam1_save);
images.push_back(frameCam2_save);
// we draw the chessboard corners, and we apply special effect to the image
// so the user can notice that we have registered this image
drawChessboardCorners(frameCam1, settings.boardSize, corners1, found1);
drawChessboardCorners(frameCam2, settings.boardSize, corners2, found2);
bitwise_not(frameCam1, frameCam1);
bitwise_not(frameCam2, frameCam2);
cout << "images: " << images.size()/2 << " / " << settings.imageNumber << endl;
detectionMode = false;
}
}
// we resize picture for a more convenient GUI display
resize(frameCam1, frameCam1, Size(frameCam1.cols/2, frameCam1.rows/2));
resize(frameCam2, frameCam2, Size(frameCam2.cols/2, frameCam2.rows/2));
// GUI
imshow("cam 1", frameCam1);
imshow("cam 2", frameCam2);
char key = waitKey(1); // wait before next frame
if (key == 'd') // start detection mode
detectionMode = true;
if (key == 'q') // exit program
exit(0);
}
destroyWindow("cam 1");
destroyWindow("cam 2");
}
// read a XML/YML image list file to put all the filenames in a vector
// @param filename -> the input XML / YML file
// @param l -> the output list of filenames
static bool readStringList( const string& filename, vector<string>& l )
{
l.resize(0);
FileStorage fs(filename, FileStorage::READ);
if( !fs.isOpened() )
return false;
FileNode n = fs.getFirstTopLevelNode();
if( n.type() != FileNode::SEQ )
return false;
FileNodeIterator it = n.begin(), it_end = n.end();
for( ; it != it_end; ++it )
l.push_back((string)*it);
return true;
}
// get images from an image list YML / XML file
// @param settings -> the settings
// @param images -> the output list of images
static void getImagesFromImageList(Settings settings, vector<Mat>& images) {
vector<string> imageFilenames;
// we get the list of images from the file
bool ok = readStringList(settings.imageListFilename, imageFilenames);
if(!ok || imageFilenames.empty())
{
cout << "can not open " << settings.imageListFilename << " or the string list is empty" << endl;
print_help();
exit(1);
}
// we load all the images
for (size_t i=0; i<imageFilenames.size(); i++)
images.push_back(imread(imageFilenames[i], CV_LOAD_IMAGE_GRAYSCALE));
}
// get images from a folder
// @param settings -> the settings
// @param images -> the output list of images
static void getImagesFromInputDir(Settings settings, vector<Mat>& images) {
// we get the list of images in the input directory, with the command ls
char list_files[1000];
sprintf(list_files, "ls %s", settings.inputDir.c_str());
FILE* f = popen(list_files, "r");
if (f == NULL) {
cerr << "Could not open input images directory\n" << endl;
print_help();
exit(1);
}
// check if we need to add a slash between folder and filename
bool add_slash = false;
if (settings.inputDir.back() != '/')
add_slash = true;
// for each image filename, we load the image
const int BUFFSIZE = 1000;
char filename[BUFFSIZE];
while(fgets(filename, BUFFSIZE, f)) {
char image_path[1000];
if (add_slash)
sprintf(image_path, "%s/%s", settings.inputDir.c_str(), filename);
else
sprintf(image_path, "%s%s", settings.inputDir.c_str(), filename);
strtok(image_path, "\n"); // we remove the trailing '\n'
cout << "loading: " << image_path << endl;
Mat image = imread(image_path, CV_LOAD_IMAGE_GRAYSCALE);
images.push_back(image);
}
cout << images.size() << " images loaded" << endl;
pclose(f);
}