Skip to content

Commit

Permalink
Merge pull request #642 from alicevision/fix_depthMap
Browse files Browse the repository at this point in the history
[depthMap] bug fix: missing allocation when reducing the number of planes
  • Loading branch information
yann-lty authored Jun 6, 2019
2 parents 0e0579f + ec7593b commit d16c093
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 44 deletions.
2 changes: 1 addition & 1 deletion src/aliceVision/depthMap/RefineRc.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -238,7 +238,7 @@ bool RefineRc::refinerc(bool checkIfExists)
ALICEVISION_LOG_DEBUG("Refine CUDA (rc: " << (_rc + 1) << " / " << _sp->mp->ncams << ")");

// generate default depthSimMap if rc has no tcam
if(_refineTCams.size() == 0 || _depths == nullptr)
if(_refineTCams.size() == 0 || _depths.empty())
{
_depthSimMapOpt = new DepthSimMap(_rc, _sp->mp, 1, 1);
return true;
Expand Down
6 changes: 3 additions & 3 deletions src/aliceVision/depthMap/SemiGlobalMatchingParams.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ std::string SemiGlobalMatchingParams::getSGM_depthsFileName(IndexT viewId)

DepthSimMap* SemiGlobalMatchingParams::getDepthSimMapFromBestIdVal(int w, int h, StaticVector<IdValue>* volumeBestIdVal,
int scale, int step, int rc, int zborder,
StaticVector<float>* planesDepths)
const StaticVector<float>& planesDepths)
{
long tall = clock();

Expand All @@ -142,9 +142,9 @@ DepthSimMap* SemiGlobalMatchingParams::getDepthSimMapFromBestIdVal(int w, int h,
Pixel pixScale1 = Pixel(pix.x * scale, pix.y * scale);
float sim = (*volumeBestIdVal)[y * volDimX + x].value;
int fpdepthId = (*volumeBestIdVal)[y * volDimX + x].id;
if((fpdepthId >= zborder) && (fpdepthId < planesDepths->size() - zborder))
if((fpdepthId >= zborder) && (fpdepthId < planesDepths.size() - zborder))
{
float fpPlaneDepth = (*planesDepths)[fpdepthId];
float fpPlaneDepth = planesDepths[fpdepthId];
Point3d planen = (mp->iRArr[rc] * Point3d(0.0f, 0.0f, 1.0f)).normalize();
Point3d planep = mp->CArr[rc] + planen * fpPlaneDepth;
Point3d v = (mp->iCamArr[rc] * Point2d((float)(pixScale1.x), (float)(pixScale1.y))).normalize();
Expand Down
2 changes: 1 addition & 1 deletion src/aliceVision/depthMap/SemiGlobalMatchingParams.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ class SemiGlobalMatchingParams
~SemiGlobalMatchingParams(void);

DepthSimMap* getDepthSimMapFromBestIdVal(int w, int h, StaticVector<IdValue>* volumeBestIdVal, int scale,
int step, int rc, int zborder, StaticVector<float>* planesDepths);
int step, int rc, int zborder, const StaticVector<float>& planesDepths);

std::string getREFINE_photo_depthMapFileName(IndexT viewId, int scale, int step);
std::string getREFINE_photo_simMapFileName(IndexT viewId, int scale, int step);
Expand Down
73 changes: 35 additions & 38 deletions src/aliceVision/depthMap/SemiGlobalMatchingRc.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -48,20 +48,19 @@ SemiGlobalMatchingRc::SemiGlobalMatchingRc(int rc, int scale, int step, SemiGlob
SemiGlobalMatchingRc::~SemiGlobalMatchingRc()
{
delete _volumeBestIdVal;
delete _depths;
}

bool SemiGlobalMatchingRc::selectBestDepthsRange(int nDepthsThr, StaticVector<float>* rcSeedsDistsAsc)
{
if(_depths->size() <= nDepthsThr)
if(_depths.size() <= nDepthsThr)
return true;

StaticVector<int> votes;
votes.reserve(_depths->size() - nDepthsThr);
for(int i = 0; i < _depths->size() - nDepthsThr; i++)
votes.reserve(_depths.size() - nDepthsThr);
for(int i = 0; i < _depths.size() - nDepthsThr; i++)
{
const float d1 = (*_depths)[i];
const float d2 = (*_depths)[i + nDepthsThr - 1];
const float d1 = _depths[i];
const float d2 = _depths[i + nDepthsThr - 1];

int id1 = rcSeedsDistsAsc->indexOfNearestSorted(d1);
int id2 = rcSeedsDistsAsc->indexOfNearestSorted(d2);
Expand All @@ -78,32 +77,31 @@ bool SemiGlobalMatchingRc::selectBestDepthsRange(int nDepthsThr, StaticVector<fl
votes.push_back(0);
}

StaticVector<float>* depthsNew;
depthsNew->reserve(nDepthsThr);
StaticVector<float> depthsNew;
depthsNew.reserve(nDepthsThr);

const int id1 = votes.maxValId();
const int id2 = id1 + nDepthsThr - 1;

for(int i = id1; i <= id2; i++)
depthsNew->push_back((*_depths)[i]);
depthsNew.push_back(_depths[i]);

std::swap(_depths, depthsNew);
delete depthsNew;
return true;
}

bool SemiGlobalMatchingRc::selectBestDepthsRange(int nDepthsThr, StaticVector<StaticVector<float>*>* alldepths)
{
if(_depths->size() <= nDepthsThr)
if(_depths.size() <= nDepthsThr)
return true;

StaticVector<float> votes;
votes.reserve(_depths->size() - nDepthsThr);
votes.reserve(_depths.size() - nDepthsThr);

for(int i = 0; i < _depths->size() - nDepthsThr; i++)
for(int i = 0; i < _depths.size() - nDepthsThr; i++)
{
const float d1 = (*_depths)[i];
const float d2 = (*_depths)[i + nDepthsThr - 1];
const float d1 = _depths[i];
const float d2 = _depths[i + nDepthsThr - 1];
float overlap = 0.0f;

for(int c = 0; c < alldepths->size(); c++)
Expand All @@ -117,17 +115,16 @@ bool SemiGlobalMatchingRc::selectBestDepthsRange(int nDepthsThr, StaticVector<St
votes.push_back(overlap);
}

StaticVector<float>* depthsNew;
depthsNew->reserve(nDepthsThr);
StaticVector<float> depthsNew;
depthsNew.reserve(nDepthsThr);

const int id1 = votes.maxValId();
const int id2 = id1 + nDepthsThr - 1;

for(int i = id1; i <= id2; i++)
depthsNew->push_back((*_depths)[i]);
depthsNew.push_back(_depths[i]);

std::swap(_depths, depthsNew);
delete depthsNew;
return true;
}

Expand Down Expand Up @@ -172,14 +169,14 @@ void SemiGlobalMatchingRc::computeDepths(float minDepth, float maxDepth, StaticV
}
}

_depths = new StaticVector<float>();
_depths->reserve(maxNdetphs);
_depths.resize(0);
_depths.reserve(maxNdetphs);

{
float depth = minDepth;
while(depth < maxDepth)
{
_depths->push_back(depth);
_depths.push_back(depth);
depth += getMinTcStepAtDepth(depth, minDepth, maxDepth, alldepths);
}
}
Expand Down Expand Up @@ -238,14 +235,14 @@ void SemiGlobalMatchingRc::computeDepthsTcamsLimits(StaticVector<StaticVector<fl
const float d1 = (*(*alldepths)[c])[0];
const float d2 = (*(*alldepths)[c])[(*alldepths)[c]->size() - 1];

int id1 = _depths->indexOfNearestSorted(d1);
int id2 = _depths->indexOfNearestSorted(d2);
int id1 = _depths.indexOfNearestSorted(d1);
int id2 = _depths.indexOfNearestSorted(d2);

if(id1 == -1)
id1 = 0;

if(id2 == -1)
id2 = _depths->size() - 1;
id2 = _depths.size() - 1;

// clamp to keep only the closest depths if we have too much inputs (> maxDepthsToSweep)
id2 = std::min(id1 + _sp->maxDepthsToSweep - 1, id2);
Expand Down Expand Up @@ -286,9 +283,9 @@ void SemiGlobalMatchingRc::computeDepthsAndResetTCams()
{
std::string fn = _sp->mp->getDepthMapsFolder() + std::to_string(_sp->mp->getViewId(_rc)) + "depthsAll.txt";
FILE* f = fopen(fn.c_str(), "w");
for(int j = 0; j < _depths->size(); j++)
for(int j = 0; j < _depths.size(); j++)
{
float depth = (*_depths)[j];
float depth = _depths[j];
fprintf(f, "%f\n", depth);
}
fclose(f);
Expand Down Expand Up @@ -326,9 +323,9 @@ void SemiGlobalMatchingRc::computeDepthsAndResetTCams()
{
std::string fn = _sp->mp->getDepthMapsFolder() + std::to_string(_sp->mp->getViewId(_rc)) + "depthsAll.txt";
FILE* f = fopen(fn.c_str(), "w");
for(int j = 0; j < _depths->size(); j++)
for(int j = 0; j < _depths.size(); j++)
{
float depth = (*_depths)[j];
float depth = _depths[j];
fprintf(f, "%f\n", depth);
}
fclose(f);
Expand Down Expand Up @@ -358,9 +355,9 @@ void SemiGlobalMatchingRc::computeDepthsAndResetTCams()
{
std::string fn = _sp->mp->getDepthMapsFolder() + std::to_string(_sp->mp->getViewId(_rc)) + "depths.txt";
FILE* f = fopen(fn.c_str(), "w");
for(int j = 0; j < _depths->size(); j++)
for(int j = 0; j < _depths.size(); j++)
{
float depth = (*_depths)[j];
float depth = _depths[j];
fprintf(f, "%f\n", depth);
}
fclose(f);
Expand Down Expand Up @@ -401,7 +398,7 @@ void SemiGlobalMatchingRc::computeDepthsAndResetTCams()
}

if(_sp->mp->verbose)
ALICEVISION_LOG_DEBUG("rc depths: " << _depths->size());
ALICEVISION_LOG_DEBUG("rc depths: " << _depths.size());

deleteArrayOfArrays<float>(&alldepths);
}
Expand All @@ -412,7 +409,7 @@ void SemiGlobalMatchingRc::getSubDepthsForTCam( int tcamid, std::vector<float>&

for(int i = 0; i<_depthsTcamsLimits[tcamid].y; i++ )
{
out[i] = (*_depths)[_depthsTcamsLimits[tcamid].x + i];
out[i] = _depths[_depthsTcamsLimits[tcamid].x + i];
}
}

Expand All @@ -436,7 +433,7 @@ bool SemiGlobalMatchingRc::sgmrc(bool checkIfExists)

const int volDimX = _width;
const int volDimY = _height;
const int volDimZ = _depths->size();
const int volDimZ = _depths.size();
float volumeMBinGPUMem = 0.0f;

StaticVector<unsigned char>* simVolume = nullptr;
Expand Down Expand Up @@ -483,8 +480,8 @@ bool SemiGlobalMatchingRc::sgmrc(bool checkIfExists)

if(_sp->exportIntermediateResults)
{
svol->exportVolumeStep(*_depths, _rc, _scale, _step, _sp->mp->getDepthMapsFolder() + std::to_string(_sp->mp->getViewId(_rc)) + "_vol_afterReduction.abc");
svol->export9PCSV(*_depths, _rc, _scale, _step, "afterReduction", _sp->mp->getDepthMapsFolder() + std::to_string(_sp->mp->getViewId(_rc)) + "_9p.csv");
svol->exportVolumeStep(_depths, _rc, _scale, _step, _sp->mp->getDepthMapsFolder() + std::to_string(_sp->mp->getViewId(_rc)) + "_vol_afterReduction.abc");
svol->export9PCSV(_depths, _rc, _scale, _step, "afterReduction", _sp->mp->getDepthMapsFolder() + std::to_string(_sp->mp->getViewId(_rc)) + "_9p.csv");
}

// filter on the 3D volume to weight voxels based on their neighborhood strongness.
Expand All @@ -496,8 +493,8 @@ bool SemiGlobalMatchingRc::sgmrc(bool checkIfExists)

if(_sp->exportIntermediateResults)
{
svol->exportVolumeStep(*_depths, _rc, _scale, _step, _sp->mp->getDepthMapsFolder() + std::to_string(_sp->mp->getViewId(_rc)) + "_vol_afterFiltering.abc");
svol->export9PCSV(*_depths, _rc, _scale, _step, "afterFiltering", _sp->mp->getDepthMapsFolder() + std::to_string(_sp->mp->getViewId(_rc)) + "_9p.csv");
svol->exportVolumeStep(_depths, _rc, _scale, _step, _sp->mp->getDepthMapsFolder() + std::to_string(_sp->mp->getViewId(_rc)) + "_vol_afterFiltering.abc");
svol->export9PCSV(_depths, _rc, _scale, _step, "afterFiltering", _sp->mp->getDepthMapsFolder() + std::to_string(_sp->mp->getViewId(_rc)) + "_9p.csv");
}

// for each pixel: choose the voxel with the minimal similarity value
Expand Down
2 changes: 1 addition & 1 deletion src/aliceVision/depthMap/SemiGlobalMatchingRc.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ class SemiGlobalMatchingRc
StaticVector<int> _sgmTCams;
StaticVector<Pixel> _depthsTcamsLimits;
StaticVector<IdValue>* _volumeBestIdVal = nullptr;
StaticVector<float>* _depths = nullptr;
StaticVector<float> _depths;

SemiGlobalMatchingParams* _sp;

Expand Down

0 comments on commit d16c093

Please sign in to comment.