Subversion Repositories seema-scanner

Rev

Rev 41 | Rev 44 | 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>
16
 
17
 
27 jakw 18
void SMReconstructionWorker::setup(){
9 jakw 19
 
27 jakw 20
    QSettings settings;
21
 
22
    // Get current calibration
33 jakw 23
    calibration = settings.value("calibration/parameters").value<SMCalibrationParameters>();
27 jakw 24
 
41 jakw 25
    // Create Algorithm
26
    dir = (CodingDir)settings.value("pattern/direction", CodingDirHorizontal).toInt();
27
    if(dir == CodingDirNone)
27 jakw 28
        std::cerr << "SMCaptureWorker: invalid coding direction " << std::endl;
29
 
36 jakw 30
    int resX = settings.value("projector/resX").toInt();
31
    int resY = settings.value("projector/resY").toInt();
27 jakw 32
    QString codec = settings.value("codec", "GrayCode").toString();
33
    if(codec == "PhaseShift")
41 jakw 34
        algorithm = new AlgorithmPhaseShift(resX, resY, dir);
27 jakw 35
    else if(codec == "GrayCode")
41 jakw 36
        algorithm = new AlgorithmGrayCode(resX, resY, dir);
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;
60
    algorithm->get3DPoints(calibration, frameSequence.frames0, frameSequence.frames1, Q, color);
9 jakw 61
 
62
    // Convert point cloud to PCL format
63
    pcl::PointCloud<pcl::PointXYZRGB>::Ptr pointCloudPCL(new pcl::PointCloud<pcl::PointXYZRGB>);
64
 
42 jakw 65
    // Interpret as unorganized point cloud
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++){
73
        pcl::PointXYZRGB point;
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
 
42 jakw 79
    SMPointCloud smPointCloud;
80
    smPointCloud.pointCloud = pointCloudPCL;
81
    smPointCloud.rotationAngle = frameSequence.rotationAngle;
82
 
9 jakw 83
    // Emit result
42 jakw 84
    emit newPointCloud(smPointCloud);
9 jakw 85
 
27 jakw 86
    std::cout << "SMReconstructionWorker: " << time.elapsed() << "ms" << std::endl;
9 jakw 87
 
88
}
89
 
27 jakw 90
void SMReconstructionWorker::reconstructPointClouds(std::vector<SMFrameSequence> frameSequences){
24 jakw 91
 
27 jakw 92
    // Process sequentially
93
    for(int i=0; i<frameSequences.size(); i++){
94
        reconstructPointCloud(frameSequences[i]);
24 jakw 95
    }
96
 
97
}
98
 
41 jakw 99
void SMReconstructionWorker::triangulate(std::vector<cv::Point2f>& q0, std::vector<cv::Point2f>& q1, std::vector<cv::Point3f> &Q){
24 jakw 100
 
41 jakw 101
    cv::Mat P0(3,4,CV_32F,cv::Scalar(0.0));
102
    cv::Mat(calibration.K0).copyTo(P0(cv::Range(0,3), cv::Range(0,3)));
103
 
104
    cv::Mat temp(3,4,CV_32F);
105
    cv::Mat(calibration.R1).copyTo(temp(cv::Range(0,3), cv::Range(0,3)));
106
    cv::Mat(calibration.T1).copyTo(temp(cv::Range(0,3), cv::Range(3,4)));
107
    cv::Mat P1 = cv::Mat(calibration.K1) * temp;
108
 
42 jakw 109
    cv::Mat QMatHomogenous, QMat;
110
    cv::triangulatePoints(P0, P1, q0, q1, QMatHomogenous);
111
    cvtools::convertMatFromHomogeneous(QMatHomogenous, QMat);
112
    cvtools::matToPoints3f(QMat, Q);
41 jakw 113
 
42 jakw 114
 
41 jakw 115
}
116
 
27 jakw 117
//void SMReconstructionWorker::triangulateFromUpVp(cv::Mat &up, cv::Mat &vp, cv::Mat &xyz){
24 jakw 118
 
25 jakw 119
//    std::cerr << "WARNING! NOT FULLY IMPLEMENTED!" << std::endl;
120
//    int N = up.rows * up.cols;
24 jakw 121
 
25 jakw 122
//    cv::Mat projPointsCam(2, N, CV_32F);
123
//    uc.reshape(0,1).copyTo(projPointsCam.row(0));
124
//    vc.reshape(0,1).copyTo(projPointsCam.row(1));
24 jakw 125
 
25 jakw 126
//    cv::Mat projPointsProj(2, N, CV_32F);
127
//    up.reshape(0,1).copyTo(projPointsProj.row(0));
128
//    vp.reshape(0,1).copyTo(projPointsProj.row(1));
24 jakw 129
 
25 jakw 130
//    cv::Mat Pc(3,4,CV_32F,cv::Scalar(0.0));
131
//    cv::Mat(calibration.Kc).copyTo(Pc(cv::Range(0,3), cv::Range(0,3)));
24 jakw 132
 
25 jakw 133
//    cv::Mat Pp(3,4,CV_32F), temp(3,4,CV_32F);
134
//    cv::Mat(calibration.Rp).copyTo(temp(cv::Range(0,3), cv::Range(0,3)));
135
//    cv::Mat(calibration.Tp).copyTo(temp(cv::Range(0,3), cv::Range(3,4)));
136
//    Pp = cv::Mat(calibration.Kp) * temp;
24 jakw 137
 
25 jakw 138
//    cv::Mat xyzw;
139
//    cv::triangulatePoints(Pc, Pp, projPointsCam, projPointsProj, xyzw);
24 jakw 140
 
25 jakw 141
//    xyz.create(3, N, CV_32F);
142
//    for(int i=0; i<N; i++){
143
//        xyz.at<float>(0,i) = xyzw.at<float>(0,i)/xyzw.at<float>(3,i);
144
//        xyz.at<float>(1,i) = xyzw.at<float>(1,i)/xyzw.at<float>(3,i);
145
//        xyz.at<float>(2,i) = xyzw.at<float>(2,i)/xyzw.at<float>(3,i);
146
//    }
24 jakw 147
 
25 jakw 148
//    xyz = xyz.t();
149
//    xyz = xyz.reshape(3, up.rows);
27 jakw 150
//}
24 jakw 151