Subversion Repositories seema-scanner

Rev

Rev 44 | Rev 70 | 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
27
    dir = (CodingDir)settings.value("pattern/direction", CodingDirHorizontal).toInt();
28
    if(dir == CodingDirNone)
27 jakw 29
        std::cerr << "SMCaptureWorker: invalid coding direction " << std::endl;
30
 
36 jakw 31
    int resX = settings.value("projector/resX").toInt();
32
    int resY = settings.value("projector/resY").toInt();
27 jakw 33
    QString codec = settings.value("codec", "GrayCode").toString();
34
    if(codec == "PhaseShift")
41 jakw 35
        algorithm = new AlgorithmPhaseShift(resX, resY, dir);
27 jakw 36
    else if(codec == "GrayCode")
41 jakw 37
        algorithm = new AlgorithmGrayCode(resX, resY, dir);
27 jakw 38
    else
39
        std::cerr << "SLScanWorker: invalid codec " << codec.toStdString() << std::endl;
40
 
41
 
42 jakw 42
//    // Precompute lens correction maps
43
//    cv::Mat eye = cv::Mat::eye(3, 3, CV_32F);
44
//    cv::initUndistortRectifyMap(calibration.K0, calibration.k0, eye, calibration.K0, cv::Size(calibration.frameWidth, calibration.frameHeight), CV_32FC1, lensMap0Horz, lensMap0Vert);
45
//    cv::initUndistortRectifyMap(calibration.K0, calibration.k0, eye, calibration.K0, cv::Size(calibration.frameWidth, calibration.frameHeight), CV_32FC1, lensMap1Horz, lensMap1Vert);
9 jakw 46
 
27 jakw 47
    //cv::Mat mapHorz, mapVert;
48
    //cv::normalize(lensMap0Horz, mapHorz, 0, 255, cv::NORM_MINMAX, CV_8U);
49
    //cv::normalize(lensMap0Vert, mapVert, 0, 255, cv::NORM_MINMAX, CV_8U);
50
    //cv::imwrite("mapHorz.png", mapHorz);
51
    //cv::imwrite("mapVert.png", mapVert);
9 jakw 52
}
53
 
27 jakw 54
void SMReconstructionWorker::reconstructPointCloud(SMFrameSequence frameSequence){
9 jakw 55
 
56
    time.start();
57
 
42 jakw 58
    // Get 3D Points
36 jakw 59
    std::vector<cv::Point3f> Q;
42 jakw 60
    std::vector<cv::Vec3b> color;
61
    algorithm->get3DPoints(calibration, frameSequence.frames0, frameSequence.frames1, Q, color);
9 jakw 62
 
63
    // Convert point cloud to PCL format
45 jakw 64
    pcl::PointCloud<pcl::PointXYZRGB>::Ptr pointCloudPCL(new pcl::PointCloud<pcl::PointXYZRGB>);
9 jakw 65
 
36 jakw 66
    pointCloudPCL->width = Q.size();
67
    pointCloudPCL->height = 1;
9 jakw 68
    pointCloudPCL->is_dense = false;
69
 
36 jakw 70
    pointCloudPCL->points.resize(Q.size());
9 jakw 71
 
36 jakw 72
    for(int i=0; i<Q.size(); i++){
45 jakw 73
        pcl::PointXYZRGB point;
36 jakw 74
        point.x = Q[i].x; point.y = Q[i].y; point.z = Q[i].z;
42 jakw 75
        point.r = color[i][0]; point.g = color[i][1]; point.b = color[i][2];
36 jakw 76
        pointCloudPCL->points[i] = point;
27 jakw 77
    }
9 jakw 78
 
45 jakw 79
//    // Estimate surface normals
80
//    pcl::NormalEstimation<pcl::PointXYZRGB, pcl::PointXYZRGBNormal> ne;
81
//    pcl::search::KdTree<pcl::PointXYZRGB>::Ptr tree(new pcl::search::KdTree<pcl::PointXYZRGB>());
82
//    ne.setSearchMethod(tree);
83
//    ne.setRadiusSearch(3);
84
//    ne.setViewPoint(0.0, 0.0, 0.0);
85
//    ne.setInputCloud(pointCloudPCL);
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
}