Subversion Repositories seema-scanner

Rev

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

Rev 33 Rev 36
1
#include "SMReconstructionWorker.h"
1
#include "SMReconstructionWorker.h"
2
 
2
 
3
#include "CodecGrayCode.h"
3
#include "CodecGrayCode.h"
4
#include "CodecPhaseShift.h"
4
#include "CodecPhaseShift.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 <pcl/filters/statistical_outlier_removal.h>
12
#include <pcl/filters/statistical_outlier_removal.h>
13
#include <pcl/io/pcd_io.h>
13
#include <pcl/io/pcd_io.h>
14
 
14
 
15
 
15
 
16
void SMReconstructionWorker::setup(){
16
void SMReconstructionWorker::setup(){
17
 
17
 
18
    QSettings settings;
18
    QSettings settings;
19
 
19
 
20
    // Get current calibration
20
    // Get current calibration
21
    calibration = settings.value("calibration/parameters").value<SMCalibrationParameters>();
21
    calibration = settings.value("calibration/parameters").value<SMCalibrationParameters>();
22
 
22
 
23
    // Create decoder
23
    // Create decoder
24
    dir = (CodecDir)settings.value("pattern/direction", CodecDirHorizontal).toInt();
24
    dir = (CodecDir)settings.value("pattern/direction", CodecDirHorizontal).toInt();
25
    if(dir == CodecDirNone)
25
    if(dir == CodecDirNone)
26
        std::cerr << "SMCaptureWorker: invalid coding direction " << std::endl;
26
        std::cerr << "SMCaptureWorker: invalid coding direction " << std::endl;
27
 
27
 
-
 
28
    int resX = settings.value("projector/resX").toInt();
-
 
29
    int resY = settings.value("projector/resY").toInt();
28
    QString codec = settings.value("codec", "GrayCode").toString();
30
    QString codec = settings.value("codec", "GrayCode").toString();
29
    if(codec == "PhaseShift")
31
    if(codec == "PhaseShift")
30
        decoder = new DecoderPhaseShift(dir);
32
        decoder = new DecoderPhaseShift(dir, resX, resY);
31
    else if(codec == "GrayCode")
33
    else if(codec == "GrayCode")
32
        decoder = new DecoderGrayCode(dir);
34
        decoder = new DecoderGrayCode(dir, resX, resY);
33
    else
35
    else
34
        std::cerr << "SLScanWorker: invalid codec " << codec.toStdString() << std::endl;
36
        std::cerr << "SLScanWorker: invalid codec " << codec.toStdString() << std::endl;
35
 
37
 
36
 
38
 
37
    // Precompute lens correction maps
39
    // Precompute lens correction maps
38
    cv::Mat eye = cv::Mat::eye(3, 3, CV_32F);
40
    cv::Mat eye = cv::Mat::eye(3, 3, CV_32F);
39
    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, lensMap0Horz, lensMap0Vert);
40
    cv::initUndistortRectifyMap(calibration.K0, calibration.k0, eye, calibration.K0, cv::Size(calibration.frameWidth, calibration.frameHeight), CV_32FC1, lensMap1Horz, lensMap1Vert);
42
    cv::initUndistortRectifyMap(calibration.K0, calibration.k0, eye, calibration.K0, cv::Size(calibration.frameWidth, calibration.frameHeight), CV_32FC1, lensMap1Horz, lensMap1Vert);
41
 
43
 
42
    //cv::Mat mapHorz, mapVert;
44
    //cv::Mat mapHorz, mapVert;
43
    //cv::normalize(lensMap0Horz, mapHorz, 0, 255, cv::NORM_MINMAX, CV_8U);
45
    //cv::normalize(lensMap0Horz, mapHorz, 0, 255, cv::NORM_MINMAX, CV_8U);
44
    //cv::normalize(lensMap0Vert, mapVert, 0, 255, cv::NORM_MINMAX, CV_8U);
46
    //cv::normalize(lensMap0Vert, mapVert, 0, 255, cv::NORM_MINMAX, CV_8U);
45
    //cv::imwrite("mapHorz.png", mapHorz);
47
    //cv::imwrite("mapHorz.png", mapHorz);
46
    //cv::imwrite("mapVert.png", mapVert);
48
    //cv::imwrite("mapVert.png", mapVert);
47
}
49
}
48
 
