Subversion Repositories seema-scanner

Rev

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

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