@@ -396,10 +396,10 @@ double CameraParameters::calc_chisqr(
396
396
397
397
int i = 0 ;
398
398
for (; ls_it != calibrationSegments.end (); ls_it++) {
399
- auto imgPts_it = (*ls_it).imgPts .begin ();
400
- for (; imgPts_it != (*ls_it).imgPts .end (); imgPts_it++) {
399
+ auto imgPts_it = (*ls_it).points .begin ();
400
+ for (; imgPts_it != (*ls_it).points .end (); imgPts_it++) {
401
401
// Integrate only if a valid point on line
402
- if (imgPts_it->second ) {
402
+ if (imgPts_it->detected ) {
403
403
GVector::vector2d<double > proj_p;
404
404
double alpha = p_alpha (i) + p (STATE_SPACE_DIMENSION + i);
405
405
@@ -417,10 +417,10 @@ double CameraParameters::calc_chisqr(
417
417
// Project into image plane
418
418
field2image (alpha_point, proj_p, p);
419
419
420
- chisqr += (proj_p.x -imgPts_it->first .x ) *
421
- (proj_p.x -imgPts_it->first .x ) * cov_lsx_inv +
422
- (proj_p.y - imgPts_it->first .y ) *
423
- (proj_p.y - imgPts_it->first .y ) * cov_lsy_inv;
420
+ chisqr += (proj_p.x -imgPts_it->img_point .x ) *
421
+ (proj_p.x -imgPts_it->img_point .x ) * cov_lsx_inv +
422
+ (proj_p.y - imgPts_it->img_point .y ) *
423
+ (proj_p.y - imgPts_it->img_point .y ) * cov_lsy_inv;
424
424
i++;
425
425
}
426
426
}
@@ -445,14 +445,99 @@ double CameraParameters::do_calibration(int cal_type) {
445
445
}
446
446
447
447
if (use_opencv_model->getBool ()) {
448
- return calibrateExtrinsicModel (p_f, p_i, cal_type);
448
+ calibrateExtrinsicModel (p_f, p_i, cal_type);
449
449
} else {
450
- return calibrate (p_f, p_i, cal_type);
450
+ calibrate (p_f, p_i, cal_type);
451
451
}
452
+
453
+ updateCalibrationDataPoints ();
454
+ if (cal_type & FOUR_POINT_INITIAL) {
455
+ return calculateFourPointRmse (p_f, p_i);
456
+ }
457
+ return calculateCalibrationDataPointsRmse ();
452
458
}
453
459
454
- void CameraParameters::reset () const {
460
+ double CameraParameters::calculateFourPointRmse (std::vector<GVector::vector3d<double > > &p_f,
461
+ std::vector<GVector::vector2d<double > > &p_i) const {
462
+ auto it_p_f = p_f.begin ();
463
+ auto it_p_i = p_i.begin ();
464
+ double sum = 0 ;
465
+ for (; it_p_f != p_f.end (); it_p_f++, it_p_i++) {
466
+ GVector::vector2d<double > proj_p;
467
+ field2image (*it_p_f, proj_p);
468
+ GVector::vector3d<double > some_point;
469
+ image2field (some_point,proj_p, 0 );
455
470
471
+ double diff_x = proj_p.x - it_p_i->x ;
472
+ double diff_y = proj_p.y - it_p_i->y ;
473
+ sum += diff_x * diff_x + diff_y * diff_y;
474
+ }
475
+ return sqrt (sum / (double ) p_f.size ());
476
+ }
477
+
478
+ void CameraParameters::updateCalibrationDataPoints () {
479
+ for (auto & segment : calibrationSegments) {
480
+ if (!segment.straightLine ) {
481
+ continue ;
482
+ }
483
+
484
+ for (auto & imgPt : segment.points ) {
485
+ if (imgPt.detected ) {
486
+ image2field (imgPt.world_point , imgPt.img_point , 0.0 );
487
+
488
+ GVector::vector2d<double > line0 (segment.p1 .x , segment.p1 .y );
489
+ GVector::vector2d<double > line1 (segment.p2 .x , segment.p2 .y );
490
+ GVector::vector2d<double > point (imgPt.world_point .x , imgPt.world_point .y );
491
+ GVector::vector2d<double > closestPoint = GVector::closest_point_on_line (line0, line1, point);
492
+ imgPt.world_closestPointToSegment .x = closestPoint.x ;
493
+ imgPt.world_closestPointToSegment .y = closestPoint.y ;
494
+ field2image (imgPt.world_closestPointToSegment , imgPt.img_closestPointToSegment );
495
+ }
496
+ }
497
+ }
498
+ }
499
+
500
+ double CameraParameters::calculateCalibrationDataPointsRmse () {
501
+ double rmse_sum_all = 0 ;
502
+ int rmse_cnt_all = 0 ;
503
+ for (auto & segment : calibrationSegments) {
504
+ if (!segment.straightLine ) {
505
+ continue ;
506
+ }
507
+
508
+ double rmse_sum_segment = 0 ;
509
+ int rmse_cnt_segment = 0 ;
510
+
511
+ for (auto imgPt : segment.points ) {
512
+ if (imgPt.detected ) {
513
+ double distance = (imgPt.img_closestPointToSegment - imgPt.img_point ).length ();
514
+ rmse_sum_segment += distance*distance;
515
+ rmse_cnt_segment++;
516
+ }
517
+ }
518
+ std::cout << " segment ["
519
+ << segment.p1 .x << " ," << segment.p1 .y
520
+ << " ] -> ["
521
+ << segment.p2 .x << " ," << segment.p2 .y
522
+ << " ] RMSE: " << sqrt (rmse_sum_segment / rmse_cnt_segment)
523
+ << std::endl;
524
+ rmse_sum_all += rmse_sum_segment;
525
+ rmse_cnt_all += rmse_cnt_segment;
526
+ }
527
+ double rmse = sqrt (rmse_sum_all / rmse_cnt_all);
528
+ std::cout << " Calibration segment RMSE: " << rmse << std::endl;
529
+ return rmse;
530
+ }
531
+
532
+ CameraParameters::CalibrationDataPoint::CalibrationDataPoint (GVector::vector2d<double > img_point, bool detected)
533
+ : detected(detected),
534
+ img_point(img_point),
535
+ img_closestPointToSegment(0 ,0 ),
536
+ world_point(0 ,0 ,0 ),
537
+ world_closestPointToSegment(0 ,0 ,0 )
538
+ {}
539
+
540
+ void CameraParameters::reset () const {
456
541
focal_length->resetToDefault ();
457
542
principal_point_x->resetToDefault ();
458
543
principal_point_y->resetToDefault ();
@@ -496,7 +581,7 @@ bool contains(const cv::Rect& rect, const cv::Point2d& pt, double margin) {
496
581
&& rect.y - margin <= pt.y && pt.y < rect.y + rect.height + margin * 2 ;
497
582
}
498
583
499
- double CameraParameters::calibrateExtrinsicModel (
584
+ void CameraParameters::calibrateExtrinsicModel (
500
585
std::vector<GVector::vector3d<double >> &p_f,
501
586
std::vector<GVector::vector2d<double >> &p_i,
502
587
int cal_type) const {
@@ -517,12 +602,12 @@ double CameraParameters::calibrateExtrinsicModel(
517
602
518
603
if (calib_field_points.size () < 4 ) {
519
604
std::cerr << " Not enough calibration points: " << calib_field_points.size () << std::endl;
520
- return - 1 ;
605
+ return ;
521
606
}
522
607
523
608
std::vector<std::vector<cv::Point3f>> object_points (1 );
524
609
std::vector<std::vector<cv::Point2f>> image_points (1 );
525
- cv::Size imageSize (2448 , 2048 );
610
+ cv::Size imageSize (0 , 0 ); // should not be required
526
611
std::vector<cv::Mat> rvecs;
527
612
std::vector<cv::Mat> tvecs;
528
613
@@ -581,17 +666,6 @@ double CameraParameters::calibrateExtrinsicModel(
581
666
// Update parameters
582
667
intrinsic_parameters->updateConfigValues ();
583
668
extrinsic_parameters->updateConfigValues ();
584
-
585
- // validate
586
- std::vector<cv::Point2d> imagePoints_out;
587
- cv::projectPoints (calib_field_points, extrinsic_parameters->rvec , extrinsic_parameters->tvec , intrinsic_parameters->camera_mat , intrinsic_parameters->dist_coeffs , imagePoints_out);
588
- double sum = 0 ;
589
- for (uint i = 0 ; i < imagePoints_out.size (); i++) {
590
- auto diff = calib_image_points[i] - imagePoints_out[i];
591
- sum += cv::norm (diff) * cv::norm (diff);
592
- }
593
- double rmse = sqrt (sum / (double ) calib_field_points.size ());
594
- return rmse;
595
669
}
596
670
597
671
void CameraParameters::detectCalibrationCorners () {
@@ -615,10 +689,9 @@ void CameraParameters::detectCalibrationCorners() {
615
689
616
690
// collect all valid image points
617
691
vector<cv::Point2d> points_img;
618
- for (auto imgPt : calibrationData.imgPts ) {
619
- bool detected = imgPt.second ;
620
- if (detected) {
621
- auto p_img = imgPt.first ;
692
+ for (auto point : calibrationData.points ) {
693
+ if (point.detected ) {
694
+ auto p_img = point.img_point ;
622
695
points_img.emplace_back (p_img.x , p_img.y );
623
696
}
624
697
}
@@ -689,7 +762,7 @@ void CameraParameters::detectCalibrationCorners() {
689
762
}
690
763
}
691
764
692
- double CameraParameters::calibrate (
765
+ void CameraParameters::calibrate (
693
766
std::vector<GVector::vector3d<double > > &p_f,
694
767
std::vector<GVector::vector2d<double > > &p_i, int cal_type) {
695
768
assert (p_f.size () == p_i.size ());
@@ -709,9 +782,9 @@ double CameraParameters::calibrate(
709
782
int count_alpha (0 ); // The number of well detected line segment points
710
783
auto ls_it = calibrationSegments.begin ();
711
784
for (; ls_it != calibrationSegments.end (); ls_it++) {
712
- auto pts_it = (*ls_it).imgPts .begin ();
713
- for (; pts_it != (*ls_it).imgPts .end (); pts_it++) {
714
- if (pts_it->second ) count_alpha ++;
785
+ auto pts_it = (*ls_it).points .begin ();
786
+ for (; pts_it != (*ls_it).points .end (); pts_it++) {
787
+ if (pts_it->detected ) count_alpha ++;
715
788
}
716
789
}
717
790
@@ -722,11 +795,11 @@ double CameraParameters::calibrate(
722
795
count_alpha = 0 ;
723
796
ls_it = calibrationSegments.begin ();
724
797
for (; ls_it != calibrationSegments.end (); ls_it++) {
725
- auto pts_it = (*ls_it).imgPts .begin ();
798
+ auto pts_it = (*ls_it).points .begin ();
726
799
auto alphas_it = (*ls_it).alphas .begin ();
727
- for (; pts_it != (*ls_it).imgPts .end () &&
800
+ for (; pts_it != (*ls_it).points .end () &&
728
801
alphas_it != (*ls_it).alphas .end (); pts_it++, alphas_it++) {
729
- if (pts_it->second ) {
802
+ if (pts_it->detected ) {
730
803
p_alpha (count_alpha++) = (*alphas_it);
731
804
}
732
805
}
@@ -814,9 +887,9 @@ double CameraParameters::calibrate(
814
887
815
888
int i = 0 ;
816
889
for (; ls_it != calibrationSegments.end (); ls_it++) {
817
- auto pts_it = (*ls_it).imgPts .begin ();
818
- for (; pts_it != (*ls_it).imgPts .end (); pts_it++) {
819
- if (pts_it->second ) {
890
+ auto pts_it = (*ls_it).points .begin ();
891
+ for (; pts_it != (*ls_it).points .end (); pts_it++) {
892
+ if (pts_it->detected ) {
820
893
GVector::vector2d<double > proj_p;
821
894
GVector::vector3d<double >alpha_point;
822
895
if (ls_it->straightLine ) {
@@ -826,7 +899,7 @@ double CameraParameters::calibrate(
826
899
alpha_point = ls_it->center + ls_it->radius *GVector::vector3d<double >(cos (theta),sin (theta),0.0 );
827
900
}
828
901
field2image (alpha_point, proj_p, p);
829
- proj_p = proj_p - (*pts_it).first ;
902
+ proj_p = proj_p - (*pts_it).img_point ;
830
903
831
904
J.setZero ();
832
905
@@ -837,9 +910,9 @@ double CameraParameters::calibrate(
837
910
p_diff (j) = p_diff (j) + epsilon;
838
911
GVector::vector2d<double > proj_p_diff;
839
912
field2image (alpha_point, proj_p_diff, p_diff);
840
- J (0 ,j) = ((proj_p_diff.x - (*pts_it).first .x ) - proj_p.x ) /
913
+ J (0 ,j) = ((proj_p_diff.x - (*pts_it).img_point .x ) - proj_p.x ) /
841
914
epsilon;
842
- J (1 ,j) = ((proj_p_diff.y - (*pts_it).first .y ) - proj_p.y ) /
915
+ J (1 ,j) = ((proj_p_diff.y - (*pts_it).img_point .y ) - proj_p.y ) /
843
916
epsilon;
844
917
}
845
918
@@ -859,9 +932,9 @@ double CameraParameters::calibrate(
859
932
GVector::vector2d<double > proj_p_diff;
860
933
field2image (alpha_point, proj_p_diff);
861
934
J (0 ,STATE_SPACE_DIMENSION + i) =
862
- ((proj_p_diff.x - (*pts_it).first .x ) - proj_p.x ) / epsilon;
935
+ ((proj_p_diff.x - (*pts_it).img_point .x ) - proj_p.x ) / epsilon;
863
936
J (1 ,STATE_SPACE_DIMENSION + i) =
864
- ((proj_p_diff.y - (*pts_it).first .y ) - proj_p.y ) / epsilon;
937
+ ((proj_p_diff.y - (*pts_it).img_point .y ) - proj_p.y ) / epsilon;
865
938
866
939
alpha += J.transpose () * cov_ls_inv * J;
867
940
beta += J.transpose () * cov_ls_inv *
@@ -980,7 +1053,7 @@ double CameraParameters::calibrate(
980
1053
GVector::vector2d<double > proj_p;
981
1054
field2image (*it_p_f, proj_p);
982
1055
GVector::vector3d<double > some_point;
983
- image2field (some_point,proj_p, 150 );
1056
+ image2field (some_point,proj_p, 0 );
984
1057
std::cerr << " Point in world: (" << it_p_f->x << " ," << it_p_f->y << " ,"
985
1058
<< it_p_f->z << " )" << std::endl;
986
1059
std::cerr << " Point should be at (" << it_p_i->x << " ," << it_p_i->y
@@ -991,7 +1064,7 @@ double CameraParameters::calibrate(
991
1064
double diff_y = proj_p.y - it_p_i->y ;
992
1065
corner_x += diff_x * diff_x;
993
1066
corner_y += diff_y * diff_y;
994
- corner_sum += sqrt ( diff_x * diff_x + diff_y * diff_y) ;
1067
+ corner_sum += diff_x * diff_x + diff_y * diff_y;
995
1068
}
996
1069
997
1070
std::cerr << " RESIDUAL CORNER POINTS: "
@@ -1009,9 +1082,9 @@ double CameraParameters::calibrate(
1009
1082
1010
1083
int i = 0 ;
1011
1084
for (; ls_it != calibrationSegments.end (); ls_it++) {
1012
- auto pts_it = (*ls_it).imgPts .begin ();
1013
- for (; pts_it != (*ls_it).imgPts .end (); pts_it++) {
1014
- if (pts_it->second ) {
1085
+ auto pts_it = (*ls_it).points .begin ();
1086
+ for (; pts_it != (*ls_it).points .end (); pts_it++) {
1087
+ if (pts_it->detected ) {
1015
1088
GVector::vector2d<double > proj_p;
1016
1089
double pts_alpha = p_alpha (i);
1017
1090
GVector::vector3d<double >alpha_point;
@@ -1027,8 +1100,8 @@ double CameraParameters::calibrate(
1027
1100
1028
1101
field2image (alpha_point, proj_p, p);
1029
1102
1030
- line_x += (proj_p.x - pts_it->first .x ) * (proj_p.x - pts_it->first .x );
1031
- line_y += (proj_p.y - pts_it->first .y ) * (proj_p.y - pts_it->first .y );
1103
+ line_x += (proj_p.x - pts_it->img_point .x ) * (proj_p.x - pts_it->img_point .x );
1104
+ line_y += (proj_p.y - pts_it->img_point .y ) * (proj_p.y - pts_it->img_point .y );
1032
1105
1033
1106
i++;
1034
1107
}
@@ -1040,7 +1113,6 @@ double CameraParameters::calibrate(
1040
1113
<< sqrt (line_y/(double )i) << std::endl;
1041
1114
}
1042
1115
}
1043
- return sqrt (corner_sum/4 );
1044
1116
#else
1045
1117
return -1 ;
1046
1118
#endif
0 commit comments