-
Notifications
You must be signed in to change notification settings - Fork 62
/
main.cpp
843 lines (714 loc) · 30.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
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
#include <string.h>
#include <fstream>
#include <vector>
#include <ctime>
#include <chrono>
#include <cstddef>
#include <cv.h>
#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/imgproc/imgproc.hpp>
#include <opencv2/features2d/features2d.hpp>
#include "utils.h"
#include "magsac.h"
#include "uniform_sampler.h"
#include "fundamental_estimator.cpp"
#include "homography_estimator.cpp"
enum SceneType { FundamentalMatrixScene, HomographyScene };
enum Dataset { kusvod2, extremeview, homogr, adelaidermf, multih };
// A method applying MAGSAC for fundamental matrix estimation to one of the built-in scenes
void testFundamentalMatrixFitting(
double ransac_confidence_,
double sigma_max_,
std::string test_scene_,
bool draw_results_ = false,
double drawing_threshold_ = 2);
// A method applying MAGSAC for homography estimation to one of the built-in scenes
void testHomographyFitting(
double ransac_confidence_,
double sigma_max_,
std::string test_scene_,
bool draw_results_ = false,
double drawing_threshold_ = 2);
// A method applying OpenCV for homography estimation to one of the built-in scenes
void opencvHomographyFitting(
double ransac_confidence_,
double threshold_,
std::string test_scene_,
bool draw_results_ = false,
const bool with_magsac_post_processing_ = true);
// A method applying OpenCV for fundamental matrix estimation to one of the built-in scenes
void opencvFundamentalMatrixFitting(
double ransac_confidence_,
double threshold_,
std::string test_scene_,
bool draw_results_ = false,
const bool with_magsac_post_processing_ = true);
// The names of built-in scenes
std::vector<std::string> getAvailableTestScenes(
SceneType scene_type_,
Dataset dataset_);
// Running tests on the selected dataset
void runTest(SceneType scene_type_,
Dataset dataset_,
const double ransac_confidence_,
const bool draw_results_,
const double drawing_threshold_);
// Returns the name of the selected dataset
std::string dataset2str(Dataset dataset_);
int main(int argc, const char* argv[])
{
/*
This is an example showing how MAGSAC is applied to homography or fundamental matrix estimation tasks.
The paper is readable here: https://arxiv.org/pdf/1803.07469.pdf
This implementation is not the one used in the experiments of the paper.
*/
const double ransac_confidence = 0.99; // The required confidence in the results
const bool draw_results = true; // A flag to draw and show the results
// The inlier threshold for visualization. This threshold is not used by the algorithm,
// it is simply for selecting the inliers to be drawn after MAGSAC finished.
const double drawing_threshold = 2.0;
runTest(SceneType::FundamentalMatrixScene, Dataset::kusvod2, ransac_confidence, draw_results, drawing_threshold);
runTest(SceneType::FundamentalMatrixScene, Dataset::adelaidermf, ransac_confidence, draw_results, drawing_threshold);
runTest(SceneType::FundamentalMatrixScene, Dataset::multih, ransac_confidence, draw_results, drawing_threshold);
runTest(SceneType::HomographyScene, Dataset::extremeview, ransac_confidence, draw_results, drawing_threshold);
runTest(SceneType::HomographyScene, Dataset::homogr, ransac_confidence, draw_results, drawing_threshold);
return 0;
}
void runTest(SceneType scene_type_,
Dataset dataset_,
const double ransac_confidence_,
const bool draw_results_,
const double drawing_threshold_)
{
const std::string dataset_name = dataset2str(dataset_);
const std::string problem_name = scene_type_ == SceneType::HomographyScene ?
"Homography" :
"Fundamental matrix";
// Test scenes for homography estimation
for (const auto& scene : getAvailableTestScenes(scene_type_, dataset_))
{
printf("--------------------------------------------------------------\n");
printf("%s estimation on scene \"%s\" from dataset \"%s\".\n",
problem_name.c_str(), scene.c_str(), dataset_name.c_str());
printf("--------------------------------------------------------------\n");
if (scene_type_ == SceneType::HomographyScene)
{
printf("1. Running OpenCV's RANSAC with threshold %f px\n", drawing_threshold_);
opencvHomographyFitting(ransac_confidence_,
drawing_threshold_, // The maximum sigma value allowed in MAGSAC
scene, // The scene type
false, // A flag to draw and show the results
false); // A flag to apply the MAGSAC post-processing to the OpenCV's output
printf("\n2. Running OpenCV's RANSAC with threshold %f px and\n", drawing_threshold_);
printf("applying MAGSAC post-processing to OpenCV's output.\n");
printf("This might be beneficial since the post-processing step has.\n");
printf("negligible time demand and, usually, improves the result.\n");
opencvHomographyFitting(ransac_confidence_,
drawing_threshold_, // The maximum sigma value allowed in MAGSAC
scene, // The scene type
false, // A flag to draw and show the results
true); // A flag to apply the MAGSAC post-processing to the OpenCV's output
printf("\n3. Running MAGSAC with reasonably set maximum threshold (%f px)\n", drawing_threshold_);
testHomographyFitting(ransac_confidence_,
drawing_threshold_, // The maximum sigma value allowed in MAGSAC
scene, // The scene type
draw_results_, // A flag to draw and show the results
drawing_threshold_); // The inlier threshold for visualization.
printf("\n4. Running MAGSAC with extreme maximum threshold (%f px)\n", 10.0);
testHomographyFitting(ransac_confidence_,
10, // The maximum sigma value allowed in MAGSAC
scene, // The scene type
draw_results_, // A flag to draw and show the results
drawing_threshold_); // The inlier threshold for visualization.
} else
{
printf("1. Running OpenCV's RANSAC with threshold %f px\n", drawing_threshold_);
opencvFundamentalMatrixFitting(ransac_confidence_,
drawing_threshold_, // The maximum sigma value allowed in MAGSAC
scene, // The scene type
false, // A flag to draw and show the results
false); // A flag to apply the MAGSAC post-processing to the OpenCV's output
printf("\n2. Running OpenCV's RANSAC with threshold %f px and\n", drawing_threshold_);
printf("applying MAGSAC post-processing to OpenCV's output.\n");
printf("This might be beneficial since the post-processing step has.\n");
printf("negligible time demand and, usually, improves the result.\n");
opencvFundamentalMatrixFitting(ransac_confidence_,
drawing_threshold_, // The maximum sigma value allowed in MAGSAC
scene, // The scene type
false, // A flag to draw and show the results
true); // A flag to apply the MAGSAC post-processing to the OpenCV's output
printf("\n3. Running MAGSAC with reasonably set maximum threshold (%f px)\n", drawing_threshold_);
testFundamentalMatrixFitting(ransac_confidence_, // The required confidence in the results
drawing_threshold_, // The maximum sigma value allowed in MAGSAC
scene, // The scene type
draw_results_, // A flag to draw and show the results
drawing_threshold_); // The inlier threshold for visualization.
printf("\n4. Running MAGSAC with extreme maximum threshold (%f px)\n", 10.0);
testFundamentalMatrixFitting(ransac_confidence_, // The required confidence in the results
10, // The maximum sigma value allowed in MAGSAC
scene, // The scene type
draw_results_, // A flag to draw and show the results
drawing_threshold_); // The inlier threshold for visualization.
}
printf("\nPress a button to continue.\n\n");
cv::waitKey(0);
}
}
std::string dataset2str(Dataset dataset_)
{
switch (dataset_)
{
case Dataset::homogr:
return "homogr";
case Dataset::extremeview:
return "extremeview";
case Dataset::kusvod2:
return "kusvod2";
case Dataset::adelaidermf:
return "adelaidermf";
case Dataset::multih:
return "multih";
default:
return "unknown";
}
}
std::vector<std::string> getAvailableTestScenes(SceneType scene_type_,
Dataset dataset_)
{
switch (scene_type_)
{
case SceneType::HomographyScene: // Available test scenes for homography estimation
switch (dataset_)
{
case Dataset::homogr:
return { "LePoint1", "LePoint2", "LePoint3", // "homogr" dataset
"graf", "ExtremeZoom", "city",
"CapitalRegion", "BruggeTower", "BruggeSquare",
"BostonLib", "boat", "adam",
"WhiteBoard", "Eiffel", "Brussels",
"Boston"};
case Dataset::extremeview:
return {"extremeview/adam", "extremeview/cafe", "extremeview/cat", // "EVD" (i.e. extremeview) dataset
"extremeview/dum", "extremeview/face", "extremeview/fox",
"extremeview/girl", "extremeview/graf", "extremeview/grand",
"extremeview/index", "extremeview/mag", "extremeview/pkk",
"extremeview/shop", "extremeview/there", "extremeview/vin"};
default:
return std::vector<std::string>();
}
case SceneType::FundamentalMatrixScene:
switch (dataset_)
{
case Dataset::kusvod2:
return {"corr", "booksh", "box",
"castle", "graff", "head",
"kampa", "leafs", "plant",
"rotunda", "shout", "valbonne",
"wall", "wash", "zoom",
"Kyoto"};
case Dataset::adelaidermf:
return {"barrsmith", "bonhall",
"bonython", "elderhalla", "elderhallb",
"hartley", "johnssonb", "ladysymon",
"library", "napiera", "napierb",
"nese", "oldclassicswing", "physics",
"sene", "unihouse", "unionhouse"};
case Dataset::multih:
return {"boxesandbooks", "glasscaseb", "stairs"};
default:
return std::vector<std::string>();
}
default:
return std::vector<std::string>();
}
}
void testFundamentalMatrixFitting(
double ransac_confidence_,
double sigma_max_,
std::string test_scene_,
bool draw_results_,
double drawing_threshold_)
{
printf("\tProcessed scene = '%s'.\n", test_scene_.c_str());
// Load the images of the current test scene
cv::Mat image1 = cv::imread("data/fundamental_matrix/" + test_scene_ + "A.png");
cv::Mat image2 = cv::imread("data/fundamental_matrix/" + test_scene_ + "B.png");
if (image1.cols == 0)
{
image1 = cv::imread("data/fundamental_matrix/" + test_scene_ + "A.jpg");
image2 = cv::imread("data/fundamental_matrix/" + test_scene_ + "B.jpg");
}
if (image1.cols == 0)
{
fprintf(stderr, "A problem occured when loading the images for test scene '%s'\n", test_scene_.c_str());
return;
}
cv::Mat points; // The point correspondences, each is of format x1 y1 1 x2 y2 1
std::vector<int> ground_truth_labels; // The ground truth labeling provided in the dataset
// A function loading the points from files
readAnnotatedPoints("data/fundamental_matrix/" + test_scene_ + "_pts.txt",
points,
ground_truth_labels);
// The number of points in the datasets
const size_t N = points.rows; // The number of points in the scene
if (N == 0) // If there are no points, return
{
fprintf(stderr, "A problem occured when loading the annotated points for test scene '%s'\n", test_scene_.c_str());
return;
}
FundamentalMatrixEstimator estimator; // The robust homography estimator class containing the function for the fitting and residual calculation
FundamentalMatrix model; // The estimated model
// In this used datasets, the manually selected inliers are not all inliers but a subset of them.
// Therefore, the manually selected inliers are augmented as follows:
// (i) First, the implied model is estimated from the manually selected inliers.
// (ii) Second, the inliers of the ground truth model are selected.
refineManualLabeling<FundamentalMatrix, FundamentalMatrixEstimator>(
points,
ground_truth_labels,
estimator,
0.35); // Threshold value from the LO*-RANSAC paper
// Select the inliers from the labeling
std::vector<int> ground_truth_inliers = getSubsetFromLabeling(ground_truth_labels, 1);
const size_t I = static_cast<double>(ground_truth_inliers.size());
printf("\tEstimated model = '%s'.\n", estimator.modelName().c_str());
printf("\tNumber of correspondences loaded = %d.\n", static_cast<int>(N));
printf("\tNumber of ground truth inliers = %d.\n", static_cast<int>(I));
printf("\tTheoretical RANSAC iteration number at %.2f confidence = %d.\n",
ransac_confidence_, static_cast<int>(log(1.0 - ransac_confidence_) / log(1.0 - pow(static_cast<double>(I) / static_cast<double>(N), 4))));
// Initialize the sampler used for selecting minimal samples
UniformSampler<cv::Mat> sampler(N);
MAGSAC<cv::Mat, FundamentalMatrixEstimator, FundamentalMatrix> magsac;
magsac.setSigmaMax(sigma_max_); // The maximum noise scale sigma allowed
magsac.setCoreNumber(5); // The number of cores used to speed up sigma-consensus
magsac.setPartitionNumber(5); // The number partitions used for speeding up sigma consensus. As the value grows, the algorithm become slower and, usually, more accurate.
magsac.setIterationLimit(1e5); // Iteration limit to interrupt the cases when the algorithm run too long.
int iteration_number = 0; // Number of iterations required
std::chrono::time_point<std::chrono::system_clock> end,
start = std::chrono::system_clock::now();
magsac.run(points, // The data points
ransac_confidence_, // The required confidence in the results
estimator, // The used estimator
sampler, // The sampler used for selecting minimal samples in each iteration
model, // The estimated model
iteration_number); // The number of iterations
end = std::chrono::system_clock::now();
std::chrono::duration<double> elapsed_seconds = end - start;
std::time_t end_time = std::chrono::system_clock::to_time_t(end);
printf("\tActual number of iterations drawn by MAGSAC at %.2f confidence: %d\n", ransac_confidence_, iteration_number);
printf("\tElapsed time: %f secs\n", elapsed_seconds.count());
// Compute the RMSE given the ground truth inliers
double rmse = 0, error;
for (const auto &inlier_idx : ground_truth_inliers)
{
error = estimator.error(points.row(inlier_idx), model);
rmse += error * error;
}
rmse = sqrt(rmse / static_cast<double>(I));
printf("\tRMSE error: %f px\n", rmse);
// Visualization part.
// Inliers are selected using threshold and the estimated model.
// This part is not necessary and is only for visualization purposes.
if (draw_results_)
{
std::vector<int> obtained_labeling(points.rows, 0);
for (auto pt_idx = 0; pt_idx < points.rows; ++pt_idx)
{
// Computing the residual of the point given the estimated model
auto residual = estimator.error(points.row(pt_idx),
model.descriptor);
// Change the label to 'inlier' if the residual is smaller than the threshold
if (drawing_threshold_ >= residual)
obtained_labeling[pt_idx] = 1;
}
// Draw the matches to the images
cv::Mat out_image;
drawMatches<double, int>(points, obtained_labeling, image1, image2, out_image);
// Show the matches
std::string window_name = "Visualization with threshold = " + std::to_string(drawing_threshold_) + " px; Maximum threshold is = " + std::to_string(sigma_max_);
showImage(out_image,
window_name,
1600,
900);
out_image.release();
}
// Clean up the memory occupied by the images
image1.release();
image2.release();
}
void testHomographyFitting(
double ransac_confidence_,
double sigma_max_,
std::string test_scene_,
bool draw_results_,
double drawing_threshold_)
{
printf("\tProcessed scene = '%s'.\n", test_scene_.c_str());
// Load the images of the current test scene
cv::Mat image1 = cv::imread("data/homography/" + test_scene_ + "A.png");
cv::Mat image2 = cv::imread("data/homography/" + test_scene_ + "B.png");
if (image1.cols == 0)
{
image1 = cv::imread("data/homography/" + test_scene_ + "A.jpg");
image2 = cv::imread("data/homography/" + test_scene_ + "B.jpg");
}
if (image1.cols == 0)
{
fprintf(stderr, "A problem occured when loading the images for test scene '%s'\n", test_scene_.c_str());
return;
}
cv::Mat points; // The point correspondences, each is of format x1 y1 1 x2 y2 1
std::vector<int> ground_truth_labels; // The ground truth labeling provided in the dataset
// A function loading the points from files
readAnnotatedPoints("data/homography/" + test_scene_ + "_pts.txt",
points,
ground_truth_labels);
// The number of points in the datasets
const size_t N = points.rows; // The number of points in the scene
if (N == 0) // If there are no points, return
{
fprintf(stderr, "A problem occured when loading the annotated points for test scene '%s'\n", test_scene_.c_str());
return;
}
RobustHomographyEstimator estimator; // The robust homography estimator class containing the function for the fitting and residual calculation
Homography model; // The estimated model
// In this used datasets, the manually selected inliers are not all inliers but a subset of them.
// Therefore, the manually selected inliers are augmented as follows:
// (i) First, the implied model is estimated from the manually selected inliers.
// (ii) Second, the inliers of the ground truth model are selected.
refineManualLabeling<Homography, RobustHomographyEstimator>(
points,
ground_truth_labels,
estimator,
2.0);
// Select the inliers from the labeling
std::vector<int> ground_truth_inliers = getSubsetFromLabeling(ground_truth_labels, 1);
const size_t I = static_cast<double>(ground_truth_inliers.size());
printf("\tEstimated model = '%s'.\n", estimator.modelName().c_str());
printf("\tNumber of correspondences loaded = %d.\n", static_cast<int>(N));
printf("\tNumber of ground truth inliers = %d.\n", static_cast<int>(I));
printf("\tTheoretical RANSAC iteration number at %.2f confidence = %d.\n",
ransac_confidence_, static_cast<int>(log(1.0 - ransac_confidence_) / log(1.0 - pow(static_cast<double>(I) / static_cast<double>(N), 4))));
// Initialize the sampler used for selecting minimal samples
UniformSampler<cv::Mat> sampler(N);
MAGSAC<cv::Mat, RobustHomographyEstimator, Homography> magsac;
magsac.setSigmaMax(sigma_max_); // The maximum noise scale sigma allowed
magsac.setCoreNumber(5); // The number of cores used to speed up sigma-consensus
magsac.setPartitionNumber(5); // The number partitions used for speeding up sigma consensus. As the value grows, the algorithm become slower and, usually, more accurate.
magsac.setIterationLimit(1e5); // Iteration limit to interrupt the cases when the algorithm run too long.
int iteration_number = 0; // Number of iterations required
std::chrono::time_point<std::chrono::system_clock> end,
start = std::chrono::system_clock::now();
magsac.run(points, // The data points
ransac_confidence_, // The required confidence in the results
estimator, // The used estimator
sampler, // The sampler used for selecting minimal samples in each iteration
model, // The estimated model
iteration_number); // The number of iterations
end = std::chrono::system_clock::now();
std::chrono::duration<double> elapsed_seconds = end - start;
std::time_t end_time = std::chrono::system_clock::to_time_t(end);
printf("\tActual number of iterations drawn by MAGSAC at %.2f confidence: %d\n", ransac_confidence_, iteration_number);
printf("\tElapsed time: %f secs\n", elapsed_seconds.count());
// Compute the RMSE given the ground truth inliers
double rmse = 0, error;
size_t inlier_number = 0;
for (const auto& inlier_idx : ground_truth_inliers)
{
error = estimator.error(points.row(inlier_idx), model);
rmse += error;
}
rmse = sqrt(rmse / static_cast<double>(I));
printf("\tRMSE error: %f px\n", rmse);
// Visualization part.
// Inliers are selected using threshold and the estimated model.
// This part is not necessary and is only for visualization purposes.
if (draw_results_)
{
std::vector<int> obtained_labeling(points.rows, 0);
for (auto pt_idx = 0; pt_idx < points.rows; ++pt_idx)
{
// Computing the residual of the point given the estimated model
auto residual = sqrt(estimator.error(points.row(pt_idx),
model.descriptor));
// Change the label to 'inlier' if the residual is smaller than the threshold
if (drawing_threshold_ >= residual)
obtained_labeling[pt_idx] = 1;
}
// Draw the matches to the images
cv::Mat out_image;
drawMatches<double, int>(points, obtained_labeling, image1, image2, out_image);
// Show the matches
std::string window_name = "Visualization with threshold = " + std::to_string(drawing_threshold_) + " px; Maximum threshold is = " + std::to_string(sigma_max_);
showImage(out_image,
window_name,
1600,
900);
out_image.release();
}
// Clean up the memory occupied by the images
image1.release();
image2.release();
}
void opencvHomographyFitting(
double ransac_confidence_,
double threshold_,
std::string test_scene_,
bool draw_results_,
const bool with_magsac_post_processing_)
{
printf("\tProcessed scene = '%s'.\n", test_scene_.c_str());
// Load the images of the current test scene
cv::Mat image1 = cv::imread("data/homography/" + test_scene_ + "A.png");
cv::Mat image2 = cv::imread("data/homography/" + test_scene_ + "B.png");
if (image1.cols == 0)
{
image1 = cv::imread("data/homography/" + test_scene_ + "A.jpg");
image2 = cv::imread("data/homography/" + test_scene_ + "B.jpg");
}
if (image1.cols == 0)
{
fprintf(stderr, "A problem occured when loading the images for test scene '%s'\n", test_scene_.c_str());
return;
}
cv::Mat points; // The point correspondences, each is of format x1 y1 1 x2 y2 1
std::vector<int> ground_truth_labels; // The ground truth labeling provided in the dataset
// A function loading the points from files
readAnnotatedPoints("data/homography/" + test_scene_ + "_pts.txt",
points,
ground_truth_labels);
// The number of points in the datasets
const size_t N = points.rows; // The number of points in the scene
if (N == 0) // If there are no points, return
{
fprintf(stderr, "A problem occured when loading the annotated points for test scene '%s'\n", test_scene_.c_str());
return;
}
RobustHomographyEstimator estimator; // The robust homography estimator class containing the function for the fitting and residual calculation
Homography model; // The estimated model
// In this used datasets, the manually selected inliers are not all inliers but a subset of them.
// Therefore, the manually selected inliers are augmented as follows:
// (i) First, the implied model is estimated from the manually selected inliers.
// (ii) Second, the inliers of the ground truth model are selected.
refineManualLabeling<Homography, RobustHomographyEstimator>(
points,
ground_truth_labels,
estimator,
2.0);
// Select the inliers from the labeling
std::vector<int> ground_truth_inliers = getSubsetFromLabeling(ground_truth_labels, 1);
const size_t I = static_cast<double>(ground_truth_inliers.size());
printf("\tEstimated model = '%s'.\n", estimator.modelName().c_str());
printf("\tNumber of correspondences loaded = %d.\n", static_cast<int>(N));
printf("\tNumber of ground truth inliers = %d.\n", static_cast<int>(I));
// Define location of sub matrices in data matrix
cv::Rect roi1( 0, 0, 3, N );
cv::Rect roi2( 3, 0, 3, N );
std::vector<int> obtained_labeling(points.rows, 0);
std::chrono::time_point<std::chrono::system_clock> end,
start = std::chrono::system_clock::now();
cv::Mat homography = cv::findHomography(cv::Mat(points, roi1),
cv::Mat(points, roi2),
CV_RANSAC,
threshold_,
obtained_labeling);
end = std::chrono::system_clock::now();
std::chrono::duration<double> elapsed_seconds = end - start;
std::time_t end_time = std::chrono::system_clock::to_time_t(end);
printf("\tElapsed time: %f secs\n", elapsed_seconds.count());
// Applying the MAGSAC post-processing step using the OpenCV's output
// as the input.
if (with_magsac_post_processing_)
{
start = std::chrono::system_clock::now();
// Initializing MAGSAC
MAGSAC<cv::Mat, RobustHomographyEstimator, Homography> magsac;
magsac.setSigmaMax(MAX(3, threshold_)); // The maximum noise scale sigma allowed
Homography ransac_output, // The model estimated by OpenCV
polished_model; // The polished model
ransac_output.descriptor = homography;
RobustHomographyEstimator estimator; // The fundamental matrix estimator
ModelScore polished_model_score; // The score of the polished model
// Applying the post-processing step to polish the model parameters
magsac.postProcessing(points,
ransac_output,
polished_model,
polished_model_score,
estimator);
homography = polished_model.descriptor;
end = std::chrono::system_clock::now();
std::chrono::duration<double> elapsed_seconds = end - start;
std::time_t end_time = std::chrono::system_clock::to_time_t(end);
printf("\tProcessing time of the post-processing step: %f secs\n", elapsed_seconds.count());
}
// Compute the RMSE given the ground truth inliers
double rmse = 0, error;
size_t inlier_number = 0;
for (const auto& inlier_idx : ground_truth_inliers)
{
error = estimator.error(points.row(inlier_idx), homography);
rmse += error;
}
rmse = sqrt(rmse / static_cast<double>(I));
printf("\tRMSE error: %f px\n", rmse);
// Visualization part.
// Inliers are selected using threshold and the estimated model.
// This part is not necessary and is only for visualization purposes.
if (draw_results_)
{
// Draw the matches to the images
cv::Mat out_image;
drawMatches<double, int>(points, obtained_labeling, image1, image2, out_image);
// Show the matches
std::string window_name = "OpenCV's RANSAC";
showImage(out_image,
window_name,
1600,
900);
out_image.release();
}
// Clean up the memory occupied by the images
image1.release();
image2.release();
}
void opencvFundamentalMatrixFitting(
double ransac_confidence_,
double threshold_,
std::string test_scene_,
bool draw_results_,
const bool with_magsac_post_processing_)
{
printf("\tProcessed scene = '%s'.\n", test_scene_.c_str());
// Load the images of the current test scene
cv::Mat image1 = cv::imread("data/fundamental_matrix/" + test_scene_ + "A.png");
cv::Mat image2 = cv::imread("data/fundamental_matrix/" + test_scene_ + "B.png");
if (image1.cols == 0)
{
image1 = cv::imread("data/fundamental_matrix/" + test_scene_ + "A.jpg");
image2 = cv::imread("data/fundamental_matrix/" + test_scene_ + "B.jpg");
}
if (image1.cols == 0)
{
fprintf(stderr, "A problem occured when loading the images for test scene '%s'\n", test_scene_.c_str());
return;
}
cv::Mat points; // The point correspondences, each is of format x1 y1 1 x2 y2 1
std::vector<int> ground_truth_labels; // The ground truth labeling provided in the dataset
// A function loading the points from files
readAnnotatedPoints("data/fundamental_matrix/" + test_scene_ + "_pts.txt",
points,
ground_truth_labels);
// The number of points in the datasets
const size_t N = points.rows; // The number of points in the scene
if (N == 0) // If there are no points, return
{
fprintf(stderr, "A problem occured when loading the annotated points for test scene '%s'\n", test_scene_.c_str());
return;
}
FundamentalMatrixEstimator estimator; // The robust homography estimator class containing the function for the fitting and residual calculation
FundamentalMatrix model; // The estimated model
// In this used datasets, the manually selected inliers are not all inliers but a subset of them.
// Therefore, the manually selected inliers are augmented as follows:
// (i) First, the implied model is estimated from the manually selected inliers.
// (ii) Second, the inliers of the ground truth model are selected.
refineManualLabeling<FundamentalMatrix, FundamentalMatrixEstimator>(
points,
ground_truth_labels,
estimator,
0.35);
// Select the inliers from the labeling
std::vector<int> ground_truth_inliers = getSubsetFromLabeling(ground_truth_labels, 1);
const size_t I = static_cast<double>(ground_truth_inliers.size());
printf("\tEstimated model = '%s'.\n", estimator.modelName().c_str());
printf("\tNumber of correspondences loaded = %d.\n", static_cast<int>(N));
printf("\tNumber of ground truth inliers = %d.\n", static_cast<int>(I));
// Define location of sub matrices in data matrix
cv::Rect roi1( 0, 0, 3, N );
cv::Rect roi2( 3, 0, 3, N );
std::vector<uchar> obtained_labeling(points.rows, 0);
std::chrono::time_point<std::chrono::system_clock> end,
start = std::chrono::system_clock::now();
// Fundamental matrix estimation using the OpenCV's function
cv::Mat fundamental_matrix = cv::findFundamentalMat(cv::Mat(points, roi1),
cv::Mat(points, roi2),
CV_FM_RANSAC,
threshold_,
ransac_confidence_,
obtained_labeling);
end = std::chrono::system_clock::now();
std::chrono::duration<double> elapsed_seconds = end - start;
std::time_t end_time = std::chrono::system_clock::to_time_t(end);
printf("\tElapsed time: %f secs\n", elapsed_seconds.count());
// Applying the MAGSAC post-processing step using the OpenCV's output
// as the input.
if (with_magsac_post_processing_)
{
start = std::chrono::system_clock::now();
// Initializing MAGSAC
MAGSAC<cv::Mat, FundamentalMatrixEstimator, FundamentalMatrix> magsac;
magsac.setSigmaMax(1.33 * threshold_); // The maximum noise scale sigma allowed
FundamentalMatrix ransac_output, // The model estimated by OpenCV
polished_model; // The polished model
ransac_output.descriptor = fundamental_matrix;
FundamentalMatrixEstimator estimator; // The fundamental matrix estimator
ModelScore tmp_score;
double polished_model_score, // The score of the polished model
original_model_score; // The score of the original model
// Applying the post-processing step to polish the model parameters
magsac.postProcessing(points,
ransac_output,
polished_model,
tmp_score,
estimator);
// Get the marginalized score of the RANSAC output and the polished model
// to decide which one to use.
double tmp_inlier_ratio;
magsac.getSigmaScore(
points,
ransac_output,
estimator,
tmp_inlier_ratio,
polished_model_score);
magsac.getSigmaScore(
points,
polished_model,
estimator,
tmp_inlier_ratio,
original_model_score);
if (polished_model_score < original_model_score)
fundamental_matrix = polished_model.descriptor;
end = std::chrono::system_clock::now();
std::chrono::duration<double> elapsed_seconds = end - start;
std::time_t end_time = std::chrono::system_clock::to_time_t(end);
printf("\tProcessing time of the post-processing step: %f secs\n", elapsed_seconds.count());
}
// Compute the RMSE given the ground truth inliers
double rmse = 0, error;
size_t inlier_number = 0;
for (const auto& inlier_idx : ground_truth_inliers)
{
error = estimator.error(points.row(inlier_idx), fundamental_matrix);
rmse += error;
}
rmse = sqrt(rmse / static_cast<double>(I));
printf("\tRMSE error: %f px\n", rmse);
// Visualization part.
// Inliers are selected using threshold and the estimated model.
// This part is not necessary and is only for visualization purposes.
if (draw_results_)
{
// Draw the matches to the images
cv::Mat out_image;
drawMatches<double, uchar>(points, obtained_labeling, image1, image2, out_image);
// Show the matches
std::string window_name = "OpenCV's RANSAC";
showImage(out_image,
window_name,
1600,
900);
out_image.release();
}
// Clean up the memory occupied by the images
image1.release();
image2.release();
}