Subversion Repositories seema-scanner

Rev

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

Rev 97 Rev 99
1
#include "SMReconstructionWorker.h"
1
#include "SMReconstructionWorker.h"
2
 
2
 
3
#include "AlgorithmGrayCode.h"
3
#include "AlgorithmGrayCode.h"
4
#include "AlgorithmGrayCodeHQ.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 == "GrayCodeHQ")
34
        algorithm = new AlgorithmGrayCodeHQ(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
    // Decompress frames
-
 
58
    int nFrames = frameSequence.compressedFrames0.size();
-
 
59
    std::vector<cv::Mat> frames0(nFrames), frames1(nFrames);
-
 
60
    for(int i=0; i<nFrames; i++){
-
 
61
        cv::imdecode(frameSequence.compressedFrames0[i], frames0[i]);
-
 
62
        cv::imdecode(frameSequence.compressedFrames1[i], frames1[i]);
-
 
63
    }
-
 
64
 
57
    // Get 3D Points
65
    // Get 3D Points
58
    std::vector<cv::Point3f> Q;
66
    std::vector<cv::Point3f> Q;
59
    std::vector<cv::Vec3b> color;
67
    std::vector<cv::Vec3b> color;
60
    algorithm->get3DPoints(calibration, frameSequence.frames0, frameSequence.frames1, Q, color);
68
    algorithm->get3DPoints(calibration, frames0, frames1, Q, color);
61
 
69
 
62
    // Convert point cloud to PCL format
70
    // Convert point cloud to PCL format
63
    pcl::PointCloud<pcl::PointXYZRGB>::Ptr pointCloudPCL(new pcl::PointCloud<pcl::PointXYZRGB>);
71
    pcl::PointCloud<pcl::PointXYZRGB>::Ptr pointCloudPCL(new pcl::PointCloud<pcl::PointXYZRGB>);
64
 
72
 
65
    pointCloudPCL->width = Q.size();
73
    pointCloudPCL->width = Q.size();
66
    pointCloudPCL->height = 1;
74
    pointCloudPCL->height = 1;
67
    pointCloudPCL->is_dense = false;
75
    pointCloudPCL->is_dense = false;
68
 
76
 
69
    pointCloudPCL->points.resize(Q.size());
77
    pointCloudPCL->points.resize(Q.size());
70
 
78
 
71
    for(unsigned int i=0; i<Q.size(); i++){
79
    for(unsigned int i=0; i<Q.size(); i++){
72
        pcl::PointXYZRGB point;
80
        pcl::PointXYZRGB point;
73
        point.x = Q[i].x; point.y = Q[i].y; point.z = Q[i].z;
81
        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];
82
        point.r = color[i][0]; point.g = color[i][1]; point.b = color[i][2];
75
        pointCloudPCL->points[i] = point;
83
        pointCloudPCL->points[i] = point;
76
    }
84
    }
77
 
85
 
78
//    // Estimate surface normals
86
//    // Estimate surface normals
79
//    pcl::NormalEstimation<pcl::PointXYZRGB, pcl::PointXYZRGBNormal> ne;
87
//    pcl::NormalEstimation<pcl::PointXYZRGB, pcl::PointXYZRGBNormal> ne;
80
//    pcl::search::KdTree<pcl::PointXYZRGB>::Ptr tree(new pcl::search::KdTree<pcl::PointXYZRGB>());
88
//    pcl::search::KdTree<pcl::PointXYZRGB>::Ptr tree(new pcl::search::KdTree<pcl::PointXYZRGB>());
81
//    ne.setSearchMethod(tree);
89
//    ne.setSearchMethod(tree);
82
//    ne.setRadiusSearch(3);
90
//    ne.setRadiusSearch(3);
83
//    ne.setViewPoint(0.0, 0.0, 0.0);
91
//    ne.setViewPoint(0.0, 0.0, 0.0);
84
//    ne.setInputCloud(pointCloudPCL);
92
//    ne.setInputCloud(pointCloudPCL);
85
//    ne.compute(*pointCloudPCL);
93
//    ne.compute(*pointCloudPCL);
86
 
94
 
87
    // Assemble SMPointCloud data structure
95
    // Assemble SMPointCloud data structure
88
    SMPointCloud smPointCloud;
96
    SMPointCloud smPointCloud;
