Subversion Repositories seema-scanner

Rev

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