Subversion Repositories seema-scanner

Rev

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