refactored opencv_stitching

This commit is contained in:
Alexey Spizhevoy 2011-08-16 12:36:11 +00:00
parent f1e8b43c7a
commit 78bb392088
6 changed files with 39 additions and 77 deletions

View File

@ -5,27 +5,14 @@ using namespace std;
using namespace cv; using namespace cv;
CameraInfo CameraInfo::load(const string &path) 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)
{ {
FileStorage fs(path, FileStorage::READ); focal = other.focal;
CV_Assert(fs.isOpened()); R = other.R.clone();
t = other.t.clone();
CameraInfo cam; return *this;
if (!fs["R"].isNone())
fs["R"] >> cam.R;
if (!fs["K"].isNone())
fs["K"] >> cam.K;
return cam;
}
void CameraInfo::save(const string &path)
{
FileStorage fs(path, FileStorage::WRITE);
CV_Assert(fs.isOpened());
if (!R.empty())
fs << "R" << R;
if (!K.empty())
fs << "K" << K;
} }

View File

@ -45,14 +45,15 @@
#include "precomp.hpp" #include "precomp.hpp"
class CameraInfo struct CameraParams
{ {
public: CameraParams();
static CameraInfo load(const std::string &path); CameraParams(const CameraParams& other);
void save(const std::string &path); const CameraParams& operator =(const CameraParams& other);
cv::Mat R; double focal; // Focal length
cv::Mat K; cv::Mat R; // Rotation
cv::Mat t; // Translation
}; };
#endif // #ifndef __OPENCV_CAMERA_HPP__ #endif // #ifndef __OPENCV_CAMERA_HPP__

View File

@ -49,21 +49,6 @@ using namespace std;
using namespace cv; 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 struct IncDistance
@ -434,7 +419,7 @@ string matchesGraphAsString(vector<string> &pathes, vector<MatchesInfo> &pairwis
const int num_images = static_cast<int>(pathes.size()); const int num_images = static_cast<int>(pathes.size());
set<pair<int,int> > span_tree_edges; set<pair<int,int> > span_tree_edges;
DjSets comps(num_images); DisjointSets comps(num_images);
for (int i = 0; i < num_images; ++i) for (int i = 0; i < num_images; ++i)
{ {
@ -442,11 +427,11 @@ string matchesGraphAsString(vector<string> &pathes, vector<MatchesInfo> &pairwis
{ {
if (pairwise_matches[i*num_images + j].confidence < conf_threshold) if (pairwise_matches[i*num_images + j].confidence < conf_threshold)
continue; continue;
int comp1 = comps.find(i); int comp1 = comps.findSetByElem(i);
int comp2 = comps.find(j); int comp2 = comps.findSetByElem(j);
if (comp1 != comp2) if (comp1 != comp2)
{ {
comps.merge(comp1, comp2); comps.mergeSets(comp1, comp2);
span_tree_edges.insert(make_pair(i, j)); span_tree_edges.insert(make_pair(i, j));
} }
} }
@ -478,7 +463,7 @@ string matchesGraphAsString(vector<string> &pathes, vector<MatchesInfo> &pairwis
for (size_t i = 0; i < comps.size.size(); ++i) 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]; string name = pathes[i];
size_t prefix_len = name.find_last_of("/\\"); size_t prefix_len = name.find_last_of("/\\");
@ -497,17 +482,17 @@ vector<int> leaveBiggestComponent(vector<ImageFeatures> &features, vector<Match
{ {
const int num_images = static_cast<int>(features.size()); const int num_images = static_cast<int>(features.size());
DjSets comps(num_images); DisjointSets comps(num_images);
for (int i = 0; i < num_images; ++i) for (int i = 0; i < num_images; ++i)
{ {
for (int j = 0; j < num_images; ++j) for (int j = 0; j < num_images; ++j)
{ {
if (pairwise_matches[i*num_images + j].confidence < conf_threshold) if (pairwise_matches[i*num_images + j].confidence < conf_threshold)
continue; continue;
int comp1 = comps.find(i); int comp1 = comps.findSetByElem(i);
int comp2 = comps.find(j); int comp2 = comps.findSetByElem(j);
if (comp1 != comp2) if (comp1 != comp2)
comps.merge(comp1, comp2); comps.mergeSets(comp1, comp2);
} }
} }
@ -516,7 +501,7 @@ vector<int> leaveBiggestComponent(vector<ImageFeatures> &features, vector<Match
vector<int> indices; vector<int> indices;
vector<int> indices_removed; vector<int> indices_removed;
for (int i = 0; i < num_images; ++i) for (int i = 0; i < num_images; ++i)
if (comps.find(i) == max_comp) if (comps.findSetByElem(i) == max_comp)
indices.push_back(i); indices.push_back(i);
else else
indices_removed.push_back(i); indices_removed.push_back(i);
@ -569,7 +554,7 @@ void findMaxSpanningTree(int num_images, const vector<MatchesInfo> &pairwise_mat
} }
} }
DjSets comps(num_images); DisjointSets comps(num_images);
span_tree.create(num_images); span_tree.create(num_images);
vector<int> span_tree_powers(num_images, 0); vector<int> span_tree_powers(num_images, 0);
@ -577,11 +562,11 @@ void findMaxSpanningTree(int num_images, const vector<MatchesInfo> &pairwise_mat
sort(edges.begin(), edges.end(), greater<GraphEdge>()); sort(edges.begin(), edges.end(), greater<GraphEdge>());
for (size_t i = 0; i < edges.size(); ++i) for (size_t i = 0; i < edges.size(); ++i)
{ {
int comp1 = comps.find(edges[i].from); int comp1 = comps.findSetByElem(edges[i].from);
int comp2 = comps.find(edges[i].to); int comp2 = comps.findSetByElem(edges[i].to);
if (comp1 != comp2) 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].from, edges[i].to, edges[i].weight);
span_tree.addEdge(edges[i].to, edges[i].from, edges[i].weight); span_tree.addEdge(edges[i].to, edges[i].from, edges[i].weight);
span_tree_powers[edges[i].from]++; span_tree_powers[edges[i].from]++;

View File

@ -45,18 +45,7 @@
#include "precomp.hpp" #include "precomp.hpp"
#include "matchers.hpp" #include "matchers.hpp"
#include "util.hpp" #include "util.hpp"
#include "camera.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
};
class Estimator class Estimator
{ {

View File

@ -44,7 +44,7 @@
using namespace std; using namespace std;
using namespace cv; using namespace cv;
void DjSets::create(int n) void DisjointSets::createOneElemSets(int n)
{ {
rank_.assign(n, 0); rank_.assign(n, 0);
size.assign(n, 1); 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; int set = elem;
while (set != parent[set]) 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]) if (rank_[set1] < rank_[set2])
{ {

View File

@ -57,14 +57,14 @@
#define LOGLN(msg) LOG(msg << std::endl) #define LOGLN(msg) LOG(msg << std::endl)
class DjSets class DisjointSets
{ {
public: public:
DjSets(int n = 0) { create(n); } DisjointSets(int elem_count = 0) { createOneElemSets(elem_count); }
void create(int n); void createOneElemSets(int elem_count);
int find(int elem); int findSetByElem(int elem);
int merge(int set1, int set2); int mergeSets(int set1, int set2);
std::vector<int> parent; std::vector<int> parent;
std::vector<int> size; std::vector<int> size;