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 |
|
|
|
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 |
|
41 |
jakw |
23 |
// Create Algorithm
|
|
|
24 |
dir = (CodingDir)settings.value("pattern/direction", CodingDirHorizontal).toInt();
|
|
|
25 |
if(dir == CodingDirNone)
|
27 |
jakw |
26 |
std::cerr << "SMCaptureWorker: invalid coding direction " << std::endl;
|
|
|
27 |
|
36 |
jakw |
28 |
int resX = settings.value("projector/resX").toInt();
|
|
|
29 |
int resY = settings.value("projector/resY").toInt();
|
27 |
jakw |
30 |
QString codec = settings.value("codec", "GrayCode").toString();
|
|
|
31 |
if(codec == "PhaseShift")
|
41 |
jakw |
32 |
algorithm = new AlgorithmPhaseShift(resX, resY, dir);
|
27 |
jakw |
33 |
else if(codec == "GrayCode")
|
41 |
jakw |
34 |
algorithm = new AlgorithmGrayCode(resX, resY, dir);
|
27 |
jakw |
35 |
else
|
|
|
36 |
std::cerr << "SLScanWorker: invalid codec " << codec.toStdString() << std::endl;
|
|
|
37 |
|
|
|
38 |
|
25 |
jakw |
39 |
// Precompute lens correction maps
|
|
|
40 |
cv::Mat eye = cv::Mat::eye(3, 3, CV_32F);
|
27 |
jakw |
41 |
cv::initUndistortRectifyMap(calibration.K0, calibration.k0, eye, calibration.K0, cv::Size(calibration.frameWidth, calibration.frameHeight), CV_32FC1, lensMap0Horz, lensMap0Vert);
|
|
|
42 |
cv::initUndistortRectifyMap(calibration.K0, calibration.k0, eye, calibration.K0, cv::Size(calibration.frameWidth, calibration.frameHeight), CV_32FC1, lensMap1Horz, lensMap1Vert);
|
9 |
jakw |
43 |
|
27 |
jakw |
44 |
//cv::Mat mapHorz, mapVert;
|
|
|
45 |
//cv::normalize(lensMap0Horz, mapHorz, 0, 255, cv::NORM_MINMAX, CV_8U);
|
|
|
46 |
//cv::normalize(lensMap0Vert, mapVert, 0, 255, cv::NORM_MINMAX, CV_8U);
|
|
|
47 |
//cv::imwrite("mapHorz.png", mapHorz);
|
|
|
48 |
//cv::imwrite("mapVert.png", mapVert);
|
9 |
jakw |
49 |
}
|
|
|
50 |
|
27 |
jakw |
51 |
void SMReconstructionWorker::reconstructPointCloud(SMFrameSequence frameSequence){
|
9 |
jakw |
52 |
|
|
|
53 |
time.start();
|
|
|
54 |
|
36 |
jakw |
55 |
// Get correspondences
|
|
|
56 |
std::vector<cv::Point2f> q0, q1;
|
|
|
57 |
std::vector<cv::Point3f> color;
|
41 |
jakw |
58 |
algorithm->getCorrespondences(calibration, frameSequence.frames0, frameSequence.frames0, q0, q1, color);
|
27 |
jakw |
59 |
|
|
|
60 |
// Triangulate
|
36 |
jakw |
61 |
std::vector<cv::Point3f> Q;
|
|
|
62 |
triangulate(q0, q1, Q);
|
9 |
jakw |
63 |
|
|
|
64 |
// Convert point cloud to PCL format
|
|
|
65 |
pcl::PointCloud<pcl::PointXYZRGB>::Ptr pointCloudPCL(new pcl::PointCloud<pcl::PointXYZRGB>);
|
|
|
66 |
|
|
|
67 |
// Interprete as organized point cloud
|
36 |
jakw |
68 |
pointCloudPCL->width = Q.size();
|
|
|
69 |
pointCloudPCL->height = 1;
|
9 |
jakw |
70 |
pointCloudPCL->is_dense = false;
|
|
|
71 |
|
36 |
jakw |
72 |
pointCloudPCL->points.resize(Q.size());
|
9 |
jakw |
73 |
|
36 |
jakw |
74 |
for(int i=0; i<Q.size(); i++){
|
|
|
75 |
pcl::PointXYZRGB point;
|
|
|
76 |
point.x = Q[i].x; point.y = Q[i].y; point.z = Q[i].z;
|
|
|
77 |
point.r = color[i].x; point.g = color[i].y; point.b = color[i].z;
|
|
|
78 |
pointCloudPCL->points[i] = point;
|
27 |
jakw |
79 |
}
|
9 |
jakw |
80 |
|
|
|
81 |
// Emit result
|
|
|
82 |
emit newPointCloud(pointCloudPCL);
|
|
|
83 |
|
27 |
jakw |
84 |
std::cout << "SMReconstructionWorker: " << time.elapsed() << "ms" << std::endl;
|
9 |
jakw |
85 |
|
|
|
86 |
}
|
|
|
87 |
|
27 |
jakw |
88 |
void SMReconstructionWorker::reconstructPointClouds(std::vector<SMFrameSequence> frameSequences){
|
24 |
jakw |
89 |
|
27 |
jakw |
90 |
// Process sequentially
|
|
|
91 |
for(int i=0; i<frameSequences.size(); i++){
|
|
|
92 |
reconstructPointCloud(frameSequences[i]);
|
24 |
jakw |
93 |
}
|
|
|
94 |
|
|
|
95 |
}
|
|
|
96 |
|
41 |
jakw |
97 |
void SMReconstructionWorker::triangulate(std::vector<cv::Point2f>& q0, std::vector<cv::Point2f>& q1, std::vector<cv::Point3f> &Q){
|
24 |
jakw |
98 |
|
41 |
jakw |
99 |
cv::Mat P0(3,4,CV_32F,cv::Scalar(0.0));
|
|
|
100 |
cv::Mat(calibration.K0).copyTo(P0(cv::Range(0,3), cv::Range(0,3)));
|
|
|
101 |
|
|
|
102 |
cv::Mat temp(3,4,CV_32F);
|
|
|
103 |
cv::Mat(calibration.R1).copyTo(temp(cv::Range(0,3), cv::Range(0,3)));
|
|
|
104 |
cv::Mat(calibration.T1).copyTo(temp(cv::Range(0,3), cv::Range(3,4)));
|
|
|
105 |
cv::Mat P1 = cv::Mat(calibration.K1) * temp;
|
|
|
106 |
|
|
|
107 |
cv::triangulatePoints(P0, P1, q0, q1, Q);
|
|
|
108 |
|
|
|
109 |
}
|
|
|
110 |
|
27 |
jakw |
111 |
//void SMReconstructionWorker::triangulateFromUpVp(cv::Mat &up, cv::Mat &vp, cv::Mat &xyz){
|
24 |
jakw |
112 |
|
25 |
jakw |
113 |
// std::cerr << "WARNING! NOT FULLY IMPLEMENTED!" << std::endl;
|
|
|
114 |
// int N = up.rows * up.cols;
|
24 |
jakw |
115 |
|
25 |
jakw |
116 |
// cv::Mat projPointsCam(2, N, CV_32F);
|
|
|
117 |
// uc.reshape(0,1).copyTo(projPointsCam.row(0));
|
|
|
118 |
// vc.reshape(0,1).copyTo(projPointsCam.row(1));
|
24 |
jakw |
119 |
|
25 |
jakw |
120 |
// cv::Mat projPointsProj(2, N, CV_32F);
|
|
|
121 |
// up.reshape(0,1).copyTo(projPointsProj.row(0));
|
|
|
122 |
// vp.reshape(0,1).copyTo(projPointsProj.row(1));
|
24 |
jakw |
123 |
|
25 |
jakw |
124 |
// cv::Mat Pc(3,4,CV_32F,cv::Scalar(0.0));
|
|
|
125 |
// cv::Mat(calibration.Kc).copyTo(Pc(cv::Range(0,3), cv::Range(0,3)));
|
24 |
jakw |
126 |
|
25 |
jakw |
127 |
// cv::Mat Pp(3,4,CV_32F), temp(3,4,CV_32F);
|
|
|
128 |
// cv::Mat(calibration.Rp).copyTo(temp(cv::Range(0,3), cv::Range(0,3)));
|
|
|
129 |
// cv::Mat(calibration.Tp).copyTo(temp(cv::Range(0,3), cv::Range(3,4)));
|
|
|
130 |
// Pp = cv::Mat(calibration.Kp) * temp;
|
24 |
jakw |
131 |
|
25 |
jakw |
132 |
// cv::Mat xyzw;
|
|
|
133 |
// cv::triangulatePoints(Pc, Pp, projPointsCam, projPointsProj, xyzw);
|
24 |
jakw |
134 |
|
25 |
jakw |
135 |
// xyz.create(3, N, CV_32F);
|
|
|
136 |
// for(int i=0; i<N; i++){
|
|
|
137 |
// xyz.at<float>(0,i) = xyzw.at<float>(0,i)/xyzw.at<float>(3,i);
|
|
|
138 |
// xyz.at<float>(1,i) = xyzw.at<float>(1,i)/xyzw.at<float>(3,i);
|
|
|
139 |
// xyz.at<float>(2,i) = xyzw.at<float>(2,i)/xyzw.at<float>(3,i);
|
|
|
140 |
// }
|
24 |
jakw |
141 |
|
25 |
jakw |
142 |
// xyz = xyz.t();
|
|
|
143 |
// xyz = xyz.reshape(3, up.rows);
|
27 |
jakw |
144 |
//}
|
24 |
jakw |
145 |
|