-
Notifications
You must be signed in to change notification settings - Fork 8
/
main.cpp
264 lines (215 loc) · 7.82 KB
/
main.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
#define _USE_MATH_DEFINES // for math constants in C++
#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/ocl/ocl.hpp>
#include <opencv2/viz/vizcore.hpp>
#include <cmath>
#include <string>
#include <iostream>
#include "Util.h"
#include "LightFieldPicture.h"
#include "ImageRenderer.h"
#include "ImageRenderer1.h"
#include "ImageRenderer4.h"
#include "DepthEstimator1.h"
#include "CDCDepthEstimator.h"
#include "DepthToPointTranslator.h"
#include "DepthToPointTranslator1.h"
#include "CameraPoseEstimator.h"
#include "CameraPoseEstimator1.h"
#include "RGBDMerger.h"
#include "RGBDMerger1.h"
#include "ReconstructionPipeline.h"
using namespace cv;
using namespace std;
const int lfpCount = 7;
const string lfpPaths[] = {
"C:\\Users\\Kai\\Downloads\\lfpextraction\\statue\\statue1.lfp",
"C:\\Users\\Kai\\Downloads\\lfpextraction\\statue\\statue2.lfp",
"C:\\Users\\Kai\\Downloads\\lfpextraction\\statue\\statue3.lfp",
"C:\\Users\\Kai\\Downloads\\lfpextraction\\statue\\statue4.lfp",
"C:\\Users\\Kai\\Downloads\\lfpextraction\\statue\\statue5.lfp",
"C:\\Users\\Kai\\Downloads\\lfpextraction\\statue\\statue6.lfp",
"C:\\Users\\Kai\\Downloads\\lfpextraction\\statue\\statue7.lfp"
};
// store compiled ocl kernels in this path
const char KERNEL_PATH[] = "C:\\Users\\Kai\\Downloads\\opencv_ocl_kernels\\";
// renders a series of images from a LightFieldPicture and displays or saves them
void showRefocusSeries(const LightFieldPicture& lightfield)
{
ImageRenderer* renderer = new ImageRenderer4();
renderer->setLightfield(lightfield);
Mat image;
string windowName;
for (int i = -20; i < lightfield.getLambdaInfinity() + 1.; i += 1)
{
renderer->setAlpha(i);
renderer->renderImage().download(image);
/*
const string path = "C:\\Users\\Kai\\Downloads\\lfpextraction\\kranhaus";
saveImageToPNGFile(path + to_string((long double)i) + ".png", image);
*/
/**/
windowName = "refocused image with alpha = " + to_string((long double) i);
namedWindow(windowName, WINDOW_AUTOSIZE);// Create a window for display. (scale down size)
imshow(windowName, image);
/**/
}
waitKey(0);
}
// reconstruct a scene from light-field data and render it
void renderReconstructionFromImageSeries(const string lfpPaths[])
{
LightFieldPicture* lightfield;
CDCDepthEstimator* estimator = new CDCDepthEstimator;
RGBDMerger* merger = new RGBDMerger1();
Mat calibrationMatrix;
vector<Mat> depthMaps = vector<Mat>(lfpCount);
vector<Mat> confidenceMaps = vector<Mat>(lfpCount);
vector<Mat> aifImages = vector<Mat>(lfpCount);
for (int i = 0; i < lfpCount; i++)
{
lightfield = new LightFieldPicture(lfpPaths[i]);
estimator->estimateDepth(*lightfield);
depthMaps[i] = estimator->getDepthMap();
confidenceMaps[i] = estimator->getConfidenceMap();
aifImages[i] = estimator->getExtendedDepthOfFieldImage();
}
calibrationMatrix = lightfield->getCalibrationMatrix();
merger->merge(aifImages, depthMaps, confidenceMaps, calibrationMatrix);
visualizePointCloud(merger->pointCloud, merger->pointColors);
}
// loads raw.lfp files using lfpPaths
vector<LightFieldPicture> loadLightFieldPictures()
{
vector<LightFieldPicture> lfps = vector<LightFieldPicture>(lfpCount);
for (int i = 0; i < lfpCount; i++)
{
lfps.at(i) = LightFieldPicture(lfpPaths[i]);
}
cout << lfpCount << " light-field files loaded" << endl;
return lfps;
}
void testCameraPoseEstimation()
{
vector<LightFieldPicture> lightfields = loadLightFieldPictures();
vector<Mat> images = vector<Mat>(lightfields.size());
ImageRenderer* renderer = new ImageRenderer4();
renderer->setAlpha(1.1);
for (int i = 0; i < images.size(); i++)
{
renderer->setLightfield(lightfields.at(i));
renderer->renderImage().download(images.at(i));
}
Mat calibrationMatrix = lightfields.at(0).getCalibrationMatrix();
CameraPoseEstimator* poseEstimator = new CameraPoseEstimator1();
double t0 = (double)getTickCount();
poseEstimator->estimateCameraPoses(images, calibrationMatrix);
double t1 = (double)getTickCount();
double d0 = (t1 - t0) / getTickFrequency();
cout << "Camera pose estimation took " << d0 << " seconds." << endl;
visualizeCameraTrajectory(*poseEstimator, Matx33d(calibrationMatrix));
}
void testPipeline()
{
vector<LightFieldPicture> lightfields = loadLightFieldPictures();
ReconstructionPipeline* pipeline = new ReconstructionPipeline();
pipeline->reconstructScene(lightfields);
visualizePointCloud(pipeline->pointCloud, pipeline->pointColors);
}
void testDepthEstimation(const LightFieldPicture& lightfield)
{
CDCDepthEstimator* estimator = new CDCDepthEstimator();
estimator->estimateDepth(lightfield);
ImageRenderer* renderer = new ImageRenderer4();
Mat image;
renderer->setLightfield(lightfield);
renderer->setAlpha(1.0);
renderer->renderImage().download(image);
/*
saveImageToPNGFile("C:\\Users\\Kai\\Downloads\\lfpextraction\\naturalImage.png",
image);
saveImageToPNGFile("C:\\Users\\Kai\\Downloads\\lfpextraction\\depthMap.png",
estimator->getDepthMap());
saveImageToPNGFile("C:\\Users\\Kai\\Downloads\\lfpextraction\\confidenceMap.png",
estimator->getConfidenceMap());
saveImageToPNGFile("C:\\Users\\Kai\\Downloads\\lfpextraction\\allInFocusImage.png",
estimator->getExtendedDepthOfFieldImage());
*/
Rect rect = Rect(5, 5, image.cols - 10, image.rows - 10);
Mat depthMap = Mat(estimator->getDepthMap(), rect);
Mat aifImage = Mat(estimator->getExtendedDepthOfFieldImage(), rect);
DepthToPointTranslator* translator = new DepthToPointTranslator1();
Mat pointCloud = translator->translateDepthToPoints(depthMap,
lightfield.getCalibrationMatrix(),
Mat::eye(3, 3, CV_64FC1), Mat::zeros(3, 1, CV_64FC1));
visualizePointCloud(pointCloud, aifImage);
}
int main( int argc, char** argv )
{
ocl::setBinaryPath(KERNEL_PATH);
if(argc != 2)
{
cout << "Usage: display_image ImageToLoadAndDisplay" << endl;
return -1;
}
Mat rawImage, subapertureImage, image1, image2, image4, image14;
oclMat ocl1, ocl2;
try {
//LightFieldPicture* lf = new LightFieldPicture("C:\\Users\\Kai\\Downloads\\lfpextraction\\fence.lfp");
//showRefocusSeries(*lf);
//testDepthEstimation(*lf);
//testCameraPoseEstimation();
testPipeline();
/*
LightFieldPicture* lf = new LightFieldPicture(argv[1]);
showRefocusSeries(*lf);
*/
//renderReconstructionFromImageSeries(lfpPaths);
/*
double t0 = (double)getTickCount();
LightFieldPicture lf(argv[1]);
double t1 = (double)getTickCount();
double d0 = (t1 - t0) / getTickFrequency();
cout << "Loading of file at " << argv[1] << " successful." << endl;
cout << "Loading of light field took " << d0 << " seconds." << endl;
ImageRenderer1 renderer = ImageRenderer1();
renderer.setAlpha(-0.5);
renderer.setLightfield(*lf);
//t0 = (double)getTickCount();
ocl1 = renderer.renderImage();
//t1 = (double)getTickCount();
d0 = (t1 - t0) / getTickFrequency();
cout << "Rendering took " << d0 << " seconds." << endl;
ocl1.download(image1);
saveImageToPNGFile("C:\\Users\\Kai\\Downloads\\lfpextraction\\Banding.png",
image1);
CDCDepthEstimator* estimator = new CDCDepthEstimator;
t0 = (double)getTickCount();
ocl2 = estimator->estimateDepth(lf);
t1 = (double)getTickCount();
d0 = (t1 - t0) / getTickFrequency();
cout << "Depth estimation took " << d0 << " seconds." << endl;
*/
/*
ocl2.download(image1);
normalize(image1, image1, 0, 1, NORM_MINMAX);
string window0 = "normalized depth";
namedWindow(window0, WINDOW_AUTOSIZE);// Create a window for display. (scale down size)
imshow(window0, image1);
waitKey(0);
*/
/*
ocl1.download(image1);
saveImageToPNGFile("depthMap.png", image1);
estimator->getConfidenceMap().download(image1);
saveImageToPNGFile("confidenceMap.png", image1);
estimator->getExtendedDepthOfFieldImage().download(image1);
saveImageToPNGFile("all-in-focus-image.png", image1);
*/
} catch (std::exception* e) {
cerr << e->what() << endl;
return -1;
}
return 0;
}