Subversion Repositories seema-scanner

Rev

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