-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathstructure.cpp
More file actions
269 lines (246 loc) · 9.1 KB
/
Copy pathstructure.cpp
File metadata and controls
269 lines (246 loc) · 9.1 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
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
#include <iostream>
#include <vector>
#include <array>
#include <cmath>
#include <Eigen/Dense>
#include <fstream>
#include <string>
#include <opencv2/core.hpp>
#include <opencv2/imgcodecs.hpp>
#include <opencv2/highgui.hpp>
#include <opencv2/features2d/features2d.hpp>
#include <opencv2/imgproc.hpp>
#include <limits>
using namespace cv;
using std::vector;
using std::array;
using std::string;
using std::fstream;
using std::stringstream;
using Eigen::MatrixXd;
struct Frame
{
Mat R = Mat::zeros(3, 3, CV_64F);
Mat T = Mat::zeros(3, 1, CV_64F);
vector<vector<int>> indexPairs; //first index corresponds to keypoint, 2nd corresponds to world point
vector<KeyPoint> features;
Mat descriptors;
vector<vector<int>> covisibility; // first index corresponds to frame index, 2nd corresponds to num of common points
};
class KeyFrame
{
public:
vector<Frame> frames;
void addKeyFrames(Mat &R, Mat &T, vector<vector<int>>& indexPairs, vector<KeyPoint> &kp, Mat &desc)
{
Frame tempFrame;
tempFrame.R = R;
tempFrame.T = T;
tempFrame.features = kp;
tempFrame.descriptors = desc;
tempFrame.indexPairs = indexPairs;
frames.push_back(tempFrame);
}
void addCovisibility(int srcFrame, int dstFrame, int commonPts)
{
frames[srcFrame].covisibility.push_back({dstFrame, commonPts});
frames[dstFrame].covisibility.push_back({srcFrame, commonPts});
}
void scalePoses(double medianDepth)
{
for (int i = 0; i < frames.size(); i++ )
{
frames[i].T.at<double>(0) = frames[i].T.at<double>(0) / medianDepth;
frames[i].T.at<double>(1) = frames[i].T.at<double>(1) / medianDepth;
frames[i].T.at<double>(2) = frames[i].T.at<double>(2) / medianDepth;
}
}
void updateIndexPairs(int frame1, int frame2, vector<vector<int>> indexPairsComb)
{
for (int i = 0; i < indexPairsComb.size(); i++)
{
frames[frame1].indexPairs.push_back({indexPairsComb[i][0], indexPairsComb[i][2]});
frames[frame2].indexPairs.push_back({indexPairsComb[i][1], indexPairsComb[i][2]});
}
}
};
class WorldPt
{
public:
vector<vector<double>> pos;
vector<vector<double>> viewDirection;
vector<double> dmax;
vector<double> dmin;
vector<vector<int>> representative; // 1st index denots frame, 2nd index denotes keypoint in the frame
bool addWorldPts(Mat &R1, Mat &R2, Mat &T1, Mat&T2, const vector<int> &ind1, const vector<int> &ind2, Mat &K, vector<KeyPoint> &kp1, vector<KeyPoint> &kp2, vector<vector<int>>& indexPairsComb, float maxReprojError, float minParallax )
{
Mat proj1 (3,4,CV_64F);
Mat proj2 (3,4,CV_64F);
getProjMat(R1, T1, K, proj1);
getProjMat(R2, T2, K, proj2);
Mat kp1Inl(2,ind1.size(), CV_64F);
Mat kp2Inl(2,ind2.size(), CV_64F);
for (int i = 0; i < ind1.size(); i++)
{
kp1Inl.at<double>(0,i) = kp1[ind1[i]].pt.x;
kp1Inl.at<double>(1,i) = kp1[ind1[i]].pt.y;
kp2Inl.at<double>(0,i) = kp2[ind2[i]].pt.x;
kp2Inl.at<double>(1,i) = kp2[ind2[i]].pt.y;
}
Mat worldPts(4,ind1.size(), CV_64F);
triangulatePoints(proj1, proj2, kp1Inl,kp2Inl,worldPts);
Mat R1T;
Mat R2T;
transpose(R1, R1T);
transpose(R2, R2T);
Mat temp;
Mat currPt(3,1, CV_64F);
Mat currImg1Pt(2,1, CV_64F);
Mat currImg2Pt(2,1, CV_64F);
Mat ray1(3,1, CV_64F);
Mat ray2(3,1, CV_64F);
vector<bool> isParallax;
float d1 = 0;
float d2 = 0;
float proj1Error = 0;
float proj2Error = 0;
Mat proj1ErrorVec(3, 1, CV_64F);
Mat proj2ErrorVec(3, 1, CV_64F);
float avgReprojError = 0;
float cosAngle = 0;
bool status;
vector<float> pt;
for (int i = 0; i < ind1.size(); i++)
{
currImg1Pt = kp1Inl.col(i);
currImg2Pt = kp2Inl.col(i);
proj1ErrorVec = proj1 * worldPts.col(i);
proj1ErrorVec.at<double>(0) = proj1ErrorVec.at<double>(0) / proj1ErrorVec.at<double>(2);
proj1ErrorVec.at<double>(1) = proj1ErrorVec.at<double>(1) / proj1ErrorVec.at<double>(2);
proj1Error = sqrt(pow(proj1ErrorVec.at<double>(0) - currImg1Pt.at<double>(0), 2) + pow(proj1ErrorVec.at<double>(1) - currImg1Pt.at<double>(1), 2));
proj2ErrorVec = proj2 * worldPts.col(i);
proj2ErrorVec.at<double>(0) = proj2ErrorVec.at<double>(0) / proj2ErrorVec.at<double>(2);
proj2ErrorVec.at<double>(1) = proj2ErrorVec.at<double>(1) / proj2ErrorVec.at<double>(2);
proj2Error = sqrt(pow(proj2ErrorVec.at<double>(0) - currImg2Pt.at<double>(0), 2) + pow(proj2ErrorVec.at<double>(1) - currImg2Pt.at<double>(1), 2));
avgReprojError = (proj1Error + proj2Error) / 2.0;
currPt.at<double>(0) = worldPts.at<double>(0,i) / worldPts.at<double>(3,i);
currPt.at<double>(1) = worldPts.at<double>(1,i) / worldPts.at<double>(3,i);
currPt.at<double>(2) = worldPts.at<double>(2,i) / worldPts.at<double>(3,i);
temp = R1T * (currPt - T1);
d1 = temp.at<double>(2);
temp = R2T * (currPt - T2);
d2 = temp.at<double>(2);
ray1 = currPt - T1;
ray2 = currPt- T2;
cosAngle = ray1.dot(ray2) / (norm(ray1) * norm(ray2));
if ( (d1 > 0 and d2 > 0) and (avgReprojError < maxReprojError) and (cosAngle <= cos((minParallax / 180.0) * M_PI)))
{
pos.push_back({currPt.at<double>(0), currPt.at<double>(1), currPt.at<double>(2) });
indexPairsComb.push_back({ind1[i], ind2[i], int(pos.size()-1)});
}
}
if (pos.size() > 0)
status = true;
else
status = false;
std::cout << "Num of points added to map are: " << pos.size() << std::endl;
return status;
}
double scaleMap()
{
vector<float> posNorms;
Mat temp(3, 1 , CV_64F);
double currNorm;
double medianDepth;
for (int i = 0; i < pos.size(); i++)
{
temp.at<double>(0) = pos[i][0];
temp.at<double>(1) = pos[i][1];
temp.at<double>(2) = pos[i][2];
currNorm = norm(temp);
posNorms.push_back(currNorm);
}
medianDepth = findMedian(posNorms, int(pos.size()));
for (int i = 0; i < pos.size(); i++)
{
pos[i][0] = pos[i][0] / medianDepth;
pos[i][1] = pos[i][1] / medianDepth;
pos[i][2] = pos[i][2] / medianDepth;
}
return medianDepth;
}
void updateDirectionDistance(vector<Frame> &frames)
{
for (int i = 0; i < pos.size(); i++)
{
double diff[3];
double distArr[frames.size()];
double currDirection[3];
double meanDirection[3] = {};
double meanDirectionDist = 0;
for (int j = 0; j < frames.size(); j++)
{
diff[0] = pos[i][0] - frames[j].T.at<double>(0);
diff[1] = pos[i][1] - frames[j].T.at<double>(1);
diff[2] = pos[i][2] - frames[j].T.at<double>(2);
distArr[j] = sqrt(diff[0]* diff[0] + diff[1]* diff[1] + diff[2]* diff[2]);
currDirection[0] = diff[0] / distArr[j];
currDirection[1] = diff[1] / distArr[j];
currDirection[2] = diff[2] / distArr[j];
meanDirection[0] = meanDirection[0] + currDirection[0];
meanDirection[1] = meanDirection[1] + currDirection[1];
meanDirection[2] = meanDirection[2] + currDirection[2];
}
dmax.push_back(*std::max_element(distArr, distArr + frames.size()));
dmin.push_back(*std::min_element(distArr, distArr + frames.size()));
meanDirection[0] = meanDirection[0] / frames.size();
meanDirection[1] = meanDirection[1] / frames.size();
meanDirection[2] = meanDirection[2] / frames.size();
meanDirectionDist = sqrt(meanDirection[0] * meanDirection[0] + meanDirection[1] * meanDirection[1] + meanDirection[2] * meanDirection[2]);
meanDirection[0] = meanDirection[0] / meanDirectionDist;
meanDirection[1] = meanDirection[1] / meanDirectionDist;
meanDirection[2] = meanDirection[2] / meanDirectionDist;
viewDirection.push_back({meanDirection[0], meanDirection[1], meanDirection[2]});
}
}
void updateRepresentativeView(vector<Frame> &frames)
{
for (int i = 0; i < pos.size(); i++)
{
vector<Mat> desc;
vector<vector<int>> imgKeypt;
int rep[2] = {-1,-1};
for (int j = 0; j < frames.size(); j++)
{
for (int k = 0; k < frames[j].indexPairs.size(); k++)
{
if (int(frames[j].indexPairs[k][1]) == int(i))
{
imgKeypt.push_back({j, frames[j].indexPairs[k][0]});
desc.push_back(frames[j].descriptors.row(frames[j].indexPairs[k][0]));
break;
}
}
}
double mindist = 1000000;
for (int j = 0; j < desc.size(); j++)
{
double currdist = 0;
for (int k = 0; k < desc.size(); k++)
{
currdist = currdist + norm(desc[j], desc[k], NORM_HAMMING);
}
if (currdist < mindist)
{
mindist = currdist;
rep[0] = imgKeypt[j][0];
rep[1] = imgKeypt[j][1];
}
}
if (rep[0] != -1)
{
representative.push_back({rep[0], rep[1]});
}
}
}
};