Subversion Repositories seema-scanner

Rev

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

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