Subversion Repositories seema-scanner

Rev

Rev 33 | Rev 41 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
27 jakw 1
#include "SMReconstructionWorker.h"
9 jakw 2
 
27 jakw 3
#include "CodecGrayCode.h"
4
#include "CodecPhaseShift.h"
5
 
9 jakw 6
#include <QCoreApplication>
7
#include <QSettings>
8
 
9
#include <iostream>
10
#include <opencv2/opencv.hpp>
11
 
12
#include <pcl/filters/statistical_outlier_removal.h>
13
#include <pcl/io/pcd_io.h>
14
 
15
 
27 jakw 16
void SMReconstructionWorker::setup(){
9 jakw 17
 
27 jakw 18
    QSettings settings;
19
 
20
    // Get current calibration
33 jakw 21
    calibration = settings.value("calibration/parameters").value<SMCalibrationParameters>();
27 jakw 22
 
23
    // Create decoder
24
    dir = (CodecDir)settings.value("pattern/direction", CodecDirHorizontal).toInt();
25
    if(dir == CodecDirNone)
26
        std::cerr << "SMCaptureWorker: invalid coding direction " << std::endl;
27
 
36 jakw 28
    int resX = settings.value("projector/resX").toInt();
29
    int resY = settings.value("projector/resY").toInt();
27 jakw 30
    QString codec = settings.value("codec", "GrayCode").toString();
31
    if(codec == "PhaseShift")
36 jakw 32
        decoder = new DecoderPhaseShift(dir, resX, resY);
27 jakw 33
    else if(codec == "GrayCode")
36 jakw 34
        decoder = new DecoderGrayCode(dir, resX, resY);
27 jakw 35
    else
36
        std::cerr << "SLScanWorker: invalid codec " << codec.toStdString() << std::endl;
37
 
38
 
25 jakw 39
    // Precompute lens correction maps
40
    cv::Mat eye = cv::Mat::eye(3, 3, CV_32F);
27 jakw 41
    cv::initUndistortRectifyMap(calibration.K0, calibration.k0, eye, calibration.K0, cv::Size(calibration.frameWidth, calibration.frameHeight), CV_32FC1, lensMap0Horz, lensMap0Vert);
42
    cv::initUndistortRectifyMap(calibration.K0, calibration.k0, eye, calibration.K0, cv::Size(calibration.frameWidth, calibration.frameHeight), CV_32FC1, lensMap1Horz, lensMap1Vert);
9 jakw 43
 
27 jakw 44
    //cv::Mat mapHorz, mapVert;
45
    //cv::normalize(lensMap0Horz, mapHorz, 0, 255, cv::NORM_MINMAX, CV_8U);
46
    //cv::normalize(lensMap0Vert, mapVert, 0, 255, cv::NORM_MINMAX, CV_8U);
47
    //cv::imwrite("mapHorz.png", mapHorz);
48
    //cv::imwrite("mapVert.png", mapVert);
9 jakw 49
}
50
 
27 jakw 51
void SMReconstructionWorker::reconstructPointCloud(SMFrameSequence frameSequence){
9 jakw 52
 
53
    time.start();
54
 
36 jakw 55
    // Get correspondences
56
    std::vector<cv::Point2f> q0, q1;
57
    std::vector<cv::Point3f> color;
58
    decoder->getCorrespondences(frameSequence.frames0, frameSequence.frames0, q0, q1, color);
27 jakw 59
 
60
    // Triangulate
36 jakw 61
    std::vector<cv::Point3f> Q;
62
    triangulate(q0, q1, Q);
9 jakw 63
 
64
    // Convert point cloud to PCL format
65
    pcl::PointCloud<pcl::PointXYZRGB>::Ptr pointCloudPCL(new pcl::PointCloud<pcl::PointXYZRGB>);
66
 
67
    // Interprete as organized point cloud
36 jakw 68
    pointCloudPCL->width = Q.size();
69
    pointCloudPCL->height = 1;
9 jakw 70
    pointCloudPCL->is_dense = false;
71
 
36 jakw 72
    pointCloudPCL->points.resize(Q.size());
9 jakw 73
 
36 jakw 74
    for(int i=0; i<Q.size(); i++){
75
        pcl::PointXYZRGB point;
76
        point.x = Q[i].x; point.y = Q[i].y; point.z = Q[i].z;
77
        point.r = color[i].x; point.g = color[i].y; point.b = color[i].z;
78
        pointCloudPCL->points[i] = point;
27 jakw 79
    }
9 jakw 80
 
81
    // Emit result
82
    emit newPointCloud(pointCloudPCL);
83
 
27 jakw 84
    std::cout << "SMReconstructionWorker: " << time.elapsed() << "ms" << std::endl;
9 jakw 85
 
86
}
87
 
