Subversion Repositories seema-scanner

Rev

Rev 208 | Rev 229 | Go to most recent revision | Only display areas with differences | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 208 Rev 225
1
#include "SMReconstructionWorker.h"
1
#include "SMReconstructionWorker.h"
2
 
2
 
3
#include "AlgorithmGrayCode.h"
3
#include "AlgorithmGrayCode.h"
4
#include "AlgorithmGrayCodeHorzVert.h"
4
#include "AlgorithmGrayCodeHorzVert.h"
5
#include "AlgorithmPhaseShiftTwoFreq.h"
5
#include "AlgorithmPhaseShiftTwoFreq.h"
6
#include "AlgorithmPhaseShiftTwoFreqHorzVert.h"
6
#include "AlgorithmPhaseShiftTwoFreqHorzVert.h"
7
#include "AlgorithmPhaseShiftThreeFreq.h"
7
#include "AlgorithmPhaseShiftThreeFreq.h"
8
#include "AlgorithmPhaseShiftEmbedded.h"
8
#include "AlgorithmPhaseShiftEmbedded.h"
9
#include "AlgorithmLineShift.h"
9
#include "AlgorithmLineShift.h"
10
 
10
 
11
#include <QCoreApplication>
11
#include <QCoreApplication>
12
#include <QSettings>
12
#include <QSettings>
13
 
13
 
14
#include <iostream>
14
#include <iostream>
15
#include <opencv2/opencv.hpp>
15
#include <opencv2/opencv.hpp>
16
 
16
 
17
#include "cvtools.h"
17
#include "cvtools.h"
18
#include <opencv2/core/eigen.hpp>
18
#include <opencv2/core/eigen.hpp>
19
 
19
 
20
#include <pcl/filters/statistical_outlier_removal.h>
20
#include <pcl/filters/statistical_outlier_removal.h>
21
#include <pcl/io/pcd_io.h>
21
#include <pcl/io/pcd_io.h>
22
#include <pcl/features/normal_3d.h>
22
#include <pcl/features/normal_3d.h>
23
#include <pcl/features/normal_3d_omp.h>
23
#include <pcl/features/normal_3d_omp.h>
24
#include <pcl/common/transforms.h>
24
#include <pcl/common/transforms.h>
25
 
25
 
26
 
26
 
27
void SMReconstructionWorker::setup(){
-
 
28
}
-
 
29
 
-
 
