1 /*M///////////////////////////////////////////////////////////////////////////////////////
3 // IMPORTANT: READ BEFORE DOWNLOADING, COPYING, INSTALLING OR USING.
5 // By downloading, copying, installing or using the software you agree to this license.
6 // If you do not agree to this license, do not download, install,
7 // copy or use the software.
11 // For Open Source Computer Vision Library
13 // Copyright (C) 2000-2008, Intel Corporation, all rights reserved.
14 // Copyright (C) 2009, Willow Garage Inc., all rights reserved.
15 // Third party copyrights are property of their respective owners.
17 // Redistribution and use in source and binary forms, with or without modification,
18 // are permitted provided that the following conditions are met:
20 // * Redistribution's of source code must retain the above copyright notice,
21 // this list of conditions and the following disclaimer.
23 // * Redistribution's in binary form must reproduce the above copyright notice,
24 // this list of conditions and the following disclaimer in the documentation
25 // and/or other materials provided with the distribution.
27 // * The name of the copyright holders may not be used to endorse or promote products
28 // derived from this software without specific prior written permission.
30 // This software is provided by the copyright holders and contributors "as is" and
31 // any express or implied warranties, including, but not limited to, the implied
32 // warranties of merchantability and fitness for a particular purpose are disclaimed.
33 // In no event shall the Intel Corporation or contributors be liable for any direct,
34 // indirect, incidental, special, exemplary, or consequential damages
35 // (including, but not limited to, procurement of substitute goods or services;
36 // loss of use, data, or profits; or business interruption) however caused
37 // and on any theory of liability, whether in contract, strict liability,
38 // or tort (including negligence or otherwise) arising in any way out of
39 // the use of this software, even if advised of the possibility of such damage.
43 #include "precomp.hpp"
45 cv::Mat cv::getDefaultNewCameraMatrix( InputArray _cameraMatrix, Size imgsize,
46 bool centerPrincipalPoint )
48 Mat cameraMatrix = _cameraMatrix.getMat();
49 if( !centerPrincipalPoint && cameraMatrix.type() == CV_64F )
53 cameraMatrix.convertTo(newCameraMatrix, CV_64F);
54 if( centerPrincipalPoint )
56 ((double*)newCameraMatrix.data)[2] = (imgsize.width-1)*0.5;
57 ((double*)newCameraMatrix.data)[5] = (imgsize.height-1)*0.5;
59 return newCameraMatrix;
62 void cv::initUndistortRectifyMap( InputArray _cameraMatrix, InputArray _distCoeffs,
63 InputArray _matR, InputArray _newCameraMatrix,
64 Size size, int m1type, OutputArray _map1, OutputArray _map2 )
66 Mat cameraMatrix = _cameraMatrix.getMat(), distCoeffs = _distCoeffs.getMat();
67 Mat matR = _matR.getMat(), newCameraMatrix = _newCameraMatrix.getMat();
71 CV_Assert( m1type == CV_16SC2 || m1type == CV_32FC1 || m1type == CV_32FC2 );
72 _map1.create( size, m1type );
73 Mat map1 = _map1.getMat(), map2;
74 if( m1type != CV_32FC2 )
76 _map2.create( size, m1type == CV_16SC2 ? CV_16UC1 : CV_32FC1 );
77 map2 = _map2.getMat();
82 Mat_<double> R = Mat_<double>::eye(3, 3);
83 Mat_<double> A = Mat_<double>(cameraMatrix), Ar;
85 if( newCameraMatrix.data )
86 Ar = Mat_<double>(newCameraMatrix);
88 Ar = getDefaultNewCameraMatrix( A, size, true );
91 R = Mat_<double>(matR);
94 distCoeffs = Mat_<double>(distCoeffs);
97 distCoeffs.create(8, 1, CV_64F);
101 CV_Assert( A.size() == Size(3,3) && A.size() == R.size() );
102 CV_Assert( Ar.size() == Size(3,3) || Ar.size() == Size(4, 3));
103 Mat_<double> iR = (Ar.colRange(0,3)*R).inv(DECOMP_LU);
104 const double* ir = &iR(0,0);
106 double u0 = A(0, 2), v0 = A(1, 2);
107 double fx = A(0, 0), fy = A(1, 1);
109 CV_Assert( distCoeffs.size() == Size(1, 4) || distCoeffs.size() == Size(4, 1) ||
110 distCoeffs.size() == Size(1, 5) || distCoeffs.size() == Size(5, 1) ||
111 distCoeffs.size() == Size(1, 8) || distCoeffs.size() == Size(8, 1));
113 if( distCoeffs.rows != 1 && !distCoeffs.isContinuous() )
114 distCoeffs = distCoeffs.t();
116 double k1 = ((double*)distCoeffs.data)[0];
117 double k2 = ((double*)distCoeffs.data)[1];
118 double p1 = ((double*)distCoeffs.data)[2];
119 double p2 = ((double*)distCoeffs.data)[3];
120 double k3 = distCoeffs.cols + distCoeffs.rows - 1 >= 5 ? ((double*)distCoeffs.data)[4] : 0.;
121 double k4 = distCoeffs.cols + distCoeffs.rows - 1 >= 8 ? ((double*)distCoeffs.data)[5] : 0.;
122 double k5 = distCoeffs.cols + distCoeffs.rows - 1 >= 8 ? ((double*)distCoeffs.data)[6] : 0.;
123 double k6 = distCoeffs.cols + distCoeffs.rows - 1 >= 8 ? ((double*)distCoeffs.data)[7] : 0.;
125 for( int i = 0; i < size.height; i++ )
127 float* m1f = (float*)(map1.data + map1.step*i);
128 float* m2f = (float*)(map2.data + map2.step*i);
129 short* m1 = (short*)m1f;
130 ushort* m2 = (ushort*)m2f;
131 double _x = i*ir[1] + ir[2], _y = i*ir[4] + ir[5], _w = i*ir[7] + ir[8];
133 for( int j = 0; j < size.width; j++, _x += ir[0], _y += ir[3], _w += ir[6] )
135 double w = 1./_w, x = _x*w, y = _y*w;
136 double x2 = x*x, y2 = y*y;
137 double r2 = x2 + y2, _2xy = 2*x*y;
138 double kr = (1 + ((k3*r2 + k2)*r2 + k1)*r2)/(1 + ((k6*r2 + k5)*r2 + k4)*r2);
139 double u = fx*(x*kr + p1*_2xy + p2*(r2 + 2*x2)) + u0;
140 double v = fy*(y*kr + p1*(r2 + 2*y2) + p2*_2xy) + v0;
141 if( m1type == CV_16SC2 )
143 int iu = saturate_cast<int>(u*INTER_TAB_SIZE);
144 int iv = saturate_cast<int>(v*INTER_TAB_SIZE);
145 m1[j*2] = (short)(iu >> INTER_BITS);
146 m1[j*2+1] = (short)(iv >> INTER_BITS);
147 m2[j] = (ushort)((iv & (INTER_TAB_SIZE-1))*INTER_TAB_SIZE + (iu & (INTER_TAB_SIZE-1)));
149 else if( m1type == CV_32FC1 )
157 m1f[j*2+1] = (float)v;
164 void cv::undistort( InputArray _src, OutputArray _dst, InputArray _cameraMatrix,
165 InputArray _distCoeffs, InputArray _newCameraMatrix )
167 Mat src = _src.getMat(), cameraMatrix = _cameraMatrix.getMat();
168 Mat distCoeffs = _distCoeffs.getMat(), newCameraMatrix = _newCameraMatrix.getMat();
170 _dst.create( src.size(), src.type() );
171 Mat dst = _dst.getMat();
173 CV_Assert( dst.data != src.data );
175 int stripe_size0 = std::min(std::max(1, (1 << 12) / std::max(src.cols, 1)), src.rows);
176 Mat map1(stripe_size0, src.cols, CV_16SC2), map2(stripe_size0, src.cols, CV_16UC1);
178 Mat_<double> A, Ar, I = Mat_<double>::eye(3,3);
180 cameraMatrix.convertTo(A, CV_64F);
181 if( distCoeffs.data )
182 distCoeffs = Mat_<double>(distCoeffs);
185 distCoeffs.create(5, 1, CV_64F);
189 if( newCameraMatrix.data )
190 newCameraMatrix.convertTo(Ar, CV_64F);
194 double v0 = Ar(1, 2);
195 for( int y = 0; y < src.rows; y += stripe_size0 )
197 int stripe_size = std::min( stripe_size0, src.rows - y );
199 Mat map1_part = map1.rowRange(0, stripe_size),
200 map2_part = map2.rowRange(0, stripe_size),
201 dst_part = dst.rowRange(y, y + stripe_size);
203 initUndistortRectifyMap( A, distCoeffs, I, Ar, Size(src.cols, stripe_size),
204 map1_part.type(), map1_part, map2_part );
205 remap( src, dst_part, map1_part, map2_part, INTER_LINEAR, BORDER_CONSTANT );
211 cvUndistort2( const CvArr* srcarr, CvArr* dstarr, const CvMat* Aarr, const CvMat* dist_coeffs, const CvMat* newAarr )
213 cv::Mat src = cv::cvarrToMat(srcarr), dst = cv::cvarrToMat(dstarr), dst0 = dst;
214 cv::Mat A = cv::cvarrToMat(Aarr), distCoeffs = cv::cvarrToMat(dist_coeffs), newA;
216 newA = cv::cvarrToMat(newAarr);
218 CV_Assert( src.size() == dst.size() && src.type() == dst.type() );
219 cv::undistort( src, dst, A, distCoeffs, newA );
223 CV_IMPL void cvInitUndistortMap( const CvMat* Aarr, const CvMat* dist_coeffs,
224 CvArr* mapxarr, CvArr* mapyarr )
226 cv::Mat A = cv::cvarrToMat(Aarr), distCoeffs = cv::cvarrToMat(dist_coeffs);
227 cv::Mat mapx = cv::cvarrToMat(mapxarr), mapy, mapx0 = mapx, mapy0;
230 mapy0 = mapy = cv::cvarrToMat(mapyarr);
232 cv::initUndistortRectifyMap( A, distCoeffs, cv::Mat(), A,
233 mapx.size(), mapx.type(), mapx, mapy );
234 CV_Assert( mapx0.data == mapx.data && mapy0.data == mapy.data );
238 cvInitUndistortRectifyMap( const CvMat* Aarr, const CvMat* dist_coeffs,
239 const CvMat *Rarr, const CvMat* ArArr, CvArr* mapxarr, CvArr* mapyarr )
241 cv::Mat A = cv::cvarrToMat(Aarr), distCoeffs, R, Ar;
242 cv::Mat mapx = cv::cvarrToMat(mapxarr), mapy, mapx0 = mapx, mapy0;
245 mapy0 = mapy = cv::cvarrToMat(mapyarr);
248 distCoeffs = cv::cvarrToMat(dist_coeffs);
250 R = cv::cvarrToMat(Rarr);
252 Ar = cv::cvarrToMat(ArArr);
254 cv::initUndistortRectifyMap( A, distCoeffs, R, Ar, mapx.size(), mapx.type(), mapx, mapy );
255 CV_Assert( mapx0.data == mapx.data && mapy0.data == mapy.data );
259 void cvUndistortPoints( const CvMat* _src, CvMat* _dst, const CvMat* _cameraMatrix,
260 const CvMat* _distCoeffs,
261 const CvMat* matR, const CvMat* matP )
263 double A[3][3], RR[3][3], k[8]={0,0,0,0,0,0,0,0}, fx, fy, ifx, ify, cx, cy;
264 CvMat matA=cvMat(3, 3, CV_64F, A), _Dk;
265 CvMat _RR=cvMat(3, 3, CV_64F, RR);
266 const CvPoint2D32f* srcf;
267 const CvPoint2D64f* srcd;
272 int i, j, n, iters = 1;
274 CV_Assert( CV_IS_MAT(_src) && CV_IS_MAT(_dst) &&
275 (_src->rows == 1 || _src->cols == 1) &&
276 (_dst->rows == 1 || _dst->cols == 1) &&
277 _src->cols + _src->rows - 1 == _dst->rows + _dst->cols - 1 &&
278 (CV_MAT_TYPE(_src->type) == CV_32FC2 || CV_MAT_TYPE(_src->type) == CV_64FC2) &&
279 (CV_MAT_TYPE(_dst->type) == CV_32FC2 || CV_MAT_TYPE(_dst->type) == CV_64FC2));
281 CV_Assert( CV_IS_MAT(_cameraMatrix) &&
282 _cameraMatrix->rows == 3 && _cameraMatrix->cols == 3 );
284 cvConvert( _cameraMatrix, &matA );
288 CV_Assert( CV_IS_MAT(_distCoeffs) &&
289 (_distCoeffs->rows == 1 || _distCoeffs->cols == 1) &&
290 (_distCoeffs->rows*_distCoeffs->cols == 4 ||
291 _distCoeffs->rows*_distCoeffs->cols == 5 ||
292 _distCoeffs->rows*_distCoeffs->cols == 8));
294 _Dk = cvMat( _distCoeffs->rows, _distCoeffs->cols,
295 CV_MAKETYPE(CV_64F,CV_MAT_CN(_distCoeffs->type)), k);
297 cvConvert( _distCoeffs, &_Dk );
303 CV_Assert( CV_IS_MAT(matR) && matR->rows == 3 && matR->cols == 3 );
304 cvConvert( matR, &_RR );
312 CvMat _P3x3, _PP=cvMat(3, 3, CV_64F, PP);
313 CV_Assert( CV_IS_MAT(matP) && matP->rows == 3 && (matP->cols == 3 || matP->cols == 4));
314 cvConvert( cvGetCols(matP, &_P3x3, 0, 3), &_PP );
315 cvMatMul( &_PP, &_RR, &_RR );
318 srcf = (const CvPoint2D32f*)_src->data.ptr;
319 srcd = (const CvPoint2D64f*)_src->data.ptr;
320 dstf = (CvPoint2D32f*)_dst->data.ptr;
321 dstd = (CvPoint2D64f*)_dst->data.ptr;
322 stype = CV_MAT_TYPE(_src->type);
323 dtype = CV_MAT_TYPE(_dst->type);
324 sstep = _src->rows == 1 ? 1 : _src->step/CV_ELEM_SIZE(stype);
325 dstep = _dst->rows == 1 ? 1 : _dst->step/CV_ELEM_SIZE(dtype);
327 n = _src->rows + _src->cols - 1;
336 for( i = 0; i < n; i++ )
339 if( stype == CV_32FC2 )
350 x0 = x = (x - cx)*ifx;
351 y0 = y = (y - cy)*ify;
353 // compensate distortion iteratively
354 for( j = 0; j < iters; j++ )
356 double r2 = x*x + y*y;
357 double icdist = (1 + ((k[7]*r2 + k[6])*r2 + k[5])*r2)/(1 + ((k[4]*r2 + k[1])*r2 + k[0])*r2);
358 double deltaX = 2*k[2]*x*y + k[3]*(r2 + 2*x*x);
359 double deltaY = k[2]*(r2 + 2*y*y) + 2*k[3]*x*y;
360 x = (x0 - deltaX)*icdist;
361 y = (y0 - deltaY)*icdist;
364 double xx = RR[0][0]*x + RR[0][1]*y + RR[0][2];
365 double yy = RR[1][0]*x + RR[1][1]*y + RR[1][2];
366 double ww = 1./(RR[2][0]*x + RR[2][1]*y + RR[2][2]);
370 if( dtype == CV_32FC2 )
372 dstf[i*dstep].x = (float)x;
373 dstf[i*dstep].y = (float)y;
384 void cv::undistortPoints( InputArray _src, OutputArray _dst,
385 InputArray _cameraMatrix,
386 InputArray _distCoeffs,
390 Mat src = _src.getMat(), cameraMatrix = _cameraMatrix.getMat();
391 Mat distCoeffs = _distCoeffs.getMat(), R = _Rmat.getMat(), P = _Pmat.getMat();
393 CV_Assert( src.isContinuous() && (src.depth() == CV_32F || src.depth() == CV_64F) &&
394 ((src.rows == 1 && src.channels() == 2) || src.cols*src.channels() == 2));
396 _dst.create(src.size(), src.type(), -1, true);
397 Mat dst = _dst.getMat();
399 CvMat _csrc = src, _cdst = dst, _ccameraMatrix = cameraMatrix;
400 CvMat matR, matP, _cdistCoeffs, *pR=0, *pP=0, *pD=0;
405 if( distCoeffs.data )
406 pD = &(_cdistCoeffs = distCoeffs);
407 cvUndistortPoints(&_csrc, &_cdst, &_ccameraMatrix, pD, pR, pP);
413 static Point2f mapPointSpherical(const Point2f& p, float alpha, Vec4d* J, int projType)
415 double x = p.x, y = p.y;
416 double beta = 1 + 2*alpha;
417 double v = x*x + y*y + 1, iv = 1/v;
418 double u = sqrt(beta*v + alpha*alpha);
420 double k = (u - alpha)*iv;
421 double kv = (v*beta/u - (u - alpha)*2)*iv*iv;
422 double kx = kv*x, ky = kv*y;
424 if( projType == PROJ_SPHERICAL_ORTHO )
427 *J = Vec4d(kx*x + k, kx*y, ky*x, ky*y + k);
428 return Point2f((float)(x*k), (float)(y*k));
430 if( projType == PROJ_SPHERICAL_EQRECT )
433 double iR = 1/(alpha + 1);
434 double x1 = std::max(std::min(x*k*iR, 1.), -1.);
435 double y1 = std::max(std::min(y*k*iR, 1.), -1.);
439 double fx1 = iR/sqrt(1 - x1*x1);
440 double fy1 = iR/sqrt(1 - y1*y1);
441 *J = Vec4d(fx1*(kx*x + k), fx1*ky*x, fy1*kx*y, fy1*(ky*y + k));
443 return Point2f((float)asin(x1), (float)asin(y1));
445 CV_Error(CV_StsBadArg, "Unknown projection type");
450 static Point2f invMapPointSpherical(Point2f _p, float alpha, int projType)
452 static int avgiter = 0, avgn = 0;
455 Vec2d p(_p.x, _p.y), q(_p.x, _p.y), err;
459 for( i = 0; i < maxiter; i++ )
461 Point2f p1 = mapPointSpherical(Point2f((float)q[0], (float)q[1]), alpha, &J, projType);
462 err = Vec2d(p1.x, p1.y) - p;
463 if( err[0]*err[0] + err[1]*err[1] < eps )
466 Vec4d JtJ(J[0]*J[0] + J[2]*J[2], J[0]*J[1] + J[2]*J[3],
467 J[0]*J[1] + J[2]*J[3], J[1]*J[1] + J[3]*J[3]);
468 double d = JtJ[0]*JtJ[3] - JtJ[1]*JtJ[2];
470 Vec4d iJtJ(JtJ[3]*d, -JtJ[1]*d, -JtJ[2]*d, JtJ[0]*d);
471 Vec2d JtErr(J[0]*err[0] + J[2]*err[1], J[1]*err[0] + J[3]*err[1]);
473 q -= Vec2d(iJtJ[0]*JtErr[0] + iJtJ[1]*JtErr[1], iJtJ[2]*JtErr[0] + iJtJ[3]*JtErr[1]);
474 //Matx22d J(kx*x + k, kx*y, ky*x, ky*y + k);
475 //q -= Vec2d((J.t()*J).inv()*(J.t()*err));
483 printf("avg iters = %g\n", (double)avgiter/avgn);
486 return i < maxiter ? Point2f((float)q[0], (float)q[1]) : Point2f(-FLT_MAX, -FLT_MAX);
491 float cv::initWideAngleProjMap( InputArray _cameraMatrix0, InputArray _distCoeffs0,
492 Size imageSize, int destImageWidth, int m1type,
493 OutputArray _map1, OutputArray _map2, int projType, double _alpha )
495 Mat cameraMatrix0 = _cameraMatrix0.getMat(), distCoeffs0 = _distCoeffs0.getMat();
496 double k[8] = {0,0,0,0,0,0,0,0}, M[9]={0,0,0,0,0,0,0,0,0};
497 Mat distCoeffs(distCoeffs0.rows, distCoeffs0.cols, CV_MAKETYPE(CV_64F,distCoeffs0.channels()), k);
498 Mat cameraMatrix(3,3,CV_64F,M);
499 Point2f scenter((float)cameraMatrix.at<double>(0,2), (float)cameraMatrix.at<double>(1,2));
500 Point2f dcenter((destImageWidth-1)*0.5f, 0.f);
501 float xmin = FLT_MAX, xmax = -FLT_MAX, ymin = FLT_MAX, ymax = -FLT_MAX;
503 std::vector<Point2f> uvec(1), vvec(1);
504 Mat I = Mat::eye(3,3,CV_64F);
505 float alpha = (float)_alpha;
507 int ndcoeffs = distCoeffs0.cols*distCoeffs0.rows*distCoeffs0.channels();
508 CV_Assert((distCoeffs0.cols == 1 || distCoeffs0.rows == 1) &&
509 (ndcoeffs == 4 || ndcoeffs == 5 || ndcoeffs == 8));
510 CV_Assert(cameraMatrix0.size() == Size(3,3));
511 distCoeffs0.convertTo(distCoeffs,CV_64F);
512 cameraMatrix0.convertTo(cameraMatrix,CV_64F);
514 alpha = std::min(alpha, 0.999f);
516 for( int i = 0; i < N; i++ )
517 for( int j = 0; j < N; j++ )
519 Point2f p((float)j*imageSize.width/(N-1), (float)i*imageSize.height/(N-1));
521 undistortPoints(uvec, vvec, cameraMatrix, distCoeffs, I, I);
522 Point2f q = mapPointSpherical(vvec[0], alpha, 0, projType);
523 if( xmin > q.x ) xmin = q.x;
524 if( xmax < q.x ) xmax = q.x;
525 if( ymin > q.y ) ymin = q.y;
526 if( ymax < q.y ) ymax = q.y;
529 float scale = (float)std::min(dcenter.x/fabs(xmax), dcenter.x/fabs(xmin));
530 Size dsize(destImageWidth, cvCeil(std::max(scale*fabs(ymin)*2, scale*fabs(ymax)*2)));
531 dcenter.y = (dsize.height - 1)*0.5f;
533 Mat mapxy(dsize, CV_32FC2);
534 double k1 = k[0], k2 = k[1], k3 = k[2], p1 = k[3], p2 = k[4], k4 = k[5], k5 = k[6], k6 = k[7];
535 double fx = cameraMatrix.at<double>(0,0), fy = cameraMatrix.at<double>(1,1), cx = scenter.x, cy = scenter.y;
537 for( int y = 0; y < dsize.height; y++ )
539 Point2f* mxy = mapxy.ptr<Point2f>(y);
540 for( int x = 0; x < dsize.width; x++ )
542 Point2f p = (Point2f((float)x, (float)y) - dcenter)*(1.f/scale);
543 Point2f q = invMapPointSpherical(p, alpha, projType);
544 if( q.x <= -FLT_MAX && q.y <= -FLT_MAX )
546 mxy[x] = Point2f(-1.f, -1.f);
549 double x2 = q.x*q.x, y2 = q.y*q.y;
550 double r2 = x2 + y2, _2xy = 2*q.x*q.y;
551 double kr = 1 + ((k3*r2 + k2)*r2 + k1)*r2/(1 + ((k6*r2 + k5)*r2 + k4)*r2);
552 double u = fx*(q.x*kr + p1*_2xy + p2*(r2 + 2*x2)) + cx;
553 double v = fy*(q.y*kr + p1*(r2 + 2*y2) + p2*_2xy) + cy;
555 mxy[x] = Point2f((float)u, (float)v);
559 if(m1type == CV_32FC2)
561 _map1.create(mapxy.size(), mapxy.type());
562 Mat map1 = _map1.getMat();
567 convertMaps(mapxy, Mat(), _map1, _map2, m1type, false);