Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/develop_2.0_fixed_extrinsics_in_…
Browse files Browse the repository at this point in the history
…ba' into develop_2.0
  • Loading branch information
pmoulon committed Mar 20, 2021
2 parents 4569d2f + cfeff2d commit 19d69e7
Show file tree
Hide file tree
Showing 15 changed files with 624 additions and 44 deletions.
53 changes: 53 additions & 0 deletions src/openMVG/cameras/Cameras_Common_command_line_helper.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,11 @@
#include <string>
#include <vector>

#include "openMVG/sfm/sfm_data_BA.hpp"
#include "openMVG/stl/split.hpp"
#include "openMVG/system/logger.hpp"


namespace openMVG
{
namespace cameras
Expand Down Expand Up @@ -76,6 +78,57 @@ StringTo_Intrinsic_Parameter_Type
}

} // namespace cameras

namespace sfm
{
// Allow to initialize an object sfm::Extrinsic_Parameter_Type BA from
// a string and delimiters('|')
//

inline
sfm::Extrinsic_Parameter_Type
StringTo_Extrinsic_Parameter_Type
(
const std::string & rhs
)
{
// Split the string by the '|' token.
std::vector<std::string> items;
stl::split(rhs, '|', items);

sfm::Extrinsic_Parameter_Type extrinsics_opt =
static_cast<sfm::Extrinsic_Parameter_Type>(0);

// Look for the "STRING KEY" parameters and initialize them
for (const std::string & item : items)
{
// cameras::Intrinsic_Parameter_Type
if (item == "NONE")
{
return sfm::Extrinsic_Parameter_Type::NONE;
}
else if (item == "ADJUST_TRANSLATION")
{
extrinsics_opt = extrinsics_opt | sfm::Extrinsic_Parameter_Type::ADJUST_TRANSLATION;
}
else if (item == "ADJUST_ROTATION")
{
extrinsics_opt = extrinsics_opt | sfm::Extrinsic_Parameter_Type::ADJUST_ROTATION;
}
else if (item == "ADJUST_ALL")
{
extrinsics_opt = sfm::Extrinsic_Parameter_Type::ADJUST_ALL;
}
else
{
OPENMVG_LOG_ERROR << "WARNING: Unknow KEY: " << item;
extrinsics_opt = static_cast<sfm::Extrinsic_Parameter_Type>(0);
break;
}
}
return extrinsics_opt;
}
} // namespace sfm
} // namespace openMVG

#endif // OPENMVG_CAMERAS_CAMERAS_COMMON_COMMAND_LINE_HELPER_HPP
8 changes: 7 additions & 1 deletion src/openMVG/features/binary_regions.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,9 @@

#include <typeinfo>

#include "openMVG/features/regions.hpp"
#include "openMVG/features/descriptor.hpp"
#include "openMVG/features/regions.hpp"
#include "openMVG/features/regions_scale_sort.hpp"
#include "openMVG/matching/metric.hpp"

namespace openMVG {
Expand Down Expand Up @@ -126,6 +127,11 @@ class Binary_Regions : public Regions
static_cast<Binary_Regions<FeatT, L> *>(region_container)->vec_descs_.push_back(vec_descs_[i]);
}

bool SortAndSelectByRegionScale(int keep_count = -1) override
{
return features::SortAndSelectByRegionScale<FeatT, DescsT>(vec_feats_, vec_descs_, keep_count);
}

private:
//--
//-- internal data
Expand Down
5 changes: 5 additions & 0 deletions src/openMVG/features/regions.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,11 @@ class Regions
/// Add the Inth region to another Region container
virtual void CopyRegion(size_t i, Regions *) const = 0;

/// Sort the regions according the scale of the feature
/// If the regions are not Scale invariant nothing is done and false is returned
/// keep_count is used to save feature from largest scale to lower (-1 keep everything)
virtual bool SortAndSelectByRegionScale(int keep_count = -1) = 0;

virtual Regions * EmptyClone() const = 0;

};
Expand Down
96 changes: 96 additions & 0 deletions src/openMVG/features/regions_scale_sort.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
// This file is part of OpenMVG, an Open Multiple View Geometry C++ library.

// Copyright (c) 2019 Pierre MOULON.

// 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 http://mozilla.org/MPL/2.0/.

#ifndef OPENMVG_FEATURES_REGIONS_SCALE_SORT_HPP
#define OPENMVG_FEATURES_REGIONS_SCALE_SORT_HPP

#include <numeric> // std::iota
#include <algorithm> // std::sort

