0a8db58d835e21e0609f15d90a58dd569ba252b9
[platform/upstream/opencv.git] / modules / calib3d / src / fisheye.cpp
1 /*M///////////////////////////////////////////////////////////////////////////////////////
2 //
3 //  IMPORTANT: READ BEFORE DOWNLOADING, COPYING, INSTALLING OR USING.
4 //
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.
8 //
9 //
10 //                           License Agreement
11 //                For Open Source Computer Vision Library
12 //
13 // Copyright (C) 2000-2008, Intel Corporation, all rights reserved.
14 // Copyright (C) 2009-2011, Willow Garage Inc., all rights reserved.
15 // Third party copyrights are property of their respective owners.
16 //
17 // Redistribution and use in source and binary forms, with or without modification,
18 // are permitted provided that the following conditions are met:
19 //
20 //   * Redistribution's of source code must retain the above copyright notice,
21 //     this list of conditions and the following disclaimer.
22 //
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.
26 //
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.
29 //
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.
40 //
41 //M*/
42
43 #include "precomp.hpp"
44 #include "fisheye.hpp"
45
46 namespace cv { namespace
47 {
48     struct JacobianRow
49     {
50         Vec2d df, dc;
51         Vec4d dk;
52         Vec3d dom, dT;
53         double dalpha;
54     };
55
56     void subMatrix(const Mat& src, Mat& dst, const std::vector<uchar>& cols, const std::vector<uchar>& rows);
57 }}
58
59 //////////////////////////////////////////////////////////////////////////////////////////////////////////////
60 /// cv::fisheye::projectPoints
61
62 void cv::fisheye::projectPoints(InputArray objectPoints, OutputArray imagePoints, const Affine3d& affine,
63     InputArray K, InputArray D, double alpha, OutputArray jacobian)
64 {
65     CV_INSTRUMENT_REGION()
66
67     projectPoints(objectPoints, imagePoints, affine.rvec(), affine.translation(), K, D, alpha, jacobian);
68 }
69
70 void cv::fisheye::projectPoints(InputArray objectPoints, OutputArray imagePoints, InputArray _rvec,
71         InputArray _tvec, InputArray _K, InputArray _D, double alpha, OutputArray jacobian)
72 {
73     CV_INSTRUMENT_REGION()
74
75     // will support only 3-channel data now for points
76     CV_Assert(objectPoints.type() == CV_32FC3 || objectPoints.type() == CV_64FC3);
77     imagePoints.create(objectPoints.size(), CV_MAKETYPE(objectPoints.depth(), 2));
78     size_t n = objectPoints.total();
79
80     CV_Assert(_rvec.total() * _rvec.channels() == 3 && (_rvec.depth() == CV_32F || _rvec.depth() == CV_64F));
81     CV_Assert(_tvec.total() * _tvec.channels() == 3 && (_tvec.depth() == CV_32F || _tvec.depth() == CV_64F));
82     CV_Assert(_tvec.getMat().isContinuous() && _rvec.getMat().isContinuous());
83
84     Vec3d om = _rvec.depth() == CV_32F ? (Vec3d)*_rvec.getMat().ptr<Vec3f>() : *_rvec.getMat().ptr<Vec3d>();
85     Vec3d T  = _tvec.depth() == CV_32F ? (Vec3d)*_tvec.getMat().ptr<Vec3f>() : *_tvec.getMat().ptr<Vec3d>();
86
87     CV_Assert(_K.size() == Size(3,3) && (_K.type() == CV_32F || _K.type() == CV_64F) && _D.type() == _K.type() && _D.total() == 4);
88
89     cv::Vec2d f, c;
90     if (_K.depth() == CV_32F)
91     {
92
93         Matx33f K = _K.getMat();
94         f = Vec2f(K(0, 0), K(1, 1));
95         c = Vec2f(K(0, 2), K(1, 2));
96     }
97     else
98     {
99         Matx33d K = _K.getMat();
100         f = Vec2d(K(0, 0), K(1, 1));
101         c = Vec2d(K(0, 2), K(1, 2));
102     }
103
104     Vec4d k = _D.depth() == CV_32F ? (Vec4d)*_D.getMat().ptr<Vec4f>(): *_D.getMat().ptr<Vec4d>();
105
106     JacobianRow *Jn = 0;
107     if (jacobian.needed())
108     {
109         int nvars = 2 + 2 + 1 + 4 + 3 + 3; // f, c, alpha, k, om, T,
110         jacobian.create(2*(int)n, nvars, CV_64F);
111         Jn = jacobian.getMat().ptr<JacobianRow>(0);
112     }
113
114     Matx33d R;
115     Matx<double, 3, 9> dRdom;
116     Rodrigues(om, R, dRdom);
117     Affine3d aff(om, T);
118
119     const Vec3f* Xf = objectPoints.getMat().ptr<Vec3f>();
120     const Vec3d* Xd = objectPoints.getMat().ptr<Vec3d>();
121     Vec2f *xpf = imagePoints.getMat().ptr<Vec2f>();
122     Vec2d *xpd = imagePoints.getMat().ptr<Vec2d>();
123
124     for(size_t i = 0; i < n; ++i)
125     {
126         Vec3d Xi = objectPoints.depth() == CV_32F ? (Vec3d)Xf[i] : Xd[i];
127         Vec3d Y = aff*Xi;
128
129         Vec2d x(Y[0]/Y[2], Y[1]/Y[2]);
130
131         double r2 = x.dot(x);
132         double r = std::sqrt(r2);
133
134         // Angle of the incoming ray:
135         double theta = atan(r);
136
137         double theta2 = theta*theta, theta3 = theta2*theta, theta4 = theta2*theta2, theta5 = theta4*theta,
138                 theta6 = theta3*theta3, theta7 = theta6*theta, theta8 = theta4*theta4, theta9 = theta8*theta;
139
140         double theta_d = theta + k[0]*theta3 + k[1]*theta5 + k[2]*theta7 + k[3]*theta9;
141
142         double inv_r = r > 1e-8 ? 1.0/r : 1;
143         double cdist = r > 1e-8 ? theta_d * inv_r : 1;
144
145         Vec2d xd1 = x * cdist;
146         Vec2d xd3(xd1[0] + alpha*xd1[1], xd1[1]);
147         Vec2d final_point(xd3[0] * f[0] + c[0], xd3[1] * f[1] + c[1]);
148
149         if (objectPoints.depth() == CV_32F)
150             xpf[i] = final_point;
151         else
152             xpd[i] = final_point;
153
154         if (jacobian.needed())
155         {
156             //Vec3d Xi = pdepth == CV_32F ? (Vec3d)Xf[i] : Xd[i];
157             //Vec3d Y = aff*Xi;
158             double dYdR[] = { Xi[0], Xi[1], Xi[2], 0, 0, 0, 0, 0, 0,
159                               0, 0, 0, Xi[0], Xi[1], Xi[2], 0, 0, 0,
160                               0, 0, 0, 0, 0, 0, Xi[0], Xi[1], Xi[2] };
161
162             Matx33d dYdom_data = Matx<double, 3, 9>(dYdR) * dRdom.t();
163             const Vec3d *dYdom = (Vec3d*)dYdom_data.val;
164
165             Matx33d dYdT_data = Matx33d::eye();
166             const Vec3d *dYdT = (Vec3d*)dYdT_data.val;
167
168             //Vec2d x(Y[0]/Y[2], Y[1]/Y[2]);
169             Vec3d dxdom[2];
170             dxdom[0] = (1.0/Y[2]) * dYdom[0] - x[0]/Y[2] * dYdom[2];
171             dxdom[1] = (1.0/Y[2]) * dYdom[1] - x[1]/Y[2] * dYdom[2];
172
173             Vec3d dxdT[2];
174             dxdT[0]  = (1.0/Y[2]) * dYdT[0] - x[0]/Y[2] * dYdT[2];
175             dxdT[1]  = (1.0/Y[2]) * dYdT[1] - x[1]/Y[2] * dYdT[2];
176
177             //double r2 = x.dot(x);
178             Vec3d dr2dom = 2 * x[0] * dxdom[0] + 2 * x[1] * dxdom[1];
179             Vec3d dr2dT  = 2 * x[0] *  dxdT[0] + 2 * x[1] *  dxdT[1];
180
181             //double r = std::sqrt(r2);
182             double drdr2 = r > 1e-8 ? 1.0/(2*r) : 1;
183             Vec3d drdom = drdr2 * dr2dom;
184             Vec3d drdT  = drdr2 * dr2dT;
185
186             // Angle of the incoming ray:
187             //double theta = atan(r);
188             double dthetadr = 1.0/(1+r2);
189             Vec3d dthetadom = dthetadr * drdom;
190             Vec3d dthetadT  = dthetadr *  drdT;
191
192             //double theta_d = theta + k[0]*theta3 + k[1]*theta5 + k[2]*theta7 + k[3]*theta9;
193             double dtheta_ddtheta = 1 + 3*k[0]*theta2 + 5*k[1]*theta4 + 7*k[2]*theta6 + 9*k[3]*theta8;
194             Vec3d dtheta_ddom = dtheta_ddtheta * dthetadom;
195             Vec3d dtheta_ddT  = dtheta_ddtheta * dthetadT;
196             Vec4d dtheta_ddk  = Vec4d(theta3, theta5, theta7, theta9);
197
198             //double inv_r = r > 1e-8 ? 1.0/r : 1;
199             //double cdist = r > 1e-8 ? theta_d / r : 1;
200             Vec3d dcdistdom = inv_r * (dtheta_ddom - cdist*drdom);
201             Vec3d dcdistdT  = inv_r * (dtheta_ddT  - cdist*drdT);
202             Vec4d dcdistdk  = inv_r *  dtheta_ddk;
203
204             //Vec2d xd1 = x * cdist;
205             Vec4d dxd1dk[2];
206             Vec3d dxd1dom[2], dxd1dT[2];
207             dxd1dom[0] = x[0] * dcdistdom + cdist * dxdom[0];
208             dxd1dom[1] = x[1] * dcdistdom + cdist * dxdom[1];
209             dxd1dT[0]  = x[0] * dcdistdT  + cdist * dxdT[0];
210             dxd1dT[1]  = x[1] * dcdistdT  + cdist * dxdT[1];
211             dxd1dk[0]  = x[0] * dcdistdk;
212             dxd1dk[1]  = x[1] * dcdistdk;
213
214             //Vec2d xd3(xd1[0] + alpha*xd1[1], xd1[1]);
215             Vec4d dxd3dk[2];
216             Vec3d dxd3dom[2], dxd3dT[2];
217             dxd3dom[0] = dxd1dom[0] + alpha * dxd1dom[1];
218             dxd3dom[1] = dxd1dom[1];
219             dxd3dT[0]  = dxd1dT[0]  + alpha * dxd1dT[1];
220             dxd3dT[1]  = dxd1dT[1];
221             dxd3dk[0]  = dxd1dk[0]  + alpha * dxd1dk[1];
222             dxd3dk[1]  = dxd1dk[1];
223
224             Vec2d dxd3dalpha(xd1[1], 0);
225
226             //final jacobian
227             Jn[0].dom = f[0] * dxd3dom[0];
228             Jn[1].dom = f[1] * dxd3dom[1];
229
230             Jn[0].dT = f[0] * dxd3dT[0];
231             Jn[1].dT = f[1] * dxd3dT[1];
232
233             Jn[0].dk = f[0] * dxd3dk[0];
234             Jn[1].dk = f[1] * dxd3dk[1];
235
236             Jn[0].dalpha = f[0] * dxd3dalpha[0];
237             Jn[1].dalpha = 0; //f[1] * dxd3dalpha[1];
238
239             Jn[0].df = Vec2d(xd3[0], 0);
240             Jn[1].df = Vec2d(0, xd3[1]);
241
242             Jn[0].dc = Vec2d(1, 0);
243             Jn[1].dc = Vec2d(0, 1);
244
245             //step to jacobian rows for next point
246             Jn += 2;
247         }
248     }
249 }
250
251 //////////////////////////////////////////////////////////////////////////////////////////////////////////////
252 /// cv::fisheye::distortPoints
253
254 void cv::fisheye::distortPoints(InputArray undistorted, OutputArray distorted, InputArray K, InputArray D, double alpha)
255 {
256     CV_INSTRUMENT_REGION()
257
258     // will support only 2-channel data now for points
259     CV_Assert(undistorted.type() == CV_32FC2 || undistorted.type() == CV_64FC2);
260     distorted.create(undistorted.size(), undistorted.type());
261     size_t n = undistorted.total();
262
263     CV_Assert(K.size() == Size(3,3) && (K.type() == CV_32F || K.type() == CV_64F) && D.total() == 4);
264
265     cv::Vec2d f, c;
266     if (K.depth() == CV_32F)
267     {
268         Matx33f camMat = K.getMat();
269         f = Vec2f(camMat(0, 0), camMat(1, 1));
270         c = Vec2f(camMat(0, 2), camMat(1, 2));
271     }
272     else
273     {
274         Matx33d camMat = K.getMat();
275         f = Vec2d(camMat(0, 0), camMat(1, 1));
276         c = Vec2d(camMat(0 ,2), camMat(1, 2));
277     }
278
279     Vec4d k = D.depth() == CV_32F ? (Vec4d)*D.getMat().ptr<Vec4f>(): *D.getMat().ptr<Vec4d>();
280
281     const Vec2f* Xf = undistorted.getMat().ptr<Vec2f>();
282     const Vec2d* Xd = undistorted.getMat().ptr<Vec2d>();
283     Vec2f *xpf = distorted.getMat().ptr<Vec2f>();
284     Vec2d *xpd = distorted.getMat().ptr<Vec2d>();
285
286     for(size_t i = 0; i < n; ++i)
287     {
288         Vec2d x = undistorted.depth() == CV_32F ? (Vec2d)Xf[i] : Xd[i];
289
290         double r2 = x.dot(x);
291         double r = std::sqrt(r2);
292
293         // Angle of the incoming ray:
294         double theta = atan(r);
295
296         double theta2 = theta*theta, theta3 = theta2*theta, theta4 = theta2*theta2, theta5 = theta4*theta,
297                 theta6 = theta3*theta3, theta7 = theta6*theta, theta8 = theta4*theta4, theta9 = theta8*theta;
298
299         double theta_d = theta + k[0]*theta3 + k[1]*theta5 + k[2]*theta7 + k[3]*theta9;
300
301         double inv_r = r > 1e-8 ? 1.0/r : 1;
302         double cdist = r > 1e-8 ? theta_d * inv_r : 1;
303
304         Vec2d xd1 = x * cdist;
305         Vec2d xd3(xd1[0] + alpha*xd1[1], xd1[1]);
306         Vec2d final_point(xd3[0] * f[0] + c[0], xd3[1] * f[1] + c[1]);
307
308         if (undistorted.depth() == CV_32F)
309             xpf[i] = final_point;
310         else
311             xpd[i] = final_point;
312     }
313 }
314
315 //////////////////////////////////////////////////////////////////////////////////////////////////////////////
316 /// cv::fisheye::undistortPoints
317
318 void cv::fisheye::undistortPoints( InputArray distorted, OutputArray undistorted, InputArray K, InputArray D, InputArray R, InputArray P)
319 {
320     CV_INSTRUMENT_REGION()
321
322     // will support only 2-channel data now for points
323     CV_Assert(distorted.type() == CV_32FC2 || distorted.type() == CV_64FC2);
324     undistorted.create(distorted.size(), distorted.type());
325
326     CV_Assert(P.empty() || P.size() == Size(3, 3) || P.size() == Size(4, 3));
327     CV_Assert(R.empty() || R.size() == Size(3, 3) || R.total() * R.channels() == 3);
328     CV_Assert(D.total() == 4 && K.size() == Size(3, 3) && (K.depth() == CV_32F || K.depth() == CV_64F));
329
330     cv::Vec2d f, c;
331     if (K.depth() == CV_32F)
332     {
333         Matx33f camMat = K.getMat();
334         f = Vec2f(camMat(0, 0), camMat(1, 1));
335         c = Vec2f(camMat(0, 2), camMat(1, 2));
336     }
337     else
338     {
339         Matx33d camMat = K.getMat();
340         f = Vec2d(camMat(0, 0), camMat(1, 1));
341         c = Vec2d(camMat(0, 2), camMat(1, 2));
342     }
343
344     Vec4d k = D.depth() == CV_32F ? (Vec4d)*D.getMat().ptr<Vec4f>(): *D.getMat().ptr<Vec4d>();
345
346     cv::Matx33d RR = cv::Matx33d::eye();
347     if (!R.empty() && R.total() * R.channels() == 3)
348     {
349         cv::Vec3d rvec;
350         R.getMat().convertTo(rvec, CV_64F);
351         RR = cv::Affine3d(rvec).rotation();
352     }
353     else if (!R.empty() && R.size() == Size(3, 3))
354         R.getMat().convertTo(RR, CV_64F);
355
356     if(!P.empty())
357     {
358         cv::Matx33d PP;
359         P.getMat().colRange(0, 3).convertTo(PP, CV_64F);
360         RR = PP * RR;
361     }
362
363     // start undistorting
364     const cv::Vec2f* srcf = distorted.getMat().ptr<cv::Vec2f>();
365     const cv::Vec2d* srcd = distorted.getMat().ptr<cv::Vec2d>();
366     cv::Vec2f* dstf = undistorted.getMat().ptr<cv::Vec2f>();
367     cv::Vec2d* dstd = undistorted.getMat().ptr<cv::Vec2d>();
368
369     size_t n = distorted.total();
370     int sdepth = distorted.depth();
371
372     for(size_t i = 0; i < n; i++ )
373     {
374         Vec2d pi = sdepth == CV_32F ? (Vec2d)srcf[i] : srcd[i];  // image point
375         Vec2d pw((pi[0] - c[0])/f[0], (pi[1] - c[1])/f[1]);      // world point
376
377         double scale = 1.0;
378
379         double theta_d = sqrt(pw[0]*pw[0] + pw[1]*pw[1]);
380         if (theta_d > 1e-8)
381         {
382             // compensate distortion iteratively
383             double theta = theta_d;
384             for(int j = 0; j < 10; j++ )
385             {
386                 double theta2 = theta*theta, theta4 = theta2*theta2, theta6 = theta4*theta2, theta8 = theta6*theta2;
387                 theta = theta_d / (1 + k[0] * theta2 + k[1] * theta4 + k[2] * theta6 + k[3] * theta8);
388             }
389
390             scale = std::tan(theta) / theta_d;
391         }
392
393         Vec2d pu = pw * scale; //undistorted point
394
395         // reproject
396         Vec3d pr = RR * Vec3d(pu[0], pu[1], 1.0); // rotated point optionally multiplied by new camera matrix
397         Vec2d fi(pr[0]/pr[2], pr[1]/pr[2]);       // final
398
399         if( sdepth == CV_32F )
400             dstf[i] = fi;
401         else
402             dstd[i] = fi;
403     }
404 }
405
406 //////////////////////////////////////////////////////////////////////////////////////////////////////////////
407 /// cv::fisheye::undistortPoints
408
409 void cv::fisheye::initUndistortRectifyMap( InputArray K, InputArray D, InputArray R, InputArray P,
410     const cv::Size& size, int m1type, OutputArray map1, OutputArray map2 )
411 {
412     CV_INSTRUMENT_REGION()
413
414     CV_Assert( m1type == CV_16SC2 || m1type == CV_32F || m1type <=0 );
415     map1.create( size, m1type <= 0 ? CV_16SC2 : m1type );
416     map2.create( size, map1.type() == CV_16SC2 ? CV_16UC1 : CV_32F );
417
418     CV_Assert((K.depth() == CV_32F || K.depth() == CV_64F) && (D.depth() == CV_32F || D.depth() == CV_64F));
419     CV_Assert((P.empty() || P.depth() == CV_32F || P.depth() == CV_64F) && (R.empty() || R.depth() == CV_32F || R.depth() == CV_64F));
420     CV_Assert(K.size() == Size(3, 3) && (D.empty() || D.total() == 4));
421     CV_Assert(R.empty() || R.size() == Size(3, 3) || R.total() * R.channels() == 3);
422     CV_Assert(P.empty() || P.size() == Size(3, 3) || P.size() == Size(4, 3));
423
424     cv::Vec2d f, c;
425     if (K.depth() == CV_32F)
426     {
427         Matx33f camMat = K.getMat();
428         f = Vec2f(camMat(0, 0), camMat(1, 1));
429         c = Vec2f(camMat(0, 2), camMat(1, 2));
430     }
431     else
432     {
433         Matx33d camMat = K.getMat();
434         f = Vec2d(camMat(0, 0), camMat(1, 1));
435         c = Vec2d(camMat(0, 2), camMat(1, 2));
436     }
437
438     Vec4d k = Vec4d::all(0);
439     if (!D.empty())
440         k = D.depth() == CV_32F ? (Vec4d)*D.getMat().ptr<Vec4f>(): *D.getMat().ptr<Vec4d>();
441
442     cv::Matx33d RR  = cv::Matx33d::eye();
443     if (!R.empty() && R.total() * R.channels() == 3)
444     {
445         cv::Vec3d rvec;
446         R.getMat().convertTo(rvec, CV_64F);
447         RR = Affine3d(rvec).rotation();
448     }
449     else if (!R.empty() && R.size() == Size(3, 3))
450         R.getMat().convertTo(RR, CV_64F);
451
452     cv::Matx33d PP = cv::Matx33d::eye();
453     if (!P.empty())
454         P.getMat().colRange(0, 3).convertTo(PP, CV_64F);
455
456     cv::Matx33d iR = (PP * RR).inv(cv::DECOMP_SVD);
457
458     for( int i = 0; i < size.height; ++i)
459     {
460         float* m1f = map1.getMat().ptr<float>(i);
461         float* m2f = map2.getMat().ptr<float>(i);
462         short*  m1 = (short*)m1f;
463         ushort* m2 = (ushort*)m2f;
464
465         double _x = i*iR(0, 1) + iR(0, 2),
466                _y = i*iR(1, 1) + iR(1, 2),
467                _w = i*iR(2, 1) + iR(2, 2);
468
469         for( int j = 0; j < size.width; ++j)
470         {
471             double x = _x/_w, y = _y/_w;
472
473             double r = sqrt(x*x + y*y);
474             double theta = atan(r);
475
476             double theta2 = theta*theta, theta4 = theta2*theta2, theta6 = theta4*theta2, theta8 = theta4*theta4;
477             double theta_d = theta * (1 + k[0]*theta2 + k[1]*theta4 + k[2]*theta6 + k[3]*theta8);
478
479             double scale = (r == 0) ? 1.0 : theta_d / r;
480             double u = f[0]*x*scale + c[0];
481             double v = f[1]*y*scale + c[1];
482
483             if( m1type == CV_16SC2 )
484             {
485                 int iu = cv::saturate_cast<int>(u*cv::INTER_TAB_SIZE);
486                 int iv = cv::saturate_cast<int>(v*cv::INTER_TAB_SIZE);
487                 m1[j*2+0] = (short)(iu >> cv::INTER_BITS);
488                 m1[j*2+1] = (short)(iv >> cv::INTER_BITS);
489                 m2[j] = (ushort)((iv & (cv::INTER_TAB_SIZE-1))*cv::INTER_TAB_SIZE + (iu & (cv::INTER_TAB_SIZE-1)));
490             }
491             else if( m1type == CV_32FC1 )
492             {
493                 m1f[j] = (float)u;
494                 m2f[j] = (float)v;
495             }
496
497             _x += iR(0, 0);
498             _y += iR(1, 0);
499             _w += iR(2, 0);
500         }
501     }
502 }
503
504 //////////////////////////////////////////////////////////////////////////////////////////////////////////////
505 /// cv::fisheye::undistortImage
506
507 void cv::fisheye::undistortImage(InputArray distorted, OutputArray undistorted,
508         InputArray K, InputArray D, InputArray Knew, const Size& new_size)
509 {
510     CV_INSTRUMENT_REGION()
511
512     Size size = new_size.area() != 0 ? new_size : distorted.size();
513
514     cv::Mat map1, map2;
515     fisheye::initUndistortRectifyMap(K, D, cv::Matx33d::eye(), Knew, size, CV_16SC2, map1, map2 );
516     cv::remap(distorted, undistorted, map1, map2, INTER_LINEAR, BORDER_CONSTANT);
517 }
518
519
520 //////////////////////////////////////////////////////////////////////////////////////////////////////////////
521 /// cv::fisheye::estimateNewCameraMatrixForUndistortRectify
522
523 void cv::fisheye::estimateNewCameraMatrixForUndistortRectify(InputArray K, InputArray D, const Size &image_size, InputArray R,
524     OutputArray P, double balance, const Size& new_size, double fov_scale)
525 {
526     CV_INSTRUMENT_REGION()
527
528     CV_Assert( K.size() == Size(3, 3)       && (K.depth() == CV_32F || K.depth() == CV_64F));
529     CV_Assert(D.empty() || ((D.total() == 4) && (D.depth() == CV_32F || D.depth() == CV_64F)));
530
531     int w = image_size.width, h = image_size.height;
532     balance = std::min(std::max(balance, 0.0), 1.0);
533
534     cv::Mat points(1, 8, CV_64FC2);
535     Vec2d* pptr = points.ptr<Vec2d>();
536     pptr[0] = Vec2d(0, 0);
537     pptr[1] = Vec2d(w/2, 0);
538     pptr[2] = Vec2d(w, 0);
539     pptr[3] = Vec2d(w, h/2);
540     pptr[4] = Vec2d(w, h);
541     pptr[5] = Vec2d(w/2, h);
542     pptr[6] = Vec2d(0, h);
543     pptr[7] = Vec2d(0, h/2);
544
545 #if 0
546     const int N = 10;
547     cv::Mat points(1, N * 4, CV_64FC2);
548     Vec2d* pptr = points.ptr<Vec2d>();
549     for(int i = 0, k = 0; i < 10; ++i)
550     {
551         pptr[k++] = Vec2d(w/2,   0) - Vec2d(w/8,   0) + Vec2d(w/4/N*i,   0);
552         pptr[k++] = Vec2d(w/2, h-1) - Vec2d(w/8, h-1) + Vec2d(w/4/N*i, h-1);
553         pptr[k++] = Vec2d(0,   h/2) - Vec2d(0,   h/8) + Vec2d(0,   h/4/N*i);
554         pptr[k++] = Vec2d(w-1, h/2) - Vec2d(w-1, h/8) + Vec2d(w-1, h/4/N*i);
555     }
556 #endif
557
558     fisheye::undistortPoints(points, points, K, D, R);
559     cv::Scalar center_mass = mean(points);
560     cv::Vec2d cn(center_mass.val);
561
562     double aspect_ratio = (K.depth() == CV_32F) ? K.getMat().at<float >(0,0)/K.getMat().at<float> (1,1)
563                                                 : K.getMat().at<double>(0,0)/K.getMat().at<double>(1,1);
564
565     // convert to identity ratio
566     cn[0] *= aspect_ratio;
567     for(size_t i = 0; i < points.total(); ++i)
568         pptr[i][1] *= aspect_ratio;
569
570     double minx = DBL_MAX, miny = DBL_MAX, maxx = -DBL_MAX, maxy = -DBL_MAX;
571     for(size_t i = 0; i < points.total(); ++i)
572     {
573         if(i!=1 && i!=5){
574             minx = std::min(minx, std::abs(pptr[i][0]-cn[0]));
575         }
576         if(i!=3 && i!=7){
577             miny = std::min(miny, std::abs(pptr[i][1]-cn[1]));
578         }
579         maxy = std::max(maxy, std::abs(pptr[i][1]-cn[1]));
580         maxx = std::max(maxx, std::abs(pptr[i][0]-cn[0]));
581     }
582
583 #if 0
584     double minx = -DBL_MAX, miny = -DBL_MAX, maxx = DBL_MAX, maxy = DBL_MAX;
585     for(size_t i = 0; i < points.total(); ++i)
586     {
587         if (i % 4 == 0) miny = std::max(miny, pptr[i][1]);
588         if (i % 4 == 1) maxy = std::min(maxy, pptr[i][1]);
589         if (i % 4 == 2) minx = std::max(minx, pptr[i][0]);
590         if (i % 4 == 3) maxx = std::min(maxx, pptr[i][0]);
591     }
592 #endif
593
594     double f1 = w * 0.5/(minx);
595     double f2 = w * 0.5/(maxx);
596     double f3 = h * 0.5 * aspect_ratio/(miny);
597     double f4 = h * 0.5 * aspect_ratio/(maxy);
598
599     double fmax = std::max(f1, f3);
600     double fmin = std::min(f2, f4);
601
602     double f = balance * fmin + (1.0 - balance) * fmax;
603     f *= fov_scale > 0 ? 1.0/fov_scale : 1.0;
604
605     cv::Vec2d new_f(f, f), new_c = -cn * f + Vec2d(w, h * aspect_ratio) * 0.5;
606
607     // restore aspect ratio
608     new_f[1] /= aspect_ratio;
609     new_c[1] /= aspect_ratio;
610
611     if (new_size.area() > 0)
612     {
613         double rx = new_size.width /(double)image_size.width;
614         double ry = new_size.height/(double)image_size.height;
615
616         new_f[0] *= rx;  new_f[1] *= ry;
617         new_c[0] *= rx;  new_c[1] *= ry;
618     }
619
620     Mat(Matx33d(new_f[0], 0, new_c[0],
621                 0, new_f[1], new_c[1],
622                 0,        0,       1)).convertTo(P, P.empty() ? K.type() : P.type());
623 }
624
625
626 //////////////////////////////////////////////////////////////////////////////////////////////////////////////
627 /// cv::fisheye::stereoRectify
628
629 void cv::fisheye::stereoRectify( InputArray K1, InputArray D1, InputArray K2, InputArray D2, const Size& imageSize,
630         InputArray _R, InputArray _tvec, OutputArray R1, OutputArray R2, OutputArray P1, OutputArray P2,
631         OutputArray Q, int flags, const Size& newImageSize, double balance, double fov_scale)
632 {
633     CV_INSTRUMENT_REGION()
634
635     CV_Assert((_R.size() == Size(3, 3) || _R.total() * _R.channels() == 3) && (_R.depth() == CV_32F || _R.depth() == CV_64F));
636     CV_Assert(_tvec.total() * _tvec.channels() == 3 && (_tvec.depth() == CV_32F || _tvec.depth() == CV_64F));
637
638
639     cv::Mat aaa = _tvec.getMat().reshape(3, 1);
640
641     Vec3d rvec; // Rodrigues vector
642     if (_R.size() == Size(3, 3))
643     {
644         cv::Matx33d rmat;
645         _R.getMat().convertTo(rmat, CV_64F);
646         rvec = Affine3d(rmat).rvec();
647     }
648     else if (_R.total() * _R.channels() == 3)
649         _R.getMat().convertTo(rvec, CV_64F);
650
651     Vec3d tvec;
652     _tvec.getMat().convertTo(tvec, CV_64F);
653
654     // rectification algorithm
655     rvec *= -0.5;              // get average rotation
656
657     Matx33d r_r;
658     Rodrigues(rvec, r_r);  // rotate cameras to same orientation by averaging
659
660     Vec3d t = r_r * tvec;
661     Vec3d uu(t[0] > 0 ? 1 : -1, 0, 0);
662
663     // calculate global Z rotation
664     Vec3d ww = t.cross(uu);
665     double nw = norm(ww);
666     if (nw > 0.0)
667         ww *= acos(fabs(t[0])/cv::norm(t))/nw;
668
669     Matx33d wr;
670     Rodrigues(ww, wr);
671
672     // apply to both views
673     Matx33d ri1 = wr * r_r.t();
674     Mat(ri1, false).convertTo(R1, R1.empty() ? CV_64F : R1.type());
675     Matx33d ri2 = wr * r_r;
676     Mat(ri2, false).convertTo(R2, R2.empty() ? CV_64F : R2.type());
677     Vec3d tnew = ri2 * tvec;
678
679     // calculate projection/camera matrices. these contain the relevant rectified image internal params (fx, fy=fx, cx, cy)
680     Matx33d newK1, newK2;
681     estimateNewCameraMatrixForUndistortRectify(K1, D1, imageSize, R1, newK1, balance, newImageSize, fov_scale);
682     estimateNewCameraMatrixForUndistortRectify(K2, D2, imageSize, R2, newK2, balance, newImageSize, fov_scale);
683
684     double fc_new = std::min(newK1(1,1), newK2(1,1));
685     Point2d cc_new[2] = { Vec2d(newK1(0, 2), newK1(1, 2)), Vec2d(newK2(0, 2), newK2(1, 2)) };
686
687     // Vertical focal length must be the same for both images to keep the epipolar constraint use fy for fx also.
688     // For simplicity, set the principal points for both cameras to be the average
689     // of the two principal points (either one of or both x- and y- coordinates)
690     if( flags & cv::CALIB_ZERO_DISPARITY )
691         cc_new[0] = cc_new[1] = (cc_new[0] + cc_new[1]) * 0.5;
692     else
693         cc_new[0].y = cc_new[1].y = (cc_new[0].y + cc_new[1].y)*0.5;
694
695     Mat(Matx34d(fc_new, 0, cc_new[0].x, 0,
696                 0, fc_new, cc_new[0].y, 0,
697                 0,      0,           1, 0), false).convertTo(P1, P1.empty() ? CV_64F : P1.type());
698
699     Mat(Matx34d(fc_new, 0, cc_new[1].x, tnew[0]*fc_new, // baseline * focal length;,
700                 0, fc_new, cc_new[1].y,              0,
701                 0,      0,           1,              0), false).convertTo(P2, P2.empty() ? CV_64F : P2.type());
702
703     if (Q.needed())
704         Mat(Matx44d(1, 0, 0,           -cc_new[0].x,
705                     0, 1, 0,           -cc_new[0].y,
706                     0, 0, 0,            fc_new,
707                     0, 0, -1./tnew[0], (cc_new[0].x - cc_new[1].x)/tnew[0]), false).convertTo(Q, Q.empty() ? CV_64F : Q.depth());
708 }
709
710 //////////////////////////////////////////////////////////////////////////////////////////////////////////////
711 /// cv::fisheye::calibrate
712
713 double cv::fisheye::calibrate(InputArrayOfArrays objectPoints, InputArrayOfArrays imagePoints, const Size& image_size,
714                                     InputOutputArray K, InputOutputArray D, OutputArrayOfArrays rvecs, OutputArrayOfArrays tvecs,
715                                     int flags , cv::TermCriteria criteria)
716 {
717     CV_INSTRUMENT_REGION()
718
719     CV_Assert(!objectPoints.empty() && !imagePoints.empty() && objectPoints.total() == imagePoints.total());
720     CV_Assert(objectPoints.type() == CV_32FC3 || objectPoints.type() == CV_64FC3);
721     CV_Assert(imagePoints.type() == CV_32FC2 || imagePoints.type() == CV_64FC2);
722     CV_Assert(K.empty() || (K.size() == Size(3,3)));
723     CV_Assert(D.empty() || (D.total() == 4));
724     CV_Assert(rvecs.empty() || (rvecs.channels() == 3));
725     CV_Assert(tvecs.empty() || (tvecs.channels() == 3));
726
727     CV_Assert((!K.empty() && !D.empty()) || !(flags & CALIB_USE_INTRINSIC_GUESS));
728
729     using namespace cv::internal;
730     //-------------------------------Initialization
731     IntrinsicParams finalParam;
732     IntrinsicParams currentParam;
733     IntrinsicParams errors;
734
735     finalParam.isEstimate[0] = 1;
736     finalParam.isEstimate[1] = 1;
737     finalParam.isEstimate[2] = flags & CALIB_FIX_PRINCIPAL_POINT ? 0 : 1;
738     finalParam.isEstimate[3] = flags & CALIB_FIX_PRINCIPAL_POINT ? 0 : 1;
739     finalParam.isEstimate[4] = flags & CALIB_FIX_SKEW ? 0 : 1;
740     finalParam.isEstimate[5] = flags & CALIB_FIX_K1 ? 0 : 1;
741     finalParam.isEstimate[6] = flags & CALIB_FIX_K2 ? 0 : 1;
742     finalParam.isEstimate[7] = flags & CALIB_FIX_K3 ? 0 : 1;
743     finalParam.isEstimate[8] = flags & CALIB_FIX_K4 ? 0 : 1;
744
745     const int recompute_extrinsic = flags & CALIB_RECOMPUTE_EXTRINSIC ? 1: 0;
746     const int check_cond = flags & CALIB_CHECK_COND ? 1 : 0;
747
748     const double alpha_smooth = 0.4;
749     const double thresh_cond = 1e6;
750     double change = 1;
751     Vec2d err_std;
752
753     Matx33d _K;
754     Vec4d _D;
755     if (flags & CALIB_USE_INTRINSIC_GUESS)
756     {
757         K.getMat().convertTo(_K, CV_64FC1);
758         D.getMat().convertTo(_D, CV_64FC1);
759         finalParam.Init(Vec2d(_K(0,0), _K(1, 1)),
760                         Vec2d(_K(0,2), _K(1, 2)),
761                         Vec4d(flags & CALIB_FIX_K1 ? 0 : _D[0],
762                               flags & CALIB_FIX_K2 ? 0 : _D[1],
763                               flags & CALIB_FIX_K3 ? 0 : _D[2],
764                               flags & CALIB_FIX_K4 ? 0 : _D[3]),
765                         _K(0, 1) / _K(0, 0));
766     }
767     else
768     {
769         finalParam.Init(Vec2d(max(image_size.width, image_size.height) / CV_PI, max(image_size.width, image_size.height) / CV_PI),
770                         Vec2d(image_size.width  / 2.0 - 0.5, image_size.height / 2.0 - 0.5));
771     }
772
773     errors.isEstimate = finalParam.isEstimate;
774
775     std::vector<Vec3d> omc(objectPoints.total()), Tc(objectPoints.total());
776
777     CalibrateExtrinsics(objectPoints, imagePoints, finalParam, check_cond, thresh_cond, omc, Tc);
778
779
780     //-------------------------------Optimization
781     for(int iter = 0; ; ++iter)
782     {
783         if ((criteria.type == 1 && iter >= criteria.maxCount)  ||
784             (criteria.type == 2 && change <= criteria.epsilon) ||
785             (criteria.type == 3 && (change <= criteria.epsilon || iter >= criteria.maxCount)))
786             break;
787
788         double alpha_smooth2 = 1 - std::pow(1 - alpha_smooth, iter + 1.0);
789
790         Mat JJ2, ex3;
791         ComputeJacobians(objectPoints, imagePoints, finalParam, omc, Tc, check_cond,thresh_cond, JJ2, ex3);
792
793         Mat G;
794         solve(JJ2, ex3, G);
795         currentParam = finalParam + alpha_smooth2*G;
796
797         change = norm(Vec4d(currentParam.f[0], currentParam.f[1], currentParam.c[0], currentParam.c[1]) -
798                 Vec4d(finalParam.f[0], finalParam.f[1], finalParam.c[0], finalParam.c[1]))
799                 / norm(Vec4d(currentParam.f[0], currentParam.f[1], currentParam.c[0], currentParam.c[1]));
800
801         finalParam = currentParam;
802
803         if (recompute_extrinsic)
804         {
805             CalibrateExtrinsics(objectPoints,  imagePoints, finalParam, check_cond,
806                                     thresh_cond, omc, Tc);
807         }
808     }
809
810     //-------------------------------Validation
811     double rms;
812     EstimateUncertainties(objectPoints, imagePoints, finalParam,  omc, Tc, errors, err_std, thresh_cond,
813                               check_cond, rms);
814
815     //-------------------------------
816     _K = Matx33d(finalParam.f[0], finalParam.f[0] * finalParam.alpha, finalParam.c[0],
817             0,                    finalParam.f[1], finalParam.c[1],
818             0,                                  0,               1);
819
820     if (K.needed()) cv::Mat(_K).convertTo(K, K.empty() ? CV_64FC1 : K.type());
821     if (D.needed()) cv::Mat(finalParam.k).convertTo(D, D.empty() ? CV_64FC1 : D.type());
822     if (rvecs.isMatVector())
823     {
824         int N = (int)objectPoints.total();
825
826         if(rvecs.empty())
827             rvecs.create(N, 1, CV_64FC3);
828
829         if(tvecs.empty())
830             tvecs.create(N, 1, CV_64FC3);
831
832         for(int i = 0; i < N; i++ )
833         {
834             rvecs.create(3, 1, CV_64F, i, true);
835             tvecs.create(3, 1, CV_64F, i, true);
836             memcpy(rvecs.getMat(i).ptr(), omc[i].val, sizeof(Vec3d));
837             memcpy(tvecs.getMat(i).ptr(), Tc[i].val, sizeof(Vec3d));
838         }
839     }
840     else
841     {
842         if (rvecs.needed()) cv::Mat(omc).convertTo(rvecs, rvecs.empty() ? CV_64FC3 : rvecs.type());
843         if (tvecs.needed()) cv::Mat(Tc).convertTo(tvecs, tvecs.empty() ? CV_64FC3 : tvecs.type());
844     }
845
846     return rms;
847 }
848
849 //////////////////////////////////////////////////////////////////////////////////////////////////////////////
850 /// cv::fisheye::stereoCalibrate
851
852 double cv::fisheye::stereoCalibrate(InputArrayOfArrays objectPoints, InputArrayOfArrays imagePoints1, InputArrayOfArrays imagePoints2,
853                                     InputOutputArray K1, InputOutputArray D1, InputOutputArray K2, InputOutputArray D2, Size imageSize,
854                                     OutputArray R, OutputArray T, int flags, TermCriteria criteria)
855 {
856     CV_INSTRUMENT_REGION()
857
858     CV_Assert(!objectPoints.empty() && !imagePoints1.empty() && !imagePoints2.empty());
859     CV_Assert(objectPoints.total() == imagePoints1.total() || imagePoints1.total() == imagePoints2.total());
860     CV_Assert(objectPoints.type() == CV_32FC3 || objectPoints.type() == CV_64FC3);
861     CV_Assert(imagePoints1.type() == CV_32FC2 || imagePoints1.type() == CV_64FC2);
862     CV_Assert(imagePoints2.type() == CV_32FC2 || imagePoints2.type() == CV_64FC2);
863
864     CV_Assert(K1.empty() || (K1.size() == Size(3,3)));
865     CV_Assert(D1.empty() || (D1.total() == 4));
866     CV_Assert(K2.empty() || (K1.size() == Size(3,3)));
867     CV_Assert(D2.empty() || (D1.total() == 4));
868
869     CV_Assert((!K1.empty() && !K2.empty() && !D1.empty() && !D2.empty()) || !(flags & CALIB_FIX_INTRINSIC));
870
871     //-------------------------------Initialization
872
873     const int threshold = 50;
874     const double thresh_cond = 1e6;
875     const int check_cond = 1;
876
877     int n_points = (int)objectPoints.getMat(0).total();
878     int n_images = (int)objectPoints.total();
879
880     double change = 1;
881
882     cv::internal::IntrinsicParams intrinsicLeft;
883     cv::internal::IntrinsicParams intrinsicRight;
884
885     cv::internal::IntrinsicParams intrinsicLeft_errors;
886     cv::internal::IntrinsicParams intrinsicRight_errors;
887
888     Matx33d _K1, _K2;
889     Vec4d _D1, _D2;
890     if (!K1.empty()) K1.getMat().convertTo(_K1, CV_64FC1);
891     if (!D1.empty()) D1.getMat().convertTo(_D1, CV_64FC1);
892     if (!K2.empty()) K2.getMat().convertTo(_K2, CV_64FC1);
893     if (!D2.empty()) D2.getMat().convertTo(_D2, CV_64FC1);
894
895     std::vector<Vec3d> rvecs1(n_images), tvecs1(n_images), rvecs2(n_images), tvecs2(n_images);
896
897     if (!(flags & CALIB_FIX_INTRINSIC))
898     {
899         calibrate(objectPoints, imagePoints1, imageSize, _K1, _D1, rvecs1, tvecs1, flags, TermCriteria(3, 20, 1e-6));
900         calibrate(objectPoints, imagePoints2, imageSize, _K2, _D2, rvecs2, tvecs2, flags, TermCriteria(3, 20, 1e-6));
901     }
902
903     intrinsicLeft.Init(Vec2d(_K1(0,0), _K1(1, 1)), Vec2d(_K1(0,2), _K1(1, 2)),
904                        Vec4d(_D1[0], _D1[1], _D1[2], _D1[3]), _K1(0, 1) / _K1(0, 0));
905
906     intrinsicRight.Init(Vec2d(_K2(0,0), _K2(1, 1)), Vec2d(_K2(0,2), _K2(1, 2)),
907                         Vec4d(_D2[0], _D2[1], _D2[2], _D2[3]), _K2(0, 1) / _K2(0, 0));
908
909     if ((flags & CALIB_FIX_INTRINSIC))
910     {
911         cv::internal::CalibrateExtrinsics(objectPoints,  imagePoints1, intrinsicLeft, check_cond, thresh_cond, rvecs1, tvecs1);
912         cv::internal::CalibrateExtrinsics(objectPoints,  imagePoints2, intrinsicRight, check_cond, thresh_cond, rvecs2, tvecs2);
913     }
914
915     intrinsicLeft.isEstimate[0] = flags & CALIB_FIX_INTRINSIC ? 0 : 1;
916     intrinsicLeft.isEstimate[1] = flags & CALIB_FIX_INTRINSIC ? 0 : 1;
917     intrinsicLeft.isEstimate[2] = flags & CALIB_FIX_INTRINSIC ? 0 : 1;
918     intrinsicLeft.isEstimate[3] = flags & CALIB_FIX_INTRINSIC ? 0 : 1;
919     intrinsicLeft.isEstimate[4] = flags & (CALIB_FIX_SKEW | CALIB_FIX_INTRINSIC) ? 0 : 1;
920     intrinsicLeft.isEstimate[5] = flags & (CALIB_FIX_K1 | CALIB_FIX_INTRINSIC) ? 0 : 1;
921     intrinsicLeft.isEstimate[6] = flags & (CALIB_FIX_K2 | CALIB_FIX_INTRINSIC) ? 0 : 1;
922     intrinsicLeft.isEstimate[7] = flags & (CALIB_FIX_K3 | CALIB_FIX_INTRINSIC) ? 0 : 1;
923     intrinsicLeft.isEstimate[8] = flags & (CALIB_FIX_K4 | CALIB_FIX_INTRINSIC) ? 0 : 1;
924
925     intrinsicRight.isEstimate[0] = flags & CALIB_FIX_INTRINSIC ? 0 : 1;
926     intrinsicRight.isEstimate[1] = flags & CALIB_FIX_INTRINSIC ? 0 : 1;
927     intrinsicRight.isEstimate[2] = flags & CALIB_FIX_INTRINSIC ? 0 : 1;
928     intrinsicRight.isEstimate[3] = flags & CALIB_FIX_INTRINSIC ? 0 : 1;
929     intrinsicRight.isEstimate[4] = flags & (CALIB_FIX_SKEW | CALIB_FIX_INTRINSIC) ? 0 : 1;
930     intrinsicRight.isEstimate[5] = flags & (CALIB_FIX_K1 | CALIB_FIX_INTRINSIC) ? 0 : 1;
931     intrinsicRight.isEstimate[6] = flags & (CALIB_FIX_K2 | CALIB_FIX_INTRINSIC) ? 0 : 1;
932     intrinsicRight.isEstimate[7] = flags & (CALIB_FIX_K3 | CALIB_FIX_INTRINSIC) ? 0 : 1;
933     intrinsicRight.isEstimate[8] = flags & (CALIB_FIX_K4 | CALIB_FIX_INTRINSIC) ? 0 : 1;
934
935     intrinsicLeft_errors.isEstimate = intrinsicLeft.isEstimate;
936     intrinsicRight_errors.isEstimate = intrinsicRight.isEstimate;
937
938     std::vector<uchar> selectedParams;
939     std::vector<int> tmp(6 * (n_images + 1), 1);
940     selectedParams.insert(selectedParams.end(), intrinsicLeft.isEstimate.begin(), intrinsicLeft.isEstimate.end());
941     selectedParams.insert(selectedParams.end(), intrinsicRight.isEstimate.begin(), intrinsicRight.isEstimate.end());
942     selectedParams.insert(selectedParams.end(), tmp.begin(), tmp.end());
943
944     //Init values for rotation and translation between two views
945     cv::Mat om_list(1, n_images, CV_64FC3), T_list(1, n_images, CV_64FC3);
946     cv::Mat om_ref, R_ref, T_ref, R1, R2;
947     for (int image_idx = 0; image_idx < n_images; ++image_idx)
948     {
949         cv::Rodrigues(rvecs1[image_idx], R1);
950         cv::Rodrigues(rvecs2[image_idx], R2);
951         R_ref = R2 * R1.t();
952         T_ref = cv::Mat(tvecs2[image_idx]) - R_ref * cv::Mat(tvecs1[image_idx]);
953         cv::Rodrigues(R_ref, om_ref);
954         om_ref.reshape(3, 1).copyTo(om_list.col(image_idx));
955         T_ref.reshape(3, 1).copyTo(T_list.col(image_idx));
956     }
957     cv::Vec3d omcur = cv::internal::median3d(om_list);
958     cv::Vec3d Tcur  = cv::internal::median3d(T_list);
959
960     cv::Mat J = cv::Mat::zeros(4 * n_points * n_images, 18 + 6 * (n_images + 1), CV_64FC1),
961             e = cv::Mat::zeros(4 * n_points * n_images, 1, CV_64FC1), Jkk, ekk;
962
963     for(int iter = 0; ; ++iter)
964     {
965         if ((criteria.type == 1 && iter >= criteria.maxCount)  ||
966             (criteria.type == 2 && change <= criteria.epsilon) ||
967             (criteria.type == 3 && (change <= criteria.epsilon || iter >= criteria.maxCount)))
968             break;
969
970         J.create(4 * n_points * n_images, 18 + 6 * (n_images + 1), CV_64FC1);
971         e.create(4 * n_points * n_images, 1, CV_64FC1);
972         Jkk.create(4 * n_points, 18 + 6 * (n_images + 1), CV_64FC1);
973         ekk.create(4 * n_points, 1, CV_64FC1);
974
975         cv::Mat omr, Tr, domrdomckk, domrdTckk, domrdom, domrdT, dTrdomckk, dTrdTckk, dTrdom, dTrdT;
976
977         for (int image_idx = 0; image_idx < n_images; ++image_idx)
978         {
979             Jkk = cv::Mat::zeros(4 * n_points, 18 + 6 * (n_images + 1), CV_64FC1);
980
981             cv::Mat object  = objectPoints.getMat(image_idx).clone();
982             cv::Mat imageLeft  = imagePoints1.getMat(image_idx).clone();
983             cv::Mat imageRight  = imagePoints2.getMat(image_idx).clone();
984             cv::Mat jacobians, projected;
985
986             //left camera jacobian
987             cv::Mat rvec = cv::Mat(rvecs1[image_idx]);
988             cv::Mat tvec  = cv::Mat(tvecs1[image_idx]);
989             cv::internal::projectPoints(object, projected, rvec, tvec, intrinsicLeft, jacobians);
990             cv::Mat(cv::Mat((imageLeft - projected).t()).reshape(1, 1).t()).copyTo(ekk.rowRange(0, 2 * n_points));
991             jacobians.colRange(8, 11).copyTo(Jkk.colRange(24 + image_idx * 6, 27 + image_idx * 6).rowRange(0, 2 * n_points));
992             jacobians.colRange(11, 14).copyTo(Jkk.colRange(27 + image_idx * 6, 30 + image_idx * 6).rowRange(0, 2 * n_points));
993             jacobians.colRange(0, 2).copyTo(Jkk.colRange(0, 2).rowRange(0, 2 * n_points));
994             jacobians.colRange(2, 4).copyTo(Jkk.colRange(2, 4).rowRange(0, 2 * n_points));
995             jacobians.colRange(4, 8).copyTo(Jkk.colRange(5, 9).rowRange(0, 2 * n_points));
996             jacobians.col(14).copyTo(Jkk.col(4).rowRange(0, 2 * n_points));
997
998             //right camera jacobian
999             cv::internal::compose_motion(rvec, tvec, omcur, Tcur, omr, Tr, domrdomckk, domrdTckk, domrdom, domrdT, dTrdomckk, dTrdTckk, dTrdom, dTrdT);
1000             rvec = cv::Mat(rvecs2[image_idx]);
1001             tvec  = cv::Mat(tvecs2[image_idx]);
1002
1003             cv::internal::projectPoints(object, projected, omr, Tr, intrinsicRight, jacobians);
1004             cv::Mat(cv::Mat((imageRight - projected).t()).reshape(1, 1).t()).copyTo(ekk.rowRange(2 * n_points, 4 * n_points));
1005             cv::Mat dxrdom = jacobians.colRange(8, 11) * domrdom + jacobians.colRange(11, 14) * dTrdom;
1006             cv::Mat dxrdT = jacobians.colRange(8, 11) * domrdT + jacobians.colRange(11, 14)* dTrdT;
1007             cv::Mat dxrdomckk = jacobians.colRange(8, 11) * domrdomckk + jacobians.colRange(11, 14) * dTrdomckk;
1008             cv::Mat dxrdTckk = jacobians.colRange(8, 11) * domrdTckk + jacobians.colRange(11, 14) * dTrdTckk;
1009
1010             dxrdom.copyTo(Jkk.colRange(18, 21).rowRange(2 * n_points, 4 * n_points));
1011             dxrdT.copyTo(Jkk.colRange(21, 24).rowRange(2 * n_points, 4 * n_points));
1012             dxrdomckk.copyTo(Jkk.colRange(24 + image_idx * 6, 27 + image_idx * 6).rowRange(2 * n_points, 4 * n_points));
1013             dxrdTckk.copyTo(Jkk.colRange(27 + image_idx * 6, 30 + image_idx * 6).rowRange(2 * n_points, 4 * n_points));
1014             jacobians.colRange(0, 2).copyTo(Jkk.colRange(9 + 0, 9 + 2).rowRange(2 * n_points, 4 * n_points));
1015             jacobians.colRange(2, 4).copyTo(Jkk.colRange(9 + 2, 9 + 4).rowRange(2 * n_points, 4 * n_points));
1016             jacobians.colRange(4, 8).copyTo(Jkk.colRange(9 + 5, 9 + 9).rowRange(2 * n_points, 4 * n_points));
1017             jacobians.col(14).copyTo(Jkk.col(9 + 4).rowRange(2 * n_points, 4 * n_points));
1018
1019             //check goodness of sterepair
1020             double abs_max  = 0;
1021             for (int i = 0; i < 4 * n_points; i++)
1022             {
1023                 if (fabs(ekk.at<double>(i)) > abs_max)
1024                 {
1025                     abs_max = fabs(ekk.at<double>(i));
1026                 }
1027             }
1028
1029             CV_Assert(abs_max < threshold); // bad stereo pair
1030
1031             Jkk.copyTo(J.rowRange(image_idx * 4 * n_points, (image_idx + 1) * 4 * n_points));
1032             ekk.copyTo(e.rowRange(image_idx * 4 * n_points, (image_idx + 1) * 4 * n_points));
1033         }
1034
1035         cv::Vec6d oldTom(Tcur[0], Tcur[1], Tcur[2], omcur[0], omcur[1], omcur[2]);
1036
1037         //update all parameters
1038         cv::subMatrix(J, J, selectedParams, std::vector<uchar>(J.rows, 1));
1039         int a = cv::countNonZero(intrinsicLeft.isEstimate);
1040         int b = cv::countNonZero(intrinsicRight.isEstimate);
1041         cv::Mat deltas;
1042         solve(J.t() * J, J.t()*e, deltas);
1043         intrinsicLeft = intrinsicLeft + deltas.rowRange(0, a);
1044         intrinsicRight = intrinsicRight + deltas.rowRange(a, a + b);
1045         omcur = omcur + cv::Vec3d(deltas.rowRange(a + b, a + b + 3));
1046         Tcur = Tcur + cv::Vec3d(deltas.rowRange(a + b + 3, a + b + 6));
1047         for (int image_idx = 0; image_idx < n_images; ++image_idx)
1048         {
1049             rvecs1[image_idx] = cv::Mat(cv::Mat(rvecs1[image_idx]) + deltas.rowRange(a + b + 6 + image_idx * 6, a + b + 9 + image_idx * 6));
1050             tvecs1[image_idx] = cv::Mat(cv::Mat(tvecs1[image_idx]) + deltas.rowRange(a + b + 9 + image_idx * 6, a + b + 12 + image_idx * 6));
1051         }
1052
1053         cv::Vec6d newTom(Tcur[0], Tcur[1], Tcur[2], omcur[0], omcur[1], omcur[2]);
1054         change = cv::norm(newTom - oldTom) / cv::norm(newTom);
1055     }
1056
1057     double rms = 0;
1058     const Vec2d* ptr_e = e.ptr<Vec2d>();
1059     for (size_t i = 0; i < e.total() / 2; i++)
1060     {
1061         rms += ptr_e[i][0] * ptr_e[i][0] + ptr_e[i][1] * ptr_e[i][1];
1062     }
1063
1064     rms /= ((double)e.total() / 2.0);
1065     rms = sqrt(rms);
1066
1067     _K1 = Matx33d(intrinsicLeft.f[0], intrinsicLeft.f[0] * intrinsicLeft.alpha, intrinsicLeft.c[0],
1068                                        0,                       intrinsicLeft.f[1], intrinsicLeft.c[1],
1069                                        0,                                        0,                 1);
1070
1071     _K2 = Matx33d(intrinsicRight.f[0], intrinsicRight.f[0] * intrinsicRight.alpha, intrinsicRight.c[0],
1072                                         0,                        intrinsicRight.f[1], intrinsicRight.c[1],
1073                                         0,                                          0,                  1);
1074
1075     Mat _R;
1076     Rodrigues(omcur, _R);
1077
1078     if (K1.needed()) cv::Mat(_K1).convertTo(K1, K1.empty() ? CV_64FC1 : K1.type());
1079     if (K2.needed()) cv::Mat(_K2).convertTo(K2, K2.empty() ? CV_64FC1 : K2.type());
1080     if (D1.needed()) cv::Mat(intrinsicLeft.k).convertTo(D1, D1.empty() ? CV_64FC1 : D1.type());
1081     if (D2.needed()) cv::Mat(intrinsicRight.k).convertTo(D2, D2.empty() ? CV_64FC1 : D2.type());
1082     if (R.needed()) _R.convertTo(R, R.empty() ? CV_64FC1 : R.type());
1083     if (T.needed()) cv::Mat(Tcur).convertTo(T, T.empty() ? CV_64FC1 : T.type());
1084
1085     return rms;
1086 }
1087
1088 namespace cv{ namespace {
1089 void subMatrix(const Mat& src, Mat& dst, const std::vector<uchar>& cols, const std::vector<uchar>& rows)
1090 {
1091     CV_Assert(src.channels() == 1);
1092
1093     int nonzeros_cols = cv::countNonZero(cols);
1094     Mat tmp(src.rows, nonzeros_cols, CV_64F);
1095
1096     for (int i = 0, j = 0; i < (int)cols.size(); i++)
1097     {
1098         if (cols[i])
1099         {
1100             src.col(i).copyTo(tmp.col(j++));
1101         }
1102     }
1103
1104     int nonzeros_rows  = cv::countNonZero(rows);
1105     dst.create(nonzeros_rows, nonzeros_cols, CV_64F);
1106     for (int i = 0, j = 0; i < (int)rows.size(); i++)
1107     {
1108         if (rows[i])
1109         {
1110             tmp.row(i).copyTo(dst.row(j++));
1111         }
1112     }
1113 }
1114
1115 }}
1116
1117 cv::internal::IntrinsicParams::IntrinsicParams():
1118     f(Vec2d::all(0)), c(Vec2d::all(0)), k(Vec4d::all(0)), alpha(0), isEstimate(9,0)
1119 {
1120 }
1121
1122 cv::internal::IntrinsicParams::IntrinsicParams(Vec2d _f, Vec2d _c, Vec4d _k, double _alpha):
1123     f(_f), c(_c), k(_k), alpha(_alpha), isEstimate(9,0)
1124 {
1125 }
1126
1127 cv::internal::IntrinsicParams cv::internal::IntrinsicParams::operator+(const Mat& a)
1128 {
1129     CV_Assert(a.type() == CV_64FC1);
1130     IntrinsicParams tmp;
1131     const double* ptr = a.ptr<double>();
1132
1133     int j = 0;
1134     tmp.f[0]    = this->f[0]    + (isEstimate[0] ? ptr[j++] : 0);
1135     tmp.f[1]    = this->f[1]    + (isEstimate[1] ? ptr[j++] : 0);
1136     tmp.c[0]    = this->c[0]    + (isEstimate[2] ? ptr[j++] : 0);
1137     tmp.alpha   = this->alpha   + (isEstimate[4] ? ptr[j++] : 0);
1138     tmp.c[1]    = this->c[1]    + (isEstimate[3] ? ptr[j++] : 0);
1139     tmp.k[0]    = this->k[0]    + (isEstimate[5] ? ptr[j++] : 0);
1140     tmp.k[1]    = this->k[1]    + (isEstimate[6] ? ptr[j++] : 0);
1141     tmp.k[2]    = this->k[2]    + (isEstimate[7] ? ptr[j++] : 0);
1142     tmp.k[3]    = this->k[3]    + (isEstimate[8] ? ptr[j++] : 0);
1143
1144     tmp.isEstimate = isEstimate;
1145     return tmp;
1146 }
1147
1148 cv::internal::IntrinsicParams& cv::internal::IntrinsicParams::operator =(const Mat& a)
1149 {
1150     CV_Assert(a.type() == CV_64FC1);
1151     const double* ptr = a.ptr<double>();
1152
1153     int j = 0;
1154
1155     this->f[0]  = isEstimate[0] ? ptr[j++] : 0;
1156     this->f[1]  = isEstimate[1] ? ptr[j++] : 0;
1157     this->c[0]  = isEstimate[2] ? ptr[j++] : 0;
1158     this->c[1]  = isEstimate[3] ? ptr[j++] : 0;
1159     this->alpha = isEstimate[4] ? ptr[j++] : 0;
1160     this->k[0]  = isEstimate[5] ? ptr[j++] : 0;
1161     this->k[1]  = isEstimate[6] ? ptr[j++] : 0;
1162     this->k[2]  = isEstimate[7] ? ptr[j++] : 0;
1163     this->k[3]  = isEstimate[8] ? ptr[j++] : 0;
1164
1165     return *this;
1166 }
1167
1168 void cv::internal::IntrinsicParams::Init(const cv::Vec2d& _f, const cv::Vec2d& _c, const cv::Vec4d& _k, const double& _alpha)
1169 {
1170     this->c = _c;
1171     this->f = _f;
1172     this->k = _k;
1173     this->alpha = _alpha;
1174 }
1175
1176 void cv::internal::projectPoints(cv::InputArray objectPoints, cv::OutputArray imagePoints,
1177                    cv::InputArray _rvec,cv::InputArray _tvec,
1178                    const IntrinsicParams& param, cv::OutputArray jacobian)
1179 {
1180     CV_INSTRUMENT_REGION()
1181
1182     CV_Assert(!objectPoints.empty() && objectPoints.type() == CV_64FC3);
1183     Matx33d K(param.f[0], param.f[0] * param.alpha, param.c[0],
1184                        0,               param.f[1], param.c[1],
1185                        0,                        0,         1);
1186     fisheye::projectPoints(objectPoints, imagePoints, _rvec, _tvec, K, param.k, param.alpha, jacobian);
1187 }
1188
1189 void cv::internal::ComputeExtrinsicRefine(const Mat& imagePoints, const Mat& objectPoints, Mat& rvec,
1190                             Mat&  tvec, Mat& J, const int MaxIter,
1191                             const IntrinsicParams& param, const double thresh_cond)
1192 {
1193     CV_Assert(!objectPoints.empty() && objectPoints.type() == CV_64FC3);
1194     CV_Assert(!imagePoints.empty() && imagePoints.type() == CV_64FC2);
1195     Vec6d extrinsics(rvec.at<double>(0), rvec.at<double>(1), rvec.at<double>(2),
1196                     tvec.at<double>(0), tvec.at<double>(1), tvec.at<double>(2));
1197     double change = 1;
1198     int iter = 0;
1199
1200     while (change > 1e-10 && iter < MaxIter)
1201     {
1202         std::vector<Point2d> x;
1203         Mat jacobians;
1204         projectPoints(objectPoints, x, rvec, tvec, param, jacobians);
1205
1206         Mat ex = imagePoints - Mat(x).t();
1207         ex = ex.reshape(1, 2);
1208
1209         J = jacobians.colRange(8, 14).clone();
1210
1211         SVD svd(J, SVD::NO_UV);
1212         double condJJ = svd.w.at<double>(0)/svd.w.at<double>(5);
1213
1214         if (condJJ > thresh_cond)
1215             change = 0;
1216         else
1217         {
1218             Vec6d param_innov;
1219             solve(J, ex.reshape(1, (int)ex.total()), param_innov, DECOMP_SVD + DECOMP_NORMAL);
1220
1221             Vec6d param_up = extrinsics + param_innov;
1222             change = norm(param_innov)/norm(param_up);
1223             extrinsics = param_up;
1224             iter = iter + 1;
1225
1226             rvec = Mat(Vec3d(extrinsics.val));
1227             tvec = Mat(Vec3d(extrinsics.val+3));
1228         }
1229     }
1230 }
1231
1232 cv::Mat cv::internal::ComputeHomography(Mat m, Mat M)
1233 {
1234     CV_INSTRUMENT_REGION()
1235
1236     int Np = m.cols;
1237
1238     if (m.rows < 3)
1239     {
1240         vconcat(m, Mat::ones(1, Np, CV_64FC1), m);
1241     }
1242     if (M.rows < 3)
1243     {
1244         vconcat(M, Mat::ones(1, Np, CV_64FC1), M);
1245     }
1246
1247     divide(m, Mat::ones(3, 1, CV_64FC1) * m.row(2), m);
1248     divide(M, Mat::ones(3, 1, CV_64FC1) * M.row(2), M);
1249
1250     Mat ax = m.row(0).clone();
1251     Mat ay = m.row(1).clone();
1252
1253     double mxx = mean(ax)[0];
1254     double myy = mean(ay)[0];
1255
1256     ax = ax - mxx;
1257     ay = ay - myy;
1258
1259     double scxx = mean(abs(ax))[0];
1260     double scyy = mean(abs(ay))[0];
1261
1262     Mat Hnorm (Matx33d( 1/scxx,        0.0,     -mxx/scxx,
1263                          0.0,     1/scyy,     -myy/scyy,
1264                          0.0,        0.0,           1.0 ));
1265
1266     Mat inv_Hnorm (Matx33d( scxx,     0,   mxx,
1267                                     0,  scyy,   myy,
1268                                     0,     0,     1 ));
1269     Mat mn =  Hnorm * m;
1270
1271     Mat L = Mat::zeros(2*Np, 9, CV_64FC1);
1272
1273     for (int i = 0; i < Np; ++i)
1274     {
1275         for (int j = 0; j < 3; j++)
1276         {
1277             L.at<double>(2 * i, j) = M.at<double>(j, i);
1278             L.at<double>(2 * i + 1, j + 3) = M.at<double>(j, i);
1279             L.at<double>(2 * i, j + 6) = -mn.at<double>(0,i) * M.at<double>(j, i);
1280             L.at<double>(2 * i + 1, j + 6) = -mn.at<double>(1,i) * M.at<double>(j, i);
1281         }
1282     }
1283
1284     if (Np > 4) L = L.t() * L;
1285     SVD svd(L);
1286     Mat hh = svd.vt.row(8) / svd.vt.row(8).at<double>(8);
1287     Mat Hrem = hh.reshape(1, 3);
1288     Mat H = inv_Hnorm * Hrem;
1289
1290     if (Np > 4)
1291     {
1292         Mat hhv = H.reshape(1, 9)(Rect(0, 0, 1, 8)).clone();
1293         for (int iter = 0; iter < 10; iter++)
1294         {
1295             Mat mrep = H * M;
1296             Mat J = Mat::zeros(2 * Np, 8, CV_64FC1);
1297             Mat MMM;
1298             divide(M, Mat::ones(3, 1, CV_64FC1) * mrep(Rect(0, 2, mrep.cols, 1)), MMM);
1299             divide(mrep, Mat::ones(3, 1, CV_64FC1) * mrep(Rect(0, 2, mrep.cols, 1)), mrep);
1300             Mat m_err = m(Rect(0,0, m.cols, 2)) - mrep(Rect(0,0, mrep.cols, 2));
1301             m_err = Mat(m_err.t()).reshape(1, m_err.cols * m_err.rows);
1302             Mat MMM2, MMM3;
1303             multiply(Mat::ones(3, 1, CV_64FC1) * mrep(Rect(0, 0, mrep.cols, 1)), MMM, MMM2);
1304             multiply(Mat::ones(3, 1, CV_64FC1) * mrep(Rect(0, 1, mrep.cols, 1)), MMM, MMM3);
1305
1306             for (int i = 0; i < Np; ++i)
1307             {
1308                 for (int j = 0; j < 3; ++j)
1309                 {
1310                     J.at<double>(2 * i, j)         = -MMM.at<double>(j, i);
1311                     J.at<double>(2 * i + 1, j + 3) = -MMM.at<double>(j, i);
1312                 }
1313
1314                 for (int j = 0; j < 2; ++j)
1315                 {
1316                     J.at<double>(2 * i, j + 6)     = MMM2.at<double>(j, i);
1317                     J.at<double>(2 * i + 1, j + 6) = MMM3.at<double>(j, i);
1318                 }
1319             }
1320             divide(M, Mat::ones(3, 1, CV_64FC1) * mrep(Rect(0,2,mrep.cols,1)), MMM);
1321             Mat hh_innov = (J.t() * J).inv() * (J.t()) * m_err;
1322             Mat hhv_up = hhv - hh_innov;
1323             Mat tmp;
1324             vconcat(hhv_up, Mat::ones(1,1,CV_64FC1), tmp);
1325             Mat H_up = tmp.reshape(1,3);
1326             hhv = hhv_up;
1327             H = H_up;
1328         }
1329     }
1330     return H;
1331 }
1332
1333 cv::Mat cv::internal::NormalizePixels(const Mat& imagePoints, const IntrinsicParams& param)
1334 {
1335     CV_INSTRUMENT_REGION()
1336
1337     CV_Assert(!imagePoints.empty() && imagePoints.type() == CV_64FC2);
1338
1339     Mat distorted((int)imagePoints.total(), 1, CV_64FC2), undistorted;
1340     const Vec2d* ptr   = imagePoints.ptr<Vec2d>();
1341     Vec2d* ptr_d = distorted.ptr<Vec2d>();
1342     for (size_t i = 0; i < imagePoints.total(); ++i)
1343     {
1344         ptr_d[i] = (ptr[i] - param.c).mul(Vec2d(1.0 / param.f[0], 1.0 / param.f[1]));
1345         ptr_d[i][0] -= param.alpha * ptr_d[i][1];
1346     }
1347     cv::fisheye::undistortPoints(distorted, undistorted, Matx33d::eye(), param.k);
1348     return undistorted;
1349 }
1350
1351 void cv::internal::InitExtrinsics(const Mat& _imagePoints, const Mat& _objectPoints, const IntrinsicParams& param, Mat& omckk, Mat& Tckk)
1352 {
1353     CV_Assert(!_objectPoints.empty() && _objectPoints.type() == CV_64FC3);
1354     CV_Assert(!_imagePoints.empty() && _imagePoints.type() == CV_64FC2);
1355
1356     Mat imagePointsNormalized = NormalizePixels(_imagePoints, param).reshape(1).t();
1357     Mat objectPoints = _objectPoints.reshape(1).t();
1358     Mat objectPointsMean, covObjectPoints;
1359     Mat Rckk;
1360     int Np = imagePointsNormalized.cols;
1361     calcCovarMatrix(objectPoints, covObjectPoints, objectPointsMean, COVAR_NORMAL | COVAR_COLS);
1362     SVD svd(covObjectPoints);
1363     Mat R(svd.vt);
1364     if (norm(R(Rect(2, 0, 1, 2))) < 1e-6)
1365         R = Mat::eye(3,3, CV_64FC1);
1366     if (determinant(R) < 0)
1367         R = -R;
1368     Mat T = -R * objectPointsMean;
1369     Mat X_new = R * objectPoints + T * Mat::ones(1, Np, CV_64FC1);
1370     Mat H = ComputeHomography(imagePointsNormalized, X_new(Rect(0,0,X_new.cols,2)));
1371     double sc = .5 * (norm(H.col(0)) + norm(H.col(1)));
1372     H = H / sc;
1373     Mat u1 = H.col(0).clone();
1374     u1  = u1 / norm(u1);
1375     Mat u2 = H.col(1).clone() - u1.dot(H.col(1).clone()) * u1;
1376     u2 = u2 / norm(u2);
1377     Mat u3 = u1.cross(u2);
1378     Mat RRR;
1379     hconcat(u1, u2, RRR);
1380     hconcat(RRR, u3, RRR);
1381     Rodrigues(RRR, omckk);
1382     Rodrigues(omckk, Rckk);
1383     Tckk = H.col(2).clone();
1384     Tckk = Tckk + Rckk * T;
1385     Rckk = Rckk * R;
1386     Rodrigues(Rckk, omckk);
1387 }
1388
1389 void cv::internal::CalibrateExtrinsics(InputArrayOfArrays objectPoints, InputArrayOfArrays imagePoints,
1390                          const IntrinsicParams& param, const int check_cond,
1391                          const double thresh_cond, InputOutputArray omc, InputOutputArray Tc)
1392 {
1393     CV_Assert(!objectPoints.empty() && (objectPoints.type() == CV_32FC3 || objectPoints.type() == CV_64FC3));
1394     CV_Assert(!imagePoints.empty() && (imagePoints.type() == CV_32FC2 || imagePoints.type() == CV_64FC2));
1395     CV_Assert(omc.type() == CV_64FC3 || Tc.type() == CV_64FC3);
1396
1397     if (omc.empty()) omc.create(1, (int)objectPoints.total(), CV_64FC3);
1398     if (Tc.empty()) Tc.create(1, (int)objectPoints.total(), CV_64FC3);
1399
1400     const int maxIter = 20;
1401
1402     for(int image_idx = 0; image_idx < (int)imagePoints.total(); ++image_idx)
1403     {
1404         Mat omckk, Tckk, JJ_kk;
1405         Mat image, object;
1406
1407         objectPoints.getMat(image_idx).convertTo(object,  CV_64FC3);
1408         imagePoints.getMat (image_idx).convertTo(image, CV_64FC2);
1409
1410         bool imT = image.rows < image.cols;
1411         bool obT = object.rows < object.cols;
1412
1413         InitExtrinsics(imT ? image.t() : image, obT ? object.t() : object, param, omckk, Tckk);
1414
1415         ComputeExtrinsicRefine(!imT ? image.t() : image, !obT ? object.t() : object, omckk, Tckk, JJ_kk, maxIter, param, thresh_cond);
1416         if (check_cond)
1417         {
1418             SVD svd(JJ_kk, SVD::NO_UV);
1419             CV_Assert(svd.w.at<double>(0) / svd.w.at<double>((int)svd.w.total() - 1) < thresh_cond);
1420         }
1421         omckk.reshape(3,1).copyTo(omc.getMat().col(image_idx));
1422         Tckk.reshape(3,1).copyTo(Tc.getMat().col(image_idx));
1423     }
1424 }
1425
1426 void cv::internal::ComputeJacobians(InputArrayOfArrays objectPoints, InputArrayOfArrays imagePoints,
1427                       const IntrinsicParams& param,  InputArray omc, InputArray Tc,
1428                       const int& check_cond, const double& thresh_cond, Mat& JJ2, Mat& ex3)
1429 {
1430     CV_Assert(!objectPoints.empty() && (objectPoints.type() == CV_32FC3 || objectPoints.type() == CV_64FC3));
1431     CV_Assert(!imagePoints.empty() && (imagePoints.type() == CV_32FC2 || imagePoints.type() == CV_64FC2));
1432
1433     CV_Assert(!omc.empty() && omc.type() == CV_64FC3);
1434     CV_Assert(!Tc.empty() && Tc.type() == CV_64FC3);
1435
1436     int n = (int)objectPoints.total();
1437
1438     JJ2 = Mat::zeros(9 + 6 * n, 9 + 6 * n, CV_64FC1);
1439     ex3 = Mat::zeros(9 + 6 * n, 1, CV_64FC1 );
1440
1441     for (int image_idx = 0; image_idx < n; ++image_idx)
1442     {
1443         Mat image, object;
1444         objectPoints.getMat(image_idx).convertTo(object, CV_64FC3);
1445         imagePoints.getMat (image_idx).convertTo(image, CV_64FC2);
1446
1447         bool imT = image.rows < image.cols;
1448         Mat om(omc.getMat().col(image_idx)), T(Tc.getMat().col(image_idx));
1449
1450         std::vector<Point2d> x;
1451         Mat jacobians;
1452         projectPoints(object, x, om, T, param, jacobians);
1453         Mat exkk = (imT ? image.t() : image) - Mat(x);
1454
1455         Mat A(jacobians.rows, 9, CV_64FC1);
1456         jacobians.colRange(0, 4).copyTo(A.colRange(0, 4));
1457         jacobians.col(14).copyTo(A.col(4));
1458         jacobians.colRange(4, 8).copyTo(A.colRange(5, 9));
1459
1460         A = A.t();
1461
1462         Mat B = jacobians.colRange(8, 14).clone();
1463         B = B.t();
1464
1465         JJ2(Rect(0, 0, 9, 9)) += A * A.t();
1466         JJ2(Rect(9 + 6 * image_idx, 9 + 6 * image_idx, 6, 6)) = B * B.t();
1467
1468         JJ2(Rect(9 + 6 * image_idx, 0, 6, 9)) = A * B.t();
1469         JJ2(Rect(0, 9 + 6 * image_idx, 9, 6)) = JJ2(Rect(9 + 6 * image_idx, 0, 6, 9)).t();
1470
1471         ex3.rowRange(0, 9) += A * exkk.reshape(1, 2 * exkk.rows);
1472         ex3.rowRange(9 + 6 * image_idx, 9 + 6 * (image_idx + 1)) = B * exkk.reshape(1, 2 * exkk.rows);
1473
1474         if (check_cond)
1475         {
1476             Mat JJ_kk = B.t();
1477             SVD svd(JJ_kk, SVD::NO_UV);
1478             CV_Assert(svd.w.at<double>(0) / svd.w.at<double>(svd.w.rows - 1) < thresh_cond);
1479         }
1480     }
1481
1482     std::vector<uchar> idxs(param.isEstimate);
1483     idxs.insert(idxs.end(), 6 * n, 1);
1484
1485     subMatrix(JJ2, JJ2, idxs, idxs);
1486     subMatrix(ex3, ex3, std::vector<uchar>(1, 1), idxs);
1487 }
1488
1489 void cv::internal::EstimateUncertainties(InputArrayOfArrays objectPoints, InputArrayOfArrays imagePoints,
1490                            const IntrinsicParams& params, InputArray omc, InputArray Tc,
1491                            IntrinsicParams& errors, Vec2d& std_err, double thresh_cond, int check_cond, double& rms)
1492 {
1493     CV_Assert(!objectPoints.empty() && (objectPoints.type() == CV_32FC3 || objectPoints.type() == CV_64FC3));
1494     CV_Assert(!imagePoints.empty() && (imagePoints.type() == CV_32FC2 || imagePoints.type() == CV_64FC2));
1495
1496     CV_Assert(!omc.empty() && omc.type() == CV_64FC3);
1497     CV_Assert(!Tc.empty() && Tc.type() == CV_64FC3);
1498
1499     int total_ex = 0;
1500     for (int image_idx = 0; image_idx < (int)objectPoints.total(); ++image_idx)
1501     {
1502         total_ex += (int)objectPoints.getMat(image_idx).total();
1503     }
1504     Mat ex(total_ex, 1, CV_64FC2);
1505     int insert_idx = 0;
1506     for (int image_idx = 0; image_idx < (int)objectPoints.total(); ++image_idx)
1507     {
1508         Mat image, object;
1509         objectPoints.getMat(image_idx).convertTo(object, CV_64FC3);
1510         imagePoints.getMat (image_idx).convertTo(image, CV_64FC2);
1511
1512         bool imT = image.rows < image.cols;
1513
1514         Mat om(omc.getMat().col(image_idx)), T(Tc.getMat().col(image_idx));
1515
1516         std::vector<Point2d> x;
1517         projectPoints(object, x, om, T, params, noArray());
1518         Mat ex_ = (imT ? image.t() : image) - Mat(x);
1519         ex_.copyTo(ex.rowRange(insert_idx, insert_idx + ex_.rows));
1520         insert_idx += ex_.rows;
1521     }
1522
1523     meanStdDev(ex, noArray(), std_err);
1524     std_err *= sqrt((double)ex.total()/((double)ex.total() - 1.0));
1525
1526     Vec<double, 1> sigma_x;
1527     meanStdDev(ex.reshape(1, 1), noArray(), sigma_x);
1528     sigma_x  *= sqrt(2.0 * (double)ex.total()/(2.0 * (double)ex.total() - 1.0));
1529
1530     Mat JJ2, ex3;
1531     ComputeJacobians(objectPoints, imagePoints, params, omc, Tc, check_cond, thresh_cond, JJ2, ex3);
1532
1533     sqrt(JJ2.inv(), JJ2);
1534
1535     errors = 3 * sigma_x(0) * JJ2.diag();
1536     rms = sqrt(norm(ex, NORM_L2SQR)/ex.total());
1537 }
1538
1539 void cv::internal::dAB(InputArray A, InputArray B, OutputArray dABdA, OutputArray dABdB)
1540 {
1541     CV_Assert(A.getMat().cols == B.getMat().rows);
1542     CV_Assert(A.type() == CV_64FC1 && B.type() == CV_64FC1);
1543
1544     int p = A.getMat().rows;
1545     int n = A.getMat().cols;
1546     int q = B.getMat().cols;
1547
1548     dABdA.create(p * q, p * n, CV_64FC1);
1549     dABdB.create(p * q, q * n, CV_64FC1);
1550
1551     dABdA.getMat() = Mat::zeros(p * q, p * n, CV_64FC1);
1552     dABdB.getMat() = Mat::zeros(p * q, q * n, CV_64FC1);
1553
1554     for (int i = 0; i < q; ++i)
1555     {
1556         for (int j = 0; j < p; ++j)
1557         {
1558             int ij = j + i * p;
1559             for (int k = 0; k < n; ++k)
1560             {
1561                 int kj = j + k * p;
1562                 dABdA.getMat().at<double>(ij, kj) = B.getMat().at<double>(k, i);
1563             }
1564         }
1565     }
1566
1567     for (int i = 0; i < q; ++i)
1568     {
1569         A.getMat().copyTo(dABdB.getMat().rowRange(i * p, i * p + p).colRange(i * n, i * n + n));
1570     }
1571 }
1572
1573 void cv::internal::JRodriguesMatlab(const Mat& src, Mat& dst)
1574 {
1575     Mat tmp(src.cols, src.rows, src.type());
1576     if (src.rows == 9)
1577     {
1578         Mat(src.row(0).t()).copyTo(tmp.col(0));
1579         Mat(src.row(1).t()).copyTo(tmp.col(3));
1580         Mat(src.row(2).t()).copyTo(tmp.col(6));
1581         Mat(src.row(3).t()).copyTo(tmp.col(1));
1582         Mat(src.row(4).t()).copyTo(tmp.col(4));
1583         Mat(src.row(5).t()).copyTo(tmp.col(7));
1584         Mat(src.row(6).t()).copyTo(tmp.col(2));
1585         Mat(src.row(7).t()).copyTo(tmp.col(5));
1586         Mat(src.row(8).t()).copyTo(tmp.col(8));
1587     }
1588     else
1589     {
1590         Mat(src.col(0).t()).copyTo(tmp.row(0));
1591         Mat(src.col(1).t()).copyTo(tmp.row(3));
1592         Mat(src.col(2).t()).copyTo(tmp.row(6));
1593         Mat(src.col(3).t()).copyTo(tmp.row(1));
1594         Mat(src.col(4).t()).copyTo(tmp.row(4));
1595         Mat(src.col(5).t()).copyTo(tmp.row(7));
1596         Mat(src.col(6).t()).copyTo(tmp.row(2));
1597         Mat(src.col(7).t()).copyTo(tmp.row(5));
1598         Mat(src.col(8).t()).copyTo(tmp.row(8));
1599     }
1600     dst = tmp.clone();
1601 }
1602
1603 void cv::internal::compose_motion(InputArray _om1, InputArray _T1, InputArray _om2, InputArray _T2,
1604                     Mat& om3, Mat& T3, Mat& dom3dom1, Mat& dom3dT1, Mat& dom3dom2,
1605                     Mat& dom3dT2, Mat& dT3dom1, Mat& dT3dT1, Mat& dT3dom2, Mat& dT3dT2)
1606 {
1607     Mat om1 = _om1.getMat();
1608     Mat om2 = _om2.getMat();
1609     Mat T1 = _T1.getMat().reshape(1, 3);
1610     Mat T2 = _T2.getMat().reshape(1, 3);
1611
1612     //% Rotations:
1613     Mat R1, R2, R3, dR1dom1(9, 3, CV_64FC1), dR2dom2;
1614     Rodrigues(om1, R1, dR1dom1);
1615     Rodrigues(om2, R2, dR2dom2);
1616     JRodriguesMatlab(dR1dom1, dR1dom1);
1617     JRodriguesMatlab(dR2dom2, dR2dom2);
1618     R3 = R2 * R1;
1619     Mat dR3dR2, dR3dR1;
1620     dAB(R2, R1, dR3dR2, dR3dR1);
1621     Mat dom3dR3;
1622     Rodrigues(R3, om3, dom3dR3);
1623     JRodriguesMatlab(dom3dR3, dom3dR3);
1624     dom3dom1 = dom3dR3 * dR3dR1 * dR1dom1;
1625     dom3dom2 = dom3dR3 * dR3dR2 * dR2dom2;
1626     dom3dT1 = Mat::zeros(3, 3, CV_64FC1);
1627     dom3dT2 = Mat::zeros(3, 3, CV_64FC1);
1628
1629     //% Translations:
1630     Mat T3t = R2 * T1;
1631     Mat dT3tdR2, dT3tdT1;
1632     dAB(R2, T1, dT3tdR2, dT3tdT1);
1633     Mat dT3tdom2 = dT3tdR2 * dR2dom2;
1634     T3 = T3t + T2;
1635     dT3dT1 = dT3tdT1;
1636     dT3dT2 = Mat::eye(3, 3, CV_64FC1);
1637     dT3dom2 = dT3tdom2;
1638     dT3dom1 = Mat::zeros(3, 3, CV_64FC1);
1639 }
1640
1641 double cv::internal::median(const Mat& row)
1642 {
1643     CV_Assert(row.type() == CV_64FC1);
1644     CV_Assert(!row.empty() && row.rows == 1);
1645     Mat tmp = row.clone();
1646     sort(tmp, tmp, 0);
1647     if ((int)tmp.total() % 2) return tmp.at<double>((int)tmp.total() / 2);
1648     else return 0.5 *(tmp.at<double>((int)tmp.total() / 2) + tmp.at<double>((int)tmp.total() / 2 - 1));
1649 }
1650
1651 cv::Vec3d cv::internal::median3d(InputArray m)
1652 {
1653     CV_Assert(m.depth() == CV_64F && m.getMat().rows == 1);
1654     Mat M = Mat(m.getMat().t()).reshape(1).t();
1655     return Vec3d(median(M.row(0)), median(M.row(1)), median(M.row(2)));
1656 }