27 |
jakw |
1 |
#include "SMReconstructionWorker.h"
|
9 |
jakw |
2 |
|
41 |
jakw |
3 |
#include "AlgorithmGrayCode.h"
|
|
|
4 |
#include "AlgorithmPhaseShift.h"
|
27 |
jakw |
5 |
|
9 |
jakw |
6 |
#include <QCoreApplication>
|
|
|
7 |
#include <QSettings>
|
|
|
8 |
|
|
|
9 |
#include <iostream>
|
|
|
10 |
#include <opencv2/opencv.hpp>
|
|
|
11 |
|
42 |
jakw |
12 |
#include "cvtools.h"
|
|
|
13 |
|
9 |
jakw |
14 |
#include <pcl/filters/statistical_outlier_removal.h>
|
|
|
15 |
#include <pcl/io/pcd_io.h>
|
44 |
jakw |
16 |
#include <pcl/features/normal_3d.h>
|
9 |
jakw |
17 |
|
|
|
18 |
|
27 |
jakw |
19 |
void SMReconstructionWorker::setup(){
|
9 |
jakw |
20 |
|
27 |
jakw |
21 |
QSettings settings;
|
|
|
22 |
|
|
|
23 |
// Get current calibration
|
33 |
jakw |
24 |
calibration = settings.value("calibration/parameters").value<SMCalibrationParameters>();
|
27 |
jakw |
25 |
|
41 |
jakw |
26 |
// Create Algorithm
|
|
|
27 |
dir = (CodingDir)settings.value("pattern/direction", CodingDirHorizontal).toInt();
|
|
|
28 |
if(dir == CodingDirNone)
|
27 |
jakw |
29 |
std::cerr << "SMCaptureWorker: invalid coding direction " << std::endl;
|
|
|
30 |
|
36 |
jakw |
31 |
int resX = settings.value("projector/resX").toInt();
|
|
|
32 |
int resY = settings.value("projector/resY").toInt();
|
27 |
jakw |
33 |
QString codec = settings.value("codec", "GrayCode").toString();
|
|
|
34 |
if(codec == "PhaseShift")
|
41 |
jakw |
35 |
algorithm = new AlgorithmPhaseShift(resX, resY, dir);
|
27 |
jakw |
36 |
else if(codec == "GrayCode")
|
41 |
jakw |
37 |
algorithm = new AlgorithmGrayCode(resX, resY, dir);
|
27 |
jakw |
38 |
else
|
|
|
39 |
std::cerr << "SLScanWorker: invalid codec " << codec.toStdString() << std::endl;
|
|
|
40 |
|
|
|
41 |
|
42 |
jakw |
42 |
// // Precompute lens correction maps
|
|
|
43 |
// cv::Mat eye = cv::Mat::eye(3, 3, CV_32F);
|
|
|
44 |
// cv::initUndistortRectifyMap(calibration.K0, calibration.k0, eye, calibration.K0, cv::Size(calibration.frameWidth, calibration.frameHeight), CV_32FC1, lensMap0Horz, lensMap0Vert);
|
|
|
45 |
// cv::initUndistortRectifyMap(calibration.K0, calibration.k0, eye, calibration.K0, cv::Size(calibration.frameWidth, calibration.frameHeight), CV_32FC1, lensMap1Horz, lensMap1Vert);
|
9 |
jakw |
46 |
|
27 |
jakw |
47 |
//cv::Mat mapHorz, mapVert;
|
|
|
48 |
//cv::normalize(lensMap0Horz, mapHorz, 0, 255, cv::NORM_MINMAX, CV_8U);
|
|
|
49 |
//cv::normalize(lensMap0Vert, mapVert, 0, 255, cv::NORM_MINMAX, CV_8U);
|
|
|
50 |
//cv::imwrite("mapHorz.png", mapHorz);
|
|
|
51 |
//cv::imwrite("mapVert.png", mapVert);
|
9 |
jakw |
52 |
}
|
|
|
53 |
|
27 |
jakw |
54 |
void SMReconstructionWorker::reconstructPointCloud(SMFrameSequence frameSequence){
|
9 |
jakw |
55 |
|
|
|
56 |
time.start();
|
|
|
57 |
|
42 |
jakw |
58 |
// Get 3D Points
|
36 |
jakw |
59 |
std::vector<cv::Point3f> Q;
|
42 |
jakw |
60 |
std::vector<cv::Vec3b> color;
|
|
|
61 |
algorithm->get3DPoints(calibration, frameSequence.frames0, frameSequence.frames1, Q, color);
|
9 |
jakw |
62 |
|
|
|
63 |
// Convert point cloud to PCL format
|
44 |
jakw |
64 |
pcl::PointCloud<pcl::PointXYZRGBNormal>::Ptr pointCloudPCL(new pcl::PointCloud<pcl::PointXYZRGBNormal>);
|
9 |
jakw |
65 |
|
36 |
jakw |
66 |
pointCloudPCL->width = Q.size();
|
|
|
67 |
pointCloudPCL->height = 1;
|
9 |
jakw |
68 |
pointCloudPCL->is_dense = false;
|
|
|
69 |
|
36 |
jakw |
70 |
pointCloudPCL->points.resize(Q.size());
|
9 |
jakw |
71 |
|
36 |
jakw |
72 |
for(int i=0; i<Q.size(); i++){
|
44 |
jakw |
73 |
pcl::PointXYZRGBNormal point;
|
36 |
jakw |
74 |
point.x = Q[i].x; point.y = Q[i].y; point.z = Q[i].z;
|
42 |
jakw |
75 |
point.r = color[i][0]; point.g = color[i][1]; point.b = color[i][2];
|
36 |
jakw |
76 |
pointCloudPCL->points[i] = point;
|
27 |
jakw |
77 |
}
|
9 |
jakw |
78 |
|
44 |
jakw |
79 |
// Estimate surface normals
|
|
|
80 |
pcl::NormalEstimation<pcl::PointXYZRGBNormal, pcl::PointXYZRGBNormal> ne;
|
|
|
81 |
pcl::search::KdTree<pcl::PointXYZRGBNormal>::Ptr tree(new pcl::search::KdTree<pcl::PointXYZRGBNormal>());
|
|
|
82 |
ne.setSearchMethod(tree);
|
|
|
83 |
ne.setRadiusSearch(3);
|
|
|
84 |
ne.setViewPoint(0.0, 0.0, 0.0);
|
|
|
85 |
ne.setInputCloud(pointCloudPCL);
|
|
|
86 |
ne.compute(*pointCloudPCL);
|
|
|
87 |
|
|
|
88 |
// Assemble SMPointCloud data structure
|
42 |
jakw |
89 |
SMPointCloud smPointCloud;
|
|
|
90 |
smPointCloud.pointCloud = pointCloudPCL;
|
|
|
91 |
smPointCloud.rotationAngle = frameSequence.rotationAngle;
|
|
|
92 |
|
44 |
jakw |
93 |
// Determine transform in world (camera0) coordinate system
|
|
|
94 |
float angleRadians = frameSequence.rotationAngle/180.0*M_PI;
|
|
|
95 |
cv::Vec3f rot_rvec(0.0, -angleRadians, 0.0);
|
|
|
96 |
cv::Mat R;
|
|
|
97 |
cv::Rodrigues(rot_rvec, R);
|
|
|
98 |
smPointCloud.R = calibration.Rr.t()*cv::Matx33f(R)*calibration.Rr;
|
|
|
99 |
smPointCloud.T = calibration.Rr.t()*cv::Matx33f(R)*calibration.Tr - calibration.Rr.t()*calibration.Tr;
|
|
|
100 |
|
9 |
jakw |
101 |
// Emit result
|
42 |
jakw |
102 |
emit newPointCloud(smPointCloud);
|
9 |
jakw |
103 |
|
27 |
jakw |
104 |
std::cout << "SMReconstructionWorker: " << time.elapsed() << "ms" << std::endl;
|
9 |
jakw |
105 |
|
|
|
106 |
}
|
|
|
107 |
|
27 |
jakw |
108 |
void SMReconstructionWorker::reconstructPointClouds(std::vector<SMFrameSequence> frameSequences){
|
24 |
jakw |
109 |
|
27 |
jakw |
110 |
// Process sequentially
|
|
|
111 |
for(int i=0; i<frameSequences.size(); i++){
|
|
|
112 |
reconstructPointCloud(frameSequences[i]);
|
24 |
jakw |
113 |
}
|
|
|
114 |
|
|
|
115 |
}
|
|
|
116 |
|
41 |
jakw |
117 |
void SMReconstructionWorker::triangulate(std::vector<cv::Point2f>& q0, std::vector<cv::Point2f>& q1, std::vector<cv::Point3f> &Q){
|
24 |
jakw |
118 |
|
41 |
jakw |
119 |
cv::Mat P0(3,4,CV_32F,cv::Scalar(0.0));
|
|
|
120 |
cv::Mat(calibration.K0).copyTo(P0(cv::Range(0,3), cv::Range(0,3)));
|
|
|
121 |
|
|
|
122 |
cv::Mat temp(3,4,CV_32F);
|
|
|
123 |
cv::Mat(calibration.R1).copyTo(temp(cv::Range(0,3), cv::Range(0,3)));
|
|
|
124 |
cv::Mat(calibration.T1).copyTo(temp(cv::Range(0,3), cv::Range(3,4)));
|
|
|
125 |
cv::Mat P1 = cv::Mat(calibration.K1) * temp;
|
|
|
126 |
|
42 |
jakw |
127 |
cv::Mat QMatHomogenous, QMat;
|
|
|
128 |
cv::triangulatePoints(P0, P1, q0, q1, QMatHomogenous);
|
|
|
129 |
cvtools::convertMatFromHomogeneous(QMatHomogenous, QMat);
|
|
|
130 |
cvtools::matToPoints3f(QMat, Q);
|
41 |
jakw |
131 |
|
42 |
jakw |
132 |
|
41 |
jakw |
133 |
}
|
|
|
134 |
|
27 |
jakw |
135 |
//void SMReconstructionWorker::triangulateFromUpVp(cv::Mat &up, cv::Mat &vp, cv::Mat &xyz){
|
24 |
jakw |
136 |
|
25 |
jakw |
137 |
// std::cerr << "WARNING! NOT FULLY IMPLEMENTED!" << std::endl;
|
|
|
138 |
// int N = up.rows * up.cols;
|
24 |
jakw |
139 |
|
25 |
jakw |
140 |
// cv::Mat projPointsCam(2, N, CV_32F);
|
|
|
141 |
// uc.reshape(0,1).copyTo(projPointsCam.row(0));
|
|
|
142 |
// vc.reshape(0,1).copyTo(projPointsCam.row(1));
|
24 |
jakw |
143 |
|
25 |
jakw |
144 |
// cv::Mat projPointsProj(2, N, CV_32F);
|
|
|
145 |
// up.reshape(0,1).copyTo(projPointsProj.row(0));
|
|
|
146 |
// vp.reshape(0,1).copyTo(projPointsProj.row(1));
|
24 |
jakw |
147 |
|
25 |
jakw |
148 |
// cv::Mat Pc(3,4,CV_32F,cv::Scalar(0.0));
|
|
|
149 |
// cv::Mat(calibration.Kc).copyTo(Pc(cv::Range(0,3), cv::Range(0,3)));
|
24 |
jakw |
150 |
|
25 |
jakw |
151 |
// cv::Mat Pp(3,4,CV_32F), temp(3,4,CV_32F);
|
|
|
152 |
// cv::Mat(calibration.Rp).copyTo(temp(cv::Range(0,3), cv::Range(0,3)));
|
|
|
153 |
// cv::Mat(calibration.Tp).copyTo(temp(cv::Range(0,3), cv::Range(3,4)));
|
|
|
154 |
// Pp = cv::Mat(calibration.Kp) * temp;
|
24 |
jakw |
155 |
|
25 |
jakw |
156 |
// cv::Mat xyzw;
|
|
|
157 |
// cv::triangulatePoints(Pc, Pp, projPointsCam, projPointsProj, xyzw);
|
24 |
jakw |
158 |
|
25 |
jakw |
159 |
// xyz.create(3, N, CV_32F);
|
|
|
160 |
// for(int i=0; i<N; i++){
|
|
|
161 |
// xyz.at<float>(0,i) = xyzw.at<float>(0,i)/xyzw.at<float>(3,i);
|
|
|
162 |
// xyz.at<float>(1,i) = xyzw.at<float>(1,i)/xyzw.at<float>(3,i);
|
|
|
163 |
// xyz.at<float>(2,i) = xyzw.at<float>(2,i)/xyzw.at<float>(3,i);
|
|
|
164 |
// }
|
24 |
jakw |
165 |
|
25 |
jakw |
166 |
// xyz = xyz.t();
|
|
|
167 |
// xyz = xyz.reshape(3, up.rows);
|
27 |
jakw |
168 |
//}
|
24 |
jakw |
169 |
|