Subversion Repositories seema-scanner

Rev

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

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