25 |
jakw |
1 |
#include "SMTriangulator.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 |
|
|
|
12 |
|
25 |
jakw |
13 |
SMTriangulator::SMTriangulator(){
|
9 |
jakw |
14 |
|
25 |
jakw |
15 |
// Precompute lens correction maps
|
|
|
16 |
cv::Mat eye = cv::Mat::eye(3, 3, CV_32F);
|
|
|
17 |
cv::initUndistortRectifyMap(calibration.K0, calibration.k0, eye, calibration.K0, cv::Size(calibration.frameWidth, calibration.frameHeight), CV_32FC1, lensMap1, lensMap2);
|
9 |
jakw |
18 |
|
25 |
jakw |
19 |
//cv::Mat map1, map2;
|
|
|
20 |
//cv::normalize(lensMap1, map1, 0, 255, cv::NORM_MINMAX, CV_8U);
|
|
|
21 |
//cv::normalize(lensMap2, map2, 0, 255, cv::NORM_MINMAX, CV_8U);
|
|
|
22 |
//cv::imwrite("map1.png", map1);
|
|
|
23 |
//cv::imwrite("map2.png", map2);
|
9 |
jakw |
24 |
}
|
|
|
25 |
|
25 |
jakw |
26 |
void SMTriangulator::triangulatePointCloud(cv::Mat up, cv::Mat vp, cv::Mat mask, cv::Mat shading){
|
9 |
jakw |
27 |
|
|
|
28 |
time.start();
|
|
|
29 |
|
|
|
30 |
// Reconstruct point cloud
|
|
|
31 |
cv::Mat pointCloud;
|
|
|
32 |
cv::Mat empty;
|
25 |
jakw |
33 |
triangulate(up, vp, mask, shading, pointCloud);
|
9 |
jakw |
34 |
|
|
|
35 |
std::vector<cv::Mat> xyz;
|
|
|
36 |
cv::split(pointCloud, xyz);
|
|
|
37 |
// emit imshow("x", xyz[0], 1400, 100);
|
|
|
38 |
// emit imshow("y", xyz[1], 1400, 450);
|
|
|
39 |
// emit imshow("z", xyz[2], 1400, 800);
|
|
|
40 |
|
|
|
41 |
// Convert point cloud to PCL format
|
|
|
42 |
pcl::PointCloud<pcl::PointXYZRGB>::Ptr pointCloudPCL(new pcl::PointCloud<pcl::PointXYZRGB>);
|
|
|
43 |
|
|
|
44 |
// Interprete as organized point cloud
|
|
|
45 |
pointCloudPCL->width = pointCloud.cols;
|
|
|
46 |
pointCloudPCL->height = pointCloud.rows;
|
|
|
47 |
pointCloudPCL->is_dense = false;
|
|
|
48 |
|
|
|
49 |
pointCloudPCL->points.resize(pointCloud.rows*pointCloud.cols);
|
|
|
50 |
|
|
|
51 |
// for(int col=0; col<pointCloud.cols; col++){
|
|
|
52 |
// for(int row=0; row<pointCloud.rows; row++){
|
|
|
53 |
// const cv::Vec3f pnt = pointCloud.at<cv::Vec3f>(row,col);
|
|
|
54 |
// unsigned char shade = shading.at<unsigned char>(row,col);
|
|
|
55 |
// pcl::PointXYZRGBRGB point;
|
|
|
56 |
// point.x = pnt[0]; point.y = pnt[1]; point.z = pnt[2];
|
|
|
57 |
// point.r = shade; point.g = shade; point.b = shade;
|
|
|
58 |
// pointCloudPCL->at(col, row) = point;
|
|
|
59 |
// }
|
|
|
60 |
// }
|
|
|
61 |
|
|
|
62 |
// stack xyz data
|
|
|
63 |
std::vector<cv::Mat> pointCloudChannels;
|
|
|
64 |
pointCloudChannels.push_back(xyz[0]);
|
|
|
65 |
pointCloudChannels.push_back(xyz[1]);
|
|
|
66 |
pointCloudChannels.push_back(xyz[2]);
|
|
|
67 |
|
|
|
68 |
// 4 byte padding
|
|
|
69 |
pointCloudChannels.push_back(cv::Mat::zeros(pointCloud.size(), CV_32F));
|
|
|
70 |
|
|
|
71 |
// triple uchar color information
|
|
|
72 |
std::vector<cv::Mat> rgb;
|
|
|
73 |
rgb.push_back(shading);
|
|
|
74 |
rgb.push_back(shading);
|
|
|
75 |
rgb.push_back(shading);
|
|
|
76 |
rgb.push_back(cv::Mat::zeros(shading.size(), CV_8U));
|
|
|
77 |
|
|
|
78 |
cv::Mat rgb8UC4;
|
|
|
79 |
cv::merge(rgb, rgb8UC4);
|
|
|
80 |
|
|
|
81 |
cv::Mat rgb32F(rgb8UC4.size(), CV_32F, rgb8UC4.data);
|
|
|
82 |
|
|
|
83 |
pointCloudChannels.push_back(rgb32F);
|
|
|
84 |
|
|
|
85 |
// 12 bytes padding
|
|
|
86 |
pointCloudChannels.push_back(cv::Mat::zeros(pointCloud.size(), CV_32F));
|
|
|
87 |
pointCloudChannels.push_back(cv::Mat::zeros(pointCloud.size(), CV_32F));
|
|
|
88 |
pointCloudChannels.push_back(cv::Mat::zeros(pointCloud.size(), CV_32F));
|
|
|
89 |
|
|
|
90 |
// merge channels
|
|
|
91 |
cv::Mat pointCloudPadded;
|
|
|
92 |
cv::merge(pointCloudChannels, pointCloudPadded);
|
|
|
93 |
|
|
|
94 |
// memcpy everything
|
|
|
95 |
memcpy(&pointCloudPCL->points[0], pointCloudPadded.data, pointCloudPadded.rows*pointCloudPadded.cols*sizeof(pcl::PointXYZRGB));
|
|
|
96 |
|
|
|
97 |
// // filtering
|
|
|
98 |
// pcl::StatisticalOutlierRemoval<pcl::PointXYZRGB> filter;
|
|
|
99 |
// filter.setMeanK(5);
|
|
|
100 |
// filter.setStddevMulThresh(1.0);
|
|
|
101 |
// filter.setInputCloud(pointCloudPCL);
|
|
|
102 |
// pcl::PointCloud<pcl::PointXYZRGB>::Ptr pointCloudFiltered(new pcl::PointCloud<pcl::PointXYZRGB>);
|
|
|
103 |
// filter.filter(*pointCloudFiltered);
|
|
|
104 |
|
|
|
105 |
// Emit result
|
|
|
106 |
emit newPointCloud(pointCloudPCL);
|
|
|
107 |
|
24 |
jakw |
108 |
std::cout << "SMTriangulator: " << time.elapsed() << "ms" << std::endl;
|
9 |
jakw |
109 |
|
|
|
110 |
//emit finished();
|
|
|
111 |
}
|
|
|
112 |
|
24 |
jakw |
113 |
void SMTriangulator::triangulate(cv::Mat &up, cv::Mat &vp, cv::Mat &mask, cv::Mat &shading, cv::Mat &pointCloud){
|
|
|
114 |
|
|
|
115 |
// Undistort up, mask and shading
|
|
|
116 |
if(!up.empty()){
|
|
|
117 |
cv::Mat upUndistort;
|
|
|
118 |
cv::remap(up, upUndistort, lensMap1, lensMap2, cv::INTER_LINEAR);
|
|
|
119 |
up = upUndistort;
|
|
|
120 |
}
|
|
|
121 |
if(!vp.empty()){
|
|
|
122 |
cv::Mat vpUndistort;
|
|
|
123 |
cv::remap(vp, vpUndistort, lensMap1, lensMap2, cv::INTER_LINEAR);
|
|
|
124 |
vp = vpUndistort;
|
|
|
125 |
}
|
|
|
126 |
|
|
|
127 |
cv::Mat maskUndistort, shadingUndistort;
|
|
|
128 |
cv::remap(mask, maskUndistort, lensMap1, lensMap2, cv::INTER_LINEAR);
|
|
|
129 |
cv::remap(shading, shadingUndistort, lensMap1, lensMap2, cv::INTER_LINEAR);
|
|
|
130 |
mask = maskUndistort;
|
|
|
131 |
shading = shadingUndistort;
|
|
|
132 |
|
|
|
133 |
// Triangulate
|
|
|
134 |
cv::Mat xyz;
|
|
|
135 |
if(!up.empty() && vp.empty())
|
|
|
136 |
triangulateFromUp(up, xyz);
|
|
|
137 |
else if(!vp.empty() && up.empty())
|
|
|
138 |
triangulateFromVp(vp, xyz);
|
|
|
139 |
else if(!up.empty() && !vp.empty())
|
|
|
140 |
triangulateFromUpVp(up, vp, xyz);
|
|
|
141 |
|
|
|
142 |
// Merge and mask
|
|
|
143 |
pointCloud = cv::Mat(up.size(), CV_32FC3, cv::Scalar(NAN, NAN, NAN));
|
|
|
144 |
xyz.copyTo(pointCloud, mask);
|
|
|
145 |
|
|
|
146 |
}
|
|
|
147 |
|
|
|
148 |
void SMTriangulator::triangulateFromUp(cv::Mat &up, cv::Mat &xyz){
|
|
|
149 |
|
|
|
150 |
// Solve for xyzw using determinant tensor
|
|
|
151 |
cv::Mat C = determinantTensor;
|
|
|
152 |
std::vector<cv::Mat> xyzw(4);
|
|
|
153 |
for(unsigned int i=0; i<4; i++){
|
|
|
154 |
xyzw[i].create(up.size(), CV_32F);
|
|
|
155 |
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 -
|
|
|
156 |
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);
|
|
|
157 |
}
|
|
|
158 |
|
|
|
159 |
// Convert to non homogenous coordinates
|
|
|
160 |
for(unsigned int i=0; i<3; i++)
|
|
|
161 |
xyzw[i] /= xyzw[3];
|
|
|
162 |
|
|
|
163 |
// Merge and mask
|
|
|
164 |
cv::merge(std::vector<cv::Mat>(xyzw.begin(), xyzw.begin()+3), xyz);
|
|
|
165 |
|
|
|
166 |
}
|
|
|
167 |
|
|
|
168 |
void SMTriangulator::triangulateFromVp(cv::Mat &vp, cv::Mat &xyz){
|
|
|
169 |
|
|
|
170 |
// Solve for xyzw using determinant tensor
|
|
|
171 |
cv::Mat C = determinantTensor;
|
|
|
172 |
std::vector<cv::Mat> xyzw(4);
|
|
|
173 |
for(unsigned int i=0; i<4; i++){
|
|
|
174 |
xyzw[i].create(vp.size(), CV_32F);
|
|
|
175 |
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 -
|
|
|
176 |
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);
|
|
|
177 |
}
|
|
|
178 |
|
|
|
179 |
// Convert to non homogenous coordinates
|
|
|
180 |
for(unsigned int i=0; i<3; i++)
|
|
|
181 |
xyzw[i] /= xyzw[3];
|
|
|
182 |
|
|
|
183 |
// Merge and mask
|
|
|
184 |
cv::merge(std::vector<cv::Mat>(xyzw.begin(), xyzw.begin()+3), xyz);
|
|
|
185 |
|
|
|
186 |
}
|
|
|
187 |
|
|
|
188 |
void SMTriangulator::triangulateFromUpVp(cv::Mat &up, cv::Mat &vp, cv::Mat &xyz){
|
|
|
189 |
|
25 |
jakw |
190 |
// std::cerr << "WARNING! NOT FULLY IMPLEMENTED!" << std::endl;
|
|
|
191 |
// int N = up.rows * up.cols;
|
24 |
jakw |
192 |
|
25 |
jakw |
193 |
// cv::Mat projPointsCam(2, N, CV_32F);
|
|
|
194 |
// uc.reshape(0,1).copyTo(projPointsCam.row(0));
|
|
|
195 |
// vc.reshape(0,1).copyTo(projPointsCam.row(1));
|
24 |
jakw |
196 |
|
25 |
jakw |
197 |
// cv::Mat projPointsProj(2, N, CV_32F);
|
|
|
198 |
// up.reshape(0,1).copyTo(projPointsProj.row(0));
|
|
|
199 |
// vp.reshape(0,1).copyTo(projPointsProj.row(1));
|
24 |
jakw |
200 |
|
25 |
jakw |
201 |
// cv::Mat Pc(3,4,CV_32F,cv::Scalar(0.0));
|
|
|
202 |
// cv::Mat(calibration.Kc).copyTo(Pc(cv::Range(0,3), cv::Range(0,3)));
|
24 |
jakw |
203 |
|
25 |
jakw |
204 |
// cv::Mat Pp(3,4,CV_32F), temp(3,4,CV_32F);
|
|
|
205 |
// cv::Mat(calibration.Rp).copyTo(temp(cv::Range(0,3), cv::Range(0,3)));
|
|
|
206 |
// cv::Mat(calibration.Tp).copyTo(temp(cv::Range(0,3), cv::Range(3,4)));
|
|
|
207 |
// Pp = cv::Mat(calibration.Kp) * temp;
|
24 |
jakw |
208 |
|
25 |
jakw |
209 |
// cv::Mat xyzw;
|
|
|
210 |
// cv::triangulatePoints(Pc, Pp, projPointsCam, projPointsProj, xyzw);
|
24 |
jakw |
211 |
|
25 |
jakw |
212 |
// xyz.create(3, N, CV_32F);
|
|
|
213 |
// for(int i=0; i<N; i++){
|
|
|
214 |
// xyz.at<float>(0,i) = xyzw.at<float>(0,i)/xyzw.at<float>(3,i);
|
|
|
215 |
// xyz.at<float>(1,i) = xyzw.at<float>(1,i)/xyzw.at<float>(3,i);
|
|
|
216 |
// xyz.at<float>(2,i) = xyzw.at<float>(2,i)/xyzw.at<float>(3,i);
|
|
|
217 |
// }
|
24 |
jakw |
218 |
|
25 |
jakw |
219 |
// xyz = xyz.t();
|
|
|
220 |
// xyz = xyz.reshape(3, up.rows);
|
24 |
jakw |
221 |
}
|
|
|
222 |
|
25 |
jakw |
223 |
SMTriangulator::~SMTriangulator(){
|
9 |
jakw |
224 |
|
25 |
jakw |
225 |
std::cout << "SMTriangulator deleted.\n" << std::flush;
|
9 |
jakw |
226 |
}
|
24 |
jakw |
227 |
|