Skip to content

Commit 5384a22

Browse files
committed
Adapted estimateNewCameraMatrix to make it work with pincushion-like distortion.
1 parent 2c4ed7f commit 5384a22

File tree

1 file changed

+23
-16
lines changed

1 file changed

+23
-16
lines changed

modules/calib3d/src/fisheye.cpp

Lines changed: 23 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -517,12 +517,16 @@ void cv::fisheye::estimateNewCameraMatrixForUndistortRectify(InputArray K, Input
517517
int w = image_size.width, h = image_size.height;
518518
balance = std::min(std::max(balance, 0.0), 1.0);
519519

520-
cv::Mat points(1, 4, CV_64FC2);
520+
cv::Mat points(1, 8, CV_64FC2);
521521
Vec2d* pptr = points.ptr<Vec2d>();
522-
pptr[0] = Vec2d(w/2, 0);
523-
pptr[1] = Vec2d(w, h/2);
524-
pptr[2] = Vec2d(w/2, h);
525-
pptr[3] = Vec2d(0, h/2);
522+
pptr[0] = Vec2d(0, 0);
523+
pptr[1] = Vec2d(w/2, 0);
524+
pptr[2] = Vec2d(w, 0);
525+
pptr[3] = Vec2d(w, h/2);
526+
pptr[4] = Vec2d(w, h);
527+
pptr[5] = Vec2d(w/2, h);
528+
pptr[6] = Vec2d(0, h);
529+
pptr[7] = Vec2d(0, h/2);
526530

527531
#if 0
528532
const int N = 10;
@@ -532,7 +536,6 @@ void cv::fisheye::estimateNewCameraMatrixForUndistortRectify(InputArray K, Input
532536
{
533537
pptr[k++] = Vec2d(w/2, 0) - Vec2d(w/8, 0) + Vec2d(w/4/N*i, 0);
534538
pptr[k++] = Vec2d(w/2, h-1) - Vec2d(w/8, h-1) + Vec2d(w/4/N*i, h-1);
535-
536539
pptr[k++] = Vec2d(0, h/2) - Vec2d(0, h/8) + Vec2d(0, h/4/N*i);
537540
pptr[k++] = Vec2d(w-1, h/2) - Vec2d(w-1, h/8) + Vec2d(w-1, h/4/N*i);
538541
}
@@ -553,10 +556,14 @@ void cv::fisheye::estimateNewCameraMatrixForUndistortRectify(InputArray K, Input
553556
double minx = DBL_MAX, miny = DBL_MAX, maxx = -DBL_MAX, maxy = -DBL_MAX;
554557
for(size_t i = 0; i < points.total(); ++i)
555558
{
556-
miny = std::min(miny, pptr[i][1]);
557-
maxy = std::max(maxy, pptr[i][1]);
558-
minx = std::min(minx, pptr[i][0]);
559-
maxx = std::max(maxx, pptr[i][0]);
559+
if(i!=1 && i!=5){
560+
minx = std::min(minx, std::abs(pptr[i][0]-cn[0]));
561+
}
562+
if(i!=3 && i!=7){
563+
miny = std::min(miny, std::abs(pptr[i][1]-cn[1]));
564+
}
565+
maxy = std::max(maxy, std::abs(pptr[i][1]-cn[1]));
566+
maxx = std::max(maxx, std::abs(pptr[i][0]-cn[0]));
560567
}
561568

562569
#if 0
@@ -570,13 +577,13 @@ void cv::fisheye::estimateNewCameraMatrixForUndistortRectify(InputArray K, Input
570577
}
571578
#endif
572579

573-
double f1 = w * 0.5/(cn[0] - minx);
574-
double f2 = w * 0.5/(maxx - cn[0]);
575-
double f3 = h * 0.5 * aspect_ratio/(cn[1] - miny);
576-
double f4 = h * 0.5 * aspect_ratio/(maxy - cn[1]);
580+
double f1 = w * 0.5/(minx);
581+
double f2 = w * 0.5/(maxx);
582+
double f3 = h * 0.5 * aspect_ratio/(miny);
583+
double f4 = h * 0.5 * aspect_ratio/(maxy);
577584

578-
double fmin = std::min(f1, std::min(f2, std::min(f3, f4)));
579-
double fmax = std::max(f1, std::max(f2, std::max(f3, f4)));
585+
double fmax = std::max(f1, f3);
586+
double fmin = std::min(f2, f4);
580587

581588
double f = balance * fmin + (1.0 - balance) * fmax;
582589
f *= fov_scale > 0 ? 1.0/fov_scale : 1.0;

0 commit comments

Comments
 (0)