Subversion Repositories seema-scanner

Rev

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

Rev 44 Rev 45
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
    dir = (CodingDir)settings.value("pattern/direction", CodingDirHorizontal).toInt();
27
    dir = (CodingDir)settings.value("pattern/direction", CodingDirHorizontal).toInt();
28
    if(dir == CodingDirNone)
28
    if(dir == CodingDirNone)
29
        std::cerr << "SMCaptureWorker: invalid coding direction " << std::endl;
29
        std::cerr << "SMCaptureWorker: invalid coding direction " << std::endl;
30
 
30
 
31
    int resX = settings.value("projector/resX").toInt();
31
    int resX = settings.value("projector/resX").toInt();
32
    int resY = settings.value("projector/resY").toInt();
32
    int resY = settings.value("projector/resY").toInt();
33
    QString codec = settings.value("codec", "GrayCode").toString();
33
    QString codec = settings.value("codec", "GrayCode").toString();
34
    if(codec == "PhaseShift")
34
    if(codec == "PhaseShift")
35
        algorithm = new AlgorithmPhaseShift(resX, resY, dir);
35
        algorithm = new AlgorithmPhaseShift(resX, resY, dir);
36
    else if(codec == "GrayCode")
36
    else if(codec == "GrayCode")
37
        algorithm = new AlgorithmGrayCode(resX, resY, dir);
37
        algorithm = new AlgorithmGrayCode(resX, resY, dir);
38
    else
38
    else
39
        std::cerr << "SLScanWorker: invalid codec " << codec.toStdString() << std::endl;
39
        std::cerr << "SLScanWorker: invalid codec " << codec.toStdString() << std::endl;
40
 
40
 
41
 
41
 
42
//    // Precompute lens correction maps
42
//    // Precompute lens correction maps
43
//    cv::Mat eye = cv::Mat::eye(3, 3, CV_32F);
43
//    cv::Mat eye = cv::Mat::eye(3, 3, CV_32F);
44
//    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, lensMap0Horz, lensMap0Vert);
45
//    cv::initUndistortRectifyMap(calibration.K0, calibration.k0, eye, calibration.K0, cv::Size(calibration.frameWidth, calibration.frameHeight), CV_32FC1, lensMap1Horz, lensMap1Vert);
45
//    cv::initUndistortRectifyMap(calibration.K0, calibration.k0, eye, calibration.K0, cv::Size(calibration.frameWidth, calibration.frameHeight), CV_32FC1, lensMap1Horz, lensMap1Vert);
46
 
46
 
47
    //cv::Mat mapHorz, mapVert;
47
    //cv::Mat mapHorz, mapVert;
48
    //cv::normalize(lensMap0Horz, mapHorz, 0, 255, cv::NORM_MINMAX, CV_8U);
48
    //cv::normalize(lensMap0Horz, mapHorz, 0, 255, cv::NORM_MINMAX, CV_8U);
49
    //cv::normalize(lensMap0Vert, mapVert, 0, 255, cv::NORM_MINMAX, CV_8U);
49
    //cv::normalize(lensMap0Vert, mapVert, 0, 255, cv::NORM_MINMAX, CV_8U);
50
    //cv::imwrite("mapHorz.png", mapHorz);
50
    //cv::imwrite("mapHorz.png", mapHorz);
51
    //cv::imwrite("mapVert.png", mapVert);
51
    //cv::imwrite("mapVert.png", mapVert);
52
}
52
}
53
 
53
 
