forked from royshil/SfM-Toy-Library
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTriangulation.cpp
More file actions
193 lines (165 loc) · 5.69 KB
/
Copy pathTriangulation.cpp
File metadata and controls
193 lines (165 loc) · 5.69 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
/*
* Triangulation.cpp
* EyeRingOpenCV
*
* Created by Roy Shilkrot on 12/23/11.
* Copyright 2011 MIT. All rights reserved.
*
*/
#include "Triangulation.h"
#include <iostream>
using namespace std;
using namespace cv;
#undef __SFM__DEBUG__
/**
From "Triangulation", Hartley, R.I. and Sturm, P., Computer vision and image understanding, 1997
*/
Mat_<double> LinearLSTriangulation(Point3d u, //homogenous image point (u,v,1)
Matx34d P, //camera 1 matrix
Point3d u1, //homogenous image point in 2nd camera
Matx34d P1 //camera 2 matrix
)
{
//build matrix A for homogenous equation system Ax = 0
//assume X = (x,y,z,1), for Linear-LS method
//which turns it into a AX = B system, where A is 4x3, X is 3x1 and B is 4x1
// cout << "u " << u <<", u1 " << u1 << endl;
// Matx<double,6,4> A; //this is for the AX=0 case, and with linear dependence..
// A(0) = u.x*P(2)-P(0);
// A(1) = u.y*P(2)-P(1);
// A(2) = u.x*P(1)-u.y*P(0);
// A(3) = u1.x*P1(2)-P1(0);
// A(4) = u1.y*P1(2)-P1(1);
// A(5) = u1.x*P(1)-u1.y*P1(0);
// Matx43d A; //not working for some reason...
// A(0) = u.x*P(2)-P(0);
// A(1) = u.y*P(2)-P(1);
// A(2) = u1.x*P1(2)-P1(0);
// A(3) = u1.y*P1(2)-P1(1);
Matx43d A(u.x*P(2,0)-P(0,0), u.x*P(2,1)-P(0,1), u.x*P(2,2)-P(0,2),
u.y*P(2,0)-P(1,0), u.y*P(2,1)-P(1,1), u.y*P(2,2)-P(1,2),
u1.x*P1(2,0)-P1(0,0), u1.x*P1(2,1)-P1(0,1), u1.x*P1(2,2)-P1(0,2),
u1.y*P1(2,0)-P1(1,0), u1.y*P1(2,1)-P1(1,1), u1.y*P1(2,2)-P1(1,2)
);
Matx41d B(-(u.x*P(2,3) -P(0,3)),
-(u.y*P(2,3) -P(1,3)),
-(u1.x*P1(2,3) -P1(0,3)),
-(u1.y*P1(2,3) -P1(1,3)));
Mat_<double> X;
solve(A,B,X,DECOMP_SVD);
return X;
}
/**
From "Triangulation", Hartley, R.I. and Sturm, P., Computer vision and image understanding, 1997
*/
Mat_<double> IterativeLinearLSTriangulation(Point3d u, //homogenous image point (u,v,1)
Matx34d P, //camera 1 matrix
Point3d u1, //homogenous image point in 2nd camera
Matx34d P1 //camera 2 matrix
) {
double wi = 1, wi1 = 1;
Mat_<double> X(4,1);
for (int i=0; i<10; i++) { //Hartley suggests 10 iterations at most
Mat_<double> X_ = LinearLSTriangulation(u,P,u1,P1);
X(0) = X_(0); X(1) = X_(1); X(2) = X_(2); X(3) = 1.0;
//recalculate weights
double p2x = Mat_<double>(Mat_<double>(P).row(2)*X)(0);
double p2x1 = Mat_<double>(Mat_<double>(P1).row(2)*X)(0);
//breaking point
if(fabsf(wi - p2x) <= EPSILON && fabsf(wi1 - p2x1) <= EPSILON) break;
wi = p2x;
wi1 = p2x1;
//reweight equations and solve
Matx43d A((u.x*P(2,0)-P(0,0))/wi, (u.x*P(2,1)-P(0,1))/wi, (u.x*P(2,2)-P(0,2))/wi,
(u.y*P(2,0)-P(1,0))/wi, (u.y*P(2,1)-P(1,1))/wi, (u.y*P(2,2)-P(1,2))/wi,
(u1.x*P1(2,0)-P1(0,0))/wi1, (u1.x*P1(2,1)-P1(0,1))/wi1, (u1.x*P1(2,2)-P1(0,2))/wi1,
(u1.y*P1(2,0)-P1(1,0))/wi1, (u1.y*P1(2,1)-P1(1,1))/wi1, (u1.y*P1(2,2)-P1(1,2))/wi1
);
Mat_<double> B = (Mat_<double>(4,1) << -(u.x*P(2,3) -P(0,3))/wi,
-(u.y*P(2,3) -P(1,3))/wi,
-(u1.x*P1(2,3) -P1(0,3))/wi1,
-(u1.y*P1(2,3) -P1(1,3))/wi1
);
solve(A,B,X_,DECOMP_SVD);
X(0) = X_(0); X(1) = X_(1); X(2) = X_(2); X(3) = 1.0;
}
return X;
}
//Triagulate points
double TriangulatePoints(const vector<KeyPoint>& pt_set1,
const vector<KeyPoint>& pt_set2,
const Mat& Kinv,
const Matx34d& P,
const Matx34d& P1,
vector<CloudPoint>& pointcloud,
vector<KeyPoint>& correspImg1Pt)
{
#ifdef __SFM__DEBUG__
vector<double> depths;
#endif
// pointcloud.clear();
correspImg1Pt.clear();
Matx44d P1_(P1(0,0),P1(0,1),P1(0,2),P1(0,3),
P1(1,0),P1(1,1),P1(1,2),P1(1,3),
P1(2,0),P1(2,1),P1(2,2),P1(2,3),
0, 0, 0, 1);
Matx44d P1inv(P1_.inv());
Mat_<double> K = Kinv.inv();
cout << "Triangulating...";
double t = getTickCount();
vector<double> reproj_error;
unsigned int pts_size = pt_set1.size();
//#pragma omp parallel for
for (unsigned int i=0; i<pts_size; i++) {
Point2f kp = pt_set1[i].pt;
Point3d u(kp.x,kp.y,1.0);
Mat_<double> um = Kinv * Mat_<double>(u);
u = um.at<Point3d>(0);
Point2f kp1 = pt_set2[i].pt;
Point3d u1(kp1.x,kp1.y,1.0);
Mat_<double> um1 = Kinv * Mat_<double>(u1);
u1 = um1.at<Point3d>(0);
Mat_<double> X = IterativeLinearLSTriangulation(u,P,u1,P1);
// cout << "3D Point: " << X << endl;
// Mat_<double> x = Mat(P1) * X;
// cout << "P1 * Point: " << x << endl;
// Mat_<double> xPt = (Mat_<double>(3,1) << x(0),x(1),x(2));
// cout << "Point: " << xPt << endl;
Mat_<double> xPt_img = K * Mat(P1) * X;
// cout << "Point * K: " << xPt_img << endl;
Point2f xPt_img_(xPt_img(0)/xPt_img(2),xPt_img(1)/xPt_img(2));
// cout << "Image point: " << xPt_img_ << endl;
reproj_error.push_back(norm(xPt_img_-kp1));
// if(/*X(2) > 6 || */X(2) < 0) continue;
#pragma omp critical
{
CloudPoint cp;
cp.pt = Point3d(X(0),X(1),X(2));
pointcloud.push_back(cp);
correspImg1Pt.push_back(pt_set1[i]);
#ifdef __SFM__DEBUG__
depths.push_back(X(2));
#endif
}
}
t = ((double)getTickCount() - t)/getTickFrequency();
cout << "Done. ("<<pointcloud.size()<<"points, " << t <<"s)"<< endl;
//show "range image"
#ifdef __SFM__DEBUG__
{
double minVal,maxVal;
minMaxLoc(depths, &minVal, &maxVal);
Mat tmp(240,320,CV_8UC3,Scalar(0,0,0)); //cvtColor(img_1_orig, tmp, CV_BGR2HSV);
for (unsigned int i=0; i<pointcloud.size(); i++) {
double _d = MAX(MIN((pointcloud[i].z-minVal)/(maxVal-minVal),1.0),0.0);
circle(tmp, correspImg1Pt[i].pt, 1, Scalar(255 * (1.0-(_d)),255,255), CV_FILLED);
}
cvtColor(tmp, tmp, CV_HSV2BGR);
imshow("Depth Map", tmp);
waitKey(0);
destroyWindow("Depth Map");
}
#endif
Scalar mse = mean(reproj_error);
return mse[0];
}