refactored stitching module

This commit is contained in:
Alexey Spizhevoy 2011-09-05 11:52:30 +00:00
parent 9be4701f24
commit fbe2e6fb01
9 changed files with 207 additions and 235 deletions

View File

@ -42,11 +42,24 @@
#include "precomp.hpp" #include "precomp.hpp"
using namespace std; using namespace std;
using namespace cv;
namespace cv namespace
{ {
template<typename _Tp> static inline bool
decomposeCholesky(_Tp* A, size_t astep, int m)
{
if (!Cholesky(A, astep, m, 0, 0, 0))
return false;
astep /= sizeof(A[0]);
for (int i = 0; i < m; ++i)
A[i*astep + i] = (_Tp)(1./A[i*astep + i]);
return true;
}
} // namespace
void focalsFromHomography(const Mat& H, double &f0, double &f1, bool &f0_ok, bool &f1_ok)
void cv::focalsFromHomography(const Mat& H, double &f0, double &f1, bool &f0_ok, bool &f1_ok)
{ {
CV_Assert(H.type() == CV_64F && H.size() == Size(3, 3)); CV_Assert(H.type() == CV_64F && H.size() == Size(3, 3));
@ -77,8 +90,8 @@ void focalsFromHomography(const Mat& H, double &f0, double &f1, bool &f0_ok, boo
} }
void estimateFocal(const vector<ImageFeatures> &features, const vector<MatchesInfo> &pairwise_matches, void cv::estimateFocal(const vector<ImageFeatures> &features, const vector<MatchesInfo> &pairwise_matches,
vector<double> &focals) vector<double> &focals)
{ {
const int num_images = static_cast<int>(features.size()); const int num_images = static_cast<int>(features.size());
focals.resize(num_images); focals.resize(num_images);
@ -118,22 +131,7 @@ void estimateFocal(const vector<ImageFeatures> &features, const vector<MatchesIn
} }
namespace bool cv::calibrateRotatingCamera(const vector<Mat> &Hs, Mat &K)
{
template<typename _Tp> static inline bool
decomposeCholesky(_Tp* A, size_t astep, int m)
{
if (!Cholesky(A, astep, m, 0, 0, 0))
return false;
astep /= sizeof(A[0]);
for (int i = 0; i < m; ++i)
A[i*astep + i] = (_Tp)(1./A[i*astep + i]);
return true;
}
} // namespace
bool calibrateRotatingCamera(const vector<Mat> &Hs, Mat &K)
{ {
int m = static_cast<int>(Hs.size()); int m = static_cast<int>(Hs.size());
CV_Assert(m >= 1); CV_Assert(m >= 1);
@ -183,5 +181,3 @@ bool calibrateRotatingCamera(const vector<Mat> &Hs, Mat &K)
K = W.t(); K = W.t();
return true; return true;
} }
} // namespace cv

View File

@ -42,13 +42,11 @@
#include "precomp.hpp" #include "precomp.hpp"
using namespace std; using namespace std;
using namespace cv;
namespace cv
{
static const float WEIGHT_EPS = 1e-5f; static const float WEIGHT_EPS = 1e-5f;
Ptr<Blender> Blender::createDefault(int type, bool try_gpu) Ptr<Blender> cv::Blender::createDefault(int type, bool try_gpu)
{ {
if (type == NO) if (type == NO)
return new Blender(); return new Blender();
@ -61,13 +59,13 @@ Ptr<Blender> Blender::createDefault(int type, bool try_gpu)
} }
void Blender::prepare(const vector<Point> &corners, const vector<Size> &sizes) void cv::Blender::prepare(const vector<Point> &corners, const vector<Size> &sizes)
{ {
prepare(resultRoi(corners, sizes)); prepare(resultRoi(corners, sizes));
} }
void Blender::prepare(Rect dst_roi) void cv::Blender::prepare(Rect dst_roi)
{ {
dst_.create(dst_roi.size(), CV_16SC3); dst_.create(dst_roi.size(), CV_16SC3);
dst_.setTo(Scalar::all(0)); dst_.setTo(Scalar::all(0));
@ -77,7 +75,7 @@ void Blender::prepare(Rect dst_roi)
} }
void Blender::feed(const Mat &img, const Mat &mask, Point tl) void cv::Blender::feed(const Mat &img, const Mat &mask, Point tl)
{ {
CV_Assert(img.type() == CV_16SC3); CV_Assert(img.type() == CV_16SC3);
CV_Assert(mask.type() == CV_8U); CV_Assert(mask.type() == CV_8U);
@ -101,7 +99,7 @@ void Blender::feed(const Mat &img, const Mat &mask, Point tl)
} }
void Blender::blend(Mat &dst, Mat &dst_mask) void cv::Blender::blend(Mat &dst, Mat &dst_mask)
{ {
dst_.setTo(Scalar::all(0), dst_mask_ == 0); dst_.setTo(Scalar::all(0), dst_mask_ == 0);
dst = dst_; dst = dst_;
@ -111,7 +109,7 @@ void Blender::blend(Mat &dst, Mat &dst_mask)
} }
void FeatherBlender::prepare(Rect dst_roi) void cv::FeatherBlender::prepare(Rect dst_roi)
{ {
Blender::prepare(dst_roi); Blender::prepare(dst_roi);
dst_weight_map_.create(dst_roi.size(), CV_32F); dst_weight_map_.create(dst_roi.size(), CV_32F);
@ -119,7 +117,7 @@ void FeatherBlender::prepare(Rect dst_roi)
} }
void FeatherBlender::feed(const Mat &img, const Mat &mask, Point tl) void cv::FeatherBlender::feed(const Mat &img, const Mat &mask, Point tl)
{ {
CV_Assert(img.type() == CV_16SC3); CV_Assert(img.type() == CV_16SC3);
CV_Assert(mask.type() == CV_8U); CV_Assert(mask.type() == CV_8U);
@ -146,7 +144,7 @@ void FeatherBlender::feed(const Mat &img, const Mat &mask, Point tl)
} }
void FeatherBlender::blend(Mat &dst, Mat &dst_mask) void cv::FeatherBlender::blend(Mat &dst, Mat &dst_mask)
{ {
normalizeUsingWeightMap(dst_weight_map_, dst_); normalizeUsingWeightMap(dst_weight_map_, dst_);
dst_mask_ = dst_weight_map_ > WEIGHT_EPS; dst_mask_ = dst_weight_map_ > WEIGHT_EPS;
@ -154,14 +152,14 @@ void FeatherBlender::blend(Mat &dst, Mat &dst_mask)
} }
MultiBandBlender::MultiBandBlender(int try_gpu, int num_bands) cv::MultiBandBlender::MultiBandBlender(int try_gpu, int num_bands)
{ {
setNumBands(num_bands); setNumBands(num_bands);
can_use_gpu_ = try_gpu && gpu::getCudaEnabledDeviceCount(); can_use_gpu_ = try_gpu && gpu::getCudaEnabledDeviceCount();
} }
void MultiBandBlender::prepare(Rect dst_roi) void cv::MultiBandBlender::prepare(Rect dst_roi)
{ {
dst_roi_final_ = dst_roi; dst_roi_final_ = dst_roi;
@ -194,7 +192,7 @@ void MultiBandBlender::prepare(Rect dst_roi)
} }
void MultiBandBlender::feed(const Mat &img, const Mat &mask, Point tl) void cv::MultiBandBlender::feed(const Mat &img, const Mat &mask, Point tl)
{ {
CV_Assert(img.type() == CV_16SC3); CV_Assert(img.type() == CV_16SC3);
CV_Assert(mask.type() == CV_8U); CV_Assert(mask.type() == CV_8U);
@ -279,7 +277,7 @@ void MultiBandBlender::feed(const Mat &img, const Mat &mask, Point tl)
} }
void MultiBandBlender::blend(Mat &dst, Mat &dst_mask) void cv::MultiBandBlender::blend(Mat &dst, Mat &dst_mask)
{ {
for (int i = 0; i <= num_bands_; ++i) for (int i = 0; i <= num_bands_; ++i)
normalizeUsingWeightMap(dst_band_weights_[i], dst_pyr_laplace_[i]); normalizeUsingWeightMap(dst_band_weights_[i], dst_pyr_laplace_[i]);
@ -300,7 +298,7 @@ void MultiBandBlender::blend(Mat &dst, Mat &dst_mask)
////////////////////////////////////////////////////////////////////////////// //////////////////////////////////////////////////////////////////////////////
// Auxiliary functions // Auxiliary functions
void normalizeUsingWeightMap(const Mat& weight, Mat& src) void cv::normalizeUsingWeightMap(const Mat& weight, Mat& src)
{ {
CV_Assert(weight.type() == CV_32F); CV_Assert(weight.type() == CV_32F);
CV_Assert(src.type() == CV_16SC3); CV_Assert(src.type() == CV_16SC3);
@ -319,7 +317,7 @@ void normalizeUsingWeightMap(const Mat& weight, Mat& src)
} }
void createWeightMap(const Mat &mask, float sharpness, Mat &weight) void cv::createWeightMap(const Mat &mask, float sharpness, Mat &weight)
{ {
CV_Assert(mask.type() == CV_8U); CV_Assert(mask.type() == CV_8U);
distanceTransform(mask, weight, CV_DIST_L1, 3); distanceTransform(mask, weight, CV_DIST_L1, 3);
@ -327,7 +325,7 @@ void createWeightMap(const Mat &mask, float sharpness, Mat &weight)
} }
void createLaplacePyr(const Mat &img, int num_levels, vector<Mat> &pyr) void cv::createLaplacePyr(const Mat &img, int num_levels, vector<Mat> &pyr)
{ {
pyr.resize(num_levels + 1); pyr.resize(num_levels + 1);
pyr[0] = img; pyr[0] = img;
@ -342,7 +340,7 @@ void createLaplacePyr(const Mat &img, int num_levels, vector<Mat> &pyr)
} }
void createLaplacePyrGpu(const Mat &img, int num_levels, vector<Mat> &pyr) void cv::createLaplacePyrGpu(const Mat &img, int num_levels, vector<Mat> &pyr)
{ {
pyr.resize(num_levels + 1); pyr.resize(num_levels + 1);
@ -363,7 +361,7 @@ void createLaplacePyrGpu(const Mat &img, int num_levels, vector<Mat> &pyr)
} }
void restoreImageFromLaplacePyr(vector<Mat> &pyr) void cv::restoreImageFromLaplacePyr(vector<Mat> &pyr)
{ {
if (pyr.size() == 0) if (pyr.size() == 0)
return; return;
@ -374,5 +372,3 @@ void restoreImageFromLaplacePyr(vector<Mat> &pyr)
add(tmp, pyr[i - 1], pyr[i - 1]); add(tmp, pyr[i - 1], pyr[i - 1]);
} }
} }
} // namespace cv

View File

@ -1,20 +1,16 @@
#include "precomp.hpp" #include "precomp.hpp"
using namespace std; using namespace std;
using namespace cv;
namespace cv cv::CameraParams::CameraParams() : focal(1), R(Mat::eye(3, 3, CV_64F)), t(Mat::zeros(3, 1, CV_64F)) {}
{
CameraParams::CameraParams() : focal(1), R(Mat::eye(3, 3, CV_64F)), t(Mat::zeros(3, 1, CV_64F)) {} cv::CameraParams::CameraParams(const CameraParams &other) { *this = other; }
CameraParams::CameraParams(const CameraParams &other) { *this = other; } const cv::CameraParams& CameraParams::operator =(const CameraParams &other)
const CameraParams& CameraParams::operator =(const CameraParams &other)
{ {
focal = other.focal; focal = other.focal;
R = other.R.clone(); R = other.R.clone();
t = other.t.clone(); t = other.t.clone();
return *this; return *this;
} }
} // namespace cv

View File

@ -42,12 +42,10 @@
#include "precomp.hpp" #include "precomp.hpp"
using namespace std; using namespace std;
using namespace cv;
using namespace cv::gpu; using namespace cv::gpu;
namespace cv Ptr<ExposureCompensator> cv::ExposureCompensator::createDefault(int type)
{
Ptr<ExposureCompensator> ExposureCompensator::createDefault(int type)
{ {
if (type == NO) if (type == NO)
return new NoExposureCompensator(); return new NoExposureCompensator();
@ -60,8 +58,8 @@ Ptr<ExposureCompensator> ExposureCompensator::createDefault(int type)
} }
void ExposureCompensator::feed(const vector<Point> &corners, const vector<Mat> &images, void cv::ExposureCompensator::feed(const vector<Point> &corners, const vector<Mat> &images,
const vector<Mat> &masks) const vector<Mat> &masks)
{ {
vector<pair<Mat,uchar> > level_masks; vector<pair<Mat,uchar> > level_masks;
for (size_t i = 0; i < masks.size(); ++i) for (size_t i = 0; i < masks.size(); ++i)
@ -70,8 +68,8 @@ void ExposureCompensator::feed(const vector<Point> &corners, const vector<Mat> &
} }
void GainCompensator::feed(const vector<Point> &corners, const vector<Mat> &images, void cv::GainCompensator::feed(const vector<Point> &corners, const vector<Mat> &images,
const vector<pair<Mat,uchar> > &masks) const vector<pair<Mat,uchar> > &masks)
{ {
LOGLN("Exposure compensation..."); LOGLN("Exposure compensation...");
int64 t = getTickCount(); int64 t = getTickCount();
@ -145,13 +143,13 @@ void GainCompensator::feed(const vector<Point> &corners, const vector<Mat> &imag
} }
void GainCompensator::apply(int index, Point /*corner*/, Mat &image, const Mat &/*mask*/) void cv::GainCompensator::apply(int index, Point /*corner*/, Mat &image, const Mat &/*mask*/)
{ {
image *= gains_(index, 0); image *= gains_(index, 0);
} }
vector<double> GainCompensator::gains() const vector<double> cv::GainCompensator::gains() const
{ {
vector<double> gains_vec(gains_.rows); vector<double> gains_vec(gains_.rows);
for (int i = 0; i < gains_.rows; ++i) for (int i = 0; i < gains_.rows; ++i)
@ -160,8 +158,8 @@ vector<double> GainCompensator::gains() const
} }
void BlocksGainCompensator::feed(const vector<Point> &corners, const vector<Mat> &images, void cv::BlocksGainCompensator::feed(const vector<Point> &corners, const vector<Mat> &images,
const vector<pair<Mat,uchar> > &masks) const vector<pair<Mat,uchar> > &masks)
{ {
CV_Assert(corners.size() == images.size() && images.size() == masks.size()); CV_Assert(corners.size() == images.size() && images.size() == masks.size());
@ -220,7 +218,7 @@ void BlocksGainCompensator::feed(const vector<Point> &corners, const vector<Mat>
} }
void BlocksGainCompensator::apply(int index, Point /*corner*/, Mat &image, const Mat &/*mask*/) void cv::BlocksGainCompensator::apply(int index, Point /*corner*/, Mat &image, const Mat &/*mask*/)
{ {
CV_Assert(image.type() == CV_8UC3); CV_Assert(image.type() == CV_8UC3);
@ -242,5 +240,3 @@ void BlocksGainCompensator::apply(int index, Point /*corner*/, Mat &image, const
} }
} }
} }
} // namespace cv

View File

@ -42,25 +42,19 @@
#include "precomp.hpp" #include "precomp.hpp"
using namespace std; using namespace std;
using namespace cv;
using namespace cv::gpu; using namespace cv::gpu;
namespace cv void cv::FeaturesFinder::operator ()(const Mat &image, ImageFeatures &features)
{
void FeaturesFinder::operator ()(const Mat &image, ImageFeatures &features)
{ {
find(image, features); find(image, features);
features.img_size = image.size(); features.img_size = image.size();
//features.img = image.clone(); //features.img = image.clone();
} }
} // namespace cv
namespace namespace
{ {
using namespace cv;
class CpuSurfFeaturesFinder : public FeaturesFinder class CpuSurfFeaturesFinder : public FeaturesFinder
{ {
public: public:
@ -155,10 +149,7 @@ namespace
} // namespace } // namespace
namespace cv cv::SurfFeaturesFinder::SurfFeaturesFinder(bool try_use_gpu, double hess_thresh, int num_octaves, int num_layers,
{
SurfFeaturesFinder::SurfFeaturesFinder(bool try_use_gpu, double hess_thresh, int num_octaves, int num_layers,
int num_octaves_descr, int num_layers_descr) int num_octaves_descr, int num_layers_descr)
{ {
if (try_use_gpu && getCudaEnabledDeviceCount() > 0) if (try_use_gpu && getCudaEnabledDeviceCount() > 0)
@ -168,12 +159,12 @@ SurfFeaturesFinder::SurfFeaturesFinder(bool try_use_gpu, double hess_thresh, int
} }
void SurfFeaturesFinder::find(const Mat &image, ImageFeatures &features) void cv::SurfFeaturesFinder::find(const Mat &image, ImageFeatures &features)
{ {
(*impl_)(image, features); (*impl_)(image, features);
} }
void SurfFeaturesFinder::releaseMemory() void cv::SurfFeaturesFinder::releaseMemory()
{ {
impl_->releaseMemory(); impl_->releaseMemory();
} }
@ -181,11 +172,11 @@ void SurfFeaturesFinder::releaseMemory()
////////////////////////////////////////////////////////////////////////////// //////////////////////////////////////////////////////////////////////////////
MatchesInfo::MatchesInfo() : src_img_idx(-1), dst_img_idx(-1), num_inliers(0), confidence(0) {} cv::MatchesInfo::MatchesInfo() : src_img_idx(-1), dst_img_idx(-1), num_inliers(0), confidence(0) {}
MatchesInfo::MatchesInfo(const MatchesInfo &other) { *this = other; } cv::MatchesInfo::MatchesInfo(const MatchesInfo &other) { *this = other; }
const MatchesInfo& MatchesInfo::operator =(const MatchesInfo &other) const cv::MatchesInfo& MatchesInfo::operator =(const MatchesInfo &other)
{ {
src_img_idx = other.src_img_idx; src_img_idx = other.src_img_idx;
dst_img_idx = other.dst_img_idx; dst_img_idx = other.dst_img_idx;
@ -200,65 +191,69 @@ const MatchesInfo& MatchesInfo::operator =(const MatchesInfo &other)
////////////////////////////////////////////////////////////////////////////// //////////////////////////////////////////////////////////////////////////////
struct DistIdxPair namespace
{ {
bool operator<(const DistIdxPair &other) const { return dist < other.dist; }
double dist;
int idx;
};
struct DistIdxPair
struct MatchPairsBody
{
MatchPairsBody(const MatchPairsBody& other)
: matcher(other.matcher), features(other.features),
pairwise_matches(other.pairwise_matches), near_pairs(other.near_pairs) {}
MatchPairsBody(FeaturesMatcher &matcher, const vector<ImageFeatures> &features,
vector<MatchesInfo> &pairwise_matches, vector<pair<int,int> > &near_pairs)
: matcher(matcher), features(features),
pairwise_matches(pairwise_matches), near_pairs(near_pairs) {}
void operator ()(const BlockedRange &r) const
{ {
const int num_images = static_cast<int>(features.size()); bool operator<(const DistIdxPair &other) const { return dist < other.dist; }
for (int i = r.begin(); i < r.end(); ++i) double dist;
int idx;
};
struct MatchPairsBody
{
MatchPairsBody(const MatchPairsBody& other)
: matcher(other.matcher), features(other.features),
pairwise_matches(other.pairwise_matches), near_pairs(other.near_pairs) {}
MatchPairsBody(FeaturesMatcher &matcher, const vector<ImageFeatures> &features,
vector<MatchesInfo> &pairwise_matches, vector<pair<int,int> > &near_pairs)
: matcher(matcher), features(features),
pairwise_matches(pairwise_matches), near_pairs(near_pairs) {}
void operator ()(const BlockedRange &r) const
{ {
int from = near_pairs[i].first; const int num_images = static_cast<int>(features.size());
int to = near_pairs[i].second; for (int i = r.begin(); i < r.end(); ++i)
int pair_idx = from*num_images + to; {
int from = near_pairs[i].first;
int to = near_pairs[i].second;
int pair_idx = from*num_images + to;
matcher(features[from], features[to], pairwise_matches[pair_idx]); matcher(features[from], features[to], pairwise_matches[pair_idx]);
pairwise_matches[pair_idx].src_img_idx = from; pairwise_matches[pair_idx].src_img_idx = from;
pairwise_matches[pair_idx].dst_img_idx = to; pairwise_matches[pair_idx].dst_img_idx = to;
size_t dual_pair_idx = to*num_images + from; size_t dual_pair_idx = to*num_images + from;
pairwise_matches[dual_pair_idx] = pairwise_matches[pair_idx]; pairwise_matches[dual_pair_idx] = pairwise_matches[pair_idx];
pairwise_matches[dual_pair_idx].src_img_idx = to; pairwise_matches[dual_pair_idx].src_img_idx = to;
pairwise_matches[dual_pair_idx].dst_img_idx = from; pairwise_matches[dual_pair_idx].dst_img_idx = from;
if (!pairwise_matches[pair_idx].H.empty()) if (!pairwise_matches[pair_idx].H.empty())
pairwise_matches[dual_pair_idx].H = pairwise_matches[pair_idx].H.inv(); pairwise_matches[dual_pair_idx].H = pairwise_matches[pair_idx].H.inv();
for (size_t j = 0; j < pairwise_matches[dual_pair_idx].matches.size(); ++j) for (size_t j = 0; j < pairwise_matches[dual_pair_idx].matches.size(); ++j)
std::swap(pairwise_matches[dual_pair_idx].matches[j].queryIdx, std::swap(pairwise_matches[dual_pair_idx].matches[j].queryIdx,
pairwise_matches[dual_pair_idx].matches[j].trainIdx); pairwise_matches[dual_pair_idx].matches[j].trainIdx);
LOG("."); LOG(".");
}
} }
}
FeaturesMatcher &matcher; FeaturesMatcher &matcher;
const vector<ImageFeatures> &features; const vector<ImageFeatures> &features;
vector<MatchesInfo> &pairwise_matches; vector<MatchesInfo> &pairwise_matches;
vector<pair<int,int> > &near_pairs; vector<pair<int,int> > &near_pairs;
private: private:
void operator =(const MatchPairsBody&); void operator =(const MatchPairsBody&);
}; };
} // namespace
void FeaturesMatcher::operator ()(const vector<ImageFeatures> &features, vector<MatchesInfo> &pairwise_matches) void cv::FeaturesMatcher::operator ()(const vector<ImageFeatures> &features, vector<MatchesInfo> &pairwise_matches)
{ {
const int num_images = static_cast<int>(features.size()); const int num_images = static_cast<int>(features.size());
@ -408,10 +403,10 @@ namespace
vector< vector<DMatch> >().swap(pair_matches); vector< vector<DMatch> >().swap(pair_matches);
} }
} // anonymous namespace } // namespace
BestOf2NearestMatcher::BestOf2NearestMatcher(bool try_use_gpu, float match_conf, int num_matches_thresh1, int num_matches_thresh2) cv::BestOf2NearestMatcher::BestOf2NearestMatcher(bool try_use_gpu, float match_conf, int num_matches_thresh1, int num_matches_thresh2)
{ {
if (try_use_gpu && getCudaEnabledDeviceCount() > 0) if (try_use_gpu && getCudaEnabledDeviceCount() > 0)
impl_ = new GpuMatcher(match_conf); impl_ = new GpuMatcher(match_conf);
@ -424,7 +419,7 @@ BestOf2NearestMatcher::BestOf2NearestMatcher(bool try_use_gpu, float match_conf,
} }
void BestOf2NearestMatcher::match(const ImageFeatures &features1, const ImageFeatures &features2, void cv::BestOf2NearestMatcher::match(const ImageFeatures &features1, const ImageFeatures &features2,
MatchesInfo &matches_info) MatchesInfo &matches_info)
{ {
(*impl_)(features1, features2, matches_info); (*impl_)(features1, features2, matches_info);
@ -502,9 +497,7 @@ void BestOf2NearestMatcher::match(const ImageFeatures &features1, const ImageFea
matches_info.H = findHomography(src_points, dst_points, CV_RANSAC); matches_info.H = findHomography(src_points, dst_points, CV_RANSAC);
} }
void BestOf2NearestMatcher::releaseMemory() void cv::BestOf2NearestMatcher::releaseMemory()
{ {
impl_->releaseMemory(); impl_->releaseMemory();
} }
} // namespace cv

View File

@ -42,50 +42,53 @@
#include "precomp.hpp" #include "precomp.hpp"
using namespace std; using namespace std;
using namespace cv;
namespace cv namespace
{ {
struct IncDistance struct IncDistance
{
IncDistance(vector<int> &dists) : dists(&dists[0]) {}
void operator ()(const GraphEdge &edge) { dists[edge.to] = dists[edge.from] + 1; }
int* dists;
};
struct CalcRotation
{
CalcRotation(int num_images, const vector<MatchesInfo> &pairwise_matches, vector<CameraParams> &cameras)
: num_images(num_images), pairwise_matches(&pairwise_matches[0]), cameras(&cameras[0]) {}
void operator ()(const GraphEdge &edge)
{ {
int pair_idx = edge.from * num_images + edge.to; IncDistance(vector<int> &dists) : dists(&dists[0]) {}
void operator ()(const GraphEdge &edge) { dists[edge.to] = dists[edge.from] + 1; }
double f_from = cameras[edge.from].focal; int* dists;
double f_to = cameras[edge.to].focal; };
Mat K_from = Mat::eye(3, 3, CV_64F);
K_from.at<double>(0, 0) = f_from;
K_from.at<double>(1, 1) = f_from;
Mat K_to = Mat::eye(3, 3, CV_64F);
K_to.at<double>(0, 0) = f_to;
K_to.at<double>(1, 1) = f_to;
Mat R = K_from.inv() * pairwise_matches[pair_idx].H.inv() * K_to;
cameras[edge.to].R = cameras[edge.from].R * R;
}
int num_images;
const MatchesInfo* pairwise_matches;
CameraParams* cameras;
};
void HomographyBasedEstimator::estimate(const vector<ImageFeatures> &features, const vector<MatchesInfo> &pairwise_matches, struct CalcRotation
vector<CameraParams> &cameras) {
CalcRotation(int num_images, const vector<MatchesInfo> &pairwise_matches, vector<CameraParams> &cameras)
: num_images(num_images), pairwise_matches(&pairwise_matches[0]), cameras(&cameras[0]) {}
void operator ()(const GraphEdge &edge)
{
int pair_idx = edge.from * num_images + edge.to;
double f_from = cameras[edge.from].focal;
double f_to = cameras[edge.to].focal;
Mat K_from = Mat::eye(3, 3, CV_64F);
K_from.at<double>(0, 0) = f_from;
K_from.at<double>(1, 1) = f_from;
Mat K_to = Mat::eye(3, 3, CV_64F);
K_to.at<double>(0, 0) = f_to;
K_to.at<double>(1, 1) = f_to;
Mat R = K_from.inv() * pairwise_matches[pair_idx].H.inv() * K_to;
cameras[edge.to].R = cameras[edge.from].R * R;
}
int num_images;
const MatchesInfo* pairwise_matches;
CameraParams* cameras;
};
} // namespace
void cv::HomographyBasedEstimator::estimate(const vector<ImageFeatures> &features, const vector<MatchesInfo> &pairwise_matches,
vector<CameraParams> &cameras)
{ {
LOGLN("Estimating rotations..."); LOGLN("Estimating rotations...");
int64 t = getTickCount(); int64 t = getTickCount();
@ -132,8 +135,8 @@ void HomographyBasedEstimator::estimate(const vector<ImageFeatures> &features, c
////////////////////////////////////////////////////////////////////////////// //////////////////////////////////////////////////////////////////////////////
void BundleAdjuster::estimate(const vector<ImageFeatures> &features, const vector<MatchesInfo> &pairwise_matches, void cv::BundleAdjuster::estimate(const vector<ImageFeatures> &features, const vector<MatchesInfo> &pairwise_matches,
vector<CameraParams> &cameras) vector<CameraParams> &cameras)
{ {
if (cost_space_ == NO) if (cost_space_ == NO)
return; return;
@ -247,7 +250,7 @@ void BundleAdjuster::estimate(const vector<ImageFeatures> &features, const vecto
} }
void BundleAdjuster::calcError(Mat &err) void cv::BundleAdjuster::calcError(Mat &err)
{ {
err.create(total_num_matches_ * 3, 1, CV_64F); err.create(total_num_matches_ * 3, 1, CV_64F);
@ -311,14 +314,19 @@ void BundleAdjuster::calcError(Mat &err)
} }
void calcDeriv(const Mat &err1, const Mat &err2, double h, Mat res) namespace
{ {
for (int i = 0; i < err1.rows; ++i)
res.at<double>(i, 0) = (err2.at<double>(i, 0) - err1.at<double>(i, 0)) / h; void calcDeriv(const Mat &err1, const Mat &err2, double h, Mat res)
} {
for (int i = 0; i < err1.rows; ++i)
res.at<double>(i, 0) = (err2.at<double>(i, 0) - err1.at<double>(i, 0)) / h;
}
} // namespace
void BundleAdjuster::calcJacobian() void cv::BundleAdjuster::calcJacobian()
{ {
J_.create(total_num_matches_ * 3, num_images_ * 4, CV_64F); J_.create(total_num_matches_ * 3, num_images_ * 4, CV_64F);
@ -366,7 +374,7 @@ void BundleAdjuster::calcJacobian()
////////////////////////////////////////////////////////////////////////////// //////////////////////////////////////////////////////////////////////////////
// TODO replace SVD with eigen // TODO replace SVD with eigen
void waveCorrect(vector<Mat> &rmats) void cv::waveCorrect(vector<Mat> &rmats)
{ {
LOGLN("Wave correcting..."); LOGLN("Wave correcting...");
int64 t = getTickCount(); int64 t = getTickCount();
@ -407,8 +415,8 @@ void waveCorrect(vector<Mat> &rmats)
////////////////////////////////////////////////////////////////////////////// //////////////////////////////////////////////////////////////////////////////
string matchesGraphAsString(vector<string> &pathes, vector<MatchesInfo> &pairwise_matches, string cv::matchesGraphAsString(vector<string> &pathes, vector<MatchesInfo> &pairwise_matches,
float conf_threshold) float conf_threshold)
{ {
stringstream str; stringstream str;
str << "graph matches_graph{\n"; str << "graph matches_graph{\n";
@ -473,8 +481,8 @@ string matchesGraphAsString(vector<string> &pathes, vector<MatchesInfo> &pairwis
return str.str(); return str.str();
} }
vector<int> leaveBiggestComponent(vector<ImageFeatures> &features, vector<MatchesInfo> &pairwise_matches, vector<int> cv::leaveBiggestComponent(vector<ImageFeatures> &features, vector<MatchesInfo> &pairwise_matches,
float conf_threshold) float conf_threshold)
{ {
const int num_images = static_cast<int>(features.size()); const int num_images = static_cast<int>(features.size());
@ -531,8 +539,8 @@ vector<int> leaveBiggestComponent(vector<ImageFeatures> &features, vector<Match
} }
void findMaxSpanningTree(int num_images, const vector<MatchesInfo> &pairwise_matches, void cv::findMaxSpanningTree(int num_images, const vector<MatchesInfo> &pairwise_matches,
Graph &span_tree, vector<int> &centers) Graph &span_tree, vector<int> &centers)
{ {
Graph graph(num_images); Graph graph(num_images);
vector<GraphEdge> edges; vector<GraphEdge> edges;
@ -600,5 +608,3 @@ void findMaxSpanningTree(int num_images, const vector<MatchesInfo> &pairwise_mat
centers.push_back(i); centers.push_back(i);
CV_Assert(centers.size() > 0 && centers.size() <= 2); CV_Assert(centers.size() > 0 && centers.size() <= 2);
} }
} // namespace cv

View File

@ -42,11 +42,9 @@
#include "precomp.hpp" #include "precomp.hpp"
using namespace std; using namespace std;
using namespace cv;
namespace cv Ptr<SeamFinder> cv::SeamFinder::createDefault(int type)
{
Ptr<SeamFinder> SeamFinder::createDefault(int type)
{ {
if (type == NO) if (type == NO)
return new NoSeamFinder(); return new NoSeamFinder();
@ -61,8 +59,8 @@ Ptr<SeamFinder> SeamFinder::createDefault(int type)
} }
void PairwiseSeamFinder::find(const vector<Mat> &src, const vector<Point> &corners, void cv::PairwiseSeamFinder::find(const vector<Mat> &src, const vector<Point> &corners,
vector<Mat> &masks) vector<Mat> &masks)
{ {
LOGLN("Finding seams..."); LOGLN("Finding seams...");
int64 t = getTickCount(); int64 t = getTickCount();
@ -88,7 +86,7 @@ void PairwiseSeamFinder::find(const vector<Mat> &src, const vector<Point> &corne
} }
void VoronoiSeamFinder::findInPair(size_t first, size_t second, Rect roi) void cv::VoronoiSeamFinder::findInPair(size_t first, size_t second, Rect roi)
{ {
const int gap = 10; const int gap = 10;
Mat submask1(roi.height + 2 * gap, roi.width + 2 * gap, CV_8U); Mat submask1(roi.height + 2 * gap, roi.width + 2 * gap, CV_8U);
@ -142,7 +140,7 @@ void VoronoiSeamFinder::findInPair(size_t first, size_t second, Rect roi)
} }
class GraphCutSeamFinder::Impl : public PairwiseSeamFinder class cv::GraphCutSeamFinder::Impl : public PairwiseSeamFinder
{ {
public: public:
Impl(int cost_type, float terminal_cost, float bad_region_penalty) Impl(int cost_type, float terminal_cost, float bad_region_penalty)
@ -165,8 +163,8 @@ private:
}; };
void GraphCutSeamFinder::Impl::find(const vector<Mat> &src, const vector<Point> &corners, void cv::GraphCutSeamFinder::Impl::find(const vector<Mat> &src, const vector<Point> &corners,
vector<Mat> &masks) vector<Mat> &masks)
{ {
// Compute gradients // Compute gradients
dx_.resize(src.size()); dx_.resize(src.size());
@ -196,8 +194,8 @@ void GraphCutSeamFinder::Impl::find(const vector<Mat> &src, const vector<Point>
} }
void GraphCutSeamFinder::Impl::setGraphWeightsColor(const Mat &img1, const Mat &img2, void cv::GraphCutSeamFinder::Impl::setGraphWeightsColor(const Mat &img1, const Mat &img2,
const Mat &mask1, const Mat &mask2, GCGraph<float> &graph) const Mat &mask1, const Mat &mask2, GCGraph<float> &graph)
{ {
const Size img_size = img1.size(); const Size img_size = img1.size();
@ -244,7 +242,7 @@ void GraphCutSeamFinder::Impl::setGraphWeightsColor(const Mat &img1, const Mat &
} }
void GraphCutSeamFinder::Impl::setGraphWeightsColorGrad( void cv::GraphCutSeamFinder::Impl::setGraphWeightsColorGrad(
const Mat &img1, const Mat &img2, const Mat &dx1, const Mat &dx2, const Mat &img1, const Mat &img2, const Mat &dx1, const Mat &dx2,
const Mat &dy1, const Mat &dy2, const Mat &mask1, const Mat &mask2, const Mat &dy1, const Mat &dy2, const Mat &mask1, const Mat &mask2,
GCGraph<float> &graph) GCGraph<float> &graph)
@ -298,7 +296,7 @@ void GraphCutSeamFinder::Impl::setGraphWeightsColorGrad(
} }
void GraphCutSeamFinder::Impl::findInPair(size_t first, size_t second, Rect roi) void cv::GraphCutSeamFinder::Impl::findInPair(size_t first, size_t second, Rect roi)
{ {
Mat img1 = images_[first], img2 = images_[second]; Mat img1 = images_[first], img2 = images_[second];
Mat dx1 = dx_[first], dx2 = dx_[second]; Mat dx1 = dx_[first], dx2 = dx_[second];
@ -396,14 +394,12 @@ void GraphCutSeamFinder::Impl::findInPair(size_t first, size_t second, Rect roi)
} }
GraphCutSeamFinder::GraphCutSeamFinder(int cost_type, float terminal_cost, float bad_region_penalty) cv::GraphCutSeamFinder::GraphCutSeamFinder(int cost_type, float terminal_cost, float bad_region_penalty)
: impl_(new Impl(cost_type, terminal_cost, bad_region_penalty)) {} : impl_(new Impl(cost_type, terminal_cost, bad_region_penalty)) {}
void GraphCutSeamFinder::find(const vector<Mat> &src, const vector<Point> &corners, void cv::GraphCutSeamFinder::find(const vector<Mat> &src, const vector<Point> &corners,
vector<Mat> &masks) vector<Mat> &masks)
{ {
impl_->find(src, corners, masks); impl_->find(src, corners, masks);
} }
} // namespace cv

View File

@ -42,11 +42,9 @@
#include "precomp.hpp" #include "precomp.hpp"
using namespace std; using namespace std;
using namespace cv;
namespace cv void cv::DisjointSets::createOneElemSets(int n)
{
void DisjointSets::createOneElemSets(int n)
{ {
rank_.assign(n, 0); rank_.assign(n, 0);
size.assign(n, 1); size.assign(n, 1);
@ -56,7 +54,7 @@ void DisjointSets::createOneElemSets(int n)
} }
int DisjointSets::findSetByElem(int elem) int cv::DisjointSets::findSetByElem(int elem)
{ {
int set = elem; int set = elem;
while (set != parent[set]) while (set != parent[set])
@ -72,7 +70,7 @@ int DisjointSets::findSetByElem(int elem)
} }
int DisjointSets::mergeSets(int set1, int set2) int cv::DisjointSets::mergeSets(int set1, int set2)
{ {
if (rank_[set1] < rank_[set2]) if (rank_[set1] < rank_[set2])
{ {
@ -93,13 +91,13 @@ int DisjointSets::mergeSets(int set1, int set2)
} }
void Graph::addEdge(int from, int to, float weight) void cv::Graph::addEdge(int from, int to, float weight)
{ {
edges_[from].push_back(GraphEdge(from, to, weight)); edges_[from].push_back(GraphEdge(from, to, weight));
} }
bool overlapRoi(Point tl1, Point tl2, Size sz1, Size sz2, Rect &roi) bool cv::overlapRoi(Point tl1, Point tl2, Size sz1, Size sz2, Rect &roi)
{ {
int x_tl = max(tl1.x, tl2.x); int x_tl = max(tl1.x, tl2.x);
int y_tl = max(tl1.y, tl2.y); int y_tl = max(tl1.y, tl2.y);
@ -114,7 +112,7 @@ bool overlapRoi(Point tl1, Point tl2, Size sz1, Size sz2, Rect &roi)
} }
Rect resultRoi(const vector<Point> &corners, const vector<Mat> &images) Rect cv::resultRoi(const vector<Point> &corners, const vector<Mat> &images)
{ {
vector<Size> sizes(images.size()); vector<Size> sizes(images.size());
for (size_t i = 0; i < images.size(); ++i) for (size_t i = 0; i < images.size(); ++i)
@ -123,7 +121,7 @@ Rect resultRoi(const vector<Point> &corners, const vector<Mat> &images)
} }
Rect resultRoi(const vector<Point> &corners, const vector<Size> &sizes) Rect cv::resultRoi(const vector<Point> &corners, const vector<Size> &sizes)
{ {
CV_Assert(sizes.size() == corners.size()); CV_Assert(sizes.size() == corners.size());
Point tl(numeric_limits<int>::max(), numeric_limits<int>::max()); Point tl(numeric_limits<int>::max(), numeric_limits<int>::max());
@ -139,7 +137,7 @@ Rect resultRoi(const vector<Point> &corners, const vector<Size> &sizes)
} }
Point resultTl(const vector<Point> &corners) Point cv::resultTl(const vector<Point> &corners)
{ {
Point tl(numeric_limits<int>::max(), numeric_limits<int>::max()); Point tl(numeric_limits<int>::max(), numeric_limits<int>::max());
for (size_t i = 0; i < corners.size(); ++i) for (size_t i = 0; i < corners.size(); ++i)
@ -151,7 +149,7 @@ Point resultTl(const vector<Point> &corners)
} }
void selectRandomSubset(int count, int size, vector<int> &subset) void cv::selectRandomSubset(int count, int size, vector<int> &subset)
{ {
subset.clear(); subset.clear();
for (int i = 0; i < size; ++i) for (int i = 0; i < size; ++i)
@ -163,5 +161,3 @@ void selectRandomSubset(int count, int size, vector<int> &subset)
} }
} }
} }
} // namespace cv

View File

@ -42,11 +42,9 @@
#include "precomp.hpp" #include "precomp.hpp"
using namespace std; using namespace std;
using namespace cv;
namespace cv Ptr<Warper> cv::Warper::createByCameraFocal(float focal, int type, bool try_gpu)
{
Ptr<Warper> Warper::createByCameraFocal(float focal, int type, bool try_gpu)
{ {
bool can_use_gpu = try_gpu && gpu::getCudaEnabledDeviceCount(); bool can_use_gpu = try_gpu && gpu::getCudaEnabledDeviceCount();
if (type == PLANE) if (type == PLANE)
@ -60,7 +58,7 @@ Ptr<Warper> Warper::createByCameraFocal(float focal, int type, bool try_gpu)
} }
void ProjectorBase::setTransformation(const Mat &R) void cv::ProjectorBase::setTransformation(const Mat &R)
{ {
CV_Assert(R.size() == Size(3, 3)); CV_Assert(R.size() == Size(3, 3));
CV_Assert(R.type() == CV_32F); CV_Assert(R.type() == CV_32F);
@ -75,7 +73,7 @@ void ProjectorBase::setTransformation(const Mat &R)
} }
void PlaneWarper::detectResultRoi(Point &dst_tl, Point &dst_br) void cv::PlaneWarper::detectResultRoi(Point &dst_tl, Point &dst_br)
{ {
float tl_uf = numeric_limits<float>::max(); float tl_uf = numeric_limits<float>::max();
float tl_vf = numeric_limits<float>::max(); float tl_vf = numeric_limits<float>::max();
@ -107,7 +105,7 @@ void PlaneWarper::detectResultRoi(Point &dst_tl, Point &dst_br)
} }
Point PlaneWarperGpu::warp(const Mat &src, float focal, const cv::Mat &R, cv::Mat &dst, int interp_mode, int border_mode) Point cv::PlaneWarperGpu::warp(const Mat &src, float focal, const cv::Mat &R, cv::Mat &dst, int interp_mode, int border_mode)
{ {
src_size_ = src.size(); src_size_ = src.size();
projector_.size = src.size(); projector_.size = src.size();
@ -133,7 +131,7 @@ Point PlaneWarperGpu::warp(const Mat &src, float focal, const cv::Mat &R, cv::Ma
} }
void SphericalWarper::detectResultRoi(Point &dst_tl, Point &dst_br) void cv::SphericalWarper::detectResultRoi(Point &dst_tl, Point &dst_br)
{ {
detectResultRoiByBorder(dst_tl, dst_br); detectResultRoiByBorder(dst_tl, dst_br);
@ -177,8 +175,8 @@ void SphericalWarper::detectResultRoi(Point &dst_tl, Point &dst_br)
} }
Point SphericalWarperGpu::warp(const Mat &src, float focal, const Mat &R, Mat &dst, Point cv::SphericalWarperGpu::warp(const Mat &src, float focal, const Mat &R, Mat &dst,
int interp_mode, int border_mode) int interp_mode, int border_mode)
{ {
src_size_ = src.size(); src_size_ = src.size();
projector_.size = src.size(); projector_.size = src.size();
@ -204,8 +202,8 @@ Point SphericalWarperGpu::warp(const Mat &src, float focal, const Mat &R, Mat &d
} }
Point CylindricalWarperGpu::warp(const Mat &src, float focal, const Mat &R, Mat &dst, Point cv::CylindricalWarperGpu::warp(const Mat &src, float focal, const Mat &R, Mat &dst,
int interp_mode, int border_mode) int interp_mode, int border_mode)
{ {
src_size_ = src.size(); src_size_ = src.size();
projector_.size = src.size(); projector_.size = src.size();
@ -230,4 +228,3 @@ Point CylindricalWarperGpu::warp(const Mat &src, float focal, const Mat &R, Mat
return dst_tl; return dst_tl;
} }
} // namespace cv