-
Notifications
You must be signed in to change notification settings - Fork 7
/
Sprase_ImageAlign.cpp
348 lines (270 loc) · 11.2 KB
/
Sprase_ImageAlign.cpp
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
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
//
// Created by buyi on 17-12-21.
//
#include "Sprase_ImageAlign.h"
namespace DSDTM
{
Sprase_ImgAlign::Sprase_ImgAlign(int tMaxLevel, int tMinLevel, int tMaxIterators):
mnMaxLevel(tMaxLevel), mnMinLevel(tMinLevel), mnMaxIterators(tMaxIterators),
mBoarder(2*mHalf_PatchSize), mPatchArea(mHalf_PatchSize*mHalf_PatchSize)
{
mnMinfts = Config::Get<int>("Camera.Min_fts");
}
Sprase_ImgAlign::~Sprase_ImgAlign()
{
}
void Sprase_ImgAlign::Reset()
{
mRefPatch.resize(0, 0);
mJocabianPatch.resize(0, Eigen::NoChange);
mRefNormals.resize(Eigen::NoChange, 0);
}
int Sprase_ImgAlign::Run(FramePtr tCurFrame, FramePtr tRefFrame)
{
Reset();
int mnPts;
if(tRefFrame->mvFeatures.size() < mnMinfts)
{
LOG(ERROR)<<"Too few features to track" << std::endl;
return 0;
}
mCurFrame = tCurFrame;
mRefFrame = tRefFrame;
mT_c2r = tCurFrame->Get_Pose()*tRefFrame->Get_Pose().inverse();
for (int i = mnMaxLevel-1; i >= mnMinLevel; --i)
{
GetJocabianMat(i);
//TicToc tc;
//CeresSolver(mT_c2r, i);
GaussNewtonSolver(mT_c2r, i, mnPts);
//std::cout <<"Cost "<< tc.toc() << " ms" << std::endl;
Reset();
}
tCurFrame->Set_Pose(mT_c2r*tRefFrame->Get_Pose());
return mnPts;
}
void Sprase_ImgAlign::GetJocabianMat(int tLevel)
{
const cv::Mat tRefIMg = mRefFrame->mvImg_Pyr[tLevel];
const float tScale = 1.0/(1<<tLevel);
const int tRefStep = tRefIMg.step.p[0];
const int boarder = 0.5*mHalf_PatchSize+1;
const Eigen::Vector3d tRefCnt = mRefFrame->Get_CameraCnt();
const float tFocalth = mRefFrame->mCamera->mf;
Eigen::Matrix<double, 2, Eigen::Dynamic, Eigen::RowMajor> tRefPts;
Eigen::Matrix<double, 3, Eigen::Dynamic, Eigen::RowMajor> tRefPoints;
Eigen::Matrix<double, 3, Eigen::Dynamic, Eigen::RowMajor> tRefNormals;
Eigen::Matrix<double, 1, Eigen::Dynamic, Eigen::RowMajor> tRefPtDepth;
int tnPts = mRefFrame->mvFeatures.size();
mVisible.resize(tnPts, true);
tRefPts.resize(Eigen::NoChange, tnPts);
tRefPoints.resize(Eigen::NoChange, tnPts);
tRefNormals.resize(Eigen::NoChange, tnPts);
int tNum = 0;
for (int i = 0; i < tnPts; ++i)
{
if(!mRefFrame->mvFeatures[i]->mbInitial)
continue;
tRefPts.col(tNum) << mRefFrame->mvFeatures[i]->mpx.x,
mRefFrame->mvFeatures[i]->mpx.y;
tRefPts.col(tNum) = tRefPts.col(tNum)*tScale;
tRefPoints.col(tNum) = mRefFrame->mvFeatures[i]->Mpt->Get_Pose();
if( tRefPoints.col(tNum).isZero(0) || tRefPts(0, tNum) - boarder < 0 || tRefPts(1, tNum) - boarder < 0 ||
tRefPts(0, tNum) + boarder >= tRefIMg.cols || tRefPts(1, tNum) + boarder >= tRefIMg.rows)
{
mVisible[i] = false;
continue;
}
tRefNormals.col(tNum) = mRefFrame->mvFeatures[i]->mNormal;
tNum++;
}
tRefPts.conservativeResize(Eigen::NoChange, tNum);
tRefPoints.conservativeResize(Eigen::NoChange, tNum);
tRefNormals.conservativeResize(Eigen::NoChange, tNum);
mRefNormals.resize(3, tNum);
tRefPtDepth.resize(1, tNum);
//! Set the size fof refpatch and jacobian patch
mRefPatch.resize(tNum, mHalf_PatchSize*mHalf_PatchSize);
mJocabianPatch.resize(tNum*mHalf_PatchSize*mHalf_PatchSize, Eigen::NoChange);
//! Calculate the points the reference frame
// TODO this should be deal with independently for RGBD and monocular
tRefPoints = tRefPoints.colwise() - tRefCnt;
tRefPtDepth = tRefPoints.colwise().norm();
mRefNormals = tRefNormals.array().rowwise()*tRefPtDepth.array();
//! Calculate the coffient Bilinear difference
Eigen::MatrixXi tRefPtsFloor = tRefPts.unaryExpr(std::ptr_fun(Eigenfloor));
Eigen::MatrixXd tRefPtsSubpix = tRefPts - tRefPtsFloor.cast<double>();
Eigen::MatrixXd tRefPtsSubpixX = Eigen::MatrixXd::Ones(1, tNum) - tRefPtsSubpix.row(0);
Eigen::MatrixXd tRefPtsSubpixY = Eigen::MatrixXd::Ones(1, tNum) - tRefPtsSubpix.row(1);
Eigen::MatrixXd tCofficientW00 = tRefPtsSubpixX.cwiseProduct(tRefPtsSubpixY);
Eigen::MatrixXd tCofficientW01 = tRefPtsSubpix.row(0).cwiseProduct(tRefPtsSubpixY);
Eigen::MatrixXd tCofficientW10 = tRefPtsSubpixX.cwiseProduct(tRefPtsSubpix.row(1));
Eigen::MatrixXd tCofficientW11 = tRefPtsSubpix.row(0).cwiseProduct(tRefPtsSubpix.row(1));
//! Calculate Jacobian matrix and reference patch
for (int j = 0; j < tNum; ++j)
{
Eigen::Matrix<double, 2, 6> tJacTrans;
tJacTrans = GetJocabianBA(mRefNormals.col(j));
int tNum1 = 0;
for (int i = 0; i < mHalf_PatchSize; ++i)
{
uchar *it = (uchar*)tRefIMg.data + (tRefPtsFloor(1, j) - 2 +i)*tRefStep + (tRefPtsFloor(0, j) - 2);
for (int k = 0; k < mHalf_PatchSize; ++k, ++it, ++tNum1)
{
mRefPatch(j, tNum1) = tCofficientW00(j)*it[0] + tCofficientW01(j)*it[1]
+ tCofficientW10(j)*it[tRefStep] + tCofficientW11(j)*it[tRefStep+1];
double dx = 0.5*((tCofficientW00(j)*it[1] + tCofficientW01(j)*it[2]
+ tCofficientW10(j)*it[tRefStep+1] + tCofficientW11(j)*it[tRefStep+2]) -
(tCofficientW00(j)*it[-1] + tCofficientW01(j)*it[0]
+ tCofficientW10(j)*it[tRefStep-1] + tCofficientW11(j)*it[tRefStep]));
double dy = 0.5*((tCofficientW00(j)*it[tRefStep] + tCofficientW01(j)*it[tRefStep+1]
+ tCofficientW10(j)*it[2*tRefStep] + tCofficientW11(j)*it[2*tRefStep+1]) -
(tCofficientW00(j)*it[-tRefStep] + tCofficientW01(j)*it[-tRefStep+1]
+ tCofficientW10(j)*it[0] + tCofficientW11(j)*it[1]));
mJocabianPatch.row(j*mPatchArea + tNum1) = (dx*tJacTrans.row(0) + dy*tJacTrans.row(1))*tFocalth*tScale;
}
}
}
}
//! The projected plane is normalization plane
Eigen::Matrix<double, 2, 6> Sprase_ImgAlign::GetJocabianBA(Eigen::Vector3d tPoint)
{
Eigen::Matrix<double, 2, 6> J;
const double x = tPoint(0);
const double y = tPoint(1);
const double z_inv = 1.0/tPoint(2);
const double z_inv2 = z_inv*z_inv;
J(0,0) = -z_inv;
J(0,1) = 0.0;
J(0,2) = x*z_inv2;
J(0,3) = y*J(0,2);
J(0,4) = -(1.0 + x*J(0,2));
J(0,5) = y*z_inv;
J(1,0) = 0.0;
J(1,1) = -z_inv;
J(1,2) = y*z_inv2;
J(1,3) = 1.0 + y*J(1,2);
J(1,4) = -x*J(1,2);
J(1,5) = -x*z_inv;
return J;
}
void Sprase_ImgAlign::CeresSolver(Sophus::SE3 &tT_c2r, int tLevel)
{
int tPatchArea = mHalf_PatchSize*mHalf_PatchSize;
const cv::Mat tCurImg = mCurFrame->mvImg_Pyr[tLevel];
const float tScale = 1.0/(1<<tLevel);
Eigen::Matrix<double, 6, 1> tT_c2rArray;
tT_c2rArray.block(0, 0, 3, 1) = tT_c2r.translation();
tT_c2rArray.block(3, 0, 3, 1) = tT_c2r.so3().log();
double *tRefPatch = (double*)mRefPatch.data();
double *tJacobianPtr = (double*)mJocabianPatch.data();
CameraPtr tCamera = mCurFrame->mCamera;
ceres::Problem problem;
ceres::LocalParameterization *local_Parameterization = new PoseLocalParameterization();
problem.AddParameterBlock(tT_c2rArray.data(), 6, local_Parameterization);
int tnPts = mRefPatch.rows();
for (int i = 0; i < tnPts; ++i)
{
DirectSE3_Problem *p = new DirectSE3_Problem(mRefNormals.col(i), tRefPatch, tJacobianPtr, &tCurImg, tScale, tCamera.get());
problem.AddResidualBlock(p, NULL, tT_c2rArray.data());
tRefPatch += tPatchArea;
tJacobianPtr += 6*tPatchArea;
}
ceres::Solver::Options options;
options.trust_region_strategy_type = ceres::DOGLEG;
options.linear_solver_type = ceres::DENSE_QR;
//options.minimizer_progress_to_stdout = true;
options.max_num_iterations = 5;
ceres::Solver::Summary summary;
//TicToc tc;
ceres::Solve(options, &problem, &summary);
//std::cout << tc.toc() << std::endl;
//std::cout << summary.FullReport() << std::endl;
Sophus::SE3 mT_c2r(Sophus::SO3::exp(tT_c2rArray.tail<3>()), tT_c2rArray.head<3>());
tT_c2r = mT_c2r;
}
double Sprase_ImgAlign::ComputeResiduals(Sophus::SE3 &tT_c2r, int level, bool linearSystem, int & tnPts)
{
int tPatchArea = mHalf_PatchSize*mHalf_PatchSize;
const cv::Mat tCurImg = mCurFrame->mvImg_Pyr[level];
const float tScale = 1.0/(1<<level);
const int mnboarder = mHalf_PatchSize-1;
double *mRefPatchAdd = (double*)mRefPatch.data();
double chi2 = 0.0;
int tResNum = 0;
tnPts = 0;
for (int n = 0; n < mRefPatch.rows(); ++n)
{
Eigen::Vector3d tCurPoint = tT_c2r*mRefNormals.col(n);
Eigen::Vector2d tCurPix = mRefFrame->mCamera->Camera2Pixel(tCurPoint)*tScale;
const double u = tCurPix(0);
const double v = tCurPix(1);
const int u_i = floor(u);
const int v_i = floor(v);
if(u_i < 0 || v_i < 0 || u_i - mnboarder < 0 || v_i - mnboarder < 0 || u_i + mnboarder >= tCurImg.cols || v_i + mnboarder >= tCurImg.rows)
continue;
const double tSubPixU = u - u_i;
const double tSubPixV = v - v_i;
const double tC_tl = (1.0 - tSubPixU)*(1.0 - tSubPixV);
const double tC_tr = tSubPixU*(1.0 - tSubPixV);
const double tC_bl = (1.0 - tSubPixU)*tSubPixV;
const double tC_br = tSubPixU*tSubPixV;
int tStep = tCurImg.step.p[0];
int tPtnum = n*tPatchArea;
int tNum = 0;
for (int i = 0; i < mHalf_PatchSize; ++i)
{
uchar *it = tCurImg.data + (v_i + i - 2)*tCurImg.cols + u_i - 2;
for (int j = 0; j < mHalf_PatchSize; ++j, ++it, ++tNum)
{
double tCurPx = tC_tl*it[0] + tC_tr*it[1] + tC_bl*it[tStep] + tC_br*it[tStep+1];
double res = -(*(mRefPatchAdd+tNum+tPtnum) - tCurPx);
chi2 += res*res;
tResNum++;
if(linearSystem)
{
const Eigen::Matrix<double, 6, 1> J = mJocabianPatch.row(tPtnum + tNum).transpose();
H.noalias() += J * J.transpose();
JRes.noalias() += J * res;
}
}
}
tnPts++;
}
return chi2/tResNum;
}
void Sprase_ImgAlign::GaussNewtonSolver(Sophus::SE3 &tT_c2r, int level, int & tnPts)
{
bool stop = false;
const double eps = 1e-8;
double chi2 = 0.0;
Sophus::SE3 tT_c2rOld(tT_c2r);
for (int i = 0; i < mnMaxIterators; ++i)
{
double chi2Diff = 0.0;
H.setZero();
JRes.setZero();
double chi2New = ComputeResiduals(tT_c2r, level, true, tnPts);
Eigen::Matrix<double, 6, 1> x = H.ldlt().solve(JRes);
//! Failed solver
if(bool(std::isnan(x(0))))
{
LOG(ERROR) << "Matrix is close to singular!" << std::endl;
std::cout<< "error" << std::endl;
stop = true;
}
if((i > 0 && chi2New > chi2) || stop)
{
tT_c2r = tT_c2rOld;
break;
}
//! Successful solver
Sophus::SE3 tT_c2rNew = tT_c2r*Sophus::SE3::exp(x);
tT_c2rOld = tT_c2r;
tT_c2r = tT_c2rNew;
chi2 = chi2New;
if((x.cwiseAbs().maxCoeff() <= eps))
break;
}
}
}// namesapce DSDTM