diff --git a/cpp/src/DO/Sara/MultiViewGeometry/Graph/EpipolarGraph.cpp b/cpp/src/DO/Sara/MultiViewGeometry/Graph/EpipolarGraph.cpp deleted file mode 100644 index 45d62a58c..000000000 --- a/cpp/src/DO/Sara/MultiViewGeometry/Graph/EpipolarGraph.cpp +++ /dev/null @@ -1,256 +0,0 @@ -// ========================================================================== // -// This file is part of Sara, a basic set of libraries in C++ for computer -// vision. -// -// Copyright (C) 2019 David Ok -// -// 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/. -// ========================================================================== // - -#include - -#include -#include -#include -#include -#include - - -namespace DO::Sara { - - auto ViewAttributes::list_images(const std::string& dirpath) -> void - { - if (!image_paths.empty()) - image_paths.clear(); - append(image_paths, ls(dirpath, ".png")); - append(image_paths, ls(dirpath, ".jpg")); - std::sort(image_paths.begin(), image_paths.end()); - - if (!group_names.empty()) - group_names.clear(); - - group_names.reserve(image_paths.size()); - std::transform( - std::begin(image_paths), std::end(image_paths), - std::back_inserter(group_names), - [&](const std::string& image_path) { return basename(image_path); }); - } - - auto ViewAttributes::read_images() -> void - { - images.resize(image_paths.size()); - std::transform(std::begin(image_paths), std::end(image_paths), - std::begin(images), // - [](const auto image_path) { - SARA_DEBUG << "Reading image from:\n\t" << image_path - << std::endl; - return imread(image_path); - }); - } - - auto ViewAttributes::read_keypoints(H5File& h5_file) -> void - { - if (!keypoints.empty()) - keypoints.clear(); - - keypoints.reserve(image_paths.size()); - std::transform(std::begin(group_names), std::end(group_names), - std::back_inserter(keypoints), - [&](const std::string& group_name) { - return DO::Sara::read_keypoints(h5_file, group_name); - }); - } - - - auto EpipolarEdgeAttributes::initialize_edges(int num_vertices) -> void - { - const auto& N = num_vertices; - edge_ids = range(N * (N - 1) / 2); - - if (!edges.empty()) - edges.clear(); - - edges.reserve(N * (N - 1) / 2); - for (int i = 0; i < N; ++i) - for (int j = i + 1; j < N; ++j) - edges.push_back(std::make_pair(i, j)); - } - - auto EpipolarEdgeAttributes::read_matches( - H5File& h5_file, const ViewAttributes& view_attributes) -> void - { - if (!index_matches.empty() || !matches.empty()) - { - index_matches.clear(); - matches.clear(); - } - - index_matches.reserve(edges.size()); - std::transform(std::begin(edges), std::end(edges), - std::back_inserter(index_matches), - [&](const std::pair& edge) { - const auto i = edge.first; - const auto j = edge.second; - - const auto match_dataset = std::string{"matches"} + "/" + - std::to_string(i) + "_" + - std::to_string(j); - - auto mij = std::vector{}; - h5_file.read_dataset(match_dataset, mij); - - return mij; - }); - - matches.reserve(edges.size()); - std::transform(std::begin(edge_ids), std::end(edge_ids), - std::back_inserter(matches), [&](int ij) { - const auto i = edges[ij].first; - const auto j = edges[ij].second; - return to_match(index_matches[ij], - view_attributes.keypoints[i], - view_attributes.keypoints[j]); - }); - } - - auto EpipolarEdgeAttributes::resize_fundamental_edge_list() -> void - { - if (edges.empty()) - return; - - F.resize(edges.size()); - F_num_samples.resize(edges.size()); - F_noise.resize(edges.size()); - F_inliers.resize(edges.size()); - F_best_samples.resize(static_cast(edges.size()), - FEstimator::num_points); - } - - auto EpipolarEdgeAttributes::resize_essential_edge_list() -> void - { - if (edges.empty()) - return; - - E.resize(edges.size()); - E_num_samples.resize(edges.size()); - E_noise.resize(edges.size()); - E_inliers.resize(edges.size()); - E_best_samples.resize(static_cast(edges.size()), - EEstimator::num_points); - } - - auto EpipolarEdgeAttributes::read_fundamental_matrices( - const ViewAttributes& view_attributes, H5File& h5_file) -> void - { - h5_file.read_dataset("F", F); - h5_file.read_dataset("F_num_samples", F_num_samples); - h5_file.read_dataset("F_noise", F_noise); - h5_file.read_dataset("F_best_samples", F_best_samples); - - std::for_each( - std::begin(edge_ids), std::end(edge_ids), [&](const auto& ij) { - const auto& eij = edges[ij]; - const auto i = eij.first; - const auto j = eij.second; - - SARA_DEBUG << "Reading fundamental matrices between images:\n" - << "- image[" << i << "] = " // - << view_attributes.group_names[i] << "\n" - << "- image[" << j << "] = " // - << view_attributes.group_names[j] << "\n"; - std::cout.flush(); - - // Estimate the fundamental matrix. - h5_file.read_dataset(format("F_inliers/%d_%d", i, j), F_inliers[ij]); - }); - } - - auto EpipolarEdgeAttributes::read_essential_matrices( - const ViewAttributes& view_attributes, H5File& h5_file) -> void - { - h5_file.read_dataset("E", E); - h5_file.read_dataset("E_num_samples", E_num_samples); - h5_file.read_dataset("E_noise", E_noise); - h5_file.read_dataset("E_best_samples", E_best_samples); - - std::for_each( - std::begin(edge_ids), std::end(edge_ids), [&](const auto& ij) { - const auto& eij = edges[ij]; - const auto i = eij.first; - const auto j = eij.second; - - SARA_DEBUG << "Reading essential matrices between images:\n" - << "- image[" << i << "] = " // - << view_attributes.group_names[i] << "\n" - << "- image[" << j << "] = " // - << view_attributes.group_names[j] << "\n"; - std::cout.flush(); - - // Estimate the fundamental matrix. - h5_file.read_dataset(format("E_inliers/%d_%d", i, j), E_inliers[ij]); - }); - } - - - auto EpipolarEdgeAttributes::read_two_view_geometries( - const ViewAttributes& view_attributes, H5File& h5_file) -> void - { - two_view_geometries.resize(edges.size()); - - std::for_each( - std::begin(edge_ids), std::end(edge_ids), [&](const auto& ij) { - const auto& eij = edges[ij]; - const auto i = eij.first; - const auto j = eij.second; - - SARA_DEBUG << "Reading two-view geometry between:\n" - << "- image[" << i << "] = " // - << view_attributes.group_names[i] << "\n" - << "- image[" << j << "] = " // - << view_attributes.group_names[j] << "\n"; - std::cout.flush(); - - if (E_inliers[ij].flat_array().count() == 0) - { - SARA_DEBUG << "No inliers... SKIPPING!" << std::endl << std::endl; - return; - } - - SARA_DEBUG << "Reading two-view geometries..." << std::endl; - // Read the cameras. - auto cameras = Tensor_{2}; - h5_file.read_dataset( - format("two_view_geometries/cameras/%d_%d", i, j), cameras); - two_view_geometries[ij].C1 = cameras(0); - two_view_geometries[ij].C2 = cameras(1); - - cameras(0).K.setIdentity(); - cameras(1).K.setIdentity(); -#ifdef DEBUG - SARA_DEBUG << "Normalized C1 =\n" << cameras(0).matrix() << std::endl; - SARA_DEBUG << "Normalized C2 =\n" << cameras(1).matrix() << std::endl; -#endif - - // Read the cameras. - h5_file.read_dataset(format("two_view_geometries/points/%d_%d", i, j), - two_view_geometries[ij].X); -#ifdef DEBUG - SARA_DEBUG << "X =\n" - << two_view_geometries[ij].X.leftCols(20) << std::endl; -#endif - - h5_file.read_dataset( - format("two_view_geometries/cheirality/%d_%d", i, j), - two_view_geometries[ij].cheirality); -#ifdef DEBUG - SARA_DEBUG << "cheirality = " - << two_view_geometries[ij].cheirality.leftCols(20) - << std::endl - << std::endl; -#endif - }); - } - -} /* namespace DO::Sara */ diff --git a/cpp/src/DO/Sara/MultiViewGeometry/Graph/EpipolarGraph.hpp b/cpp/src/DO/Sara/MultiViewGeometry/Graph/EpipolarGraph.hpp deleted file mode 100644 index ed2d5e36f..000000000 --- a/cpp/src/DO/Sara/MultiViewGeometry/Graph/EpipolarGraph.hpp +++ /dev/null @@ -1,129 +0,0 @@ -// ========================================================================== // -// This file is part of Sara, a basic set of libraries in C++ for computer -// vision. -// -// Copyright (C) 2019 David Ok -// -// 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/. -// ========================================================================== // - -#pragma once - -#include - -#include -#include -#include -#include -#include -#include -#include -#include - - -namespace DO::Sara { - - //! @ingroup MultiViewGeometry - //! @defgroup EpipolarGraph Epipolar Graph - //! @{ - - //! @brief View attribute structure. - struct ViewAttributes - { - //! @brief Image metadata. - std::vector group_names; - std::vector image_paths; - //! @brief Image data. - std::vector> images; - //! @brief Keypoints. - std::vector> keypoints; - //! @brief Absolute camera poses. - //! - //! TODO: use more sophisticated camera to estimate distortion parameters - //! later on. - std::vector cameras; - - DO_SARA_EXPORT - auto list_images(const std::string& dirpath) -> void; - - DO_SARA_EXPORT - auto read_images() -> void; - - DO_SARA_EXPORT - auto read_keypoints(H5File& h5_file) -> void; - }; - - - struct EpipolarEdgeAttributes - { - using EEstimator = NisterFivePointAlgorithm; - using FEstimator = EightPointAlgorithm; - - //! @brief An edge 'e' is an index the range [0, N * (N - 1)/ 2[. - //! where N is the number of photographs. - Tensor_ edge_ids; - - // @brief An edge 'e' identifies a photograph pair (i,j) where - // i, j are in [0, N[. - std::vector> edges; - - //! @{ - //! @brief List of feature-based matches for edge (i,j). - std::vector> index_matches; - std::vector> matches; - //! @} - - //! @brief Fundamental matrix F[i,j] for each edge (i,j). - std::vector F; - //! @{ - //! @brief RANSAC metadata for each F[i,j]. - std::vector F_num_samples; - std::vector F_noise; - std::vector> F_inliers; - Tensor_ F_best_samples; - //! @} - - //! @brief Essential matrix E[i,j] for each edge (i,j). - std::vector E; - //! @{ - //! @brief RANSAC metadata for each E[i,j]. - std::vector E_num_samples; - std::vector E_noise; - std::vector> E_inliers; - Tensor_ E_best_samples; - //! @} - - // Two-view geometry G[i,j] for each edge (i,j). - std::vector two_view_geometries; - - DO_SARA_EXPORT - auto initialize_edges(int num_vertices) -> void; - - DO_SARA_EXPORT - auto resize_fundamental_edge_list() -> void; - - DO_SARA_EXPORT - auto resize_essential_edge_list() -> void; - - DO_SARA_EXPORT - auto read_matches(H5File& h5_file, const ViewAttributes& view_attributes) - -> void; - - DO_SARA_EXPORT - auto read_fundamental_matrices(const ViewAttributes& view_attributes, - H5File& h5_file) -> void; - - DO_SARA_EXPORT - auto read_essential_matrices(const ViewAttributes& view_attributes, - H5File& h5_file) -> void; - - DO_SARA_EXPORT - auto read_two_view_geometries(const ViewAttributes& view_attributes, - H5File& h5_file) -> void; - }; - - //! @} - -} /* namespace DO::Sara */