From fa3cc528b1ad3c547957eed9da49ba08f74dee58 Mon Sep 17 00:00:00 2001 From: Fabien Castan Date: Wed, 24 Jul 2019 20:14:57 +0200 Subject: [PATCH] [depthMap] bug fix: use RAII instead of pointers In particular configurations (failed to find Tc cameras), sgmrc can stop early before allocating the _volumeBestIdVal, which is later used in refine step. --- src/aliceVision/depthMap/RefineRc.cpp | 4 ++-- src/aliceVision/depthMap/SemiGlobalMatchingRc.cpp | 15 +++++++-------- src/aliceVision/depthMap/SemiGlobalMatchingRc.hpp | 2 +- .../depthMap/SemiGlobalMatchingVolume.cpp | 13 +++++-------- .../depthMap/SemiGlobalMatchingVolume.hpp | 2 +- 5 files changed, 16 insertions(+), 20 deletions(-) diff --git a/src/aliceVision/depthMap/RefineRc.cpp b/src/aliceVision/depthMap/RefineRc.cpp index 2031011411..03d8cb2187 100644 --- a/src/aliceVision/depthMap/RefineRc.cpp +++ b/src/aliceVision/depthMap/RefineRc.cpp @@ -57,12 +57,12 @@ DepthSimMap* RefineRc::getDepthPixSizeMapFromSGM() StaticVector volumeBestIdVal; volumeBestIdVal.reserve(_width * _height); - for(int i = 0; i < _volumeBestIdVal->size(); i++) + 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; + const int id = _volumeBestIdVal[i].id; if(id > 0) volumeBestIdVal.push_back(IdValue(id, sim)); diff --git a/src/aliceVision/depthMap/SemiGlobalMatchingRc.cpp b/src/aliceVision/depthMap/SemiGlobalMatchingRc.cpp index f7b3f439c9..8c67532f7e 100644 --- a/src/aliceVision/depthMap/SemiGlobalMatchingRc.cpp +++ b/src/aliceVision/depthMap/SemiGlobalMatchingRc.cpp @@ -46,7 +46,6 @@ SemiGlobalMatchingRc::SemiGlobalMatchingRc(int rc, int scale, int step, SemiGlob SemiGlobalMatchingRc::~SemiGlobalMatchingRc() { - delete _volumeBestIdVal; } bool SemiGlobalMatchingRc::selectBestDepthsRange(int nDepthsThr, StaticVector* rcSeedsDistsAsc) @@ -498,7 +497,7 @@ bool SemiGlobalMatchingRc::sgmrc(bool checkIfExists) // for each pixel: choose the voxel with the minimal similarity value const int zborder = 2; - _volumeBestIdVal = svol->getOrigVolumeBestIdValFromVolumeStepZ(zborder); + svol->getOrigVolumeBestIdValFromVolumeStepZ(_volumeBestIdVal, zborder); delete svol; @@ -508,8 +507,8 @@ bool SemiGlobalMatchingRc::sgmrc(bool checkIfExists) { if((*rcSilhoueteMap)[i]) { - (*_volumeBestIdVal)[i].id = 0; - (*_volumeBestIdVal)[i].value = 1.0f; + _volumeBestIdVal[i].id = 0; + _volumeBestIdVal[i].value = 1.0f; } } delete rcSilhoueteMap; @@ -520,13 +519,13 @@ bool SemiGlobalMatchingRc::sgmrc(bool checkIfExists) if(_sp->exportIntermediateResults) { - DepthSimMap* depthSimMapFinal = _sp->getDepthSimMapFromBestIdVal(_width, _height, _volumeBestIdVal, _scale, _step, _rc, zborder, _depths); + DepthSimMap* depthSimMapFinal = _sp->getDepthSimMapFromBestIdVal(_width, _height, &_volumeBestIdVal, _scale, _step, _rc, zborder, _depths); depthSimMapFinal->saveToImage(_sp->mp->getDepthMapsFolder() + "sgm_" + std::to_string(_sp->mp->getViewId(_rc)) + "_" + "scale" + mvsUtils::num2str(depthSimMapFinal->scale) + "_step" + mvsUtils::num2str(depthSimMapFinal->step) + ".png", 1.0f); delete depthSimMapFinal; - std::vector volumeBestId(_volumeBestIdVal->size()); - for(int i = 0; i < _volumeBestIdVal->size(); i++) - volumeBestId.at(i) = std::max(0, (*_volumeBestIdVal)[i].id); + std::vector volumeBestId(_volumeBestIdVal.size()); + for(int i = 0; i < _volumeBestIdVal.size(); i++) + volumeBestId.at(i) = std::max(0, _volumeBestIdVal[i].id); using namespace imageIO; OutputFileColorSpace colorspace(EImageColorSpace::NO_CONVERSION); diff --git a/src/aliceVision/depthMap/SemiGlobalMatchingRc.hpp b/src/aliceVision/depthMap/SemiGlobalMatchingRc.hpp index e1bf1d1578..f5cce66840 100644 --- a/src/aliceVision/depthMap/SemiGlobalMatchingRc.hpp +++ b/src/aliceVision/depthMap/SemiGlobalMatchingRc.hpp @@ -35,7 +35,7 @@ class SemiGlobalMatchingRc StaticVector _sgmTCams; StaticVector _depthsTcamsLimits; - StaticVector* _volumeBestIdVal = nullptr; + StaticVector _volumeBestIdVal; StaticVector _depths; SemiGlobalMatchingParams* _sp; diff --git a/src/aliceVision/depthMap/SemiGlobalMatchingVolume.cpp b/src/aliceVision/depthMap/SemiGlobalMatchingVolume.cpp index f153e50d42..010e02c6fa 100644 --- a/src/aliceVision/depthMap/SemiGlobalMatchingVolume.cpp +++ b/src/aliceVision/depthMap/SemiGlobalMatchingVolume.cpp @@ -271,16 +271,15 @@ void SemiGlobalMatchingVolume::SGMoptimizeVolumeStepZ(int rc, int volStepXY, int mvsUtils::printfElapsedTime(tall, "SemiGlobalMatchingVolume::SGMoptimizeVolumeStepZ"); } -StaticVector* SemiGlobalMatchingVolume::getOrigVolumeBestIdValFromVolumeStepZ(int zborder) +void SemiGlobalMatchingVolume::getOrigVolumeBestIdValFromVolumeStepZ(StaticVector& out_volumeBestIdVal, int zborder) { long tall = clock(); - StaticVector* volumeBestIdVal = new StaticVector(); - volumeBestIdVal->reserve(volDimX * volDimY); - volumeBestIdVal->resize_with(volDimX * volDimY, IdValue(-1, 1.0f)); + out_volumeBestIdVal.reserve(volDimX * volDimY); + out_volumeBestIdVal.resize_with(volDimX * volDimY, IdValue(-1, 1.0f)); unsigned char* _volumeStepZPtr = _volumeStepZ->getDataWritable().data(); int* _volumeBestZPtr = _volumeBestZ->getDataWritable().data(); - IdValue* volumeBestIdValPtr = volumeBestIdVal->getDataWritable().data(); + IdValue* out_volumeBestIdValPtr = out_volumeBestIdVal.getDataWritable().data(); for(int z = zborder; z < volDimZ / volStepZ - zborder; z++) { for(int y = 1; y < volDimY - 1; y++) @@ -291,7 +290,7 @@ StaticVector* SemiGlobalMatchingVolume::getOrigVolumeBestIdValFromVolum // value from volumeStepZ converted from (0, 255) to (-1, +1) float val = (((float)_volumeStepZPtr[volumeIndex]) / 255.0f) * 2.0f - 1.0f; int bestZ = _volumeBestZPtr[volumeIndex]; // TODO: what is bestZ? - IdValue& idVal = volumeBestIdValPtr[y * volDimX + x]; + IdValue& idVal = out_volumeBestIdValPtr[y * volDimX + x]; assert(bestZ >= 0); if(idVal.id == -1) @@ -312,8 +311,6 @@ StaticVector* SemiGlobalMatchingVolume::getOrigVolumeBestIdValFromVolum if(sp->mp->verbose) mvsUtils::printfElapsedTime(tall, "SemiGlobalMatchingVolume::getOrigVolumeBestIdValFromVolumeStepZ "); - - return volumeBestIdVal; } void SemiGlobalMatchingVolume::copyVolume(const StaticVector* volume, int zFrom, int nZSteps) diff --git a/src/aliceVision/depthMap/SemiGlobalMatchingVolume.hpp b/src/aliceVision/depthMap/SemiGlobalMatchingVolume.hpp index 06b495b2a8..91847a5c12 100644 --- a/src/aliceVision/depthMap/SemiGlobalMatchingVolume.hpp +++ b/src/aliceVision/depthMap/SemiGlobalMatchingVolume.hpp @@ -28,7 +28,7 @@ class SemiGlobalMatchingVolume void cloneVolumeSecondStepZ(); void SGMoptimizeVolumeStepZ(int rc, int volStepXY, int volLUX, int volLUY, int scale); - StaticVector* getOrigVolumeBestIdValFromVolumeStepZ(int zborder); + void getOrigVolumeBestIdValFromVolumeStepZ(StaticVector& out_volumeBestIdVal, int zborder); void exportVolume(StaticVector& depths, int camIndex, int scale, int step, const std::string& filepath) const; void exportVolumeStep(StaticVector& depths, int camIndex, int scale, int step, const std::string& filepath) const;