Subversion Repositories seema-scanner

Rev

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