Skip to content

Commit

Permalink
refine: reduce memory requirements some more
Browse files Browse the repository at this point in the history
  • Loading branch information
cdcseacave committed Oct 9, 2015
1 parent 5206bbc commit b7389f7
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 23 deletions.
17 changes: 6 additions & 11 deletions apps/RefineMesh/RefineMesh.cpp
Original file line number Diff line number Diff line change
@@ -1,17 +1,13 @@
/*
* RefineMesh.cpp
*
* Copyright (c) 2014-2015 FOXEL SA - http://foxel.ch
* Please read <http://foxel.ch/license> for more information.
*
* Copyright (c) 2014-2015 SEACAVE
*
* Author(s):
*
* cDc <[email protected]>
*
*
* This file is part of the FOXEL project <http://foxel.ch>.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
Expand All @@ -31,9 +27,6 @@
* You are required to preserve legal notices and author attributions in
* that material or in the Appropriate Legal Notices displayed by works
* containing it.
*
* You are required to attribute the work as explained in the "Usage and
* Attribution" section of <http://foxel.ch/license>.
*/

#include "../../libs/MVS/Common.h"
Expand Down Expand Up @@ -62,6 +55,7 @@ unsigned nCloseHoles;
unsigned nEnsureEdgeSize;
unsigned nScales;
float fScaleStep;
unsigned nReduceMemory;
unsigned nAlternatePair;
float fRegularityWeight;
float fRatioRigidityElasticity;
Expand Down Expand Up @@ -119,7 +113,8 @@ bool Initialize(size_t argc, LPCTSTR* argv)
("max-face-area", boost::program_options::value<unsigned>(&OPT::nMaxFaceArea)->default_value(64), "maximum face area projected in any pair of images that is not subdivided (0 - disabled)")
("scales", boost::program_options::value<unsigned>(&OPT::nScales)->default_value(3), "how many iterations to run mesh optimization on multi-scale images")
("scale-step", boost::program_options::value<float>(&OPT::fScaleStep)->default_value(0.5f), "image scale factor used at each mesh optimization step")
("alternate-pair", boost::program_options::value<unsigned>(&OPT::nAlternatePair)->default_value(1), "refine mesh using an image pair alternatively as reference")
("reduce-memory", boost::program_options::value<unsigned>(&OPT::nReduceMemory)->default_value(1), "recompute some data in order to reduce memory requirements")
("alternate-pair", boost::program_options::value<unsigned>(&OPT::nAlternatePair)->default_value(0), "refine mesh using an image pair alternatively as reference (0 - both, 1 - alternate, 2 - only left, 3 - only right)")
("regularity-weight", boost::program_options::value<float>(&OPT::fRegularityWeight)->default_value(0.1f), "scalar regularity weight to balance between photo-consistency and regularization terms during mesh optimization")
("rigidity-elasticity-ratio", boost::program_options::value<float>(&OPT::fRatioRigidityElasticity)->default_value(0.9f), "scalar ratio used to compute the regularity gradient as a combination of rigidity and elasticity")
("gradient-step", boost::program_options::value<float>(&OPT::fGradientStep)->default_value(45.05f), "gradient step to be used instead (0 - auto)")
Expand Down Expand Up @@ -242,7 +237,7 @@ int main(int argc, LPCTSTR* argv)
OPT::fDecimateMesh, OPT::nCloseHoles, OPT::nEnsureEdgeSize,
OPT::nMaxFaceArea,
OPT::nScales, OPT::fScaleStep,
OPT::nAlternatePair>1 ? true : false,
OPT::nAlternatePair>10 ? OPT::nAlternatePair%10 : 0,
OPT::fRegularityWeight,
OPT::fRatioRigidityElasticity,
OPT::fGradientStep))
Expand All @@ -251,7 +246,7 @@ int main(int argc, LPCTSTR* argv)
OPT::fDecimateMesh, OPT::nCloseHoles, OPT::nEnsureEdgeSize,
OPT::nMaxFaceArea,
OPT::nScales, OPT::fScaleStep,
OPT::nAlternatePair!=0,
OPT::nReduceMemory, OPT::nAlternatePair,
OPT::fRegularityWeight,
OPT::fRatioRigidityElasticity,
OPT::fPlanarVertexRatio,
Expand Down
8 changes: 7 additions & 1 deletion libs/Common/Types.h
Original file line number Diff line number Diff line change
Expand Up @@ -2141,8 +2141,10 @@ class TBitMatrix