54
void SMReconstructionWorker::reconstructPointCloud(SMFrameSequence frameSequence){
54
void SMReconstructionWorker::reconstructPointCloud(SMFrameSequence frameSequence){
55
 
55
 
56
    time.start();
56
    time.start();
57
 
57
 
58
    // Get 3D Points
58
    // Get 3D Points
59
    std::vector<cv::Point3f> Q;
59
    std::vector<cv::Point3f> Q;
60
    std::vector<cv::Vec3b> color;
60
    std::vector<cv::Vec3b> color;
61
    algorithm->get3DPoints(calibration, frameSequence.frames0, frameSequence.frames1, Q, color);
61
    algorithm->get3DPoints(calibration, frameSequence.frames0, frameSequence.frames1, Q, color);
62
 
62
 
63
    // Convert point cloud to PCL format
63
    // Convert point cloud to PCL format
64
    pcl::PointCloud<pcl::PointXYZRGBNormal>::Ptr pointCloudPCL(new pcl::PointCloud<pcl::PointXYZRGBNormal>);
64
    pcl::PointCloud<pcl::PointXYZRGB>::Ptr pointCloudPCL(new pcl::PointCloud<pcl::PointXYZRGB>);
65
 
65
 
66
    pointCloudPCL->width = Q.size();
66
    pointCloudPCL->width = Q.size();
67
    pointCloudPCL->height = 1;
67
    pointCloudPCL->height = 1;
68
    pointCloudPCL->is_dense = false;
68
    pointCloudPCL->is_dense = false;
69
 
69
 
70
    pointCloudPCL->points.resize(Q.size());
70
    pointCloudPCL->points.resize(Q.size());
71
 
71
 
72
    for(int i=0; i<Q.size(); i++){
72
    for(int i=0; i<Q.size(); i++){
73
        pcl::PointXYZRGBNormal point;
73
        pcl::PointXYZRGB point;
74
        point.x = Q[i].x; point.y = Q[i].y; point.z = Q[i].z;
74
        point.x = Q[i].x; point.y = Q[i].y; point.z = Q[i].z;
75
        point.r = color[i][0]; point.g = color[i][1]; point.b = color[i][2];
75
        point.r = color[i][0]; point.g = color[i][1]; point.b = color[i][2];
76
        pointCloudPCL->points[i] = point;
76
        pointCloudPCL->points[i] = point;
77
    }
77
    }
78
 
78
 
79
    // Estimate surface normals
79
//    // Estimate surface normals
80
    pcl::NormalEstimation<pcl::PointXYZRGBNormal, pcl::PointXYZRGBNormal> ne;
80
//    pcl::NormalEstimation<pcl::PointXYZRGB, pcl::PointXYZRGBNormal> ne;
81
    pcl::search::KdTree<pcl::PointXYZRGBNormal>::Ptr tree(new pcl::search::KdTree<pcl::PointXYZRGBNormal>());
81
//    pcl::search::KdTree<pcl::PointXYZRGB>::Ptr tree(new pcl::search::KdTree<pcl::PointXYZRGB>());
82
    ne.setSearchMethod(tree);
82
//    ne.setSearchMethod(tree);
83
    ne.setRadiusSearch(3);
83
//    ne.setRadiusSearch(3);
84
    ne.setViewPoint(0.0, 0.0, 0.0);
84
//    ne.setViewPoint(0.0, 0.0, 0.0);
85
    ne.setInputCloud(pointCloudPCL);
85
//    ne.setInputCloud(pointCloudPCL);
86
    ne.compute(*pointCloudPCL);
86
//    ne.compute(*pointCloudPCL);
87
 
87
 
88
    // Assemble SMPointCloud data structure
88
    // Assemble SMPointCloud data structure
89
    SMPointCloud smPointCloud;
89
    SMPointCloud smPointCloud;
-
 
90
    smPointCloud.id = frameSequence.id;
90
    smPointCloud.pointCloud = pointCloudPCL;
91
    smPointCloud.pointCloud = pointCloudPCL;
91
    smPointCloud.rotationAngle = frameSequence.rotationAngle;
92
    smPointCloud.rotationAngle = frameSequence.rotationAngle;
92
 
93
 
93
    // Determine transform in world (camera0) coordinate system
94
    // Determine transform in world (camera0) coordinate system
94
    float angleRadians = frameSequence.rotationAngle/180.0*M_PI;
95
    float angleRadians = frameSequence.rotationAngle/180.0*M_PI;
95
    cv::Vec3f rot_rvec(0.0, -angleRadians, 0.0);
96
    cv::Vec3f rot_rvec(0.0, -angleRadians, 0.0);
96
    cv::Mat R;
97
    cv::Mat R;
97
    cv::Rodrigues(rot_rvec, R);
98
    cv::Rodrigues(rot_rvec, R);
98
    smPointCloud.R = calibration.Rr.t()*cv::Matx33f(R)*calibration.Rr;
99
    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;
100
    smPointCloud.T = calibration.Rr.t()*cv::Matx33f(R)*calibration.Tr - calibration.Rr.t()*calibration.Tr;
100
 
101
 
101
    // Emit result
102
    // Emit result
102
    emit newPointCloud(smPointCloud);
103
    emit newPointCloud(smPointCloud);
103
 
104
 
104
    std::cout << "SMReconstructionWorker: " << time.elapsed() << "ms" << std::endl;
105
    std::cout << "SMReconstructionWorker: " << time.elapsed() << "ms" << std::endl;
105
 
106
 
106
}
107
}
107
 
108
 
108
void SMReconstructionWorker::reconstructPointClouds(std::vector<SMFrameSequence> frameSequences){
109
void SMReconstructionWorker::reconstructPointClouds(std::vector<SMFrameSequence> frameSequences){
109
 
110
 
110
    // Process sequentially
111
    // Process sequentially
111
    for(int i=0; i<frameSequences.size(); i++){
112
    for(int i=0; i<frameSequences.size(); i++){
112
        reconstructPointCloud(frameSequences[i]);
113
        reconstructPointCloud(frameSequences[i]);
113
    }
114
    }
114
 
115
 
115
}
116
}
116
 
