27 |
jakw |
1 |
#include "SMReconstructionWorker.h"
|
9 |
jakw |
2 |
|
27 |
jakw |
3 |
#include "CodecGrayCode.h"
|
|
|
4 |
#include "CodecPhaseShift.h"
|
|
|
5 |
|
9 |
jakw |
6 |
#include <QCoreApplication>
|
|
|
7 |
#include <QSettings>
|
|
|
8 |
|
|
|
9 |
#include <iostream>
|
|
|
10 |
#include <opencv2/opencv.hpp>
|
|
|
11 |
|
|
|
12 |
#include <pcl/filters/statistical_outlier_removal.h>
|
|
|
13 |
#include <pcl/io/pcd_io.h>
|
|
|
14 |
|
|
|
15 |
|
27 |
jakw |
16 |
void SMReconstructionWorker::setup(){
|
9 |
jakw |
17 |
|
27 |
jakw |
18 |
QSettings settings;
|
|
|
19 |
|
|
|
20 |
// Get current calibration
|
33 |
jakw |
21 |
calibration = settings.value("calibration/parameters").value<SMCalibrationParameters>();
|
27 |
jakw |
22 |
|
|
|
23 |
// Create decoder
|
|
|
24 |
dir = (CodecDir)settings.value("pattern/direction", CodecDirHorizontal).toInt();
|
|
|
25 |
if(dir == CodecDirNone)
|
|
|
26 |
std::cerr << "SMCaptureWorker: invalid coding direction " << std::endl;
|
|
|
27 |
|
|
|
28 |
QString codec = settings.value("codec", "GrayCode").toString();
|
|
|
29 |
if(codec == "PhaseShift")
|
|
|
30 |
decoder = new DecoderPhaseShift(dir);
|
|
|
31 |
else if(codec == "GrayCode")
|
|
|
32 |
decoder = new DecoderGrayCode(dir);
|
|
|
33 |
else
|
|
|
34 |
std::cerr << "SLScanWorker: invalid codec " << codec.toStdString() << std::endl;
|
|
|
35 |
|
|
|
36 |
|
25 |
jakw |
37 |
// Precompute lens correction maps
|
|
|
38 |
cv::Mat eye = cv::Mat::eye(3, 3, CV_32F);
|
27 |
jakw |
39 |
cv::initUndistortRectifyMap(calibration.K0, calibration.k0, eye, calibration.K0, cv::Size(calibration.frameWidth, calibration.frameHeight), CV_32FC1, lensMap0Horz, lensMap0Vert);
|
|
|
40 |
cv::initUndistortRectifyMap(calibration.K0, calibration.k0, eye, calibration.K0, cv::Size(calibration.frameWidth, calibration.frameHeight), CV_32FC1, lensMap1Horz, lensMap1Vert);
|
9 |
jakw |
41 |
|
27 |
jakw |
42 |
//cv::Mat mapHorz, mapVert;
|
|
|
43 |
//cv::normalize(lensMap0Horz, mapHorz, 0, 255, cv::NORM_MINMAX, CV_8U);
|
|
|
44 |
//cv::normalize(lensMap0Vert, mapVert, 0, 255, cv::NORM_MINMAX, CV_8U);
|
|
|
45 |
//cv::imwrite("mapHorz.png", mapHorz);
|
|
|
46 |
//cv::imwrite("mapVert.png", mapVert);
|
9 |
jakw |
47 |
}
|
|
|
48 |
|
27 |
jakw |
49 |
void SMReconstructionWorker::reconstructPointCloud(SMFrameSequence frameSequence){
|
9 |
jakw |
50 |
|
|
|
51 |
time.start();
|
|
|
52 |
|
27 |
jakw |
53 |
// Decode frames
|
|
|
54 |
cv::Mat up0, vp0, up1, vp1, shading0, mask0, shading1, mask1;
|
|
|
55 |
decoder->decodeFrames(frameSequence.frames0, up0, vp0, mask0, shading0);
|
|
|
56 |
decoder->decodeFrames(frameSequence.frames1, up1, vp1, mask1, shading1);
|
|
|
57 |
|
|
|
58 |
// Triangulate
|
9 |
jakw |
59 |
cv::Mat pointCloud;
|
27 |
jakw |
60 |
if(dir == CodecDirBoth)
|
|
|
61 |
triangulateFromUpVp(up0, vp0, mask0, up1, vp1, mask1, pointCloud);
|
|
|
62 |
else if(dir == CodecDirHorizontal)
|
|
|
63 |
triangulateFromUp(up0, mask0, up1, mask1, pointCloud);
|
|
|
64 |
else if(dir == CodecDirVertical)
|
|
|
65 |
triangulateFromVp(vp0, mask0, vp1, mask1, pointCloud);
|
9 |
jakw |
66 |
|
27 |
jakw |
67 |
// Simply use shading information from camera 0 (for now)
|
|
|
68 |
cv::Mat shading = shading0;
|
9 |
jakw |
69 |
|
|
|
70 |
// Convert point cloud to PCL format
|
|
|
71 |
pcl::PointCloud<pcl::PointXYZRGB>::Ptr pointCloudPCL(new pcl::PointCloud<pcl::PointXYZRGB>);
|
|
|
72 |
|
|
|
73 |
// Interprete as organized point cloud
|
|
|
74 |
pointCloudPCL->width = pointCloud.cols;
|
|
|
75 |
pointCloudPCL->height = pointCloud.rows;
|
|
|
76 |
pointCloudPCL->is_dense = false;
|
|
|
77 |
|
|
|
78 |
pointCloudPCL->points.resize(pointCloud.rows*pointCloud.cols);
|
|
|
79 |
|
27 |
jakw |
80 |
for(int row=0; row<pointCloud.rows; row++){
|
|
|
81 |
int offset = row * pointCloudPCL->width;
|
|
|
82 |
for(int col=0; col<pointCloud.cols; col++){
|
|
|
83 |
const cv::Vec3f pnt = pointCloud.at<cv::Vec3f>(row,col);
|
|
|
84 |
unsigned char shade = shading.at<unsigned short>(row,col) >> 8;
|
|
|
85 |
pcl::PointXYZRGB point;
|
|
|
86 |
point.x = pnt[0]; point.y = pnt[1]; point.z = pnt[2];
|
|
|
87 |
point.r = shade; point.g = shade; point.b = shade;
|
|
|
88 |
pointCloudPCL->points[offset + col] = point;
|
|
|
89 |
}
|
|
|
90 |
}
|
9 |
jakw |
91 |
|
27 |
jakw |
92 |
/* // stack xyz data
|
|
|
93 |
std::vector<cv::Mat> xyz;
|
|
|
94 |
cv::split(pointCloud, xyz);
|
9 |
jakw |
95 |
std::vector<cv::Mat> pointCloudChannels;
|
|
|
96 |
pointCloudChannels.push_back(xyz[0]);
|
|
|
97 |
pointCloudChannels.push_back(xyz[1]);
|
|
|
98 |
pointCloudChannels.push_back(xyz[2]);
|
|
|
99 |
|
|
|
100 |
// 4 byte padding
|
|
|
101 |
pointCloudChannels.push_back(cv::Mat::zeros(pointCloud.size(), CV_32F));
|
|
|
102 |
|
|
|
103 |
// triple uchar color information
|
|
|
104 |
std::vector<cv::Mat> rgb;
|
|
|
105 |
rgb.push_back(shading);
|
|
|
106 |
rgb.push_back(shading);
|
|
|
107 |
rgb.push_back(shading);
|
|
|
108 |
rgb.push_back(cv::Mat::zeros(shading.size(), CV_8U));
|
|
|
109 |
|
|
|
110 |
cv::Mat rgb8UC4;
|
|
|
111 |
cv::merge(rgb, rgb8UC4);
|
|
|
112 |
|
|
|
113 |
cv::Mat rgb32F(rgb8UC4.size(), CV_32F, rgb8UC4.data);
|
|
|
114 |
|
|
|
115 |
pointCloudChannels.push_back(rgb32F);
|
|
|
116 |
|
|
|
117 |
// 12 bytes padding
|
|
|
118 |
pointCloudChannels.push_back(cv::Mat::zeros(pointCloud.size(), CV_32F));
|
|
|
119 |
pointCloudChannels.push_back(cv::Mat::zeros(pointCloud.size(), CV_32F));
|
|
|
120 |
pointCloudChannels.push_back(cv::Mat::zeros(pointCloud.size(), CV_32F));
|
|
|
121 |
|
|
|
122 |
// merge channels
|
|
|
123 |
cv::Mat pointCloudPadded;
|
|
|
124 |
cv::merge(pointCloudChannels, pointCloudPadded);
|
|
|
125 |
|
|
|
126 |
// memcpy everything
|
27 |
jakw |
127 |
memcpy(&pointCloudPCL->points[0], pointCloudPadded.data, pointCloudPadded.rows*pointCloudPadded.cols*sizeof(pcl::PointXYZRGB));*/
|
9 |
jakw |
128 |
|
|
|
129 |
|
|
|
130 |
// Emit result
|
|
|
131 |
emit newPointCloud(pointCloudPCL);
|
|
|
132 |
|
27 |
jakw |
133 |
std::cout << "SMReconstructionWorker: " << time.elapsed() << "ms" << std::endl;
|
9 |
jakw |
134 |
|
|
|
135 |
}
|
|
|
136 |
|
27 |
jakw |
137 |
void SMReconstructionWorker::reconstructPointClouds(std::vector<SMFrameSequence> frameSequences){
|
24 |
jakw |
138 |
|
27 |
jakw |
139 |
// Process sequentially
|
|
|
140 |
for(int i=0; i<frameSequences.size(); i++){
|
|
|
141 |
reconstructPointCloud(frameSequences[i]);
|
24 |
jakw |
142 |
}
|
|
|
143 |
|
|
|
144 |
}
|
|
|
145 |
|
28 |
jakw |
146 |
void SMReconstructionWorker::triangulateFromUp(cv::Mat up0, cv::Mat mask0,cv::Mat up1, cv::Mat mask1,cv::Mat &xyz){}
|
|
|
147 |
void SMReconstructionWorker::triangulateFromVp(cv::Mat vp0, cv::Mat mask0, cv::Mat vp1, cv::Mat mask1, cv::Mat &xyz){}
|
|
|
148 |
void SMReconstructionWorker::triangulateFromUpVp(cv::Mat up0, cv::Mat vp0, cv::Mat mask0, cv::Mat up1, cv::Mat vp1, cv::Mat mask1, cv::Mat &xyz){}
|
24 |
jakw |
149 |
|
27 |
jakw |
150 |
//void SMReconstructionWorker::triangulateFromUpVp(cv::Mat &up, cv::Mat &vp, cv::Mat &xyz){
|
24 |
jakw |
151 |
|
25 |
jakw |
152 |
// std::cerr << "WARNING! NOT FULLY IMPLEMENTED!" << std::endl;
|
|
|
153 |
// int N = up.rows * up.cols;
|
24 |
jakw |
154 |
|
25 |
jakw |
155 |
// cv::Mat projPointsCam(2, N, CV_32F);
|
|
|
156 |
// uc.reshape(0,1).copyTo(projPointsCam.row(0));
|
|
|
157 |
// vc.reshape(0,1).copyTo(projPointsCam.row(1));
|
24 |
jakw |
158 |
|
25 |
jakw |
159 |
// cv::Mat projPointsProj(2, N, CV_32F);
|
|
|
160 |
// up.reshape(0,1).copyTo(projPointsProj.row(0));
|
|
|
161 |
// vp.reshape(0,1).copyTo(projPointsProj.row(1));
|
24 |
jakw |
162 |
|
25 |
jakw |
163 |
// cv::Mat Pc(3,4,CV_32F,cv::Scalar(0.0));
|
|
|
164 |
// cv::Mat(calibration.Kc).copyTo(Pc(cv::Range(0,3), cv::Range(0,3)));
|
24 |
jakw |
165 |
|
25 |
jakw |
166 |
// cv::Mat Pp(3,4,CV_32F), temp(3,4,CV_32F);
|
|
|
167 |
// cv::Mat(calibration.Rp).copyTo(temp(cv::Range(0,3), cv::Range(0,3)));
|
|
|
168 |
// cv::Mat(calibration.Tp).copyTo(temp(cv::Range(0,3), cv::Range(3,4)));
|
|
|
169 |
// Pp = cv::Mat(calibration.Kp) * temp;
|
24 |
jakw |
170 |
|
25 |
jakw |
171 |
// cv::Mat xyzw;
|
|
|
172 |
// cv::triangulatePoints(Pc, Pp, projPointsCam, projPointsProj, xyzw);
|
24 |
jakw |
173 |
|
25 |
jakw |
174 |
// xyz.create(3, N, CV_32F);
|
|
|
175 |
// for(int i=0; i<N; i++){
|
|
|
176 |
// xyz.at<float>(0,i) = xyzw.at<float>(0,i)/xyzw.at<float>(3,i);
|
|
|
177 |
// xyz.at<float>(1,i) = xyzw.at<float>(1,i)/xyzw.at<float>(3,i);
|
|
|
178 |
// xyz.at<float>(2,i) = xyzw.at<float>(2,i)/xyzw.at<float>(3,i);
|
|
|
179 |
// }
|
24 |
jakw |
180 |
|
25 |
jakw |
181 |
// xyz = xyz.t();
|
|
|
182 |
// xyz = xyz.reshape(3, up.rows);
|
27 |
jakw |
183 |
//}
|
24 |
jakw |
184 |
|