Subversion Repositories seema-scanner

Rev

Rev 70 | Rev 75 | Go to most recent revision | Only display areas with differences | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

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