9 |
jakw |
1 |
#include "SMTriangulationWorker.h"
|
|
|
2 |
|
|
|
3 |
#include <QCoreApplication>
|
|
|
4 |
#include <QSettings>
|
|
|
5 |
|
|
|
6 |
#include <iostream>
|
|
|
7 |
#include <opencv2/opencv.hpp>
|
|
|
8 |
|
|
|
9 |
#include <pcl/filters/statistical_outlier_removal.h>
|
|
|
10 |
#include <pcl/io/pcd_io.h>
|
|
|
11 |
|
|
|
12 |
void SMTriangulationWorker::setup(){
|
|
|
13 |
|
|
|
14 |
// Initialize triangulator with calibration
|
|
|
15 |
calibration = new SMCalibrationParams;
|
|
|
16 |
calibration->load("calibration.yml");
|
|
|
17 |
triangulator = new Triangulator(*calibration, frameWidth, frameHeight);
|
|
|
18 |
|
|
|
19 |
QSettings settings("SLStudio");
|
|
|
20 |
writeToDisk = settings.value("writeToDisk/pointclouds",false).toBool();
|
|
|
21 |
|
|
|
22 |
}
|
|
|
23 |
|
|
|
24 |
|
|
|
25 |
void SMTriangulationWorker::triangulatePointCloud(cv::Mat up, cv::Mat vp, cv::Mat mask, cv::Mat shading){
|
|
|
26 |
|
|
|
27 |
time.start();
|
|
|
28 |
|
|
|
29 |
// Reconstruct point cloud
|
|
|
30 |
cv::Mat pointCloud;
|
|
|
31 |
cv::Mat empty;
|
|
|
32 |
triangulator->triangulate(up, vp, mask, shading, pointCloud);
|
|
|
33 |
|
|
|
34 |
std::vector<cv::Mat> xyz;
|
|
|
35 |
cv::split(pointCloud, xyz);
|
|
|
36 |
// emit imshow("x", xyz[0], 1400, 100);
|
|
|
37 |
// emit imshow("y", xyz[1], 1400, 450);
|
|
|
38 |
// emit imshow("z", xyz[2], 1400, 800);
|
|
|
39 |
|
|
|
40 |
// Convert point cloud to PCL format
|
|
|
41 |
pcl::PointCloud<pcl::PointXYZRGB>::Ptr pointCloudPCL(new pcl::PointCloud<pcl::PointXYZRGB>);
|
|
|
42 |
|
|
|
43 |
// Interprete as organized point cloud
|
|
|
44 |
pointCloudPCL->width = pointCloud.cols;
|
|
|
45 |
pointCloudPCL->height = pointCloud.rows;
|
|
|
46 |
pointCloudPCL->is_dense = false;
|
|
|
47 |
|
|
|
48 |
pointCloudPCL->points.resize(pointCloud.rows*pointCloud.cols);
|
|
|
49 |
|
|
|
50 |
// for(int col=0; col<pointCloud.cols; col++){
|
|
|
51 |
// for(int row=0; row<pointCloud.rows; row++){
|
|
|
52 |
// const cv::Vec3f pnt = pointCloud.at<cv::Vec3f>(row,col);
|
|
|
53 |
// unsigned char shade = shading.at<unsigned char>(row,col);
|
|
|
54 |
// pcl::PointXYZRGBRGB point;
|
|
|
55 |
// point.x = pnt[0]; point.y = pnt[1]; point.z = pnt[2];
|
|
|
56 |
// point.r = shade; point.g = shade; point.b = shade;
|
|
|
57 |
// pointCloudPCL->at(col, row) = point;
|
|
|
58 |
// }
|
|
|
59 |
// }
|
|
|
60 |
|
|
|
61 |
// stack xyz data
|
|
|
62 |
std::vector<cv::Mat> pointCloudChannels;
|
|
|
63 |
pointCloudChannels.push_back(xyz[0]);
|
|
|
64 |
pointCloudChannels.push_back(xyz[1]);
|
|
|
65 |
pointCloudChannels.push_back(xyz[2]);
|
|
|
66 |
|
|
|
67 |
// 4 byte padding
|
|
|
68 |
pointCloudChannels.push_back(cv::Mat::zeros(pointCloud.size(), CV_32F));
|
|
|
69 |
|
|
|
70 |
// triple uchar color information
|
|
|
71 |
std::vector<cv::Mat> rgb;
|
|
|
72 |
rgb.push_back(shading);
|
|
|
73 |
rgb.push_back(shading);
|
|
|
74 |
rgb.push_back(shading);
|
|
|
75 |
rgb.push_back(cv::Mat::zeros(shading.size(), CV_8U));
|
|
|
76 |
|
|
|
77 |
cv::Mat rgb8UC4;
|
|
|
78 |
cv::merge(rgb, rgb8UC4);
|
|
|
79 |
|
|
|
80 |
cv::Mat rgb32F(rgb8UC4.size(), CV_32F, rgb8UC4.data);
|
|
|
81 |
|
|
|
82 |
pointCloudChannels.push_back(rgb32F);
|
|
|
83 |
|
|
|
84 |
// 12 bytes padding
|
|
|
85 |
pointCloudChannels.push_back(cv::Mat::zeros(pointCloud.size(), CV_32F));
|
|
|
86 |
pointCloudChannels.push_back(cv::Mat::zeros(pointCloud.size(), CV_32F));
|
|
|
87 |
pointCloudChannels.push_back(cv::Mat::zeros(pointCloud.size(), CV_32F));
|
|
|
88 |
|
|
|
89 |
// merge channels
|
|
|
90 |
cv::Mat pointCloudPadded;
|
|
|
91 |
cv::merge(pointCloudChannels, pointCloudPadded);
|
|
|
92 |
|
|
|
93 |
// memcpy everything
|
|
|
94 |
memcpy(&pointCloudPCL->points[0], pointCloudPadded.data, pointCloudPadded.rows*pointCloudPadded.cols*sizeof(pcl::PointXYZRGB));
|
|
|
95 |
|
|
|
96 |
// // filtering
|
|
|
97 |
// pcl::StatisticalOutlierRemoval<pcl::PointXYZRGB> filter;
|
|
|
98 |
// filter.setMeanK(5);
|
|
|
99 |
// filter.setStddevMulThresh(1.0);
|
|
|
100 |
// filter.setInputCloud(pointCloudPCL);
|
|
|
101 |
// pcl::PointCloud<pcl::PointXYZRGB>::Ptr pointCloudFiltered(new pcl::PointCloud<pcl::PointXYZRGB>);
|
|
|
102 |
// filter.filter(*pointCloudFiltered);
|
|
|
103 |
|
|
|
104 |
// Emit result
|
|
|
105 |
emit newPointCloud(pointCloudPCL);
|
|
|
106 |
|
|
|
107 |
std::cout << "Triangulator: " << time.elapsed() << "ms" << std::endl;
|
|
|
108 |
|
|
|
109 |
if(writeToDisk){
|
|
|
110 |
QString fileName = QDateTime::currentDateTime().toString("yyyyMMdd_HHmmsszzz");
|
|
|
111 |
fileName.append(".pcd");
|
|
|
112 |
pcl::io::savePCDFileBinary(fileName.toStdString(), *pointCloudPCL);
|
|
|
113 |
}
|
|
|
114 |
|
|
|
115 |
//emit finished();
|
|
|
116 |
}
|
|
|
117 |
|
|
|
118 |
SMTriangulationWorker::~SMTriangulationWorker(){
|
|
|
119 |
delete calibration;
|
|
|
120 |
delete triangulator;
|
|
|
121 |
|
|
|
122 |
std::cout<<"triangulatorWorker deleted\n"<<std::flush;
|
|
|
123 |
}
|