-
-
Notifications
You must be signed in to change notification settings - Fork 872
Expand file tree
/
Copy pathmapIO.cpp
More file actions
687 lines (582 loc) · 24.9 KB
/
mapIO.cpp
File metadata and controls
687 lines (582 loc) · 24.9 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
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
// This file is part of the AliceVision project.
// Copyright (c) 2022 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 "mapIO.hpp"
#include <aliceVision/mvsData/Point2d.hpp>
#include <aliceVision/mvsUtils/fileIO.hpp>
#include <aliceVision/numeric/numeric.hpp>
#include <aliceVision/image/io.hpp>
#include <aliceVision/utils/filesIO.hpp>
#include <boost/regex.hpp>
#include <filesystem>
namespace fs = std::filesystem;
namespace aliceVision {
namespace mvsUtils {
/**
* @brief Get the map name from the given fileType enum
* @param[in] fileType the map fileType enum
* @return map type string
*/
std::string getMapNameFromFileType(EFileType fileType)
{
switch (fileType)
{
case EFileType::depthMap:
return "depth map";
break;
case EFileType::depthMapFiltered:
return "filtered depth map";
break;
case EFileType::simMap:
return "similarity map";
break;
case EFileType::simMapFiltered:
return "filtered similarity map";
break;
case EFileType::normalMap:
return "normal map";
break;
case EFileType::normalMapFiltered:
return "filtered normal map";
break;
case EFileType::thicknessMap:
return "thickness map";
break;
case EFileType::pixSizeMap:
return "pixSize map";
break;
default:
break;
}
return "unknown map";
}
/**
* @brief Get tile map ROI from file metadata.
* @param[in] mapTilePath the tile map file path
* @param[in,out] out_roi the corresponding region-of-interest read from file metadata
*/
void getRoiFromMetadata(const std::string& mapTilePath, ROI& out_roi)
{
const oiio::ParamValueList metadata = image::readImageMetadata(mapTilePath);
const auto roiBeginXIt = metadata.find("AliceVision:roiBeginX");
const auto roiBeginYIt = metadata.find("AliceVision:roiBeginY");
const auto roiEndXIt = metadata.find("AliceVision:roiEndX");
const auto roiEndYIt = metadata.find("AliceVision:roiEndY");
if (roiBeginXIt != metadata.end() && roiBeginXIt->type() == oiio::TypeDesc::INT)
out_roi.x.begin = roiBeginXIt->get_int();
if (roiBeginYIt != metadata.end() && roiBeginYIt->type() == oiio::TypeDesc::INT)
out_roi.y.begin = roiBeginYIt->get_int();
if (roiEndXIt != metadata.end() && roiEndXIt->type() == oiio::TypeDesc::INT)
out_roi.x.end = roiEndXIt->get_int();
if (roiEndYIt != metadata.end() && roiEndYIt->type() == oiio::TypeDesc::INT)
out_roi.y.end = roiEndYIt->get_int();
// invalid or no roi metadata
if ((out_roi.x.begin < 0) || (out_roi.y.begin < 0) || (out_roi.x.end <= 0) || (out_roi.y.end <= 0))
{
ALICEVISION_THROW_ERROR("Cannot find ROI information in file: " << fs::path(mapTilePath).filename().string());
}
}
/**
* @brief Get tile map TileParams from file metadata.
* @param[in] mapTilePath the tile map file path
* @param[in,out] out_tileParams the corresponding TileParams read from file metadata
*/
void getTileParamsFromMetadata(const std::string& mapTilePath, TileParams& out_tileParams)
{
const oiio::ParamValueList metadata = image::readImageMetadata(mapTilePath);
const auto tileWidthIt = metadata.find("AliceVision:tileBufferWidth");
const auto tileHeightIt = metadata.find("AliceVision:tileBufferHeight");
const auto tilePaddingIt = metadata.find("AliceVision:tilePadding");
if (tileWidthIt != metadata.end() && tileWidthIt->type() == oiio::TypeDesc::INT)
out_tileParams.bufferWidth = tileWidthIt->get_int();
if (tileHeightIt != metadata.end() && tileHeightIt->type() == oiio::TypeDesc::INT)
out_tileParams.bufferHeight = tileHeightIt->get_int();
if (tilePaddingIt != metadata.end() && tilePaddingIt->type() == oiio::TypeDesc::INT)
out_tileParams.padding = tilePaddingIt->get_int();
// invalid or no tile metadata
if ((out_tileParams.bufferWidth <= 0) || (out_tileParams.bufferHeight <= 0) || (out_tileParams.padding < 0))
{
ALICEVISION_THROW_ERROR("Cannot find tile parameters in file: " << fs::path(mapTilePath).filename().string());
}
}
/**
* @brief Get the tile map path list for a R camera et a given scale / stepXY.
* @param[in] rc the related R camera index
* @param[in] mp the multi-view parameters
* @param[in] fileType the map fileType enum
* @param[in] customSuffix the map filename custom suffix
* @param[in,out] out_mapTilePathList the tile map path list
*/
void getTilePathList(int rc,
const MultiViewParams& mp,
const EFileType fileType,
const std::string& customSuffix,
std::vector<std::string>& out_mapTilePathList)
{
const fs::path mapPath(getFileNameFromIndex(mp, rc, fileType, customSuffix));
const fs::path mapDirectory(mapPath.parent_path());
if (!is_directory(mapDirectory))
ALICEVISION_THROW_ERROR("Cannot find " << getMapNameFromFileType(fileType) << " directory (rc: " << rc << ").");
const boost::regex mapPattern(mapPath.stem().string() + "_\\d+_\\d+" + mapPath.extension().string());
for (auto& entry : fs::directory_iterator{mapDirectory})
{
if (boost::regex_match(entry.path().filename().string(), mapPattern))
out_mapTilePathList.push_back(entry.path().string());
}
}
/**
* @brief Weight one of the corners/edges of a tile according to the size of the padding.
*
* When merging tiles, there are 8 intersection areas:
* * 4 corners (intersection of 4 tiles or 2 tiles when the tile is on one image edge)
* * 4 edges (intersection of 2 tiles)
*
* @param a alpha for top-left
* @param b alpha for top-right
* @param c alpha for bottom-right
* @param d alpha for bottom-left
* @param borderWidth tiles intersection area width (could be the intersection between 2 or 4 tiles)
* @param borderHeight tiles intersection area height
* @param lu left-up corner of the intersection area in the tile coordinate system
* @param in_tileMap image of the tile
*/
template<typename T>
void weightTileBorder(int a, int b, int c, int d, int borderWidth, int borderHeight, const Point2d& lu, image::Image<T>& in_tileMap)
{
const Point2d rd = lu + Point2d(borderWidth, borderHeight);
const int endX = std::min(int(rd.x), in_tileMap.width());
const int endY = std::min(int(rd.y), in_tileMap.height());
// Add small margin where alpha is 0 for corners (lu and rd)
static const double margin = 2.0;
const Point2d lu_m(lu.x + margin, lu.y + margin);
const Point2d rd_m(rd.x - margin, rd.y - margin);
const double borderWidth_m = borderWidth - 2.0 * margin;
const double borderHeight_m = borderHeight - 2.0 * margin;
for (int x = lu.x; x < endX; ++x)
{
for (int y = lu.y; y < endY; ++y)
{
// bilinear interpolation
const float r_x = clamp((rd_m.x - x) / borderWidth_m, 0.0, 1.0);
const float r_y = clamp((rd_m.y - y) / borderHeight_m, 0.0, 1.0);
const float l_x = clamp((x - lu_m.x) / borderWidth_m, 0.0, 1.0);
const float l_y = clamp((y - lu_m.y) / borderHeight_m, 0.0, 1.0);
const float weight = r_y * (r_x * a + l_x * b) + l_y * (r_x * d + l_x * c);
// apply weight to tile depth/sim map
in_tileMap(y, x) *= weight;
}
}
}
/**
* @brief Add a single tile to a full map with weighting.
* @param[in] rc the related R camera index
* @param[in] mp the multi-view parameters
* @param[in] tileParams tile workflow parameters
* @param[in] roi the 2d region of interest without any downscale apply
* @param[in] downscale the map downscale factor
* @param[in] in_tileMap the tile map to add
* @param[in,out] inout_map the full output map
*/
template<typename T>
void addSingleTileMapWeighted(int rc,
const MultiViewParams& mp,
const TileParams& tileParams,
const ROI& roi,
int downscale,
image::Image<T>& in_tileMap,
image::Image<T>& inout_map)
{
// get downscaled ROI
const ROI downscaledRoi = downscaleROI(roi, downscale);
// get tile border size
const int tileWidth = downscaledRoi.width();
const int tileHeight = downscaledRoi.height();
const int tilePadding = tileParams.padding / downscale;
// get tile position information
const bool firstColumn = (roi.x.begin == 0);
const bool lastColumn = (roi.x.end == mp.getWidth(rc));
const bool firstRow = (roi.y.begin == 0);
const bool lastRow = (roi.y.end == mp.getHeight(rc));
// weight the top left corner
if (!firstColumn || !firstRow)
{
const Point2d lu(0, 0);
const int b = (firstRow) ? 1 : 0;
const int d = (firstColumn) ? 1 : 0;
weightTileBorder(0, b, 1, d, tilePadding, tilePadding, lu, in_tileMap);
}
// weight the bottom left corner
if (!firstColumn || !lastRow)
{
const Point2d lu(0, tileHeight - tilePadding);
const int a = (firstColumn) ? 1 : 0;
const int c = (lastRow) ? 1 : 0;
weightTileBorder(a, 1, c, 0, tilePadding, tilePadding, lu, in_tileMap);
}
// weight the top right corner
if (!lastColumn || !firstRow)
{
const Point2d lu(tileWidth - tilePadding, 0);
const int a = (firstRow) ? 1 : 0;
const int c = (lastColumn) ? 1 : 0;
weightTileBorder(a, 0, c, 1, tilePadding, tilePadding, lu, in_tileMap);
}
// weight the bottom right corner
if (!lastColumn || !lastRow)
{
const Point2d lu(tileWidth - tilePadding, tileHeight - tilePadding);
const int b = (lastColumn) ? 1 : 0;
const int d = (lastRow) ? 1 : 0;
weightTileBorder(1, b, 0, d, tilePadding, tilePadding, lu, in_tileMap);
}
// weight the top border
if (!firstRow)
{
const Point2d lu(tilePadding, 0);
weightTileBorder(0, 0, 1, 1, tileWidth - 2 * tilePadding, tilePadding, lu, in_tileMap);
}
// weight the bottom border
if (!lastRow)
{
const Point2d lu(tilePadding, tileHeight - tilePadding);
weightTileBorder(1, 1, 0, 0, tileWidth - 2 * tilePadding, tilePadding, lu, in_tileMap);
}
// weight the left border
if (!firstColumn)
{
const Point2d lu(0, tilePadding);
weightTileBorder(0, 1, 1, 0, tilePadding, tileHeight - 2 * tilePadding, lu, in_tileMap);
}
// weight the right border
if (!lastColumn)
{
const Point2d lu(tileWidth - tilePadding, tilePadding);
weightTileBorder(1, 0, 0, 1, tilePadding, tileHeight - 2 * tilePadding, lu, in_tileMap);
}
// add weighted tile to the depth/sim map
for (int x = downscaledRoi.x.begin; x < downscaledRoi.x.end; ++x)
{
for (int y = downscaledRoi.y.begin; y < downscaledRoi.y.end; ++y)
{
const int tx = x - downscaledRoi.x.begin;
const int ty = y - downscaledRoi.y.begin;
inout_map(y, x) += in_tileMap(ty, tx);
}
}
}
template<typename T>
void readMapFromFileOrTiles(int rc,
const MultiViewParams& mp,
EFileType fileType,
image::Image<T>& out_map,
int scale,
int step,
const std::string& customSuffix)
{
// assert scale & step
assert(scale > 0);
assert(step > 0);
// single file fullsize map path
const std::string mapPath = getFileNameFromIndex(mp, rc, fileType, customSuffix);
// check single file fullsize map exists
if (utils::exists(mapPath))
{
ALICEVISION_LOG_TRACE("Load depth map (full image): " << mapPath << ", scale: " << scale << ", step: " << step);
// read single file fullsize map
image::readImage(mapPath, out_map, image::EImageColorSpace::NO_CONVERSION);
return;
}
ALICEVISION_LOG_TRACE("No full image depth map: " << mapPath << ", scale: " << scale << ", step: " << step << ". Looking for tiles.");
// read map from tiles
const ROI imageRoi(Range(0, mp.getWidth(rc)), Range(0, mp.getHeight(rc)));
const int scaleStep = scale * step;
const int width = divideRoundUp(mp.getWidth(rc), scaleStep);
const int height = divideRoundUp(mp.getHeight(rc), scaleStep);
// the output full map
out_map.resize(width, height, true, T(0.f)); // should be initialized, additive process
// get tile map path list for the given R camera
std::vector<std::string> mapTilePathList;
getTilePathList(rc, mp, fileType, customSuffix, mapTilePathList);
if (mapTilePathList.empty())
{
// map can be empty
ALICEVISION_LOG_INFO("Cannot find any " << getMapNameFromFileType(fileType) << " tile file (rc: " << rc << ").");
return; // nothing to do, already initialized
}
else
{
ALICEVISION_LOG_TRACE("Load depth map from " << mapTilePathList.size() << " tiles. First tile: " << mapTilePathList[0] << ", scale: " << scale
<< ", step: " << step);
}
// get tileParams from first tile file metadata
TileParams tileParams;
getTileParamsFromMetadata(mapTilePathList.front(), tileParams);
// get tile roi list from each file metadata
std::vector<ROI> tileRoiList;
tileRoiList.resize(mapTilePathList.size());
for (size_t i = 0; i < mapTilePathList.size(); ++i)
{
getRoiFromMetadata(mapTilePathList.at(i), tileRoiList.at(i));
}
// read and add each tile to the full map
for (size_t i = 0; i < tileRoiList.size(); ++i)
{
const ROI roi = intersect(tileRoiList.at(i), imageRoi);
const std::string mapTilePath = getFileNameFromIndex(mp, rc, fileType, customSuffix, roi.x.begin, roi.y.begin);
if (roi.isEmpty())
continue;
try
{
// read tile
image::Image<T> tileMap;
image::readImage(mapTilePath, tileMap, image::EImageColorSpace::NO_CONVERSION);
// add tile to the full map
addSingleTileMapWeighted(rc, mp, tileParams, roi, scaleStep, tileMap, out_map);
}
catch (const std::exception& e)
{
ALICEVISION_LOG_WARNING("Cannot find map (rc: " << rc << "): " << fs::path(mapTilePath).filename().string());
}
}
}
template<typename T>
void writeMapToFileOrTile(int rc,
const MultiViewParams& mp,
const EFileType fileType,
const TileParams& tileParams,
const ROI& roi,
const image::Image<T>& in_map,
int scale,
int step,
const std::string& customSuffix = "")
{
// assert scale & step
assert(scale > 0);
assert(step > 0);
const int scaleStep = scale * step;
// get image dimensions at scale / stepXY
const int imageWidth = divideRoundUp(mp.getWidth(rc), scaleStep);
const int imageHeight = divideRoundUp(mp.getHeight(rc), scaleStep);
// get downscaled ROI
const ROI downscaledROI = downscaleROI(roi, scaleStep);
// check input map dimensions
assert(in_map.width() == downscaledROI.width() && in_map.width() <= imageWidth);
assert(in_map.height() == downscaledROI.height() && in_map.height() <= imageHeight);
// set OIIO ROI for map writing
// displayRoi is the image region of interest for display (image size)
// pixelRoi is the buffer region of interest within the displayRoi (tile size)
// no tiling if displayRoi == pixelRoi
const oiio::ROI displayRoi(0, imageWidth, 0, imageHeight);
const oiio::ROI pixelRoi(downscaledROI.x.begin, downscaledROI.x.end, downscaledROI.y.begin, downscaledROI.y.end);
// output map path
std::string mapPath;
if (downscaledROI.width() != imageWidth || downscaledROI.height() != imageHeight) // is a tile
{
// tiled map
mapPath = getFileNameFromIndex(mp, rc, fileType, customSuffix, roi.x.begin, roi.y.begin);
}
else
{
// fullsize map
mapPath = getFileNameFromIndex(mp, rc, fileType, customSuffix);
}
// original picture file metadata
oiio::ParamValueList metadata = image::getMetadataFromMap(mp.getMetadata(rc));
// downscale metadata
metadata.push_back(oiio::ParamValue("AliceVision:downscale", mp.getDownscaleFactor(rc) * scaleStep));
// roi metadata
{
metadata.push_back(oiio::ParamValue("AliceVision:roiBeginX", int(roi.x.begin)));
metadata.push_back(oiio::ParamValue("AliceVision:roiBeginY", int(roi.y.begin)));
metadata.push_back(oiio::ParamValue("AliceVision:roiEndX", int(roi.x.end)));
metadata.push_back(oiio::ParamValue("AliceVision:roiEndY", int(roi.y.end)));
}
// tile params metadata
{
metadata.push_back(oiio::ParamValue("AliceVision:tileBufferWidth", tileParams.bufferWidth));
metadata.push_back(oiio::ParamValue("AliceVision:tileBufferHeight", tileParams.bufferHeight));
metadata.push_back(oiio::ParamValue("AliceVision:tilePadding", tileParams.padding));
}
// projection matrix metadata
{
std::vector<double> matrixP = mp.getOriginalP(rc);
metadata.push_back(oiio::ParamValue("AliceVision:P", oiio::TypeDesc(oiio::TypeDesc::DOUBLE, oiio::TypeDesc::MATRIX44), 1, matrixP.data()));
}
// CArr & iCamArr metadata
{
Point3d C = mp.CArr[rc];
Matrix3x3 iP = mp.iCamArr[rc];
if (scaleStep > 1)
{
Matrix3x4 P = mp.camArr[rc];
for (int i = 0; i < 8; ++i)
P.m[i] /= double(scaleStep);
Matrix3x3 K, iK;
Matrix3x3 R, iR;
P.decomposeProjectionMatrix(K, R, C); // replace C
iK = K.inverse();
iR = R.inverse();
iP = iR * iK; // replace iP
}
metadata.push_back(oiio::ParamValue("AliceVision:CArr", oiio::TypeDesc(oiio::TypeDesc::DOUBLE, oiio::TypeDesc::VEC3), 1, C.m));
metadata.push_back(oiio::ParamValue("AliceVision:iCamArr", oiio::TypeDesc(oiio::TypeDesc::DOUBLE, oiio::TypeDesc::MATRIX33), 1, iP.m));
}
// min/max/nb depth metadata (for depth map only)
if ((fileType == EFileType::depthMap) || (fileType == EFileType::depthMapFiltered))
{
const int nbDepthValues = std::count_if(in_map.data(), in_map.data() + in_map.size(), [](float v) { return v > 0.0f; });
float maxDepth = -1.0f;
float minDepth = std::numeric_limits<float>::max();
for (int i = 0; i < in_map.size(); ++i)
{
const float depth = in_map(i);
if (depth <= -1.0f)
continue;
maxDepth = std::max(maxDepth, depth);
minDepth = std::min(minDepth, depth);
}
metadata.push_back(oiio::ParamValue("AliceVision:nbDepthValues", nbDepthValues));
metadata.push_back(oiio::ParamValue("AliceVision:minDepth", minDepth));
metadata.push_back(oiio::ParamValue("AliceVision:maxDepth", maxDepth));
}
// set colorspace
image::ImageWriteOptions mapWriteOptions;
mapWriteOptions.toColorSpace(image::EImageColorSpace::NO_CONVERSION);
// set storage type
if ((fileType == EFileType::depthMap) || (fileType == EFileType::depthMapFiltered))
{
mapWriteOptions.storageDataType(image::EStorageDataType::Float);
}
else
{
mapWriteOptions.storageDataType(image::EStorageDataType::Half);
}
// write map
image::writeImage(mapPath, in_map, mapWriteOptions, metadata, displayRoi, pixelRoi);
}
void addTileMapWeighted(int rc,
const MultiViewParams& mp,
const TileParams& tileParams,
const ROI& roi,
int downscale,
image::Image<float>& in_tileMap,
image::Image<float>& inout_map)
{
addSingleTileMapWeighted(rc, mp, tileParams, roi, downscale, in_tileMap, inout_map);
}
void readMap(int rc,
const MultiViewParams& mp,
const EFileType fileType,
image::Image<float>& out_map,
int scale,
int step,
const std::string& customSuffix)
{
readMapFromFileOrTiles(rc, mp, fileType, out_map, scale, step, customSuffix);
}
void readMap(int rc,
const MultiViewParams& mp,
const EFileType fileType,
image::Image<image::RGBfColor>& out_map,
int scale,
int step,
const std::string& customSuffix)
{
readMapFromFileOrTiles(rc, mp, fileType, out_map, scale, step, customSuffix);
}
void writeMap(int rc,
const MultiViewParams& mp,
const EFileType fileType,
const TileParams& tileParams,
const ROI& roi,
const image::Image<float>& in_map,
int scale,
int step,
const std::string& customSuffix)
{
writeMapToFileOrTile(rc, mp, fileType, tileParams, roi, in_map, scale, step, customSuffix);
}
void writeMap(int rc,
const MultiViewParams& mp,
const EFileType fileType,
const TileParams& tileParams,
const ROI& roi,
const image::Image<image::RGBfColor>& in_map,
int scale,
int step,
const std::string& customSuffix)
{
writeMapToFileOrTile(rc, mp, fileType, tileParams, roi, in_map, scale, step, customSuffix);
}
unsigned long getNbDepthValuesFromDepthMap(int rc, const MultiViewParams& mp, int scale, int step, const std::string& customSuffix)
{
const std::string depthMapPath = getFileNameFromIndex(mp, rc, EFileType::depthMapFiltered, customSuffix);
int nbDepthValues = -1;
bool fileExists = false;
bool fromTiles = false;
// get nbDepthValues from metadata
if (utils::exists(depthMapPath)) // untiled
{
fileExists = true;
const oiio::ParamValueList metadata = image::readImageMetadata(depthMapPath);
nbDepthValues = metadata.get_int("AliceVision:nbDepthValues", -1);
}
else // tiled
{
std::vector<std::string> mapTilePathList;
getTilePathList(rc, mp, EFileType::depthMapFiltered, customSuffix, mapTilePathList);
if (mapTilePathList.empty()) // depth map can be empty
{
ALICEVISION_LOG_INFO("Cannot find any depth map tile file (rc: " << rc << ").");
}
else
{
fileExists = true;
fromTiles = true;
}
for (const std::string& mapTilePath : mapTilePathList)
{
const oiio::ParamValueList metadata = image::readImageMetadata(mapTilePath);
const int nbTileDepthValues = metadata.get_int("AliceVision:nbDepthValues", -1);
if (nbTileDepthValues < 0)
ALICEVISION_THROW_ERROR("Cannot find or incorrect 'AliceVision:nbDepthValues' metadata in depth map tile (rc: " << rc << ")");
nbDepthValues += nbTileDepthValues;
}
}
ALICEVISION_LOG_TRACE("DepthMap: " << depthMapPath << ", fileExists: " << fileExists << ", fromTiles: " << fromTiles
<< ", nbDepthValues (from metadata): " << nbDepthValues);
// no metadata compute number of depth values
if (fileExists && nbDepthValues < 0)
{
image::Image<float> depthMap;
ALICEVISION_LOG_WARNING("Can't find or invalid 'nbDepthValues' metadata in depth map (rc: " << rc
<< "). Recompute the number of valid values.");
readMap(rc, mp, EFileType::depthMapFiltered, depthMap, scale, step, customSuffix);
nbDepthValues = std::count_if(depthMap.data(), depthMap.data() + depthMap.size(), [](float v) { return v > 0.0f; });
ALICEVISION_LOG_WARNING("nbDepthValues (recomputed from pixels): " << nbDepthValues);
}
return nbDepthValues;
}
void deleteMapTiles(int rc, const MultiViewParams& mp, const EFileType fileType, const std::string& customSuffix)
{
std::vector<std::string> mapTilePathList;
getTilePathList(rc, mp, fileType, customSuffix, mapTilePathList);
if (mapTilePathList.empty()) // depth map can be empty
ALICEVISION_LOG_INFO("Cannot find any " << getMapNameFromFileType(fileType) << " tile file to delete (rc: " << rc << ").");
// delete map tile files
for (const std::string& mapTilePath : mapTilePathList)
{
try
{
fs::remove(mapTilePath);
}
catch (const std::exception& e)
{
ALICEVISION_LOG_WARNING("Cannot delete map tile file (rc: " << rc << "): " << fs::path(mapTilePath).filename().string());
}
}
}
} // namespace mvsUtils
} // namespace aliceVision