30
void SMReconstructionWorker::reconstructPointCloud(const SMFrameSequence &frameSequence){
27
void SMReconstructionWorker::reconstructPointCloud(const SMFrameSequence &frameSequence){
31
 
28
 
32
    QSettings settings;
29
    QSettings settings;
33
 
30
 
34
    // Get current calibration
31
    // Get current calibration
35
    calibration = settings.value("calibration/parameters").value<SMCalibrationParameters>();
32
    calibration = settings.value("calibration/parameters").value<SMCalibrationParameters>();
36
 
33
 
37
    // Create Algorithm
34
    // Create Algorithm
38
    QString codec = frameSequence.codec;
35
    QString codec = frameSequence.codec;
39
    int resX = settings.value("projector/resX").toInt();
36
    int resX = settings.value("projector/resX").toInt();
40
    int resY = settings.value("projector/resY").toInt();
37
    int resY = settings.value("projector/resY").toInt();
41
 
38
 
42
    if(codec == "GrayCode")
39
    if(codec == "GrayCode")
43
        algorithm = new AlgorithmGrayCode(resX, resY);
40
        algorithm = new AlgorithmGrayCode(resX, resY);
44
    else if(codec == "GrayCodeHorzVert")
41
    else if(codec == "GrayCodeHorzVert")
45
        algorithm = new AlgorithmGrayCodeHorzVert(resX, resY);
42
        algorithm = new AlgorithmGrayCodeHorzVert(resX, resY);
46
    else if(codec == "PhaseShiftTwoFreq")
43
    else if(codec == "PhaseShiftTwoFreq")
47
        algorithm = new AlgorithmPhaseShiftTwoFreq(resX, resY);
44
        algorithm = new AlgorithmPhaseShiftTwoFreq(resX, resY);
48
    else if(codec == "PhaseShiftTwoFreqHorzVert")
45
    else if(codec == "PhaseShiftTwoFreqHorzVert")
49
        algorithm = new AlgorithmPhaseShiftTwoFreqHorzVert(resX, resY);
46
        algorithm = new AlgorithmPhaseShiftTwoFreqHorzVert(resX, resY);
50
    else if(codec == "PhaseShiftThreeFreq")
47
    else if(codec == "PhaseShiftThreeFreq")
51
        algorithm = new AlgorithmPhaseShiftThreeFreq(resX, resY);
48
        algorithm = new AlgorithmPhaseShiftThreeFreq(resX, resY);
52
    else if(codec == "PhaseShiftEmbedded")
49
    else if(codec == "PhaseShiftEmbedded")
53
        algorithm = new AlgorithmPhaseShiftEmbedded(resX, resY);
50
        algorithm = new AlgorithmPhaseShiftEmbedded(resX, resY);
54
    else if(codec == "LineShift")
51
    else if(codec == "LineShift")
55
        algorithm = new AlgorithmLineShift(resX, resY);
52
        algorithm = new AlgorithmLineShift(resX, resY);
56
    else{
53
    else{
57
        std::cerr << "SLScanWorker: invalid codec (Please set codec in preferences): " << codec.toStdString() << std::endl;
54
        std::cerr << "SLScanWorker: invalid codec (Please set codec in preferences): " << codec.toStdString() << std::endl;
58
        return; // otherwise segfault TODO no default?
55
        return; // otherwise segfault TODO no default?
59
    }
56
    }
60
 
57
 
-
 
58
    assert(frameSequence.frames0.size() == algorithm->getNPatterns());
-
 
59
    assert(frameSequence.frames1.size() == algorithm->getNPatterns());
-
 
60
 
61
    time.start();
61
    time.start();
62
 
62
 
63
    // Get 3D Points
63
    // Get 3D Points
64
    std::vector<cv::Point3f> Q;
64
    std::vector<cv::Point3f> Q;
65
    std::vector<cv::Vec3b> color;
65
    std::vector<cv::Vec3b> color;
66
    algorithm->get3DPoints(calibration, frameSequence.frames0, frameSequence.frames1, Q, color);
66
    algorithm->get3DPoints(calibration, frameSequence.frames0, frameSequence.frames1, Q, color);
67
 
67
 
68
    // Convert point cloud to PCL format
68
    // Convert point cloud to PCL format
69
    pcl::PointCloud<pcl::PointXYZRGBNormal>::Ptr pointCloudPCL(new pcl::PointCloud<pcl::PointXYZRGBNormal>);
69
    pcl::PointCloud<pcl::PointXYZRGBNormal>::Ptr pointCloudPCL(new pcl::PointCloud<pcl::PointXYZRGBNormal>);
70
 
70
 
71
    pointCloudPCL->width = Q.size();
71
    pointCloudPCL->width = Q.size();
72
    pointCloudPCL->height = 1;
72
    pointCloudPCL->height = 1;
73
    pointCloudPCL->is_dense = true;
73
    pointCloudPCL->is_dense = true;
74
 
74
 
75
    pointCloudPCL->points.resize(Q.size());
75
    pointCloudPCL->points.resize(Q.size());
76
 
76
 
77
    for(unsigned int i=0; i<Q.size(); i++){
77
    for(unsigned int i=0; i<Q.size(); i++){
78
        pcl::PointXYZRGBNormal point;
78
        pcl::PointXYZRGBNormal point;
79
        point.x = Q[i].x; point.y = Q[i].y; point.z = Q[i].z;
79
        point.x = Q[i].x; point.y = Q[i].y; point.z = Q[i].z;
80
        point.r = color[i][0]; point.g = color[i][1]; point.b = color[i][2];
80
        point.r = color[i][0]; point.g = color[i][1]; point.b = color[i][2];
81
        pointCloudPCL->points[i] = point;
81
        pointCloudPCL->points[i] = point;
82
    }
82
    }
83
 
83
 
84
    // Transform point cloud to rotation axis coordinate system
84
    // Transform point cloud to rotation axis coordinate system
85
    /*cv::Mat TRCV(3, 4, CV_32F);
85
    /*cv::Mat TRCV(3, 4, CV_32F);
86
    cv::Mat(calibration.Rr).copyTo(TRCV.colRange(0, 3));
86
    cv::Mat(calibration.Rr).copyTo(TRCV.colRange(0, 3));
87
    cv::Mat(calibration.Tr).copyTo(TRCV.col(3));
87
    cv::Mat(calibration.Tr).copyTo(TRCV.col(3));
88
    Eigen::Affine3f TR;
88
    Eigen::Affine3f TR;
89
    cv::cv2eigen(TRCV, TR.matrix());
89
    cv::cv2eigen(TRCV, TR.matrix());
90
    pcl::transformPointCloud(*pointCloudPCL, *pointCloudPCL, TR);
90
    pcl::transformPointCloud(*pointCloudPCL, *pointCloudPCL, TR);
91
 
91
 
92
    // Estimate surface normals (does not produce proper normals...)
92
    // Estimate surface normals (does not produce proper normals...)
93
    std::cout << "Estimating normals..." << std::endl;
93
    std::cout << "Estimating normals..." << std::endl;
94
    pcl::PointCloud<pcl::PointXYZ>::Ptr points(new pcl::PointCloud<pcl::PointXYZ>);
94
    pcl::PointCloud<pcl::PointXYZ>::Ptr points(new pcl::PointCloud<pcl::PointXYZ>);
95
    pcl::copyPointCloud(*pointCloudPCL, *points);
95
    pcl::copyPointCloud(*pointCloudPCL, *points);
96
    pcl::PointCloud<pcl::Normal>::Ptr normals(new pcl::PointCloud<pcl::Normal>);
96
    pcl::PointCloud<pcl::Normal>::Ptr normals(new pcl::PointCloud<pcl::Normal>);
97
    pcl::NormalEstimationOMP<pcl::PointXYZ, pcl::Normal> ne;
97
    pcl::NormalEstimationOMP<pcl::PointXYZ, pcl::Normal> ne;
98
    pcl::search::KdTree<pcl::PointXYZ>::Ptr tree (new pcl::search::KdTree<pcl::PointXYZ>());
98
    pcl::search::KdTree<pcl::PointXYZ>::Ptr tree (new pcl::search::KdTree<pcl::PointXYZ>());
99
    tree->setInputCloud(points);
99
    tree->setInputCloud(points);
100
    ne.setSearchMethod(tree);
100
    ne.setSearchMethod(tree);
101
    ne.setRadiusSearch(1.0);
101
    ne.setRadiusSearch(1.0);
102
    //ne.setKSearch(50);
102
    //ne.setKSearch(50);
103
    ne.setViewPoint(0.0, 0.0, 0.0);
103
    ne.setViewPoint(0.0, 0.0, 0.0);
104
    ne.setInputCloud(points);
104
    ne.setInputCloud(points);
105
    ne.compute(*normals);
105
    ne.compute(*normals);
106
    pcl::copyPointCloud(*normals, *pointCloudPCL);*/
106
    pcl::copyPointCloud(*normals, *pointCloudPCL);*/
107
 
107
 
108
    // Assemble SMPointCloud data structure
108
    // Assemble SMPointCloud data structure
109
    SMPointCloud smPointCloud;
109
    SMPointCloud smPointCloud;
110
    smPointCloud.id = frameSequence.id;
110
    smPointCloud.id = frameSequence.id;
111
    smPointCloud.pointCloud = pointCloudPCL;
111
    smPointCloud.pointCloud = pointCloudPCL;
112
    smPointCloud.rotationAngle = frameSequence.rotationAngle;
112
    smPointCloud.rotationAngle = frameSequence.rotationAngle;
113
 
113
 
114
    // Determine transform in world (camera0) coordinate system
114
    // Determine transform in world (camera0) coordinate system
115
    float angleRadians = frameSequence.rotationAngle/180.0*M_PI;
115
    float angleRadians = frameSequence.rotationAngle/180.0*M_PI;
116
    cv::Vec3f rot_rvec(0.0, -angleRadians, 0.0);
116
    cv::Vec3f rot_rvec(0.0, -angleRadians, 0.0);
117
    cv::Mat R;
117
    cv::Mat R;
118
    cv::Rodrigues(rot_rvec, R);
118
    cv::Rodrigues(rot_rvec, R);
119
    smPointCloud.R = calibration.Rr.t()*cv::Matx33f(R)*calibration.Rr;
119
    smPointCloud.R = calibration.Rr.t()*cv::Matx33f(R)*calibration.Rr;
120
    smPointCloud.T = calibration.Rr.t()*cv::Matx33f(R)*calibration.Tr - calibration.Rr.t()*calibration.Tr;
120
    smPointCloud.T = calibration.Rr.t()*cv::Matx33f(R)*calibration.Tr - calibration.Rr.t()*calibration.Tr;
121
 
121
 
122
 
122
 
123
    // Determine transform in world (camera0) coordinate system
123
    // Determine transform in world (camera0) coordinate system
124
    /*float angleRadians = frameSequence.rotationAngle/180.0*M_PI;
124
    /*float angleRadians = frameSequence.rotationAngle/180.0*M_PI;
125
    cv::Vec3f rot_rvec(0.0, -angleRadians, 0.0);
125
    cv::Vec3f rot_rvec(0.0, -angleRadians, 0.0);
126
    cv::Mat R;
126
    cv::Mat R;
127
    cv::Rodrigues(rot_rvec, R);
127
    cv::Rodrigues(rot_rvec, R);
128
    smPointCloud.R = cv::Matx33f(R);
128
    smPointCloud.R = cv::Matx33f(R);
129
    smPointCloud.T = cv::Vec3f(0.0,0.0,0.0);*/
129
    smPointCloud.T = cv::Vec3f(0.0,0.0,0.0);*/
130
 
130
 
131
    // Emit result
131
    // Emit result
132
    #pragma omp critical (CBRECOupdateUI1)
-
 
133
    {
-
 
134
        emit newPointCloud(smPointCloud);
132
    emit newPointCloud(smPointCloud);
135
        std::cout << "SMReconstructionWorker: " << time.elapsed() << "ms" << std::endl;
133
    std::cout << "SMReconstructionWorker: " << time.elapsed() << "ms" << std::endl;
136
        std::cout << "SMReconstructionWorker: " << smPointCloud.pointCloud->size() << " Points" << std::endl;
134
    std::cout << "SMReconstructionWorker: " << smPointCloud.pointCloud->size() << " Points" << std::endl;
137
    }
135
 
138
}
136
}
139
 
