Subversion Repositories seema-scanner

Rev

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