Skip to content
10 changes: 5 additions & 5 deletions apps/DensifyPointCloud/DensifyPointCloud.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -234,11 +234,11 @@ int main(int argc, LPCTSTR* argv)
return EXIT_FAILURE;
}
if ((ARCHIVE_TYPE)OPT::nArchiveType != ARCHIVE_MVS) {
TD_TIMER_START();
if (!scene.DenseReconstruction())
return EXIT_FAILURE;
VERBOSE("Densifying point-cloud completed: %u points (%s)", scene.pointcloud.GetSize(), TD_TIMER_GET_FMT().c_str());
}
TD_TIMER_START();
if (!scene.DenseReconstruction(std::vector<BitMatrix>()))
return EXIT_FAILURE;
VERBOSE("Densifying point-cloud completed: %u points (%s)", scene.pointcloud.GetSize(), TD_TIMER_GET_FMT().c_str());
}

// save the final mesh
const String baseFileName(MAKE_PATH_SAFE(Util::getFileFullName(OPT::strOutputFileName)));
Expand Down
16 changes: 8 additions & 8 deletions libs/Common/Types.inl
Original file line number Diff line number Diff line change
Expand Up @@ -3379,23 +3379,23 @@ enum ARCHIVE_TYPE {

// export the current state of the given reconstruction object
template <typename TYPE>
bool SerializeSave(const TYPE& obj, std::ofstream& fs, ARCHIVE_TYPE type, unsigned flags=0)
bool SerializeSave(const TYPE& obj, std::ostream& os, ARCHIVE_TYPE type, unsigned flags=0)
{
// serialize out the current state
switch (type) {
case ARCHIVE_TEXT: {
boost::archive::text_oarchive ar(fs, flags);
boost::archive::text_oarchive ar(os, flags);
ar << obj;
break; }
case ARCHIVE_BINARY: {
boost::archive::binary_oarchive ar(fs, flags);
boost::archive::binary_oarchive ar(os, flags);
ar << obj;
break; }
case ARCHIVE_BINARY_ZIP: {
namespace io = boost::iostreams;
io::filtering_streambuf<io::output> ffs;
ffs.push(io::zlib_compressor(io::zlib::best_speed));
ffs.push(fs);
ffs.push(os);
boost::archive::binary_oarchive ar(ffs, flags);
ar << obj;
break; }
Expand All @@ -3418,24 +3418,24 @@ bool SerializeSave(const TYPE& obj, const SEACAVE::String& fileName, ARCHIVE_TYP

// import the state to the given reconstruction object
template <typename TYPE>
bool SerializeLoad(TYPE& obj, std::ifstream& fs, ARCHIVE_TYPE type, unsigned flags=0)
bool SerializeLoad(TYPE& obj, std::istream& is, ARCHIVE_TYPE type, unsigned flags=0)
{
try {
// serialize in the saved state
switch (type) {
case ARCHIVE_TEXT: {
boost::archive::text_iarchive ar(fs, flags);
boost::archive::text_iarchive ar(is, flags);
ar >> obj;
break; }
case ARCHIVE_BINARY: {
boost::archive::binary_iarchive ar(fs, flags);
boost::archive::binary_iarchive ar(is, flags);
ar >> obj;
break; }
case ARCHIVE_BINARY_ZIP: {
namespace io = boost::iostreams;
io::filtering_streambuf<io::input> ffs;
ffs.push(io::zlib_decompressor());
ffs.push(fs);
ffs.push(is);
boost::archive::binary_iarchive ar(ffs, flags);
ar >> obj;
break; }
Expand Down
50 changes: 50 additions & 0 deletions libs/MVS/DepthMap.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -189,11 +189,23 @@ bool DepthData::Save(const String& fileName) const
ASSERT(IsValid() && !depthMap.empty() && !confMap.empty());
return SerializeSave(*this, fileName, ARCHIVE_BINARY_ZIP);
}
bool DepthData::Save(std::ostream &os) const
{
ASSERT(IsValid() && !depthMap.empty() && !confMap.empty());
return SerializeSave(*this, os, ARCHIVE_BINARY_ZIP);
}
bool DepthData::Load(const String& fileName)
{
ASSERT(IsValid());
return SerializeLoad(*this, fileName, ARCHIVE_BINARY_ZIP);
}
bool DepthData::Load(std::istream &is)
{
ASSERT(IsValid());
bool ok = SerializeLoad(*this, is, ARCHIVE_BINARY_ZIP);
is.seekg(0, is.beg);
return ok;
}
/*----------------------------------------------------------------*/


Expand All @@ -210,6 +222,14 @@ unsigned DepthData::IncRef(const String& fileName)
return 0;
return ++references;
}
unsigned DepthData::IncRef(std::istream &is)
{
Lock l(cs);
ASSERT(!IsEmpty() || references == 0);
if (IsEmpty() && !Load(is))
return 0;
return ++references;
}
unsigned DepthData::DecRef()
{
Lock l(cs);
Expand Down Expand Up @@ -740,12 +760,27 @@ bool MVS::SaveDepthMap(const String& fileName, const DepthMap& depthMap)
return SerializeSave(depthMap, fileName, ARCHIVE_BINARY_ZIP);
} // SaveDepthMap
/*----------------------------------------------------------------*/
// save the depth map in a stream
bool MVS::SaveDepthMap(std::ostream &os, const DepthMap &depthMap)
{
ASSERT(!depthMap.empty());
return SerializeSave(depthMap, os, ARCHIVE_BINARY_ZIP);
} // SaveDepthMap
/*----------------------------------------------------------------*/
// load the depth map from our .dmap file format
bool MVS::LoadDepthMap(const String& fileName, DepthMap& depthMap)
{
return SerializeLoad(depthMap, fileName, ARCHIVE_BINARY_ZIP);
} // LoadDepthMap
/*----------------------------------------------------------------*/
// load the depth map from a stream
bool MVS::LoadDepthMap(std::istream &is, DepthMap &depthMap)
{
bool ok = SerializeLoad(depthMap, is, ARCHIVE_BINARY_ZIP);
is.seekg(is.beg);
return ok;
} // LoadDepthMap
/*----------------------------------------------------------------*/

// save the normal map in our .nmap file format
bool MVS::SaveNormalMap(const String& fileName, const NormalMap& normalMap)
Expand All @@ -768,12 +803,27 @@ bool MVS::SaveConfidenceMap(const String& fileName, const ConfidenceMap& confMap
return SerializeSave(confMap, fileName, ARCHIVE_BINARY_ZIP);
} // SaveConfidenceMap
/*----------------------------------------------------------------*/
// save the confidence map in stream
bool MVS::SaveConfidenceMap(std::ostream &os, const ConfidenceMap& confMap)
{
ASSERT(!confMap.empty());
return SerializeSave(confMap, os, ARCHIVE_BINARY_ZIP);
} // SaveConfidenceMap
/*----------------------------------------------------------------*/
// load the confidence map from our .cmap file format
bool MVS::LoadConfidenceMap(const String& fileName, ConfidenceMap& confMap)
{
return SerializeLoad(confMap, fileName, ARCHIVE_BINARY_ZIP);
} // LoadConfidenceMap
/*----------------------------------------------------------------*/
// load the confidence map from stream
bool MVS::LoadConfidenceMap(std::istream &is, ConfidenceMap& confMap)
{
bool ok = SerializeLoad(confMap, is, ARCHIVE_BINARY_ZIP);
is.seekg(is.beg);
return ok;
} // LoadConfidenceMap
/*----------------------------------------------------------------*/



Expand Down
7 changes: 7 additions & 0 deletions libs/MVS/DepthMap.h
Original file line number Diff line number Diff line change
Expand Up @@ -162,10 +162,13 @@ struct MVS_API DepthData {
void GetNormal(const Point2f& x, Point3f& N, const TImage<Point3f>* pPointMap=NULL) const;

bool Save(const String& fileName) const;
bool Save(std::ostream &os) const;
bool Load(const String& fileName);
bool Load(std::istream &is);

unsigned GetRef();
unsigned IncRef(const String& fileName);
unsigned IncRef(std::istream &is);
unsigned DecRef();

#ifdef _USE_BOOST
Expand Down Expand Up @@ -365,11 +368,15 @@ MVS_API void EstimatePointColors(const ImageArr& images, PointCloud& pointcloud)
MVS_API void EstimatePointNormals(const ImageArr& images, PointCloud& pointcloud, int numNeighbors=16/*K-nearest neighbors*/);

MVS_API bool SaveDepthMap(const String& fileName, const DepthMap& depthMap);
MVS_API bool SaveDepthMap(std::ostream &os, const DepthMap &depthMap);
MVS_API bool LoadDepthMap(const String& fileName, DepthMap& depthMap);
MVS_API bool LoadDepthMap(std::istream &is, DepthMap &depthMap);
MVS_API bool SaveNormalMap(const String& fileName, const NormalMap& normalMap);
MVS_API bool LoadNormalMap(const String& fileName, NormalMap& normalMap);
MVS_API bool SaveConfidenceMap(const String& fileName, const ConfidenceMap& confMap);
MVS_API bool SaveConfidenceMap(std::ostream &os, const ConfidenceMap & confMap);
MVS_API bool LoadConfidenceMap(const String& fileName, ConfidenceMap& confMap);
MVS_API bool LoadConfidenceMap(std::istream &is, ConfidenceMap& confMap);

MVS_API bool ExportDepthMap(const String& fileName, const DepthMap& depthMap, Depth minDepth=FLT_MAX, Depth maxDepth=0);
MVS_API bool ExportNormalMap(const String& fileName, const NormalMap& normalMap);
Expand Down
52 changes: 52 additions & 0 deletions libs/MVS/Image.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,52 @@ bool Image::ReloadImage(unsigned nMaxResolution, bool bLoadPixels)
} // ReloadImage
/*----------------------------------------------------------------*/

// reads again the image data from the input vector
bool Image::ReloadImage(const uint32_t &_width, const uint32_t &_height, const uint32_t &depth, unsigned nMaxResolution, bool bLoadPixels, const std::vector<unsigned char> &imageData)
{
width = _width;
height = _height;

if (bLoadPixels)
{
image.create(height, width);

uint8_t *pData = image.data;

const uint8_t *buffer = imageData.data();

switch (depth)
{
case 1:
{
if (!CImage::FilterFormat(pData, PF_R8G8B8, 3, buffer, PF_GRAY8, depth, imageData.size()))
{
return false;
}
}
break;
case 3:
memcpy(pData, buffer, imageData.size());
break;
case 4:
{
if (!CImage::FilterFormat(pData, PF_R8G8B8, 3, buffer, PF_R8G8B8A8, depth, imageData.size()))
{
return false;
}
}
break;
default:
return false;
}
}

scale = ResizeImage(nMaxResolution);

return true;
} // ReloadImage
/*----------------------------------------------------------------*/

// free the image data
void Image::ReleaseImage()
{
Expand Down Expand Up @@ -173,6 +219,12 @@ unsigned Image::RecomputeMaxResolution(unsigned& level, unsigned minImageSize) c
} // RecomputeMaxResolution
/*----------------------------------------------------------------*/

// compute image scale for a given max and min resolution, using the current image file data
unsigned Image::RecomputeMaxResolution(const uint32_t &width, const uint32_t &height, unsigned& level, unsigned minImageSize) const
{
return Image8U3::computeMaxResolution(MAXF(width, height), level, minImageSize);
} // RecomputeMaxResolution
/*----------------------------------------------------------------*/

// compute the camera extrinsics from the platform pose and the relative camera pose to the platform
Camera Image::GetCamera(const PlatformArr& platforms, const Image8U::Size& resolution) const
Expand Down
2 changes: 2 additions & 0 deletions libs/MVS/Image.h
Original file line number Diff line number Diff line change
Expand Up @@ -103,9 +103,11 @@ class MVS_API Image
static bool ReadImage(IMAGEPTR pImage, Image8U3& image);
bool LoadImage(const String& fileName, unsigned nMaxResolution=0);
bool ReloadImage(unsigned nMaxResolution=0, bool bLoadPixels=true);
bool ReloadImage(const uint32_t &width, const uint32_t &height, const uint32_t &depth, unsigned nMaxResolution=0, bool bLoadPixels=false, const std::vector<unsigned char> &imageData=std::vector<unsigned char>());
void ReleaseImage();
float ResizeImage(unsigned nMaxResolution=0);
unsigned RecomputeMaxResolution(unsigned& level, unsigned minImageSize) const;
unsigned RecomputeMaxResolution(const uint32_t &width, const uint32_t &height, unsigned& level, unsigned minImageSize) const;

Camera GetCamera(const PlatformArr& platforms, const Image8U::Size& resolution) const;
void UpdateCamera(const PlatformArr& platforms);
Expand Down
13 changes: 13 additions & 0 deletions libs/MVS/Interface.h
Original file line number Diff line number Diff line change
Expand Up @@ -258,6 +258,19 @@ bool SerializeLoad(_Tp& obj, const std::string& fileName, uint32_t* pVersion=NUL
return true;
}

// Main exporter & importer
template<typename _Tp>
bool SerializeSave(const _Tp& obj, std::ostream &stream) {
ARCHIVE::ArchiveSave serializer(stream);
serializer & obj;
return true;
}
template<typename _Tp>
bool SerializeLoad(_Tp& obj, std::istream &stream) {
ARCHIVE::ArchiveLoad serializer(stream);
serializer & obj;
return true;
}

#define ARCHIVE_DEFINE_TYPE(TYPE) \
template<> \
Expand Down
22 changes: 20 additions & 2 deletions libs/MVS/Scene.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,25 @@ bool Scene::LoadInterface(const String & fileName)
if (!ARCHIVE::SerializeLoad(obj, fileName))
return false;

return LoadInterface(obj);
} // LoadInterface

bool Scene::LoadInterface(std::istream &is, const uint32_t &width, const uint32_t &height)
{
TD_TIMER_STARTD();
Interface obj;

// serialize in the current state
if (!ARCHIVE::SerializeLoad(obj, is))
return false;

return LoadInterface(obj, width, height);
} // LoadInterface

bool Scene::LoadInterface(Interface &obj, const uint32_t &width, const uint32_t &height)
{
// import platforms and cameras
TD_TIMER_STARTD();
ASSERT(!obj.platforms.empty());
platforms.Reserve((uint32_t)obj.platforms.size());
for (Interface::PlatformArr::const_iterator itPlatform=obj.platforms.begin(); itPlatform!=obj.platforms.end(); ++itPlatform) {
Expand Down Expand Up @@ -132,8 +150,8 @@ bool Scene::LoadInterface(const String & fileName)
imageData.scale = 1;
} else {
// read image header for resolution
if (!imageData.ReloadImage(0, false))
return false;
if ((width == 0 && height == 0) ? !imageData.ReloadImage(0, false) : !imageData.ReloadImage(width, height, 0, 0, false))
return false;
}
imageData.UpdateCamera(platforms);
++nCalibratedImages;
Expand Down
7 changes: 6 additions & 1 deletion libs/MVS/Scene.h
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,8 @@

namespace MVS {

struct Interface;

class MVS_API Scene
{
public:
Expand All @@ -65,6 +67,7 @@ class MVS_API Scene
bool IsEmpty() const;

bool LoadInterface(const String& fileName);
bool LoadInterface(std::istream &is, const uint32_t &width = 0, const uint32_t &height = 0);
bool SaveInterface(const String& fileName) const;

bool Load(const String& fileName);
Expand All @@ -76,7 +79,7 @@ class MVS_API Scene
bool ExportCamerasMLP(const String& fileName, const String& fileNameScene) const;

// Dense reconstruction
bool DenseReconstruction();
bool DenseReconstruction(std::vector<BitMatrix>& theMasks, std::vector<std::vector<unsigned char>> &images = std::vector<std::vector<unsigned char>>(), const uint32_t &width = 0, const uint32_t &height = 0, const uint32_t &depth = 0);
void DenseReconstructionEstimate(void*);
void DenseReconstructionFilter(void*);

Expand Down Expand Up @@ -105,6 +108,8 @@ class MVS_API Scene
ar & mesh;
}
#endif
private:
bool LoadInterface(Interface &obj, const uint32_t &width = 0, const uint32_t &height = 0);
};
/*----------------------------------------------------------------*/

Expand Down
Loading