Subversion Repositories seema-scanner

Rev

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