Subversion Repositories seema-scanner

Rev

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