Subversion Repositories seema-scanner

Rev

Rev 236 | Rev 245 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

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