Subversion Repositories seema-scanner

Rev

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