24 |
jakw |
1 |
#include "SMSMTriangulator.h"
|
9 |
jakw |
2 |
|
|
|
3 |
#include <QCoreApplication>
|
|
|
4 |
#include <QSettings>
|
|
|
5 |
|
|
|
6 |
#include <iostream>
|
|
|
7 |
#include <opencv2/opencv.hpp>
|
|
|
8 |
|
|
|
9 |
#include <pcl/filters/statistical_outlier_removal.h>
|
|
|
10 |
#include <pcl/io/pcd_io.h>
|
|
|
11 |
|
24 |
jakw |
12 |
void SMSMTriangulator::setup(){
|
9 |
jakw |
13 |
|
24 |
jakw |
14 |
// Initialize SMTriangulator with calibration
|
9 |
jakw |
15 |
calibration = new SMCalibrationParams;
|
|
|
16 |
calibration->load("calibration.yml");
|
24 |
jakw |
17 |
SMTriangulator = new SMTriangulator(*calibration, frameWidth, frameHeight);
|
9 |
jakw |
18 |
|
|
|
19 |
QSettings settings("SLStudio");
|
|
|
20 |
writeToDisk = settings.value("writeToDisk/pointclouds",false).toBool();
|
|
|
21 |
|
|
|
22 |
}
|
|
|
23 |
|
|
|
24 |
|
24 |
jakw |
25 |
void SMSMTriangulator::triangulatePointCloud(cv::Mat up, cv::Mat vp, cv::Mat mask, cv::Mat shading){
|
9 |
jakw |
26 |
|
|
|
27 |
time.start();
|
|
|
28 |
|
|
|
29 |
// Reconstruct point cloud
|
|
|
30 |
cv::Mat pointCloud;
|
|
|
31 |
cv::Mat empty;
|
24 |
jakw |
32 |
SMTriangulator->triangulate(up, vp, mask, shading, pointCloud);
|
9 |
jakw |
33 |
|
|
|
34 |
std::vector<cv::Mat> xyz;
|
|
|
35 |
cv::split(pointCloud, xyz);
|
|
|
36 |
// emit imshow("x", xyz[0], 1400, 100);
|
|
|
37 |
// emit imshow("y", xyz[1], 1400, 450);
|
|
|
38 |
// emit imshow("z", xyz[2], 1400, 800);
|
|
|
39 |
|
|
|
40 |
// Convert point cloud to PCL format
|
|
|
41 |
pcl::PointCloud<pcl::PointXYZRGB>::Ptr pointCloudPCL(new pcl::PointCloud<pcl::PointXYZRGB>);
|
|
|
42 |
|
|
|
43 |
// Interprete as organized point cloud
|
|
|
44 |
pointCloudPCL->width = pointCloud.cols;
|
|
|
45 |
pointCloudPCL->height = pointCloud.rows;
|
|
|
46 |
pointCloudPCL->is_dense = false;
|
|
|
47 |
|
|
|
48 |
pointCloudPCL->points.resize(pointCloud.rows*pointCloud.cols);
|
|
|
49 |
|
|
|
50 |
// for(int col=0; col<pointCloud.cols; col++){
|
|
|
51 |
// for(int row=0; row<pointCloud.rows; row++){
|
|
|
52 |
// const cv::Vec3f pnt = pointCloud.at<cv::Vec3f>(row,col);
|
|
|
53 |
// unsigned char shade = shading.at<unsigned char>(row,col);
|
|
|
54 |
// pcl::PointXYZRGBRGB point;
|
|
|
55 |
// point.x = pnt[0]; point.y = pnt[1]; point.z = pnt[2];
|
|
|
56 |
// point.r = shade; point.g = shade; point.b = shade;
|
|
|
57 |
// pointCloudPCL->at(col, row) = point;
|
|
|
58 |
// }
|
|
|
59 |
// }
|
|
|
60 |
|
|
|
61 |
// stack xyz data
|
|
|
62 |
std::vector<cv::Mat> pointCloudChannels;
|
|
|
63 |
pointCloudChannels.push_back(xyz[0]);
|
|
|
64 |
pointCloudChannels.push_back(xyz[1]);
|
|
|
65 |
pointCloudChannels.push_back(xyz[2]);
|
|
|
66 |
|
|
|
67 |
// 4 byte padding
|
|
|
68 |
pointCloudChannels.push_back(cv::Mat::zeros(pointCloud.size(), CV_32F));
|
|
|
69 |
|
|
|
70 |
// triple uchar color information
|
|
|
71 |
std::vector<cv::Mat> rgb;
|
|
|
72 |
rgb.push_back(shading);
|
|
|
73 |
rgb.push_back(shading);
|
|
|
74 |
rgb.push_back(shading);
|
|
|
75 |
rgb.push_back(cv::Mat::zeros(shading.size(), CV_8U));
|
|
|
76 |
|
|
|
77 |
cv::Mat rgb8UC4;
|
|
|
78 |
cv::merge(rgb, rgb8UC4);
|
|
|
79 |
|
|
|
80 |
cv::Mat rgb32F(rgb8UC4.size(), CV_32F, rgb8UC4.data);
|
|
|
81 |
|
|
|
82 |
pointCloudChannels.push_back(rgb32F);
|
|
|
83 |
|
|
|
84 |
// 12 bytes padding
|
|
|
85 |
pointCloudChannels.push_back(cv::Mat::zeros(pointCloud.size(), CV_32F));
|
|
|
86 |
pointCloudChannels.push_back(cv::Mat::zeros(pointCloud.size(), CV_32F));
|
|
|
87 |
pointCloudChannels.push_back(cv::Mat::zeros(pointCloud.size(), CV_32F));
|
|
|
88 |
|
|
|
89 |
// merge channels
|
|
|
90 |
cv::Mat pointCloudPadded;
|
|
|
91 |
cv::merge(pointCloudChannels, pointCloudPadded);
|
|
|
92 |
|
|
|
93 |
// memcpy everything
|
|
|
94 |
memcpy(&pointCloudPCL->points[0], pointCloudPadded.data, pointCloudPadded.rows*pointCloudPadded.cols*sizeof(pcl::PointXYZRGB));
|
|
|
95 |
|
|
|
96 |
// // filtering
|
|
|
97 |
// pcl::StatisticalOutlierRemoval<pcl::PointXYZRGB> filter;
|
|
|
98 |
// filter.setMeanK(5);
|
|
|
99 |
// filter.setStddevMulThresh(1.0);
|
|
|
100 |
// filter.setInputCloud(pointCloudPCL);
|
|
|
101 |
// pcl::PointCloud<pcl::PointXYZRGB>::Ptr pointCloudFiltered(new pcl::PointCloud<pcl::PointXYZRGB>);
|
|
|
102 |
// filter.filter(*pointCloudFiltered);
|
|
|
103 |
|
|
|
104 |
// Emit result
|
|
|
105 |
emit newPointCloud(pointCloudPCL);
|
|
|
106 |
|
24 |
jakw |
107 |
std::cout << "SMTriangulator: " << time.elapsed() << "ms" << std::endl;
|
9 |
jakw |
108 |
|
|
|
109 |
if(writeToDisk){
|
|
|
110 |
QString fileName = QDateTime::currentDateTime().toString("yyyyMMdd_HHmmsszzz");
|
|
|
111 |
fileName.append(".pcd");
|
|
|
112 |
pcl::io::savePCDFileBinary(fileName.toStdString(), *pointCloudPCL);
|
|
|
113 |
}
|
|
|
114 |
|
|
|
115 |
//emit finished();
|
|
|
116 |
}
|
|
|
117 |
|
24 |
jakw |
118 |
|
|
|
119 |
SMTriangulator::SMTriangulator(SMCalibrationParams _calibration, unsigned int frameWidth, unsigned int frameHeight) : calibration(_calibration){
|
|
|
120 |
|
|
|
121 |
// Precompute uc, vc maps
|
|
|
122 |
uc.create(frameHeight, frameWidth, CV_32F);
|
|
|
123 |
vc.create(frameHeight, frameWidth, CV_32F);
|
|
|
124 |
|
|
|
125 |
for(unsigned int row=0; row<frameHeight; row++){
|
|
|
126 |
for(unsigned int col=0; col<frameWidth; col++){
|
|
|
127 |
uc.at<float>(row, col) = col;
|
|
|
128 |
vc.at<float>(row, col) = row;
|
|
|
129 |
}
|
|
|
130 |
}
|
|
|
131 |
|
|
|
132 |
// Precompute determinant tensor
|
|
|
133 |
cv::Mat Pc(3,4,CV_32F,cv::Scalar(0.0));
|
|
|
134 |
cv::Mat(calibration.Kc).copyTo(Pc(cv::Range(0,3), cv::Range(0,3)));
|
|
|
135 |
|
|
|
136 |
cv::Mat Pp(3,4,CV_32F), temp(3,4,CV_32F);
|
|
|
137 |
cv::Mat(calibration.Rp).copyTo(temp(cv::Range(0,3), cv::Range(0,3)));
|
|
|
138 |
cv::Mat(calibration.Tp).copyTo(temp(cv::Range(0,3), cv::Range(3,4)));
|
|
|
139 |
Pp = cv::Mat(calibration.Kp) * temp;
|
|
|
140 |
|
|
|
141 |
cv::Mat e = cv::Mat::eye(4, 4, CV_32F);
|
|
|
142 |
|
|
|
143 |
int sz[] = {4, 3, 3, 3};
|
|
|
144 |
cv::Mat C(4, sz, CV_32F, cv::Scalar::all(0));
|
|
|
145 |
for(int k=0; k<4; k++){
|
|
|
146 |
for(int i=0; i<3; i++){
|
|
|
147 |
for(int j=0; j<3; j++){
|
|
|
148 |
for(int l=0; l<3; l++){
|
|
|
149 |
cv::Mat op(4, 4, CV_32F);
|
|
|
150 |
Pc.row(i).copyTo(op.row(0));
|
|
|
151 |
Pc.row(j).copyTo(op.row(1));
|
|
|
152 |
Pp.row(l).copyTo(op.row(2));
|
|
|
153 |
e.row(k).copyTo(op.row(3));
|
|
|
154 |
C.at<float>(cv::Vec4i(k,i,j,l)) = cv::determinant(op.t());
|
|
|
155 |
}
|
|
|
156 |
}
|
|
|
157 |
}
|
|
|
158 |
}
|
|
|
159 |
determinantTensor = C;
|
|
|
160 |
|
|
|
161 |
// Precompute lens correction maps
|
|
|
162 |
cv::Mat eye = cv::Mat::eye(3, 3, CV_32F);
|
|
|
163 |
cv::initUndistortRectifyMap(calibration.Kc, calibration.kc, eye, calibration.Kc, cv::Size(frameWidth, frameHeight), CV_32FC1, lensMap1, lensMap2);
|
|
|
164 |
|
|
|
165 |
//cv::Mat map1, map2;
|
|
|
166 |
//cv::normalize(lensMap1, map1, 0, 255, cv::NORM_MINMAX, CV_8U);
|
|
|
167 |
//cv::normalize(lensMap2, map2, 0, 255, cv::NORM_MINMAX, CV_8U);
|
|
|
168 |
//cv::imwrite("map1.png", map1);
|
|
|
169 |
//cv::imwrite("map2.png", map2);
|
|
|
170 |
}
|
|
|
171 |
|
|
|
172 |
void SMTriangulator::triangulate(cv::Mat &up, cv::Mat &vp, cv::Mat &mask, cv::Mat &shading, cv::Mat &pointCloud){
|
|
|
173 |
|
|
|
174 |
// Undistort up, mask and shading
|
|
|
175 |
if(!up.empty()){
|
|
|
176 |
cv::Mat upUndistort;
|
|
|
177 |
cv::remap(up, upUndistort, lensMap1, lensMap2, cv::INTER_LINEAR);
|
|
|
178 |
up = upUndistort;
|
|
|
179 |
}
|
|
|
180 |
if(!vp.empty()){
|
|
|
181 |
cv::Mat vpUndistort;
|
|
|
182 |
cv::remap(vp, vpUndistort, lensMap1, lensMap2, cv::INTER_LINEAR);
|
|
|
183 |
vp = vpUndistort;
|
|
|
184 |
}
|
|
|
185 |
|
|
|
186 |
cv::Mat maskUndistort, shadingUndistort;
|
|
|
187 |
cv::remap(mask, maskUndistort, lensMap1, lensMap2, cv::INTER_LINEAR);
|
|
|
188 |
cv::remap(shading, shadingUndistort, lensMap1, lensMap2, cv::INTER_LINEAR);
|
|
|
189 |
mask = maskUndistort;
|
|
|
190 |
shading = shadingUndistort;
|
|
|
191 |
|
|
|
192 |
// Triangulate
|
|
|
193 |
cv::Mat xyz;
|
|
|
194 |
if(!up.empty() && vp.empty())
|
|
|
195 |
triangulateFromUp(up, xyz);
|
|
|
196 |
else if(!vp.empty() && up.empty())
|
|
|
197 |
triangulateFromVp(vp, xyz);
|
|
|
198 |
else if(!up.empty() && !vp.empty())
|
|
|
199 |
triangulateFromUpVp(up, vp, xyz);
|
|
|
200 |
|
|
|
201 |
// Merge and mask
|
|
|
202 |
pointCloud = cv::Mat(up.size(), CV_32FC3, cv::Scalar(NAN, NAN, NAN));
|
|
|
203 |
xyz.copyTo(pointCloud, mask);
|
|
|
204 |
|
|
|
205 |
}
|
|
|
206 |
|
|
|
207 |
void SMTriangulator::triangulateFromUp(cv::Mat &up, cv::Mat &xyz){
|
|
|
208 |
|
|
|
209 |
// Solve for xyzw using determinant tensor
|
|
|
210 |
cv::Mat C = determinantTensor;
|
|
|
211 |
std::vector<cv::Mat> xyzw(4);
|
|
|
212 |
for(unsigned int i=0; i<4; i++){
|
|
|
213 |
xyzw[i].create(up.size(), CV_32F);
|
|
|
214 |
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 -
|
|
|
215 |
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);
|
|
|
216 |
}
|
|
|
217 |
|
|
|
218 |
// Convert to non homogenous coordinates
|
|
|
219 |
for(unsigned int i=0; i<3; i++)
|
|
|
220 |
xyzw[i] /= xyzw[3];
|
|
|
221 |
|
|
|
222 |
// Merge and mask
|
|
|
223 |
cv::merge(std::vector<cv::Mat>(xyzw.begin(), xyzw.begin()+3), xyz);
|
|
|
224 |
|
|
|
225 |
}
|
|
|
226 |
|
|
|
227 |
void SMTriangulator::triangulateFromVp(cv::Mat &vp, cv::Mat &xyz){
|
|
|
228 |
|
|
|
229 |
// Solve for xyzw using determinant tensor
|
|
|
230 |
cv::Mat C = determinantTensor;
|
|
|
231 |
std::vector<cv::Mat> xyzw(4);
|
|
|
232 |
for(unsigned int i=0; i<4; i++){
|
|
|
233 |
xyzw[i].create(vp.size(), CV_32F);
|
|
|
234 |
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 -
|
|
|
235 |
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);
|
|
|
236 |
}
|
|
|
237 |
|
|
|
238 |
// Convert to non homogenous coordinates
|
|
|
239 |
for(unsigned int i=0; i<3; i++)
|
|
|
240 |
xyzw[i] /= xyzw[3];
|
|
|
241 |
|
|
|
242 |
// Merge and mask
|
|
|
243 |
cv::merge(std::vector<cv::Mat>(xyzw.begin(), xyzw.begin()+3), xyz);
|
|
|
244 |
|
|
|
245 |
}
|
|
|
246 |
|
|
|
247 |
void SMTriangulator::triangulateFromUpVp(cv::Mat &up, cv::Mat &vp, cv::Mat &xyz){
|
|
|
248 |
|
|
|
249 |
std::cerr << "WARNING! NOT FULLY IMPLEMENTED!" << std::endl;
|
|
|
250 |
int N = up.rows * up.cols;
|
|
|
251 |
|
|
|
252 |
cv::Mat projPointsCam(2, N, CV_32F);
|
|
|
253 |
uc.reshape(0,1).copyTo(projPointsCam.row(0));
|
|
|
254 |
vc.reshape(0,1).copyTo(projPointsCam.row(1));
|
|
|
255 |
|
|
|
256 |
cv::Mat projPointsProj(2, N, CV_32F);
|
|
|
257 |
up.reshape(0,1).copyTo(projPointsProj.row(0));
|
|
|
258 |
vp.reshape(0,1).copyTo(projPointsProj.row(1));
|
|
|
259 |
|
|
|
260 |
cv::Mat Pc(3,4,CV_32F,cv::Scalar(0.0));
|
|
|
261 |
cv::Mat(calibration.Kc).copyTo(Pc(cv::Range(0,3), cv::Range(0,3)));
|
|
|
262 |
|
|
|
263 |
cv::Mat Pp(3,4,CV_32F), temp(3,4,CV_32F);
|
|
|
264 |
cv::Mat(calibration.Rp).copyTo(temp(cv::Range(0,3), cv::Range(0,3)));
|
|
|
265 |
cv::Mat(calibration.Tp).copyTo(temp(cv::Range(0,3), cv::Range(3,4)));
|
|
|
266 |
Pp = cv::Mat(calibration.Kp) * temp;
|
|
|
267 |
|
|
|
268 |
cv::Mat xyzw;
|
|
|
269 |
cv::triangulatePoints(Pc, Pp, projPointsCam, projPointsProj, xyzw);
|
|
|
270 |
|
|
|
271 |
xyz.create(3, N, CV_32F);
|
|
|
272 |
for(int i=0; i<N; i++){
|
|
|
273 |
xyz.at<float>(0,i) = xyzw.at<float>(0,i)/xyzw.at<float>(3,i);
|
|
|
274 |
xyz.at<float>(1,i) = xyzw.at<float>(1,i)/xyzw.at<float>(3,i);
|
|
|
275 |
xyz.at<float>(2,i) = xyzw.at<float>(2,i)/xyzw.at<float>(3,i);
|
|
|
276 |
}
|
|
|
277 |
|
|
|
278 |
xyz = xyz.t();
|
|
|
279 |
xyz = xyz.reshape(3, up.rows);
|
|
|
280 |
}
|
|
|
281 |
|
|
|
282 |
SMSMTriangulator::~SMSMTriangulator(){
|
9 |
jakw |
283 |
delete calibration;
|
24 |
jakw |
284 |
delete SMTriangulator;
|
9 |
jakw |
285 |
|
24 |
jakw |
286 |
std::cout<<"SMTriangulatorWorker deleted\n"<<std::flush;
|
9 |
jakw |
287 |
}
|
24 |
jakw |
288 |
|