4 |
jakw |
1 |
#include "Triangulator.h"
|
|
|
2 |
#include <math.h>
|
|
|
3 |
#include <iostream>
|
|
|
4 |
|
|
|
5 |
|
|
|
6 |
#ifdef WIN32
|
|
|
7 |
#ifndef NAN
|
|
|
8 |
static const unsigned long __nan[2] = {0xffffffff, 0x7fffffff};
|
|
|
9 |
#define NAN (*(const float *) __nan)
|
|
|
10 |
#endif
|
|
|
11 |
#endif
|
|
|
12 |
|
|
|
13 |
Triangulator::Triangulator(SMCalibrationParams _calibration, unsigned int frameWidth, unsigned int frameHeight) : calibration(_calibration){
|
|
|
14 |
|
|
|
15 |
// Precompute uc, vc maps
|
|
|
16 |
uc.create(frameHeight, frameWidth, CV_32F);
|
|
|
17 |
vc.create(frameHeight, frameWidth, CV_32F);
|
|
|
18 |
|
|
|
19 |
for(unsigned int row=0; row<frameHeight; row++){
|
|
|
20 |
for(unsigned int col=0; col<frameWidth; col++){
|
|
|
21 |
uc.at<float>(row, col) = col;
|
|
|
22 |
vc.at<float>(row, col) = row;
|
|
|
23 |
}
|
|
|
24 |
}
|
|
|
25 |
|
|
|
26 |
// Precompute determinant tensor
|
|
|
27 |
cv::Mat Pc(3,4,CV_32F,cv::Scalar(0.0));
|
|
|
28 |
cv::Mat(calibration.Kc).copyTo(Pc(cv::Range(0,3), cv::Range(0,3)));
|
|
|
29 |
|
|
|
30 |
cv::Mat Pp(3,4,CV_32F), temp(3,4,CV_32F);
|
|
|
31 |
cv::Mat(calibration.Rp).copyTo(temp(cv::Range(0,3), cv::Range(0,3)));
|
|
|
32 |
cv::Mat(calibration.Tp).copyTo(temp(cv::Range(0,3), cv::Range(3,4)));
|
|
|
33 |
Pp = cv::Mat(calibration.Kp) * temp;
|
|
|
34 |
|
|
|
35 |
cv::Mat e = cv::Mat::eye(4, 4, CV_32F);
|
|
|
36 |
|
|
|
37 |
int sz[] = {4, 3, 3, 3};
|
|
|
38 |
cv::Mat C(4, sz, CV_32F, cv::Scalar::all(0));
|
|
|
39 |
for(int k=0; k<4; k++){
|
|
|
40 |
for(int i=0; i<3; i++){
|
|
|
41 |
for(int j=0; j<3; j++){
|
|
|
42 |
for(int l=0; l<3; l++){
|
|
|
43 |
cv::Mat op(4, 4, CV_32F);
|
|
|
44 |
Pc.row(i).copyTo(op.row(0));
|
|
|
45 |
Pc.row(j).copyTo(op.row(1));
|
|
|
46 |
Pp.row(l).copyTo(op.row(2));
|
|
|
47 |
e.row(k).copyTo(op.row(3));
|
|
|
48 |
C.at<float>(cv::Vec4i(k,i,j,l)) = cv::determinant(op.t());
|
|
|
49 |
}
|
|
|
50 |
}
|
|
|
51 |
}
|
|
|
52 |
}
|
|
|
53 |
determinantTensor = C;
|
|
|
54 |
|
|
|
55 |
// Precompute lens correction maps
|
|
|
56 |
cv::Mat eye = cv::Mat::eye(3, 3, CV_32F);
|
|
|
57 |
cv::initUndistortRectifyMap(calibration.Kc, calibration.kc, eye, calibration.Kc, cv::Size(frameWidth, frameHeight), CV_32FC1, lensMap1, lensMap2);
|
|
|
58 |
|
|
|
59 |
//cv::Mat map1, map2;
|
|
|
60 |
//cv::normalize(lensMap1, map1, 0, 255, cv::NORM_MINMAX, CV_8U);
|
|
|
61 |
//cv::normalize(lensMap2, map2, 0, 255, cv::NORM_MINMAX, CV_8U);
|
|
|
62 |
//cv::imwrite("map1.png", map1);
|
|
|
63 |
//cv::imwrite("map2.png", map2);
|
|
|
64 |
}
|
|
|
65 |
|
|
|
66 |
void Triangulator::triangulate(cv::Mat &up, cv::Mat &vp, cv::Mat &mask, cv::Mat &shading, cv::Mat &pointCloud){
|
|
|
67 |
|
|
|
68 |
// Undistort up, mask and shading
|
|
|
69 |
if(!up.empty()){
|
|
|
70 |
cv::Mat upUndistort;
|
|
|
71 |
cv::remap(up, upUndistort, lensMap1, lensMap2, cv::INTER_LINEAR);
|
|
|
72 |
up = upUndistort;
|
|
|
73 |
}
|
|
|
74 |
if(!vp.empty()){
|
|
|
75 |
cv::Mat vpUndistort;
|
|
|
76 |
cv::remap(vp, vpUndistort, lensMap1, lensMap2, cv::INTER_LINEAR);
|
|
|
77 |
vp = vpUndistort;
|
|
|
78 |
}
|
|
|
79 |
|
|
|
80 |
cv::Mat maskUndistort, shadingUndistort;
|
|
|
81 |
cv::remap(mask, maskUndistort, lensMap1, lensMap2, cv::INTER_LINEAR);
|
|
|
82 |
cv::remap(shading, shadingUndistort, lensMap1, lensMap2, cv::INTER_LINEAR);
|
|
|
83 |
mask = maskUndistort;
|
|
|
84 |
shading = shadingUndistort;
|
|
|
85 |
|
|
|
86 |
// Triangulate
|
|
|
87 |
cv::Mat xyz;
|
|
|
88 |
if(!up.empty() && vp.empty())
|
|
|
89 |
triangulateFromUp(up, xyz);
|
|
|
90 |
else if(!vp.empty() && up.empty())
|
|
|
91 |
triangulateFromVp(vp, xyz);
|
|
|
92 |
else if(!up.empty() && !vp.empty())
|
|
|
93 |
triangulateFromUpVp(up, vp, xyz);
|
|
|
94 |
|
|
|
95 |
// Merge and mask
|
|
|
96 |
pointCloud = cv::Mat(up.size(), CV_32FC3, cv::Scalar(NAN, NAN, NAN));
|
|
|
97 |
xyz.copyTo(pointCloud, mask);
|
|
|
98 |
|
|
|
99 |
}
|
|
|
100 |
|
|
|
101 |
void Triangulator::triangulateFromUp(cv::Mat &up, cv::Mat &xyz){
|
|
|
102 |
|
|
|
103 |
// Solve for xyzw using determinant tensor
|
|
|
104 |
cv::Mat C = determinantTensor;
|
|
|
105 |
std::vector<cv::Mat> xyzw(4);
|
|
|
106 |
for(unsigned int i=0; i<4; i++){
|
|
|
107 |
xyzw[i].create(up.size(), CV_32F);
|
|
|
108 |
xyzw[i] = C.at<float>(cv::Vec4i(i,0,1,0)) - C.at<float>(cv::Vec4i(i,2,1,0))*uc - C.at<float>(cv::Vec4i(i,0,2,0))*vc -
|
|
|
109 |
C.at<float>(cv::Vec4i(i,0,1,2))*up + C.at<float>(cv::Vec4i(i,2,1,2))*up.mul(uc) + C.at<float>(cv::Vec4i(i,0,2,2))*up.mul(vc);
|
|
|
110 |
}
|
|
|
111 |
|
|
|
112 |
// Convert to non homogenous coordinates
|
|
|
113 |
for(unsigned int i=0; i<3; i++)
|
|
|
114 |
xyzw[i] /= xyzw[3];
|
|
|
115 |
|
|
|
116 |
// Merge and mask
|
|
|
117 |
cv::merge(std::vector<cv::Mat>(xyzw.begin(), xyzw.begin()+3), xyz);
|
|
|
118 |
|
|
|
119 |
}
|
|
|
120 |
|
|
|
121 |
void Triangulator::triangulateFromVp(cv::Mat &vp, cv::Mat &xyz){
|
|
|
122 |
|
|
|
123 |
// Solve for xyzw using determinant tensor
|
|
|
124 |
cv::Mat C = determinantTensor;
|
|
|
125 |
std::vector<cv::Mat> xyzw(4);
|
|
|
126 |
for(unsigned int i=0; i<4; i++){
|
|
|
127 |
xyzw[i].create(vp.size(), CV_32F);
|
|
|
128 |
xyzw[i] = C.at<float>(cv::Vec4i(i,0,1,1)) - C.at<float>(cv::Vec4i(i,2,1,1))*uc - C.at<float>(cv::Vec4i(i,0,2,1))*vc -
|
|
|
129 |
C.at<float>(cv::Vec4i(i,0,1,2))*vp + C.at<float>(cv::Vec4i(i,2,1,2))*vp.mul(uc) + C.at<float>(cv::Vec4i(i,0,2,2))*vp.mul(vc);
|
|
|
130 |
}
|
|
|
131 |
|
|
|
132 |
// Convert to non homogenous coordinates
|
|
|
133 |
for(unsigned int i=0; i<3; i++)
|
|
|
134 |
xyzw[i] /= xyzw[3];
|
|
|
135 |
|
|
|
136 |
// Merge and mask
|
|
|
137 |
cv::merge(std::vector<cv::Mat>(xyzw.begin(), xyzw.begin()+3), xyz);
|
|
|
138 |
|
|
|
139 |
}
|
|
|
140 |
|
|
|
141 |
void Triangulator::triangulateFromUpVp(cv::Mat &up, cv::Mat &vp, cv::Mat &xyz){
|
|
|
142 |
|
|
|
143 |
std::cerr << "WARNING! NOT FULLY IMPLEMENTED!" << std::endl;
|
|
|
144 |
int N = up.rows * up.cols;
|
|
|
145 |
|
|
|
146 |
cv::Mat projPointsCam(2, N, CV_32F);
|
|
|
147 |
uc.reshape(0,1).copyTo(projPointsCam.row(0));
|
|
|
148 |
vc.reshape(0,1).copyTo(projPointsCam.row(1));
|
|
|
149 |
|
|
|
150 |
cv::Mat projPointsProj(2, N, CV_32F);
|
|
|
151 |
up.reshape(0,1).copyTo(projPointsProj.row(0));
|
|
|
152 |
vp.reshape(0,1).copyTo(projPointsProj.row(1));
|
|
|
153 |
|
|
|
154 |
cv::Mat Pc(3,4,CV_32F,cv::Scalar(0.0));
|
|
|
155 |
cv::Mat(calibration.Kc).copyTo(Pc(cv::Range(0,3), cv::Range(0,3)));
|
|
|
156 |
|
|
|
157 |
cv::Mat Pp(3,4,CV_32F), temp(3,4,CV_32F);
|
|
|
158 |
cv::Mat(calibration.Rp).copyTo(temp(cv::Range(0,3), cv::Range(0,3)));
|
|
|
159 |
cv::Mat(calibration.Tp).copyTo(temp(cv::Range(0,3), cv::Range(3,4)));
|
|
|
160 |
Pp = cv::Mat(calibration.Kp) * temp;
|
|
|
161 |
|
|
|
162 |
cv::Mat xyzw;
|
|
|
163 |
cv::triangulatePoints(Pc, Pp, projPointsCam, projPointsProj, xyzw);
|
|
|
164 |
|
|
|
165 |
xyz.create(3, N, CV_32F);
|
|
|
166 |
for(int i=0; i<N; i++){
|
|
|
167 |
xyz.at<float>(0,i) = xyzw.at<float>(0,i)/xyzw.at<float>(3,i);
|
|
|
168 |
xyz.at<float>(1,i) = xyzw.at<float>(1,i)/xyzw.at<float>(3,i);
|
|
|
169 |
xyz.at<float>(2,i) = xyzw.at<float>(2,i)/xyzw.at<float>(3,i);
|
|
|
170 |
}
|
|
|
171 |
|
|
|
172 |
xyz = xyz.t();
|
|
|
173 |
xyz = xyz.reshape(3, up.rows);
|
|
|
174 |
}
|
|
|
175 |
|
|
|
176 |
|