137
 
140
void SMReconstructionWorker::reconstructPointClouds(const std::vector<SMFrameSequence> &frameSequences){
138
void SMReconstructionWorker::reconstructPointClouds(const std::vector<SMFrameSequence> &frameSequences){
-
 
139
 
141
    // Process sequentially
140
    // Process sequentially
142
    #pragma omp parallel for
141
    #pragma omp parallel for
143
    for(unsigned int i=0; i<frameSequences.size(); i++){
142
    for(unsigned int i=0; i<frameSequences.size(); i++){
144
        if(!frameSequences[i].reconstructed) reconstructPointCloud(frameSequences[i]);
143
        if(!frameSequences[i].reconstructed) reconstructPointCloud(frameSequences[i]);
145
    }
144
    }
146
}
145
}
147
 
146
 
148
void SMReconstructionWorker::triangulate(const std::vector<cv::Point2f>& q0, const std::vector<cv::Point2f>& q1, std::vector<cv::Point3f> &Q){
147
void SMReconstructionWorker::triangulate(const std::vector<cv::Point2f>& q0, const std::vector<cv::Point2f>& q1, std::vector<cv::Point3f> &Q){
149
    cv::Mat P0(3,4,CV_32F,cv::Scalar(0.0));
148
    cv::Mat P0(3,4,CV_32F,cv::Scalar(0.0));
150
    cv::Mat(calibration.K0).copyTo(P0(cv::Range(0,3), cv::Range(0,3)));
149
    cv::Mat(calibration.K0).copyTo(P0(cv::Range(0,3), cv::Range(0,3)));
151
 
150
 
152
    cv::Mat temp(3,4,CV_32F);
151
    cv::Mat temp(3,4,CV_32F);
153
    cv::Mat(calibration.R1).copyTo(temp(cv::Range(0,3), cv::Range(0,3)));
152
    cv::Mat(calibration.R1).copyTo(temp(cv::Range(0,3), cv::Range(0,3)));
154
    cv::Mat(calibration.T1).copyTo(temp(cv::Range(0,3), cv::Range(3,4)));
153
    cv::Mat(calibration.T1).copyTo(temp(cv::Range(0,3), cv::Range(3,4)));
155
    cv::Mat P1 = cv::Mat(calibration.K1) * temp;
154
    cv::Mat P1 = cv::Mat(calibration.K1) * temp;
156
 
155
 
157
    cv::Mat QMatHomogenous, QMat;
156
    cv::Mat QMatHomogenous, QMat;
158
    cv::triangulatePoints(P0, P1, q0, q1, QMatHomogenous);
157
    cv::triangulatePoints(P0, P1, q0, q1, QMatHomogenous);
159
    cvtools::convertMatFromHomogeneous(QMatHomogenous, QMat);
158
    cvtools::convertMatFromHomogeneous(QMatHomogenous, QMat);
160
    cvtools::matToPoints3f(QMat, Q);
159
    cvtools::matToPoints3f(QMat, Q);
161
}
160
}
162
 
161