27 jakw 88
void SMReconstructionWorker::reconstructPointClouds(std::vector<SMFrameSequence> frameSequences){
24 jakw 89
 
27 jakw 90
    // Process sequentially
91
    for(int i=0; i<frameSequences.size(); i++){
92
        reconstructPointCloud(frameSequences[i]);
24 jakw 93
    }
94
 
95
}
96
 
36 jakw 97
void SMReconstructionWorker::triangulate(std::vector<cv::Point2f>& q0, std::vector<cv::Point2f>& q1, std::vector<cv::Point3f> &Q){}
24 jakw 98
 
27 jakw 99
//void SMReconstructionWorker::triangulateFromUpVp(cv::Mat &up, cv::Mat &vp, cv::Mat &xyz){
24 jakw 100
 
25 jakw 101
//    std::cerr << "WARNING! NOT FULLY IMPLEMENTED!" << std::endl;
102
//    int N = up.rows * up.cols;
24 jakw 103
 
25 jakw 104
//    cv::Mat projPointsCam(2, N, CV_32F);
105
//    uc.reshape(0,1).copyTo(projPointsCam.row(0));
106
//    vc.reshape(0,1).copyTo(projPointsCam.row(1));
24 jakw 107
 
25 jakw 108
//    cv::Mat projPointsProj(2, N, CV_32F);
109
//    up.reshape(0,1).copyTo(projPointsProj.row(0));
110
//    vp.reshape(0,1).copyTo(projPointsProj.row(1));
24 jakw 111
 
25 jakw 112
//    cv::Mat Pc(3,4,CV_32F,cv::Scalar(0.0));
113
//    cv::Mat(calibration.Kc).copyTo(Pc(cv::Range(0,3), cv::Range(0,3)));
24 jakw 114
 
25 jakw 115
//    cv::Mat Pp(3,4,CV_32F), temp(3,4,CV_32F);
116
//    cv::Mat(calibration.Rp).copyTo(temp(cv::Range(0,3), cv::Range(0,3)));
117
//    cv::Mat(calibration.Tp).copyTo(temp(cv::Range(0,3), cv::Range(3,4)));
118
//    Pp = cv::Mat(calibration.Kp) * temp;
24 jakw 119
 
25 jakw 120
//    cv::Mat xyzw;
121
//    cv::triangulatePoints(Pc, Pp, projPointsCam, projPointsProj, xyzw);
24 jakw 122
 
25 jakw 123
//    xyz.create(3, N, CV_32F);
124
//    for(int i=0; i<N; i++){
125
//        xyz.at<float>(0,i) = xyzw.at<float>(0,i)/xyzw.at<float>(3,i);
126
//        xyz.at<float>(1,i) = xyzw.at<float>(1,i)/xyzw.at<float>(3,i);
127
//        xyz.at<float>(2,i) = xyzw.at<float>(2,i)/xyzw.at<float>(3,i);
128
//    }
24 jakw 129
 
25 jakw 130
//    xyz = xyz.t();
131
//    xyz = xyz.reshape(3, up.rows);
27 jakw 132
//}
24 jakw 133