public:
inline TBitMatrix() : data(NULL) {}
inline TBitMatrix(int _rows, int _cols) : rows(_rows), cols(_cols), data(rows && cols ? new Type[computeLength(rows, cols)] : NULL) {}
inline TBitMatrix(int _rows, int _cols=1) : rows(_rows), cols(_cols), data(rows && cols ? new Type[computeLength(rows, cols)] : NULL) {}
inline TBitMatrix(int _rows, int _cols, uint8_t v) : rows(_rows), cols(_cols), data(rows && cols ? new Type[computeLength(rows, cols)] : NULL) { if (!empty()) memset(v); }
inline TBitMatrix(const Size& sz) : rows(sz.height), cols(sz.width), data(rows && cols ? new Type[computeLength(sz)] : NULL) {}
inline TBitMatrix(const Size& sz, uint8_t v) : rows(sz.height), cols(sz.width), data(rows && cols ? new Type[computeLength(sz)] : NULL) { if (!empty()) memset(v); }
inline ~TBitMatrix() { delete[] data; }

inline void create(int _rows, int _cols=1) {
Expand Down Expand Up @@ -2190,6 +2192,10 @@ class TBitMatrix
inline int area() const { return cols*rows; }
inline int length() const { return computeLength(rows, cols); }

inline bool operator() (int i) const { return isSet(i); }
inline bool operator() (int i, int j) const { return isSet(i,j); }
inline bool operator() (const ImageRef& ir) const { return isSet(ir); }

inline bool isSet(int i) const { ASSERT(!empty() && i<area()); const Index idx(computeIndex(i)); return (data[idx.idx] & idx.flag) != 0; }
inline bool isSet(int i, int j) const { ASSERT(!empty() && i<rows && j<cols); const Index idx(computeIndex(i, j, cols)); return (data[idx.idx] & idx.flag) != 0; }
inline bool isSet(const ImageRef& ir) const { ASSERT(!empty() && ir.y<rows && ir.x<cols); const Index idx(computeIndex(ir, cols)); return (data[idx.idx] & idx.flag) != 0; }
Expand Down
22 changes: 11 additions & 11 deletions libs/MVS/SceneRefine.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -143,19 +143,19 @@ class MeshRefine {
static void ImageMeshWarp(
const DepthMap& depthMapA, const Camera& cameraA,
const DepthMap& depthMapB, const Camera& cameraB,
const Image32F& imageB, Image32F& imageA, Image8U& mask);
const Image32F& imageB, Image32F& imageA, BitMatrix& mask);
static void ComputeLocalVariance(
const Image32F& image, const Image8U& mask,
const Image32F& image, const BitMatrix& mask,
TImage<Real>& imageMean, TImage<Real>& imageVar);
static float ComputeLocalZNCC(
const Image32F& imageA, const TImage<Real>& imageMeanA, const TImage<Real>& imageVarA,
const Image32F& imageB, const TImage<Real>& imageMeanB, const TImage<Real>& imageVarB,
const Image8U& mask, TImage<Real>& imageZNCC, TImage<Real>& imageDZNCC);
const BitMatrix& mask, TImage<Real>& imageZNCC, TImage<Real>& imageDZNCC);
static void ComputePhotometricGradient(
const Mesh::FaceArr& faces, const Mesh::NormalArr& normals,
const DepthMap& depthMapA, const FaceMap& faceMapA, const BaryMap& baryMapA, const Camera& cameraA,
const Camera& cameraB, const View& viewB,
const TImage<Real>& imageDZNCC, const Image8U& mask, GradArr& photoGrad, UnsignedArr& photoGradNorm, Real RegularizationScale);
const TImage<Real>& imageDZNCC, const BitMatrix& mask, GradArr& photoGrad, UnsignedArr& photoGradNorm, Real RegularizationScale);
static float ComputeSmoothnessGradient1(
const Mesh::VertexArr& vertices, const Mesh::VertexVerticesArr& vertexVertices, const BoolArr& vertexBoundary,
GradArr& smoothGrad1, VIndex idxStart, VIndex idxEnd);
Expand Down Expand Up @@ -767,7 +767,7 @@ void MeshRefine::ProjectMesh(
void MeshRefine::ImageMeshWarp(
const DepthMap& depthMapA, const Camera& cameraA,
const DepthMap& depthMapB, const Camera& cameraB,
const Image32F& imageB, Image32F& imageA, Image8U& mask)
const Image32F& imageB, Image32F& imageA, BitMatrix& mask)
{
ASSERT(!imageA.empty());
typedef Sampler::Linear<float> Sampler;
Expand All @@ -785,13 +785,13 @@ void MeshRefine::ImageMeshWarp(
if (!IsDepthSimilar(depthMapB, pt, ptC.z))
continue;
imageA(j,i) = imageB.sample<Sampler,Sampler::Type>(sampler, pt);
mask(j,i) = 1;
mask.set(j,i);
}
}
}

