forked from alicevision/AliceVision
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRefineRc.cpp
More file actions
472 lines (373 loc) · 16.4 KB
/
RefineRc.cpp
File metadata and controls
472 lines (373 loc) · 16.4 KB
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
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
// This file is part of the AliceVision project.
// Copyright (c) 2017 AliceVision contributors.
// This Source Code Form is subject to the terms of the Mozilla Public License,
// v. 2.0. If a copy of the MPL was not distributed with this file,
// You can obtain one at https://mozilla.org/MPL/2.0/.
#include "RefineRc.hpp"
#include <aliceVision/system/Logger.hpp>
#include <aliceVision/gpu/gpu.hpp>
#include <aliceVision/mvsData/Point2d.hpp>
#include <aliceVision/mvsData/Point3d.hpp>
#include <aliceVision/mvsUtils/common.hpp>
#include <aliceVision/mvsUtils/fileIO.hpp>
#include <aliceVision/mvsData/imageIO.hpp>
#include <aliceVision/alicevision_omp.hpp>
#include <boost/filesystem.hpp>
namespace aliceVision {
namespace depthMap {
namespace bfs = boost::filesystem;
RefineRc::RefineRc(int rc, int scale, int step, SemiGlobalMatchingParams* sp)
: SemiGlobalMatchingRc(rc, scale, step, sp)
{
const int nbNearestCams = sp->mp->userParams.get<int>("refineRc.maxTCams", 6);
_refineTCams = sp->mp->findNearestCamsFromLandmarks(rc, nbNearestCams);
_userTcOrPixSize = _sp->mp->userParams.get<bool>("refineRc.useTcOrRcPixSize", false);
_nbDepthsToRefine = _sp->mp->userParams.get<int>("refineRc.ndepthsToRefine", 31);
_refineWsh = _sp->mp->userParams.get<int>("refineRc.wsh", 3);
_refineNiters = _sp->mp->userParams.get<int>("refineRc.niters", 100);
_refineNSamplesHalf = _sp->mp->userParams.get<int>("refineRc.nSamplesHalf", 150);
_refineSigma = static_cast<float>(_sp->mp->userParams.get<double>("refineRc.sigma", 15.0));
_refineGammaC = static_cast<float>(_sp->mp->userParams.get<double>("refineRc.gammaC", 15.5));
_refineGammaP = static_cast<float>(_sp->mp->userParams.get<double>("refineRc.gammaP", 8.0));
}
RefineRc::~RefineRc()
{
delete _depthSimMapOpt;
}
void RefineRc::preloadSgmTcams_async()
{
for(int tc : _sgmTCams.getData())
_sp->cps._ic.refreshData_async(tc);
}
DepthSimMap* RefineRc::getDepthPixSizeMapFromSGM()
{
const int w11 = _sp->mp->getWidth(_rc);
const int h11 = _sp->mp->getHeight(_rc);
const int zborder = 2;
StaticVector<IdValue> volumeBestIdVal;
volumeBestIdVal.reserve(_width * _height);
for(int i = 0; i < _volumeBestIdVal.size(); i++)
{
// float sim = (*depthSimMapFinal->dsm)[i].y;
// sim = std::min(sim,mp->simThr);
const float sim = _sp->mp->simThr - 0.0001;
const int id = _volumeBestIdVal[i].id;
if(id > 0)
volumeBestIdVal.push_back(IdValue(id, sim));
else
volumeBestIdVal.push_back(IdValue(0, sim));
}
DepthSimMap* depthSimMapScale1Step1 = new DepthSimMap(_rc, _sp->mp, 1, 1);
{
DepthSimMap* depthSimMap = _sp->getDepthSimMapFromBestIdVal(_width, _height, &volumeBestIdVal, _scale, _step, _rc, zborder, _depths);
depthSimMapScale1Step1->add11(depthSimMap);
delete depthSimMap;
}
// set sim (y) to pixsize
for(int y = 0; y < h11; ++y)
{
for(int x = 0; x < w11; ++x)
{
Point3d p = _sp->mp->CArr[_rc] + (_sp->mp->iCamArr[_rc] * Point2d(static_cast<float>(x), static_cast<float>(y))).normalize() * (*depthSimMapScale1Step1->dsm)[y * w11 + x].depth;
if(_userTcOrPixSize)
(*depthSimMapScale1Step1->dsm)[y * w11 + x].sim = _sp->mp->getCamsMinPixelSize(p, _refineTCams);
else
(*depthSimMapScale1Step1->dsm)[y * w11 + x].sim = _sp->mp->getCamPixelSize(p, _rc);
}
}
return depthSimMapScale1Step1;
}
DepthSimMap* RefineRc::refineAndFuseDepthSimMapCUDA(DepthSimMap* depthPixSizeMapVis)
{
int w11 = _sp->mp->getWidth(_rc);
int h11 = _sp->mp->getHeight(_rc);
StaticVector<DepthSimMap*>* dataMaps = new StaticVector<DepthSimMap*>();
dataMaps->reserve(_refineTCams.size() + 1);
dataMaps->push_back(depthPixSizeMapVis); //!!DO NOT ERASE!!!
const int scale = 1;
for(int c = 0; c < _refineTCams.size(); c++)
{
int tc = _refineTCams[c];
DepthSimMap* depthSimMapC = new DepthSimMap(_rc, _sp->mp, scale, 1);
StaticVector<float>* depthMap = depthPixSizeMapVis->getDepthMap();
depthSimMapC->initJustFromDepthMap(depthMap, 1.0f);
delete depthMap;
_sp->prt->refineRcTcDepthSimMap(_userTcOrPixSize, depthSimMapC, _rc, tc, _nbDepthsToRefine, _refineWsh, _refineGammaC, _refineGammaP,
0.0f);
dataMaps->push_back(depthSimMapC);
if(_sp->exportIntermediateResults)
depthSimMapC->saveToImage(_sp->mp->getDepthMapsFolder() + "refine_photo_" + std::to_string(_sp->mp->getViewId(_rc)) + "_tc_" +
std::to_string(_sp->mp->getViewId(tc)) + ".png", -2.0f);
}
// in order to fit into GPU memory
DepthSimMap* depthSimMapFused = new DepthSimMap(_rc, _sp->mp, scale, 1);
int nhParts = 4;
int hPartHeightGlob = h11 / nhParts;
for(int hPart = 0; hPart < nhParts; hPart++)
{
int hPartHeight = std::min(h11, (hPart + 1) * hPartHeightGlob) - hPart * hPartHeightGlob;
// vector of one depthSimMap tile per Tc
StaticVector<StaticVector<DepthSim>*>* dataMapsHPart =
new StaticVector<StaticVector<DepthSim>*>();
dataMapsHPart->reserve(dataMaps->size());
for(int i = 0; i < dataMaps->size(); i++) // iterate over Tc cameras
{
StaticVector<DepthSim>* dataMapHPart = new StaticVector<DepthSim>();
dataMapHPart->reserve(w11 * hPartHeight);
dataMapHPart->resize(w11 * hPartHeight);
#pragma omp parallel for
for(int y = 0; y < hPartHeight; y++)
{
for(int x = 0; x < w11; x++)
{
(*dataMapHPart)[y * w11 + x] = (*(*dataMaps)[i]->dsm)[(y + hPart * hPartHeightGlob) * w11 + x];
}
}
dataMapsHPart->push_back(dataMapHPart);
}
StaticVector<DepthSim>* depthSimMapFusedHPart = new StaticVector<DepthSim>();
depthSimMapFusedHPart->reserve(w11 * hPartHeight);
depthSimMapFusedHPart->resize_with(w11 * hPartHeight, DepthSim(-1.0f, 1.0f));
_sp->cps.fuseDepthSimMapsGaussianKernelVoting(w11, hPartHeight, depthSimMapFusedHPart, dataMapsHPart,
_refineNSamplesHalf, _nbDepthsToRefine, _refineSigma);
#pragma omp parallel for
for(int y = 0; y < hPartHeight; y++)
{
for(int x = 0; x < w11; x++)
{
(*depthSimMapFused->dsm)[(y + hPart * hPartHeightGlob) * w11 + x] =
(*depthSimMapFusedHPart)[y * w11 + x];
}
}
delete depthSimMapFusedHPart;
deleteArrayOfArrays<DepthSim>(&dataMapsHPart);
}
(*dataMaps)[0] = nullptr; // it is input dsmap we dont want to delete it
for(int c = 0; c < _refineTCams.size(); c++)
{
delete(*dataMaps)[c + 1];
}
delete dataMaps;
return depthSimMapFused;
}
DepthSimMap* RefineRc::optimizeDepthSimMapCUDA(DepthSimMap* depthPixSizeMapVis,
DepthSimMap* depthSimMapPhoto)
{
int h11 = _sp->mp->getHeight(_rc);
StaticVector<DepthSimMap*>* dataMaps = new StaticVector<DepthSimMap*>();
dataMaps->reserve(2);
dataMaps->push_back(depthPixSizeMapVis); //!!DO NOT ERASE!!!
dataMaps->push_back(depthSimMapPhoto); //!!DO NOT ERASE!!!
DepthSimMap* depthSimMapOptimized = new DepthSimMap(_rc, _sp->mp, 1, 1);
{
StaticVector<StaticVector<DepthSim>*>* dataMapsPtrs = new StaticVector<StaticVector<DepthSim>*>();
dataMapsPtrs->reserve(dataMaps->size());
for(int i = 0; i < dataMaps->size(); i++)
{
dataMapsPtrs->push_back((*dataMaps)[i]->dsm);
}
int nParts = 4;
int hPart = h11 / nParts;
for(int part = 0; part < nParts; part++)
{
int yFrom = part * hPart;
int hPartAct = std::min(hPart, h11 - yFrom);
_sp->cps.optimizeDepthSimMapGradientDescent(depthSimMapOptimized->dsm, dataMapsPtrs, _rc, _refineNSamplesHalf,
_nbDepthsToRefine, _refineSigma, _refineNiters, yFrom, hPartAct);
}
for(int i = 0; i < dataMaps->size(); i++)
{
(*dataMapsPtrs)[i] = nullptr;
}
delete dataMapsPtrs;
}
(*dataMaps)[0] = nullptr; // it is input dsmap we dont want to delete it
(*dataMaps)[1] = nullptr; // it is input dsmap we dont want to delete it
delete dataMaps;
return depthSimMapOptimized;
}
bool RefineRc::refinerc(bool checkIfExists)
{
const IndexT viewId = _sp->mp->getViewId(_rc);
if(_sp->mp->verbose)
ALICEVISION_LOG_DEBUG("Refine CUDA (rc: " << (_rc + 1) << " / " << _sp->mp->ncams << ")");
// generate default depthSimMap if rc has no tcam
if(_refineTCams.size() == 0 || _depths.empty())
{
_depthSimMapOpt = new DepthSimMap(_rc, _sp->mp, 1, 1);
return true;
}
if(checkIfExists && (mvsUtils::FileExists(_sp->getREFINE_opt_simMapFileName(viewId, 1, 1))))
{
ALICEVISION_LOG_INFO("Already computed: " + _sp->getREFINE_opt_simMapFileName(viewId, 1, 1));
return false;
}
long tall = clock();
DepthSimMap* depthPixSizeMapVis = getDepthPixSizeMapFromSGM();
DepthSimMap* depthSimMapPhoto = refineAndFuseDepthSimMapCUDA(depthPixSizeMapVis);
if(_sp->doRefineRc)
{
_depthSimMapOpt = optimizeDepthSimMapCUDA(depthPixSizeMapVis, depthSimMapPhoto);
}
else
{
_depthSimMapOpt = new DepthSimMap(_rc, _sp->mp, 1, 1);
_depthSimMapOpt->add(depthSimMapPhoto);
}
if(_sp->exportIntermediateResults)
{
depthPixSizeMapVis->saveToImage(_sp->mp->getDepthMapsFolder() + "refine_" + std::to_string(viewId) + "_vis.png", 0.0f);
depthSimMapPhoto->saveToImage(_sp->mp->getDepthMapsFolder() + "refine_" + std::to_string(viewId) + "_photo.png", 0.0f);
depthSimMapPhoto->saveRefine(_rc, _sp->getREFINE_photo_depthMapFileName(viewId, 1, 1), _sp->getREFINE_photo_simMapFileName(viewId, 1, 1));
_depthSimMapOpt->saveToImage(_sp->mp->getDepthMapsFolder() + "refine_" + std::to_string(viewId) + "_opt.png", 0.0f);
_depthSimMapOpt->saveRefine(_rc, _sp->getREFINE_opt_depthMapFileName(viewId, 1, 1), _sp->getREFINE_opt_simMapFileName(viewId, 1, 1));
}
mvsUtils::printfElapsedTime(tall, "Refine CUDA (rc: " + mvsUtils::num2str(_rc) + " / " + mvsUtils::num2str(_sp->mp->ncams) + ")");
delete depthPixSizeMapVis;
delete depthSimMapPhoto;
return true;
}
void RefineRc::writeDepthMap()
{
if(_depthSimMapOpt)
_depthSimMapOpt->save(_rc, _refineTCams);
}
void estimateAndRefineDepthMaps(mvsUtils::MultiViewParams* mp, const std::vector<int>& cams, int nbGPUs)
{
const int numGpus = listCUDADevices(true);
const int numCpuThreads = omp_get_num_procs();
int numThreads = std::min(numGpus, numCpuThreads);
ALICEVISION_LOG_INFO("# GPU devices: " << numGpus << ", # CPU threads: " << numCpuThreads);
if(nbGPUs > 0)
numThreads = nbGPUs;
if(numThreads == 1)
{
// the GPU sorting is determined by an environment variable named CUDA_DEVICE_ORDER
// possible values: FASTEST_FIRST (default) or PCI_BUS_ID
const int cudaDeviceNo = 0;
estimateAndRefineDepthMaps(cudaDeviceNo, mp, cams);
}
else
{
omp_set_num_threads(numThreads); // create as many CPU threads as there are CUDA devices
#pragma omp parallel
{
const int cpuThreadId = omp_get_thread_num();
const int cudaDeviceNo = cpuThreadId % numThreads;
const int rcFrom = cudaDeviceNo * (cams.size() / numThreads);
int rcTo = (cudaDeviceNo + 1) * (cams.size() / numThreads);
ALICEVISION_LOG_INFO("CPU thread " << cpuThreadId << " / " << numThreads << " uses CUDA device: " << cudaDeviceNo);
if(cudaDeviceNo == numThreads - 1)
rcTo = cams.size();
std::vector<int> subcams;
subcams.reserve(cams.size());
for(int rc = rcFrom; rc < rcTo; rc++)
subcams.push_back(cams[rc]);
estimateAndRefineDepthMaps(cpuThreadId, mp, subcams);
}
}
}
void estimateAndRefineDepthMaps(int cudaDeviceNo, mvsUtils::MultiViewParams* mp, const std::vector<int>& cams)
{
const int fileScale = 1; // input images scale (should be one)
int sgmScale = mp->userParams.get<int>("semiGlobalMatching.scale", -1);
int sgmStep = mp->userParams.get<int>("semiGlobalMatching.step", -1);
if(sgmScale == -1)
{
// compute the number of scales that will be used in the plane sweeping.
// the highest scale should have a minimum resolution of 700x550.
const int width = mp->getMaxImageWidth();
const int height = mp->getMaxImageHeight();
const int scaleTmp = computeStep(mp, fileScale, (width > height ? 700 : 550), (width > height ? 550 : 700));
sgmScale = std::min(2, scaleTmp);
sgmStep = computeStep(mp, fileScale * sgmScale, (width > height ? 700 : 550), (width > height ? 550 : 700));
ALICEVISION_LOG_INFO("Plane sweeping parameters:\n"
"\t- scale: " << sgmScale << "\n"
"\t- step: " << sgmStep);
}
// load images from files into RAM
mvsUtils::ImagesCache ic(mp, imageIO::EImageColorSpace::LINEAR);
// load stuff on GPU memory and creates multi-level images and computes gradients
PlaneSweepingCuda cps(cudaDeviceNo, ic, mp, sgmScale);
// init plane sweeping parameters
SemiGlobalMatchingParams sp(mp, cps);
for(const int rc : cams)
{
RefineRc sgmRefineRc(rc, sgmScale, sgmStep, &sp);
sgmRefineRc.preloadSgmTcams_async();
ALICEVISION_LOG_INFO("Estimate depth map, view id: " << mp->getViewId(rc));
sgmRefineRc.sgmrc();
ALICEVISION_LOG_INFO("Refine depth map, view id: " << mp->getViewId(rc));
sgmRefineRc.refinerc();
// write results
sgmRefineRc.writeDepthMap();
}
}
void computeNormalMaps(int CUDADeviceNo, mvsUtils::MultiViewParams* mp, const StaticVector<int>& cams)
{
const float igammaC = 1.0f;
const float igammaP = 1.0f;
const int wsh = 3;
mvsUtils::ImagesCache ic(mp, imageIO::EImageColorSpace::LINEAR);
PlaneSweepingCuda cps(CUDADeviceNo, ic, mp, 1);
for(const int rc : cams)
{
const std::string normalMapFilepath = getFileNameFromIndex(mp, rc, mvsUtils::EFileType::normalMap, 0);
if(!mvsUtils::FileExists(normalMapFilepath))
{
StaticVector<float> depthMap;
int w = 0;
int h = 0;
imageIO::readImage(getFileNameFromIndex(mp, rc, mvsUtils::EFileType::depthMap, 0), w, h, depthMap.getDataWritable(), imageIO::EImageColorSpace::NO_CONVERSION);
StaticVector<Color> normalMap;
normalMap.resize(mp->getWidth(rc) * mp->getHeight(rc));
cps.computeNormalMap(&depthMap, &normalMap, rc, 1, igammaC, igammaP, wsh);
using namespace imageIO;
OutputFileColorSpace colorspace(EImageColorSpace::NO_CONVERSION);
writeImage(normalMapFilepath, mp->getWidth(rc), mp->getHeight(rc), normalMap.getDataWritable(), EImageQuality::LOSSLESS, colorspace);
}
}
}
void computeNormalMaps(mvsUtils::MultiViewParams* mp, const StaticVector<int>& cams)
{
const int nbGPUs = listCUDADevices(true);
const int nbCPUThreads = omp_get_num_procs();
ALICEVISION_LOG_INFO("Number of GPU devices: " << nbGPUs << ", number of CPU threads: " << nbCPUThreads);
const int nbGPUsToUse = mp->userParams.get<int>("refineRc.num_gpus_to_use", 1);
int nbThreads = std::min(nbGPUs, nbCPUThreads);
if(nbGPUsToUse > 0)
{
nbThreads = nbGPUsToUse;
}
if(nbThreads == 1)
{
const int CUDADeviceNo = 0;
computeNormalMaps(CUDADeviceNo, mp, cams);
}
else
{
omp_set_num_threads(nbThreads); // create as many CPU threads as there are CUDA devices
#pragma omp parallel
{
const int cpuThreadId = omp_get_thread_num();
const int CUDADeviceNo = cpuThreadId % nbThreads;
ALICEVISION_LOG_INFO("CPU thread " << cpuThreadId << " (of " << nbThreads << ") uses CUDA device: " << CUDADeviceNo);
const int nbCamsPerThread = (cams.size() / nbThreads);
const int rcFrom = CUDADeviceNo * nbCamsPerThread;
int rcTo = (CUDADeviceNo + 1) * nbCamsPerThread;
if(CUDADeviceNo == nbThreads - 1)
{
rcTo = cams.size();
}
StaticVector<int> subcams;
subcams.reserve(cams.size());
for(int rc = rcFrom; rc < rcTo; ++rc)
{
subcams.push_back(cams[rc]);
}
computeNormalMaps(CUDADeviceNo, mp, subcams);
}
}
}
} // namespace depthMap
} // namespace aliceVision