namespace openMVG {
namespace features {

/// Get the reordering indexes of the feature according the feature scale
/// reordered by increasing scale.
template <typename T>
std::vector<size_t> sort_indexes_by_scale(const std::vector<T> &v) {

// initialize original index locations
std::vector<size_t> idx(v.size());
std::iota(idx.begin(), idx.end(), 0);

// sort indexes based on comparing scale values of the features in v
std::sort(idx.begin(), idx.end(),
[&v](size_t i1, size_t i2) {return v[i1].scale() < v[i2].scale();});

return idx;
}

/// Sort the features and descriptor according the scale of the feature
/// A `keep_count` filter can be used to select only the feature with the largest scale
template
<
typename FeatT,
typename DescsT
>
bool SortAndSelectByRegionScale
(
std::vector<FeatT> & feats,
DescsT & descs,
int keep_count = -1
)
{
return false;
}

/// SortAndSelectByRegionScale specialization for the SIOPointFeature type.
template
<
typename FeatT,
typename DescsT
>
bool SortAndSelectByRegionScale
(
std::vector<SIOPointFeature> & feats,
DescsT & descs,
int keep_count = -1
)
{
// Reorder features and descriptors
// a. get the re-ordering sequence
// b. apply the re-ordering sequence
const std::vector<size_t> re_ordering = sort_indexes_by_scale(feats);
auto feats_reordered = feats;
auto descs_reordered = descs;
for (size_t i = 0; i < re_ordering.size(); ++i)
{
feats_reordered[i] = feats[re_ordering[i]];
descs_reordered[i] = descs[re_ordering[i]];
}

// Keep only the regions with the largest scale:
{
const int region_to_keep =
(keep_count == -1) ?
feats_reordered.size() :
std::min(keep_count, static_cast<int>(feats_reordered.size()));

auto start_feat_it = std::prev(feats_reordered.end(), region_to_keep);
feats.assign(start_feat_it, feats_reordered.end());

auto start_desc_it = std::prev(descs_reordered.end(), region_to_keep);
descs.assign(start_desc_it, descs_reordered.end());

}
return true;
}

} // namespace features
} // namespace openMVG

#endif // OPENMVG_FEATURES_REGIONS_SCALE_SORT_HPP
8 changes: 7 additions & 1 deletion src/openMVG/features/scalar_regions.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,9 @@

#include <typeinfo>

#include "openMVG/features/regions.hpp"
#include "openMVG/features/descriptor.hpp"
#include "openMVG/features/regions.hpp"
#include "openMVG/features/regions_scale_sort.hpp"
#include "openMVG/matching/metric.hpp"

namespace openMVG {
Expand Down Expand Up @@ -122,6 +123,11 @@ class Scalar_Regions : public Regions
static_cast<Scalar_Regions<FeatT, T, L> *>(region_container)->vec_descs_.push_back(vec_descs_[i]);
}

bool SortAndSelectByRegionScale(int keep_count = -1) override
{
return features::SortAndSelectByRegionScale<FeatT, DescsT>(vec_feats_, vec_descs_, keep_count);
}