// compute local variance for each image pixel
void MeshRefine::ComputeLocalVariance(const Image32F& image, const Image8U& mask, TImage<Real>& imageMean, TImage<Real>& imageVar)
void MeshRefine::ComputeLocalVariance(const Image32F& image, const BitMatrix& mask, TImage<Real>& imageMean, TImage<Real>& imageVar)
{
ASSERT(image.size() == mask.size());
imageMean.create(image.size());
Expand Down Expand Up @@ -836,7 +836,7 @@ void MeshRefine::ComputeLocalVariance(const Image32F& image, const Image8U& mask
float MeshRefine::ComputeLocalZNCC(
const Image32F& imageA, const TImage<Real>& imageMeanA, const TImage<Real>& imageVarA,
const Image32F& imageB, const TImage<Real>& imageMeanB, const TImage<Real>& imageVarB,
const Image8U& mask, TImage<Real>& imageZNCC, TImage<Real>& imageDZNCC)
const BitMatrix& mask, TImage<Real>& imageZNCC, TImage<Real>& imageDZNCC)
{
ASSERT(imageA.size() == mask.size() && imageB.size() == mask.size() && !mask.empty());
float score(0);
Expand Down Expand Up @@ -911,7 +911,7 @@ void MeshRefine::ComputePhotometricGradient(
const Mesh::FaceArr& faces, const Mesh::NormalArr& normals,
const DepthMap& depthMapA, const FaceMap& faceMapA, const BaryMap& baryMapA, const Camera& cameraA,
const Camera& cameraB, const View& viewB,
const TImage<Real>& imageDZNCC, const Image8U& mask, GradArr& photoGrad, UnsignedArr& photoGradNorm, Real RegularizationScale)
const TImage<Real>& imageDZNCC, const BitMatrix& mask, GradArr& photoGrad, UnsignedArr& photoGradNorm, Real RegularizationScale)
{
ASSERT(faces.GetSize() == normals.GetSize() && !faces.IsEmpty());
ASSERT(depthMapA.size() == mask.size() && faceMapA.size() == mask.size() && baryMapA.size() == mask.size() && imageDZNCC.size() == mask.size() && !mask.empty());
Expand Down Expand Up @@ -1089,7 +1089,7 @@ void MeshRefine::ThInitImage(uint32_t idxImage, Real scale, Real sigma)
imageData.UpdateCamera(scene.platforms);
if (!nReduceMemory) {
// compute image mean and variance
ComputeLocalVariance(img, Image8U(img.size(), 1), view.imageMean, view.imageVar);
ComputeLocalVariance(img, BitMatrix(img.size(), 0xFF), view.imageMean, view.imageVar);
}
// compute image gradient
typedef View::Grad::Type GradType;
Expand Down Expand Up @@ -1137,7 +1137,7 @@ void MeshRefine::ThProcessPair(uint32_t idxImageA, uint32_t idxImageB)
const Image32F& imageB = viewB.image;
const Camera& cameraB = imageDataB.camera;
// warp imageB to imageA using the mesh
Image8U mask;
BitMatrix mask;
Image32F imageAB; imageA.copyTo(imageAB);
ImageMeshWarp(depthMapA, cameraA, depthMapB, cameraB, imageB, imageAB, mask);
// compute ZNCC and its gradient
Expand Down

0 comments on commit b7389f7

Please sign in to comment.