50
 
49
void SMReconstructionWorker::reconstructPointCloud(SMFrameSequence frameSequence){
51
void SMReconstructionWorker::reconstructPointCloud(SMFrameSequence frameSequence){
50
 
52
 
51
    time.start();
53
    time.start();
52
 
54
 
53
    // Decode frames
55
    // Get correspondences
54
    cv::Mat up0, vp0, up1, vp1, shading0, mask0, shading1, mask1;
56
    std::vector<cv::Point2f> q0, q1;
55
    decoder->decodeFrames(frameSequence.frames0, up0, vp0, mask0, shading0);
57
    std::vector<cv::Point3f> color;
56
    decoder->decodeFrames(frameSequence.frames1, up1, vp1, mask1, shading1);
58
    decoder->getCorrespondences(frameSequence.frames0, frameSequence.frames0, q0, q1, color);
57
 
59
 
58
    // Triangulate
60
    // Triangulate
59
    cv::Mat pointCloud;
61
    std::vector<cv::Point3f> Q;
60
    if(dir == CodecDirBoth)
-
 
61
        triangulateFromUpVp(up0, vp0, mask0, up1, vp1, mask1, pointCloud);
-
 
62
    else if(dir == CodecDirHorizontal)
-
 
63
        triangulateFromUp(up0, mask0, up1, mask1, pointCloud);
-
 
64
    else if(dir == CodecDirVertical)
-
 
65
        triangulateFromVp(vp0, mask0, vp1, mask1, pointCloud);
-
 
66
 
-
 
67
    // Simply use shading information from camera 0 (for now)
-
 
68
    cv::Mat shading = shading0;
62
    triangulate(q0, q1, Q);
69
 
63
 
70
    // Convert point cloud to PCL format
64
    // Convert point cloud to PCL format
71
    pcl::PointCloud<pcl::PointXYZRGB>::Ptr pointCloudPCL(new pcl::PointCloud<pcl::PointXYZRGB>);
65
    pcl::PointCloud<pcl::PointXYZRGB>::Ptr pointCloudPCL(new pcl::PointCloud<pcl::PointXYZRGB>);
72
 
66
 
73
    // Interprete as organized point cloud
67
    // Interprete as organized point cloud
74
    pointCloudPCL->width = pointCloud.cols;
68
    pointCloudPCL->width = Q.size();
75
    pointCloudPCL->height = pointCloud.rows;
69
    pointCloudPCL->height = 1;
76
    pointCloudPCL->is_dense = false;
70
    pointCloudPCL->is_dense = false;
77
 
71
 
78
    pointCloudPCL->points.resize(pointCloud.rows*pointCloud.cols);
72
    pointCloudPCL->points.resize(Q.size());
79
 
73
 
80
    for(int row=0; row<pointCloud.rows; row++){
74
    for(int i=0; i<Q.size(); i++){
81
        int offset = row * pointCloudPCL->width;
-
 
82
        for(int col=0; col<pointCloud.cols; col++){
-
 
83
            const cv::Vec3f pnt = pointCloud.at<cv::Vec3f>(row,col);
-
 
84
            unsigned char shade = shading.at<unsigned short>(row,col) >> 8;
-
 
85
            pcl::PointXYZRGB point;
75
        pcl::PointXYZRGB point;
86
            point.x = pnt[0]; point.y = pnt[1]; point.z = pnt[2];
76
        point.x = Q[i].x; point.y = Q[i].y; point.z = Q[i].z;
87
            point.r = shade; point.g = shade; point.b = shade;
77
        point.r = color[i].x; point.g = color[i].y; point.b = color[i].z;
88
            pointCloudPCL->points[offset + col] = point;
78
        pointCloudPCL->points[i] = point;
89
        }
-
 
90
    }
79
    }
91
 
80
 
92
/*    // stack xyz data
-
 
93
    std::vector<cv::Mat> xyz;
-
 
94
    cv::split(pointCloud, xyz);
-
 
95
    std::vector<cv::Mat> pointCloudChannels;
-
 
96
    pointCloudChannels.push_back(xyz[0]);
-
 
97
    pointCloudChannels.push_back(xyz[1]);
-
 
98
    pointCloudChannels.push_back(xyz[2]);
-
 
99
 
-
 
100
    // 4 byte padding
-
 
101
    pointCloudChannels.push_back(cv::Mat::zeros(pointCloud.size(), CV_32F));
-
 
102
 
-
 
103
    // triple uchar color information
-
 
104
    std::vector<cv::Mat> rgb;
-
 
105
    rgb.push_back(shading);
-
 
106
    rgb.push_back(shading);
-
 
107
    rgb.push_back(shading);
-
 
108
    rgb.push_back(cv::Mat::zeros(shading.size(), CV_8U));
-
 
109
 
-
 
110
    cv::Mat rgb8UC4;
-
 
111
    cv::merge(rgb, rgb8UC4);
-
 
112
 
-
 
113
    cv::Mat rgb32F(rgb8UC4.size(), CV_32F, rgb8UC4.data);
-
 
114
 
-
 
115
    pointCloudChannels.push_back(rgb32F);
-
 
116
 
-
 
117
    // 12 bytes padding
-
 
118
    pointCloudChannels.push_back(cv::Mat::zeros(pointCloud.size(), CV_32F));
-
 
119
    pointCloudChannels.push_back(cv::Mat::zeros(pointCloud.size(), CV_32F));
-
 
120
    pointCloudChannels.push_back(cv::Mat::zeros(pointCloud.size(), CV_32F));
-
 
121
 
-
 
122
    // merge channels
-
 
123
    cv::Mat pointCloudPadded;
-
 
124
    cv::merge(pointCloudChannels, pointCloudPadded);
-
 
125
 
-
 
126
    // memcpy everything
-
 
127
    memcpy(&pointCloudPCL->points[0], pointCloudPadded.data, pointCloudPadded.rows*pointCloudPadded.cols*sizeof(pcl::PointXYZRGB));*/
-
 
128
 
-
 
129
 
-
 
130
    // Emit result
81
    // Emit result
131
    emit newPointCloud(pointCloudPCL);
82
    emit newPointCloud(pointCloudPCL);
132
 
83
 
133
    std::cout << "SMReconstructionWorker: " << time.elapsed() << "ms" << std::endl;
84
    std::cout << "SMReconstructionWorker: " << time.elapsed() << "ms" << std::endl;
134
 
85
 
135
}
86
}
136
 
