Subversion Repositories seema-scanner

Rev

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