Subversion Repositories seema-scanner

Rev

Rev 137 | Rev 167 | 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
 
128 jakw 78
    // Estimate surface normals
79
    pcl::NormalEstimation<pcl::PointXYZRGBNormal, pcl::PointXYZRGBNormal> ne;
80
    pcl::PointCloud<pcl::PointXYZRGBNormal>::Ptr pointCloudPCLCopy(new pcl::PointCloud<pcl::PointXYZRGBNormal>);
81
    pcl::copyPointCloud(*pointCloudPCL, *pointCloudPCLCopy);
82
    //ne.setKSearch(10);
83
    ne.setRadiusSearch(0.5);
84
    ne.setViewPoint(0.0, 0.0, 0.0);
85
    ne.setInputCloud(pointCloudPCLCopy);
86
    ne.compute(*pointCloudPCL);
44 jakw 87
 
88
    // Assemble SMPointCloud data structure
42 jakw 89
    SMPointCloud smPointCloud;
45 jakw 90
    smPointCloud.id = frameSequence.id;
42 jakw 91
    smPointCloud.pointCloud = pointCloudPCL;
92
    smPointCloud.rotationAngle = frameSequence.rotationAngle;
93
 
44 jakw 94
    // Determine transform in world (camera0) coordinate system
95
    float angleRadians = frameSequence.rotationAngle/180.0*M_PI;
96
    cv::Vec3f rot_rvec(0.0, -angleRadians, 0.0);
97
    cv::Mat R;
98
    cv::Rodrigues(rot_rvec, R);
99
    smPointCloud.R = calibration.Rr.t()*cv::Matx33f(R)*calibration.Rr;
100
    smPointCloud.T = calibration.Rr.t()*cv::Matx33f(R)*calibration.Tr - calibration.Rr.t()*calibration.Tr;
101
 
9 jakw 102
    // Emit result
42 jakw 103
    emit newPointCloud(smPointCloud);
9 jakw 104
 
27 jakw 105
    std::cout << "SMReconstructionWorker: " << time.elapsed() << "ms" << std::endl;
9 jakw 106
 
107
}
108
 
27 jakw 109
void SMReconstructionWorker::reconstructPointClouds(std::vector<SMFrameSequence> frameSequences){
24 jakw 110
 
27 jakw 111
    // Process sequentially
112
    for(int i=0; i<frameSequences.size(); i++){
113
        reconstructPointCloud(frameSequences[i]);
24 jakw 114
    }
115
 
116
}
117
 
41 jakw 118
void SMReconstructionWorker::triangulate(std::vector<cv::Point2f>& q0, std::vector<cv::Point2f>& q1, std::vector<cv::Point3f> &Q){
24 jakw 119
 
41 jakw 120
    cv::Mat P0(3,4,CV_32F,cv::Scalar(0.0));
121
    cv::Mat(calibration.K0).copyTo(P0(cv::Range(0,3), cv::Range(0,3)));
122
 
123
    cv::Mat temp(3,4,CV_32F);
124
    cv::Mat(calibration.R1).copyTo(temp(cv::Range(0,3), cv::Range(0,3)));
125
    cv::Mat(calibration.T1).copyTo(temp(cv::Range(0,3), cv::Range(3,4)));
126
    cv::Mat P1 = cv::Mat(calibration.K1) * temp;
127
 
42 jakw 128
    cv::Mat QMatHomogenous, QMat;
129
    cv::triangulatePoints(P0, P1, q0, q1, QMatHomogenous);
130
    cvtools::convertMatFromHomogeneous(QMatHomogenous, QMat);
131
    cvtools::matToPoints3f(QMat, Q);
41 jakw 132
 
42 jakw 133
 
41 jakw 134
}