From 78bb392088311e16d0e71dcffd0a28600b4ee488 Mon Sep 17 00:00:00 2001 From: Alexey Spizhevoy Date: Tue, 16 Aug 2011 12:36:11 +0000 Subject: [PATCH] refactored opencv_stitching --- modules/stitching/camera.cpp | 27 ++++++--------------- modules/stitching/camera.hpp | 13 +++++----- modules/stitching/motion_estimators.cpp | 43 +++++++++++---------------------- modules/stitching/motion_estimators.hpp | 13 +--------- modules/stitching/util.cpp | 6 ++--- modules/stitching/util.hpp | 10 ++++---- 6 files changed, 37 insertions(+), 75 deletions(-) diff --git a/modules/stitching/camera.cpp b/modules/stitching/camera.cpp index a81776a..3c295e7 100644 --- a/modules/stitching/camera.cpp +++ b/modules/stitching/camera.cpp @@ -5,27 +5,14 @@ using namespace std; using namespace cv; -CameraInfo CameraInfo::load(const string &path) -{ - FileStorage fs(path, FileStorage::READ); - CV_Assert(fs.isOpened()); - - CameraInfo cam; - if (!fs["R"].isNone()) - fs["R"] >> cam.R; - if (!fs["K"].isNone()) - fs["K"] >> cam.K; - return cam; -} +CameraParams::CameraParams() : focal(1), R(Mat::eye(3, 3, CV_64F)), t(Mat::zeros(3, 1, CV_64F)) {} +CameraParams::CameraParams(const CameraParams &other) { *this = other; } -void CameraInfo::save(const string &path) +const CameraParams& CameraParams::operator =(const CameraParams &other) { - FileStorage fs(path, FileStorage::WRITE); - CV_Assert(fs.isOpened()); - - if (!R.empty()) - fs << "R" << R; - if (!K.empty()) - fs << "K" << K; + focal = other.focal; + R = other.R.clone(); + t = other.t.clone(); + return *this; } diff --git a/modules/stitching/camera.hpp b/modules/stitching/camera.hpp index 9d18a60..15ce898 100644 --- a/modules/stitching/camera.hpp +++ b/modules/stitching/camera.hpp @@ -45,14 +45,15 @@ #include "precomp.hpp" -class CameraInfo +struct CameraParams { -public: - static CameraInfo load(const std::string &path); - void save(const std::string &path); + CameraParams(); + CameraParams(const CameraParams& other); + const CameraParams& operator =(const CameraParams& other); - cv::Mat R; - cv::Mat K; + double focal; // Focal length + cv::Mat R; // Rotation + cv::Mat t; // Translation }; #endif // #ifndef __OPENCV_CAMERA_HPP__ diff --git a/modules/stitching/motion_estimators.cpp b/modules/stitching/motion_estimators.cpp index 8387bd8..40e70ed 100644 --- a/modules/stitching/motion_estimators.cpp +++ b/modules/stitching/motion_estimators.cpp @@ -51,21 +51,6 @@ using namespace cv; ////////////////////////////////////////////////////////////////////////////// -CameraParams::CameraParams() : focal(1), R(Mat::eye(3, 3, CV_64F)), t(Mat::zeros(3, 1, CV_64F)) {} - -CameraParams::CameraParams(const CameraParams &other) { *this = other; } - -const CameraParams& CameraParams::operator =(const CameraParams &other) -{ - focal = other.focal; - R = other.R.clone(); - t = other.t.clone(); - return *this; -} - - -////////////////////////////////////////////////////////////////////////////// - struct IncDistance { IncDistance(vector &dists) : dists(&dists[0]) {} @@ -434,7 +419,7 @@ string matchesGraphAsString(vector &pathes, vector &pairwis const int num_images = static_cast(pathes.size()); set > span_tree_edges; - DjSets comps(num_images); + DisjointSets comps(num_images); for (int i = 0; i < num_images; ++i) { @@ -442,11 +427,11 @@ string matchesGraphAsString(vector &pathes, vector &pairwis { if (pairwise_matches[i*num_images + j].confidence < conf_threshold) continue; - int comp1 = comps.find(i); - int comp2 = comps.find(j); + int comp1 = comps.findSetByElem(i); + int comp2 = comps.findSetByElem(j); if (comp1 != comp2) { - comps.merge(comp1, comp2); + comps.mergeSets(comp1, comp2); span_tree_edges.insert(make_pair(i, j)); } } @@ -478,7 +463,7 @@ string matchesGraphAsString(vector &pathes, vector &pairwis for (size_t i = 0; i < comps.size.size(); ++i) { - if (comps.size[comps.find(i)] == 1) + if (comps.size[comps.findSetByElem(i)] == 1) { string name = pathes[i]; size_t prefix_len = name.find_last_of("/\\"); @@ -497,17 +482,17 @@ vector leaveBiggestComponent(vector &features, vector(features.size()); - DjSets comps(num_images); + DisjointSets comps(num_images); for (int i = 0; i < num_images; ++i) { for (int j = 0; j < num_images; ++j) { if (pairwise_matches[i*num_images + j].confidence < conf_threshold) continue; - int comp1 = comps.find(i); - int comp2 = comps.find(j); + int comp1 = comps.findSetByElem(i); + int comp2 = comps.findSetByElem(j); if (comp1 != comp2) - comps.merge(comp1, comp2); + comps.mergeSets(comp1, comp2); } } @@ -516,7 +501,7 @@ vector leaveBiggestComponent(vector &features, vector indices; vector indices_removed; for (int i = 0; i < num_images; ++i) - if (comps.find(i) == max_comp) + if (comps.findSetByElem(i) == max_comp) indices.push_back(i); else indices_removed.push_back(i); @@ -569,7 +554,7 @@ void findMaxSpanningTree(int num_images, const vector &pairwise_mat } } - DjSets comps(num_images); + DisjointSets comps(num_images); span_tree.create(num_images); vector span_tree_powers(num_images, 0); @@ -577,11 +562,11 @@ void findMaxSpanningTree(int num_images, const vector &pairwise_mat sort(edges.begin(), edges.end(), greater()); for (size_t i = 0; i < edges.size(); ++i) { - int comp1 = comps.find(edges[i].from); - int comp2 = comps.find(edges[i].to); + int comp1 = comps.findSetByElem(edges[i].from); + int comp2 = comps.findSetByElem(edges[i].to); if (comp1 != comp2) { - comps.merge(comp1, comp2); + comps.mergeSets(comp1, comp2); span_tree.addEdge(edges[i].from, edges[i].to, edges[i].weight); span_tree.addEdge(edges[i].to, edges[i].from, edges[i].weight); span_tree_powers[edges[i].from]++; diff --git a/modules/stitching/motion_estimators.hpp b/modules/stitching/motion_estimators.hpp index bb121b5..4618563 100644 --- a/modules/stitching/motion_estimators.hpp +++ b/modules/stitching/motion_estimators.hpp @@ -45,18 +45,7 @@ #include "precomp.hpp" #include "matchers.hpp" #include "util.hpp" - -struct CameraParams -{ - CameraParams(); - CameraParams(const CameraParams& other); - const CameraParams& operator =(const CameraParams& other); - - double focal; // Focal length - cv::Mat R; // Rotation - cv::Mat t; // Translation -}; - +#include "camera.hpp" class Estimator { diff --git a/modules/stitching/util.cpp b/modules/stitching/util.cpp index 6cba9e3..9020271 100644 --- a/modules/stitching/util.cpp +++ b/modules/stitching/util.cpp @@ -44,7 +44,7 @@ using namespace std; using namespace cv; -void DjSets::create(int n) +void DisjointSets::createOneElemSets(int n) { rank_.assign(n, 0); size.assign(n, 1); @@ -54,7 +54,7 @@ void DjSets::create(int n) } -int DjSets::find(int elem) +int DisjointSets::findSetByElem(int elem) { int set = elem; while (set != parent[set]) @@ -70,7 +70,7 @@ int DjSets::find(int elem) } -int DjSets::merge(int set1, int set2) +int DisjointSets::mergeSets(int set1, int set2) { if (rank_[set1] < rank_[set2]) { diff --git a/modules/stitching/util.hpp b/modules/stitching/util.hpp index 4c2db0d..265a20b 100644 --- a/modules/stitching/util.hpp +++ b/modules/stitching/util.hpp @@ -57,14 +57,14 @@ #define LOGLN(msg) LOG(msg << std::endl) -class DjSets +class DisjointSets { public: - DjSets(int n = 0) { create(n); } + DisjointSets(int elem_count = 0) { createOneElemSets(elem_count); } - void create(int n); - int find(int elem); - int merge(int set1, int set2); + void createOneElemSets(int elem_count); + int findSetByElem(int elem); + int mergeSets(int set1, int set2); std::vector parent; std::vector size; -- 2.7.4