117
 
117
void SMReconstructionWorker::triangulate(std::vector<cv::Point2f>& q0, std::vector<cv::Point2f>& q1, std::vector<cv::Point3f> &Q){
118
void SMReconstructionWorker::triangulate(std::vector<cv::Point2f>& q0, std::vector<cv::Point2f>& q1, std::vector<cv::Point3f> &Q){
118
 
119
 
119
    cv::Mat P0(3,4,CV_32F,cv::Scalar(0.0));
120
    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)));
121
    cv::Mat(calibration.K0).copyTo(P0(cv::Range(0,3), cv::Range(0,3)));
121
 
122
 
122
    cv::Mat temp(3,4,CV_32F);
123
    cv::Mat temp(3,4,CV_32F);
123
    cv::Mat(calibration.R1).copyTo(temp(cv::Range(0,3), cv::Range(0,3)));
124
    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)));
125
    cv::Mat(calibration.T1).copyTo(temp(cv::Range(0,3), cv::Range(3,4)));
125
    cv::Mat P1 = cv::Mat(calibration.K1) * temp;
126
    cv::Mat P1 = cv::Mat(calibration.K1) * temp;
126
 
127
 
127
    cv::Mat QMatHomogenous, QMat;
128
    cv::Mat QMatHomogenous, QMat;
128
    cv::triangulatePoints(P0, P1, q0, q1, QMatHomogenous);
129
    cv::triangulatePoints(P0, P1, q0, q1, QMatHomogenous);
129
    cvtools::convertMatFromHomogeneous(QMatHomogenous, QMat);
130
    cvtools::convertMatFromHomogeneous(QMatHomogenous, QMat);
130
    cvtools::matToPoints3f(QMat, Q);
131
    cvtools::matToPoints3f(QMat, Q);
131
 
132
 
132
 
133
 
133
}
134
}
134
 
-
 
135
//void SMReconstructionWorker::triangulateFromUpVp(cv::Mat &up, cv::Mat &vp, cv::Mat &xyz){
-
 
136
 
-
 
137
//    std::cerr << "WARNING! NOT FULLY IMPLEMENTED!" << std::endl;
-
 
138
//    int N = up.rows * up.cols;
-
 
139
 
-
 
140
//    cv::Mat projPointsCam(2, N, CV_32F);
-
 
141
//    uc.reshape(0,1).copyTo(projPointsCam.row(0));
-
 
142
//    vc.reshape(0,1).copyTo(projPointsCam.row(1));
-
 
143
 
-
 
144
//    cv::Mat projPointsProj(2, N, CV_32F);
-
 
145
//    up.reshape(0,1).copyTo(projPointsProj.row(0));
-
 
146
//    vp.reshape(0,1).copyTo(projPointsProj.row(1));
-
 
147
 
-
 
148
//    cv::Mat Pc(3,4,CV_32F,cv::Scalar(0.0));
-
 
149
//    cv::Mat(calibration.Kc).copyTo(Pc(cv::Range(0,3), cv::Range(0,3)));
-
 
150
 
-
 
151
//    cv::Mat Pp(3,4,CV_32F), temp(3,4,CV_32F);
-
 
152
//    cv::Mat(calibration.Rp).copyTo(temp(cv::Range(0,3), cv::Range(0,3)));
-
 
153
//    cv::Mat(calibration.Tp).copyTo(temp(cv::Range(0,3), cv::Range(3,4)));
-
 
154
//    Pp = cv::Mat(calibration.Kp) * temp;
-
 
155
 
-
 
156
//    cv::Mat xyzw;
-
 
157
//    cv::triangulatePoints(Pc, Pp, projPointsCam, projPointsProj, xyzw);
-
 
158
 
-
 
159
//    xyz.create(3, N, CV_32F);
-
 
160
//    for(int i=0; i<N; i++){
-
 
161
//        xyz.at<float>(0,i) = xyzw.at<float>(0,i)/xyzw.at<float>(3,i);
-
 
162
//        xyz.at<float>(1,i) = xyzw.at<float>(1,i)/xyzw.at<float>(3,i);
-
 
163
//        xyz.at<float>(2,i) = xyzw.at<float>(2,i)/xyzw.at<float>(3,i);
-
 
164
//    }
-
 
165
 
-
 
166
//    xyz = xyz.t();
-
 
167
//    xyz = xyz.reshape(3, up.rows);
-
 
168
//}
-
 
169
 
-
 
170
 
135