private:
//--
//-- internal data
Expand Down
4 changes: 2 additions & 2 deletions src/openMVG/sfm/pipelines/sequential/sequential_SfM2.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -191,7 +191,7 @@ bool SequentialSfMReconstructionEngine2::Process() {
//-- Reconstruction done.
//-- Display some statistics
OPENMVG_LOG_INFO
<< "\n\n-------------------------------" << "\n"
<< "\n-------------------------------" << "\n"
<< "-- Structure from Motion (statistics):\n"
<< "-- #Camera calibrated: " << sfm_data_.GetPoses().size()
<< " from " << sfm_data_.GetViews().size() << " input images.\n"
Expand Down Expand Up @@ -524,7 +524,7 @@ bool SequentialSfMReconstructionEngine2::BundleAdjustment()
Bundle_Adjustment_Ceres bundle_adjustment_obj(options);
const Optimize_Options ba_refine_options
( ReconstructionEngine::intrinsic_refinement_options_,
Extrinsic_Parameter_Type::ADJUST_ALL, // Adjust camera motion
ReconstructionEngine::extrinsic_refinement_options_,
Structure_Parameter_Type::ADJUST_ALL, // Adjust scene structure
Control_Point_Parameter(),
this->b_use_motion_prior_
Expand Down
16 changes: 16 additions & 0 deletions src/openMVG/sfm/pipelines/sfm_engine.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@

#include "openMVG/cameras/Camera_Common.hpp"
#include "openMVG/sfm/sfm_data.hpp"
#include "openMVG/sfm/sfm_data_BA.hpp"

namespace openMVG {
namespace sfm {
Expand All @@ -31,6 +32,7 @@ class ReconstructionEngine
:sOut_directory_(soutDirectory),
sfm_data_(sfm_data),
intrinsic_refinement_options_(cameras::Intrinsic_Parameter_Type::ADJUST_ALL),
extrinsic_refinement_options_(sfm::Extrinsic_Parameter_Type::ADJUST_ALL),
b_use_motion_prior_(false)
{
}
Expand All @@ -52,6 +54,19 @@ class ReconstructionEngine
intrinsic_refinement_options_ = rhs;
}

Extrinsic_Parameter_Type Get_Extrinsics_Refinement_Type() const
{
return extrinsic_refinement_options_;
}

void Set_Extrinsics_Refinement_Type
(
sfm::Extrinsic_Parameter_Type rhs
)
{
extrinsic_refinement_options_ = rhs;
}

void Set_Use_Motion_Prior
(
bool rhs
Expand All @@ -74,6 +89,7 @@ class ReconstructionEngine
//-- Reconstruction parameters
//-----
cameras::Intrinsic_Parameter_Type intrinsic_refinement_options_;
sfm::Extrinsic_Parameter_Type extrinsic_refinement_options_;
bool b_use_motion_prior_;
};

Expand Down
90 changes: 90 additions & 0 deletions src/openMVG/sfm/pipelines/sfm_preemptive_regions_provider.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
// This file is part of OpenMVG, an Open Multiple View Geometry C++ library.

// Copyright (c) 2019 Pierre MOULON.

// 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 http://mozilla.org/MPL/2.0/.

#ifndef OPENMVG_SFM_SFM_PREEMPTIVE_REGIONS_PROVIDER_HPP
#define OPENMVG_SFM_SFM_PREEMPTIVE_REGIONS_PROVIDER_HPP

#include "openMVG/sfm/pipelines/sfm_regions_provider.hpp"

namespace openMVG {
namespace sfm {

/// Abstract Regions provider
/// Allow to load and return a subset of regions related to a view
struct Preemptive_Regions_Provider : public Regions_Provider
{
public:

/// Set the number of regions to keep per image (largest scale kept first)
explicit Preemptive_Regions_Provider(int kept_regions_count = 100):
Regions_Provider(), kept_regions_count_(kept_regions_count)
{};

// Load Regions related to a provided SfM_Data View container
bool load(
const SfM_Data & sfm_data,
const std::string & feat_directory,
std::unique_ptr<features::Regions>& region_type,
system::ProgressInterface * my_progress_bar = nullptr) override
{
if (!my_progress_bar)
my_progress_bar = &system::ProgressInterface::dummy();
region_type_.reset(region_type->EmptyClone());

my_progress_bar->Restart(sfm_data.GetViews().size(), "- Regions ---- Loading -");
// Read for each view the corresponding regions and store them
std::atomic<bool> bContinue(true);
#ifdef OPENMVG_USE_OPENMP
#pragma omp parallel
#endif
for (Views::const_iterator iter = sfm_data.GetViews().begin();
iter != sfm_data.GetViews().end() && bContinue; ++iter)
{
if (my_progress_bar->hasBeenCanceled())
{
bContinue = false;
continue;
}
#ifdef OPENMVG_USE_OPENMP
#pragma omp single nowait
#endif
{
const std::string sImageName = stlplus::create_filespec(sfm_data.s_root_path, iter->second->s_Img_path);
const std::string basename = stlplus::basename_part(sImageName);
const std::string featFile = stlplus::create_filespec(feat_directory, basename, ".feat");
const std::string descFile = stlplus::create_filespec(feat_directory, basename, ".desc");

std::unique_ptr<features::Regions> regions_ptr(region_type->EmptyClone());
if (!regions_ptr->Load(featFile, descFile))
{
OPENMVG_LOG_ERROR << "Invalid regions files for the view: " << sImageName;
bContinue = false;
}
//else
#ifdef OPENMVG_USE_OPENMP
#pragma omp critical
#endif
{
// Sort regions by feature scale & keep the desired count
regions_ptr->SortAndSelectByRegionScale(kept_regions_count_);
cache_[iter->second->id_view] = std::move(regions_ptr);
}
++(*my_progress_bar);
}
}
return bContinue;
}

protected:
int kept_regions_count_ = 100;
}; // Preemptive_Regions_Provider

} // namespace sfm
} // namespace openMVG

#endif // OPENMVG_SFM_SFM_PREEMPTIVE_REGIONS_PROVIDER_HPP
10 changes: 4 additions & 6 deletions src/openMVG/sfm/pipelines/sfm_regions_provider.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -75,15 +75,13 @@ struct Regions_Provider

virtual std::shared_ptr<features::Regions> get(const IndexT x) const
{
auto it = cache_.find(x);
std::shared_ptr<features::Regions> ret;

if (it != end(cache_))
const auto it = cache_.find(x);
if (it != std::end(cache_))
{
ret = it->second;
return it->second;
}
// else Invalid ressource
return ret;
return {};
}

// Load Regions related to a provided SfM_Data View container
Expand Down
Loading

0 comments on commit 19d69e7

Please sign in to comment.