87
 
137
void SMReconstructionWorker::reconstructPointClouds(std::vector<SMFrameSequence> frameSequences){
88
void SMReconstructionWorker::reconstructPointClouds(std::vector<SMFrameSequence> frameSequences){
138
 
89
 
139
    // Process sequentially
90
    // Process sequentially
140
    for(int i=0; i<frameSequences.size(); i++){
91
    for(int i=0; i<frameSequences.size(); i++){
141
        reconstructPointCloud(frameSequences[i]);
92
        reconstructPointCloud(frameSequences[i]);
142
    }
93
    }
143
 
94
 
144
}
95
}
145
 
96
 
146
void SMReconstructionWorker::triangulateFromUp(cv::Mat up0, cv::Mat mask0,cv::Mat up1, cv::Mat mask1,cv::Mat &xyz){}
97
void SMReconstructionWorker::triangulate(std::vector<cv::Point2f>& q0, std::vector<cv::Point2f>& q1, std::vector<cv::Point3f> &Q){}
147
void SMReconstructionWorker::triangulateFromVp(cv::Mat vp0, cv::Mat mask0, cv::Mat vp1, cv::Mat mask1, cv::Mat &xyz){}
-
 
148
void SMReconstructionWorker::triangulateFromUpVp(cv::Mat up0, cv::Mat vp0, cv::Mat mask0, cv::Mat up1, cv::Mat vp1, cv::Mat mask1, cv::Mat &xyz){}
-
 
149
 
98
 
150
//void SMReconstructionWorker::triangulateFromUpVp(cv::Mat &up, cv::Mat &vp, cv::Mat &xyz){
99
//void SMReconstructionWorker::triangulateFromUpVp(cv::Mat &up, cv::Mat &vp, cv::Mat &xyz){
151
 
