Subversion Repositories seema-scanner

Rev

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

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