Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
Tetragramm
GitHub Repository: Tetragramm/opencv
Path: blob/master/modules/calib3d/src/triangulate.cpp
16354 views
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
// Intel License Agreement
11
// For Open Source Computer Vision Library
12
//
13
// Copyright (C) 2009, Intel Corporation and others, all rights reserved.
14
// Third party copyrights are property of their respective owners.
15
//
16
// Redistribution and use in source and binary forms, with or without modification,
17
// are permitted provided that the following conditions are met:
18
//
19
// * Redistribution's of source code must retain the above copyright notice,
20
// this list of conditions and the following disclaimer.
21
//
22
// * Redistribution's in binary form must reproduce the above copyright notice,
23
// this list of conditions and the following disclaimer in the documentation
24
// and/or other materials provided with the distribution.
25
//
26
// * The name of Intel Corporation may not be used to endorse or promote products
27
// derived from this software without specific prior written permission.
28
//
29
// This software is provided by the copyright holders and contributors "as is" and
30
// any express or implied warranties, including, but not limited to, the implied
31
// warranties of merchantability and fitness for a particular purpose are disclaimed.
32
// In no event shall the Intel Corporation or contributors be liable for any direct,
33
// indirect, incidental, special, exemplary, or consequential damages
34
// (including, but not limited to, procurement of substitute goods or services;
35
// loss of use, data, or profits; or business interruption) however caused
36
// and on any theory of liability, whether in contract, strict liability,
37
// or tort (including negligence or otherwise) arising in any way out of
38
// the use of this software, even if advised of the possibility of such damage.
39
//
40
//M*/
41
42
#include "precomp.hpp"
43
#include "opencv2/calib3d/calib3d_c.h"
44
45
// cvCorrectMatches function is Copyright (C) 2009, Jostein Austvik Jacobsen.
46
// cvTriangulatePoints function is derived from icvReconstructPointsFor3View, originally by Valery Mosyagin.
47
48
// HZ, R. Hartley and A. Zisserman, Multiple View Geometry in Computer Vision, Cambridge Univ. Press, 2003.
49
50
51
52
// This method is the same as icvReconstructPointsFor3View, with only a few numbers adjusted for two-view geometry
53
CV_IMPL void
54
cvTriangulatePoints(CvMat* projMatr1, CvMat* projMatr2, CvMat* projPoints1, CvMat* projPoints2, CvMat* points4D)
55
{
56
if( projMatr1 == 0 || projMatr2 == 0 ||
57
projPoints1 == 0 || projPoints2 == 0 ||
58
points4D == 0)
59
CV_Error( CV_StsNullPtr, "Some of parameters is a NULL pointer" );
60
61
if( !CV_IS_MAT(projMatr1) || !CV_IS_MAT(projMatr2) ||
62
!CV_IS_MAT(projPoints1) || !CV_IS_MAT(projPoints2) ||
63
!CV_IS_MAT(points4D) )
64
CV_Error( CV_StsUnsupportedFormat, "Input parameters must be matrices" );
65
66
int numPoints = projPoints1->cols;
67
68
if( numPoints < 1 )
69
CV_Error( CV_StsOutOfRange, "Number of points must be more than zero" );
70
71
if( projPoints2->cols != numPoints || points4D->cols != numPoints )
72
CV_Error( CV_StsUnmatchedSizes, "Number of points must be the same" );
73
74
if( projPoints1->rows != 2 || projPoints2->rows != 2)
75
CV_Error( CV_StsUnmatchedSizes, "Number of proj points coordinates must be == 2" );
76
77
if( points4D->rows != 4 )
78
CV_Error( CV_StsUnmatchedSizes, "Number of world points coordinates must be == 4" );
79
80
if( projMatr1->cols != 4 || projMatr1->rows != 3 ||
81
projMatr2->cols != 4 || projMatr2->rows != 3)
82
CV_Error( CV_StsUnmatchedSizes, "Size of projection matrices must be 3x4" );
83
84
// preallocate SVD matrices on stack
85
cv::Matx<double, 4, 4> matrA;
86
cv::Matx<double, 4, 4> matrU;
87
cv::Matx<double, 4, 1> matrW;
88
cv::Matx<double, 4, 4> matrV;
89
90
CvMat* projPoints[2] = {projPoints1, projPoints2};
91
CvMat* projMatrs[2] = {projMatr1, projMatr2};
92
93
/* Solve system for each point */
94
for( int i = 0; i < numPoints; i++ )/* For each point */
95
{
96
/* Fill matrix for current point */
97
for( int j = 0; j < 2; j++ )/* For each view */
98
{
99
double x,y;
100
x = cvmGet(projPoints[j],0,i);
101
y = cvmGet(projPoints[j],1,i);
102
for( int k = 0; k < 4; k++ )
103
{
104
matrA(j*2+0, k) = x * cvmGet(projMatrs[j],2,k) - cvmGet(projMatrs[j],0,k);
105
matrA(j*2+1, k) = y * cvmGet(projMatrs[j],2,k) - cvmGet(projMatrs[j],1,k);
106
}
107
}
108
/* Solve system for current point */
109
cv::SVD::compute(matrA, matrW, matrU, matrV);
110
111
/* Copy computed point */
112
cvmSet(points4D,0,i,matrV(3,0));/* X */
113
cvmSet(points4D,1,i,matrV(3,1));/* Y */
114
cvmSet(points4D,2,i,matrV(3,2));/* Z */
115
cvmSet(points4D,3,i,matrV(3,3));/* W */
116
}
117
}
118
119
120
/*
121
* The Optimal Triangulation Method (see HZ for details)
122
* For each given point correspondence points1[i] <-> points2[i], and a fundamental matrix F,
123
* computes the corrected correspondences new_points1[i] <-> new_points2[i] that minimize the
124
* geometric error d(points1[i],new_points1[i])^2 + d(points2[i],new_points2[i])^2 (where d(a,b)
125
* is the geometric distance between points a and b) subject to the epipolar constraint
126
* new_points2' * F * new_points1 = 0.
127
*
128
* F_ : 3x3 fundamental matrix
129
* points1_ : 1xN matrix containing the first set of points
130
* points2_ : 1xN matrix containing the second set of points
131
* new_points1 : the optimized points1_. if this is NULL, the corrected points are placed back in points1_
132
* new_points2 : the optimized points2_. if this is NULL, the corrected points are placed back in points2_
133
*/
134
CV_IMPL void
135
cvCorrectMatches(CvMat *F_, CvMat *points1_, CvMat *points2_, CvMat *new_points1, CvMat *new_points2)
136
{
137
cv::Ptr<CvMat> tmp33;
138
cv::Ptr<CvMat> tmp31, tmp31_2;
139
cv::Ptr<CvMat> T1i, T2i;
140
cv::Ptr<CvMat> R1, R2;
141
cv::Ptr<CvMat> TFT, TFTt, RTFTR;
142
cv::Ptr<CvMat> U, S, V;
143
cv::Ptr<CvMat> e1, e2;
144
cv::Ptr<CvMat> polynomial;
145
cv::Ptr<CvMat> result;
146
cv::Ptr<CvMat> points1, points2;
147
cv::Ptr<CvMat> F;
148
149
if (!CV_IS_MAT(F_) || !CV_IS_MAT(points1_) || !CV_IS_MAT(points2_) )
150
CV_Error( CV_StsUnsupportedFormat, "Input parameters must be matrices" );
151
if (!( F_->cols == 3 && F_->rows == 3))
152
CV_Error( CV_StsUnmatchedSizes, "The fundamental matrix must be a 3x3 matrix");
153
if (!(((F_->type & CV_MAT_TYPE_MASK) >> 3) == 0 ))
154
CV_Error( CV_StsUnsupportedFormat, "The fundamental matrix must be a single-channel matrix" );
155
if (!(points1_->rows == 1 && points2_->rows == 1 && points1_->cols == points2_->cols))
156
CV_Error( CV_StsUnmatchedSizes, "The point-matrices must have one row, and an equal number of columns" );
157
if (((points1_->type & CV_MAT_TYPE_MASK) >> 3) != 1 )
158
CV_Error( CV_StsUnmatchedSizes, "The first set of points must contain two channels; one for x and one for y" );
159
if (((points2_->type & CV_MAT_TYPE_MASK) >> 3) != 1 )
160
CV_Error( CV_StsUnmatchedSizes, "The second set of points must contain two channels; one for x and one for y" );
161
if (new_points1 != NULL) {
162
CV_Assert(CV_IS_MAT(new_points1));
163
if (new_points1->cols != points1_->cols || new_points1->rows != 1)
164
CV_Error( CV_StsUnmatchedSizes, "The first output matrix must have the same dimensions as the input matrices" );
165
if (CV_MAT_CN(new_points1->type) != 2)
166
CV_Error( CV_StsUnsupportedFormat, "The first output matrix must have two channels; one for x and one for y" );
167
}
168
if (new_points2 != NULL) {
169
CV_Assert(CV_IS_MAT(new_points2));
170
if (new_points2->cols != points2_->cols || new_points2->rows != 1)
171
CV_Error( CV_StsUnmatchedSizes, "The second output matrix must have the same dimensions as the input matrices" );
172
if (CV_MAT_CN(new_points2->type) != 2)
173
CV_Error( CV_StsUnsupportedFormat, "The second output matrix must have two channels; one for x and one for y" );
174
}
175
176
// Make sure F uses double precision
177
F.reset(cvCreateMat(3,3,CV_64FC1));
178
cvConvert(F_, F);
179
180
// Make sure points1 uses double precision
181
points1.reset(cvCreateMat(points1_->rows,points1_->cols,CV_64FC2));
182
cvConvert(points1_, points1);
183
184
// Make sure points2 uses double precision
185
points2.reset(cvCreateMat(points2_->rows,points2_->cols,CV_64FC2));
186
cvConvert(points2_, points2);
187
188
tmp33.reset(cvCreateMat(3,3,CV_64FC1));
189
tmp31.reset(cvCreateMat(3,1,CV_64FC1)), tmp31_2.reset(cvCreateMat(3,1,CV_64FC1));
190
T1i.reset(cvCreateMat(3,3,CV_64FC1)), T2i.reset(cvCreateMat(3,3,CV_64FC1));
191
R1.reset(cvCreateMat(3,3,CV_64FC1)), R2.reset(cvCreateMat(3,3,CV_64FC1));
192
TFT.reset(cvCreateMat(3,3,CV_64FC1)), TFTt.reset(cvCreateMat(3,3,CV_64FC1)), RTFTR.reset(cvCreateMat(3,3,CV_64FC1));
193
U.reset(cvCreateMat(3,3,CV_64FC1));
194
S.reset(cvCreateMat(3,3,CV_64FC1));
195
V.reset(cvCreateMat(3,3,CV_64FC1));
196
e1.reset(cvCreateMat(3,1,CV_64FC1)), e2.reset(cvCreateMat(3,1,CV_64FC1));
197
198
double x1, y1, x2, y2;
199
double scale;
200
double f1, f2, a, b, c, d;
201
polynomial.reset(cvCreateMat(1,7,CV_64FC1));
202
result.reset(cvCreateMat(1,6,CV_64FC2));
203
double t_min, s_val, t, s;
204
for (int p = 0; p < points1->cols; ++p) {
205
// Replace F by T2-t * F * T1-t
206
x1 = points1->data.db[p*2];
207
y1 = points1->data.db[p*2+1];
208
x2 = points2->data.db[p*2];
209
y2 = points2->data.db[p*2+1];
210
211
cvSetZero(T1i);
212
cvSetReal2D(T1i,0,0,1);
213
cvSetReal2D(T1i,1,1,1);
214
cvSetReal2D(T1i,2,2,1);
215
cvSetReal2D(T1i,0,2,x1);
216
cvSetReal2D(T1i,1,2,y1);
217
cvSetZero(T2i);
218
cvSetReal2D(T2i,0,0,1);
219
cvSetReal2D(T2i,1,1,1);
220
cvSetReal2D(T2i,2,2,1);
221
cvSetReal2D(T2i,0,2,x2);
222
cvSetReal2D(T2i,1,2,y2);
223
cvGEMM(T2i,F,1,0,0,tmp33,CV_GEMM_A_T);
224
cvSetZero(TFT);
225
cvGEMM(tmp33,T1i,1,0,0,TFT);
226
227
// Compute the right epipole e1 from F * e1 = 0
228
cvSetZero(U);
229
cvSetZero(S);
230
cvSetZero(V);
231
cvSVD(TFT,S,U,V);
232
scale = sqrt(cvGetReal2D(V,0,2)*cvGetReal2D(V,0,2) + cvGetReal2D(V,1,2)*cvGetReal2D(V,1,2));
233
cvSetReal2D(e1,0,0,cvGetReal2D(V,0,2)/scale);
234
cvSetReal2D(e1,1,0,cvGetReal2D(V,1,2)/scale);
235
cvSetReal2D(e1,2,0,cvGetReal2D(V,2,2)/scale);
236
if (cvGetReal2D(e1,2,0) < 0) {
237
cvSetReal2D(e1,0,0,-cvGetReal2D(e1,0,0));
238
cvSetReal2D(e1,1,0,-cvGetReal2D(e1,1,0));
239
cvSetReal2D(e1,2,0,-cvGetReal2D(e1,2,0));
240
}
241
242
// Compute the left epipole e2 from e2' * F = 0 => F' * e2 = 0
243
cvSetZero(TFTt);
244
cvTranspose(TFT, TFTt);
245
cvSetZero(U);
246
cvSetZero(S);
247
cvSetZero(V);
248
cvSVD(TFTt,S,U,V);
249
cvSetZero(e2);
250
scale = sqrt(cvGetReal2D(V,0,2)*cvGetReal2D(V,0,2) + cvGetReal2D(V,1,2)*cvGetReal2D(V,1,2));
251
cvSetReal2D(e2,0,0,cvGetReal2D(V,0,2)/scale);
252
cvSetReal2D(e2,1,0,cvGetReal2D(V,1,2)/scale);
253
cvSetReal2D(e2,2,0,cvGetReal2D(V,2,2)/scale);
254
if (cvGetReal2D(e2,2,0) < 0) {
255
cvSetReal2D(e2,0,0,-cvGetReal2D(e2,0,0));
256
cvSetReal2D(e2,1,0,-cvGetReal2D(e2,1,0));
257
cvSetReal2D(e2,2,0,-cvGetReal2D(e2,2,0));
258
}
259
260
// Replace F by R2 * F * R1'
261
cvSetZero(R1);
262
cvSetReal2D(R1,0,0,cvGetReal2D(e1,0,0));
263
cvSetReal2D(R1,0,1,cvGetReal2D(e1,1,0));
264
cvSetReal2D(R1,1,0,-cvGetReal2D(e1,1,0));
265
cvSetReal2D(R1,1,1,cvGetReal2D(e1,0,0));
266
cvSetReal2D(R1,2,2,1);
267
cvSetZero(R2);
268
cvSetReal2D(R2,0,0,cvGetReal2D(e2,0,0));
269
cvSetReal2D(R2,0,1,cvGetReal2D(e2,1,0));
270
cvSetReal2D(R2,1,0,-cvGetReal2D(e2,1,0));
271
cvSetReal2D(R2,1,1,cvGetReal2D(e2,0,0));
272
cvSetReal2D(R2,2,2,1);
273
cvGEMM(R2,TFT,1,0,0,tmp33);
274
cvGEMM(tmp33,R1,1,0,0,RTFTR,CV_GEMM_B_T);
275
276
// Set f1 = e1(3), f2 = e2(3), a = F22, b = F23, c = F32, d = F33
277
f1 = cvGetReal2D(e1,2,0);
278
f2 = cvGetReal2D(e2,2,0);
279
a = cvGetReal2D(RTFTR,1,1);
280
b = cvGetReal2D(RTFTR,1,2);
281
c = cvGetReal2D(RTFTR,2,1);
282
d = cvGetReal2D(RTFTR,2,2);
283
284
// Form the polynomial g(t) = k6*t^6 + k5*t^5 + k4*t^4 + k3*t^3 + k2*t^2 + k1*t + k0
285
// from f1, f2, a, b, c and d
286
cvSetReal2D(polynomial,0,6,( +b*c*c*f1*f1*f1*f1*a-a*a*d*f1*f1*f1*f1*c ));
287
cvSetReal2D(polynomial,0,5,( +f2*f2*f2*f2*c*c*c*c+2*a*a*f2*f2*c*c-a*a*d*d*f1*f1*f1*f1+b*b*c*c*f1*f1*f1*f1+a*a*a*a ));
288
cvSetReal2D(polynomial,0,4,( +4*a*a*a*b+2*b*c*c*f1*f1*a+4*f2*f2*f2*f2*c*c*c*d+4*a*b*f2*f2*c*c+4*a*a*f2*f2*c*d-2*a*a*d*f1*f1*c-a*d*d*f1*f1*f1*f1*b+b*b*c*f1*f1*f1*f1*d ));
289
cvSetReal2D(polynomial,0,3,( +6*a*a*b*b+6*f2*f2*f2*f2*c*c*d*d+2*b*b*f2*f2*c*c+2*a*a*f2*f2*d*d-2*a*a*d*d*f1*f1+2*b*b*c*c*f1*f1+8*a*b*f2*f2*c*d ));
290
cvSetReal2D(polynomial,0,2,( +4*a*b*b*b+4*b*b*f2*f2*c*d+4*f2*f2*f2*f2*c*d*d*d-a*a*d*c+b*c*c*a+4*a*b*f2*f2*d*d-2*a*d*d*f1*f1*b+2*b*b*c*f1*f1*d ));
291
cvSetReal2D(polynomial,0,1,( +f2*f2*f2*f2*d*d*d*d+b*b*b*b+2*b*b*f2*f2*d*d-a*a*d*d+b*b*c*c ));
292
cvSetReal2D(polynomial,0,0,( -a*d*d*b+b*b*c*d ));
293
294
// Solve g(t) for t to get 6 roots
295
cvSetZero(result);
296
cvSolvePoly(polynomial, result, 100, 20);
297
298
// Evaluate the cost function s(t) at the real part of the 6 roots
299
t_min = DBL_MAX;
300
s_val = 1./(f1*f1) + (c*c)/(a*a+f2*f2*c*c);
301
for (int ti = 0; ti < 6; ++ti) {
302
t = result->data.db[2*ti];
303
s = (t*t)/(1 + f1*f1*t*t) + ((c*t + d)*(c*t + d))/((a*t + b)*(a*t + b) + f2*f2*(c*t + d)*(c*t + d));
304
if (s < s_val) {
305
s_val = s;
306
t_min = t;
307
}
308
}
309
310
// find the optimal x1 and y1 as the points on l1 and l2 closest to the origin
311
tmp31->data.db[0] = t_min*t_min*f1;
312
tmp31->data.db[1] = t_min;
313
tmp31->data.db[2] = t_min*t_min*f1*f1+1;
314
tmp31->data.db[0] /= tmp31->data.db[2];
315
tmp31->data.db[1] /= tmp31->data.db[2];
316
tmp31->data.db[2] /= tmp31->data.db[2];
317
cvGEMM(T1i,R1,1,0,0,tmp33,CV_GEMM_B_T);
318
cvGEMM(tmp33,tmp31,1,0,0,tmp31_2);
319
x1 = tmp31_2->data.db[0];
320
y1 = tmp31_2->data.db[1];
321
322
tmp31->data.db[0] = f2*pow(c*t_min+d,2);
323
tmp31->data.db[1] = -(a*t_min+b)*(c*t_min+d);
324
tmp31->data.db[2] = f2*f2*pow(c*t_min+d,2) + pow(a*t_min+b,2);
325
tmp31->data.db[0] /= tmp31->data.db[2];
326
tmp31->data.db[1] /= tmp31->data.db[2];
327
tmp31->data.db[2] /= tmp31->data.db[2];
328
cvGEMM(T2i,R2,1,0,0,tmp33,CV_GEMM_B_T);
329
cvGEMM(tmp33,tmp31,1,0,0,tmp31_2);
330
x2 = tmp31_2->data.db[0];
331
y2 = tmp31_2->data.db[1];
332
333
// Return the points in the matrix format that the user wants
334
points1->data.db[p*2] = x1;
335
points1->data.db[p*2+1] = y1;
336
points2->data.db[p*2] = x2;
337
points2->data.db[p*2+1] = y2;
338
}
339
340
if( new_points1 )
341
cvConvert( points1, new_points1 );
342
if( new_points2 )
343
cvConvert( points2, new_points2 );
344
}
345
346
void cv::triangulatePoints( InputArray _projMatr1, InputArray _projMatr2,
347
InputArray _projPoints1, InputArray _projPoints2,
348
OutputArray _points4D )
349
{
350
CV_INSTRUMENT_REGION();
351
352
Mat matr1 = _projMatr1.getMat(), matr2 = _projMatr2.getMat();
353
Mat points1 = _projPoints1.getMat(), points2 = _projPoints2.getMat();
354
355
if((points1.rows == 1 || points1.cols == 1) && points1.channels() == 2)
356
points1 = points1.reshape(1, static_cast<int>(points1.total())).t();
357
358
if((points2.rows == 1 || points2.cols == 1) && points2.channels() == 2)
359
points2 = points2.reshape(1, static_cast<int>(points2.total())).t();
360
361
CvMat cvMatr1 = cvMat(matr1), cvMatr2 = cvMat(matr2);
362
CvMat cvPoints1 = cvMat(points1), cvPoints2 = cvMat(points2);
363
364
_points4D.create(4, points1.cols, points1.type());
365
Mat cvPoints4D_ = _points4D.getMat();
366
CvMat cvPoints4D = cvMat(cvPoints4D_);
367
368
cvTriangulatePoints(&cvMatr1, &cvMatr2, &cvPoints1, &cvPoints2, &cvPoints4D);
369
}
370
371
void cv::correctMatches( InputArray _F, InputArray _points1, InputArray _points2,
372
OutputArray _newPoints1, OutputArray _newPoints2 )
373
{
374
CV_INSTRUMENT_REGION();
375
376
Mat F = _F.getMat();
377
Mat points1 = _points1.getMat(), points2 = _points2.getMat();
378
379
CvMat cvPoints1 = cvMat(points1), cvPoints2 = cvMat(points2);
380
CvMat cvF = cvMat(F);
381
382
_newPoints1.create(points1.size(), points1.type());
383
_newPoints2.create(points2.size(), points2.type());
384
Mat cvNewPoints1_ = _newPoints1.getMat(), cvNewPoints2_ = _newPoints2.getMat();
385
CvMat cvNewPoints1 = cvMat(cvNewPoints1_), cvNewPoints2 = cvMat(cvNewPoints2_);
386
387
cvCorrectMatches(&cvF, &cvPoints1, &cvPoints2, &cvNewPoints1, &cvNewPoints2);
388
}
389
390