100
 
152
//    std::cerr << "WARNING! NOT FULLY IMPLEMENTED!" << std::endl;
101
//    std::cerr << "WARNING! NOT FULLY IMPLEMENTED!" << std::endl;
153
//    int N = up.rows * up.cols;
102
//    int N = up.rows * up.cols;
154
 
103
 
155
//    cv::Mat projPointsCam(2, N, CV_32F);
104
//    cv::Mat projPointsCam(2, N, CV_32F);
156
//    uc.reshape(0,1).copyTo(projPointsCam.row(0));
105
//    uc.reshape(0,1).copyTo(projPointsCam.row(0));
157
//    vc.reshape(0,1).copyTo(projPointsCam.row(1));
106
//    vc.reshape(0,1).copyTo(projPointsCam.row(1));
158
 
107
 
159
//    cv::Mat projPointsProj(2, N, CV_32F);
108
//    cv::Mat projPointsProj(2, N, CV_32F);
160
//    up.reshape(0,1).copyTo(projPointsProj.row(0));
109
//    up.reshape(0,1).copyTo(projPointsProj.row(0));
161
//    vp.reshape(0,1).copyTo(projPointsProj.row(1));
110
//    vp.reshape(0,1).copyTo(projPointsProj.row(1));
162
 
111
 
163
//    cv::Mat Pc(3,4,CV_32F,cv::Scalar(0.0));
112
//    cv::Mat Pc(3,4,CV_32F,cv::Scalar(0.0));
164
//    cv::Mat(calibration.Kc).copyTo(Pc(cv::Range(0,3), cv::Range(0,3)));
113
//    cv::Mat(calibration.Kc).copyTo(Pc(cv::Range(0,3), cv::Range(0,3)));
165
 
114
 
166
//    cv::Mat Pp(3,4,CV_32F), temp(3,4,CV_32F);
115
//    cv::Mat Pp(3,4,CV_32F), temp(3,4,CV_32F);
167
//    cv::Mat(calibration.Rp).copyTo(temp(cv::Range(0,3), cv::Range(0,3)));
116
//    cv::Mat(calibration.Rp).copyTo(temp(cv::Range(0,3), cv::Range(0,3)));
168
//    cv::Mat(calibration.Tp).copyTo(temp(cv::Range(0,3), cv::Range(3,4)));
117
//    cv::Mat(calibration.Tp).copyTo(temp(cv::Range(0,3), cv::Range(3,4)));
169
//    Pp = cv::Mat(calibration.Kp) * temp;
118
//    Pp = cv::Mat(calibration.Kp) * temp;
170
 
119
 
171
//    cv::Mat xyzw;
120
//    cv::Mat xyzw;
172
//    cv::triangulatePoints(Pc, Pp, projPointsCam, projPointsProj, xyzw);
121
//    cv::triangulatePoints(Pc, Pp, projPointsCam, projPointsProj, xyzw);
173
 
122
 
174
//    xyz.create(3, N, CV_32F);
123
//    xyz.create(3, N, CV_32F);
175
//    for(int i=0; i<N; i++){
124
//    for(int i=0; i<N; i++){
176
//        xyz.at<float>(0,i) = xyzw.at<float>(0,i)/xyzw.at<float>(3,i);
125
//        xyz.at<float>(0,i) = xyzw.at<float>(0,i)/xyzw.at<float>(3,i);
177
//        xyz.at<float>(1,i) = xyzw.at<float>(1,i)/xyzw.at<float>(3,i);
126
//        xyz.at<float>(1,i) = xyzw.at<float>(1,i)/xyzw.at<float>(3,i);
178
//        xyz.at<float>(2,i) = xyzw.at<float>(2,i)/xyzw.at<float>(3,i);
127
//        xyz.at<float>(2,i) = xyzw.at<float>(2,i)/xyzw.at<float>(3,i);
179
//    }
128
//    }
180
 
129
 
181
//    xyz = xyz.t();
130
//    xyz = xyz.t();
182
//    xyz = xyz.reshape(3, up.rows);
131
//    xyz = xyz.reshape(3, up.rows);
183
//}
132
//}
184
 
133
 
185
 
134