89
    smPointCloud.id = frameSequence.id;
97
    smPointCloud.id = frameSequence.id;
90
    smPointCloud.pointCloud = pointCloudPCL;
98
    smPointCloud.pointCloud = pointCloudPCL;
91
    smPointCloud.rotationAngle = frameSequence.rotationAngle;
99
    smPointCloud.rotationAngle = frameSequence.rotationAngle;
92
 
100
 
93
    // Determine transform in world (camera0) coordinate system
101
    // Determine transform in world (camera0) coordinate system
94
    float angleRadians = frameSequence.rotationAngle/180.0*M_PI;
102
    float angleRadians = frameSequence.rotationAngle/180.0*M_PI;
95
    cv::Vec3f rot_rvec(0.0, -angleRadians, 0.0);
103
    cv::Vec3f rot_rvec(0.0, -angleRadians, 0.0);
96
    cv::Mat R;
104
    cv::Mat R;
97
    cv::Rodrigues(rot_rvec, R);
105
    cv::Rodrigues(rot_rvec, R);
98
    smPointCloud.R = calibration.Rr.t()*cv::Matx33f(R)*calibration.Rr;
106
    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;
107
    smPointCloud.T = calibration.Rr.t()*cv::Matx33f(R)*calibration.Tr - calibration.Rr.t()*calibration.Tr;
100
 
108
 
101
    // Emit result
109
    // Emit result
102
    emit newPointCloud(smPointCloud);
110
    emit newPointCloud(smPointCloud);
103
 
111
 
104
    std::cout << "SMReconstructionWorker: " << time.elapsed() << "ms" << std::endl;
112
    std::cout << "SMReconstructionWorker: " << time.elapsed() << "ms" << std::endl;
105
 
113
 
106
}
114
}
107
 
115
 
108
void SMReconstructionWorker::reconstructPointClouds(std::vector<SMFrameSequence> frameSequences){
116
void SMReconstructionWorker::reconstructPointClouds(std::vector<SMFrameSequence> frameSequences){
109
 
117
 
110
    // Process sequentially
118
    // Process sequentially
111
    for(int i=0; i<frameSequences.size(); i++){
119
    for(int i=0; i<frameSequences.size(); i++){
112
        reconstructPointCloud(frameSequences[i]);
120
        reconstructPointCloud(frameSequences[i]);
113
    }
121
    }
114
 
122
 
115
}
123
}
116
 
124
 
117
void SMReconstructionWorker::triangulate(std::vector<cv::Point2f>& q0, std::vector<cv::Point2f>& q1, std::vector<cv::Point3f> &Q){
125
void SMReconstructionWorker::triangulate(std::vector<cv::Point2f>& q0, std::vector<cv::Point2f>& q1, std::vector<cv::Point3f> &Q){
118
 
126
 
119
    cv::Mat P0(3,4,CV_32F,cv::Scalar(0.0));
127
    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)));
128
    cv::Mat(calibration.K0).copyTo(P0(cv::Range(0,3), cv::Range(0,3)));
121
 
129
 
122
    cv::Mat temp(3,4,CV_32F);
130
    cv::Mat temp(3,4,CV_32F);
123
    cv::Mat(calibration.R1).copyTo(temp(cv::Range(0,3), cv::Range(0,3)));
131
    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)));
132
    cv::Mat(calibration.T1).copyTo(temp(cv::Range(0,3), cv::Range(3,4)));
125
    cv::Mat P1 = cv::Mat(calibration.K1) * temp;
133
    cv::Mat P1 = cv::Mat(calibration.K1) * temp;
126
 
134
 
127
    cv::Mat QMatHomogenous, QMat;
135
    cv::Mat QMatHomogenous, QMat;
128
    cv::triangulatePoints(P0, P1, q0, q1, QMatHomogenous);
136
    cv::triangulatePoints(P0, P1, q0, q1, QMatHomogenous);
129
    cvtools::convertMatFromHomogeneous(QMatHomogenous, QMat);
137
    cvtools::convertMatFromHomogeneous(QMatHomogenous, QMat);
130
    cvtools::matToPoints3f(QMat, Q);
138
    cvtools::matToPoints3f(QMat, Q);
131
 
139
 
132
 
140
 
133
}
141
}
134
 
142