From fbe2e6fb01153bfb8ada4399d607f6a559c578eb Mon Sep 17 00:00:00 2001 From: Alexey Spizhevoy Date: Mon, 5 Sep 2011 11:52:30 +0000 Subject: [PATCH] refactored stitching module --- modules/stitching/src/autocalib.cpp | 40 +++--- modules/stitching/src/blenders.cpp | 40 +++--- modules/stitching/src/camera.cpp | 12 +- modules/stitching/src/exposure_compensate.cpp | 26 ++-- modules/stitching/src/matchers.cpp | 125 +++++++++--------- modules/stitching/src/motion_estimators.cpp | 102 +++++++------- modules/stitching/src/seam_finders.cpp | 32 ++--- modules/stitching/src/util.cpp | 24 ++-- modules/stitching/src/warpers.cpp | 23 ++-- 9 files changed, 198 insertions(+), 226 deletions(-) diff --git a/modules/stitching/src/autocalib.cpp b/modules/stitching/src/autocalib.cpp index 109eb01be9..806fe3d315 100644 --- a/modules/stitching/src/autocalib.cpp +++ b/modules/stitching/src/autocalib.cpp @@ -42,11 +42,24 @@ #include "precomp.hpp" using namespace std; +using namespace cv; -namespace cv +namespace { + template 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)); @@ -77,8 +90,8 @@ void focalsFromHomography(const Mat& H, double &f0, double &f1, bool &f0_ok, boo } -void estimateFocal(const vector &features, const vector &pairwise_matches, - vector &focals) +void cv::estimateFocal(const vector &features, const vector &pairwise_matches, + vector &focals) { const int num_images = static_cast(features.size()); focals.resize(num_images); @@ -118,22 +131,7 @@ void estimateFocal(const vector &features, const vector 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 &Hs, Mat &K) +bool cv::calibrateRotatingCamera(const vector &Hs, Mat &K) { int m = static_cast(Hs.size()); CV_Assert(m >= 1); @@ -183,5 +181,3 @@ bool calibrateRotatingCamera(const vector &Hs, Mat &K) K = W.t(); return true; } - -} // namespace cv diff --git a/modules/stitching/src/blenders.cpp b/modules/stitching/src/blenders.cpp index ea1d2039fb..9806d9bbf6 100644 --- a/modules/stitching/src/blenders.cpp +++ b/modules/stitching/src/blenders.cpp @@ -42,13 +42,11 @@ #include "precomp.hpp" using namespace std; - -namespace cv -{ +using namespace cv; static const float WEIGHT_EPS = 1e-5f; -Ptr Blender::createDefault(int type, bool try_gpu) +Ptr cv::Blender::createDefault(int type, bool try_gpu) { if (type == NO) return new Blender(); @@ -61,13 +59,13 @@ Ptr Blender::createDefault(int type, bool try_gpu) } -void Blender::prepare(const vector &corners, const vector &sizes) +void cv::Blender::prepare(const vector &corners, const vector &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_.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(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 = 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); 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(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_); 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); 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; @@ -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(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) normalizeUsingWeightMap(dst_band_weights_[i], dst_pyr_laplace_[i]); @@ -300,7 +298,7 @@ void MultiBandBlender::blend(Mat &dst, Mat &dst_mask) ////////////////////////////////////////////////////////////////////////////// // 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(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); 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 &pyr) +void cv::createLaplacePyr(const Mat &img, int num_levels, vector &pyr) { pyr.resize(num_levels + 1); pyr[0] = img; @@ -342,7 +340,7 @@ void createLaplacePyr(const Mat &img, int num_levels, vector &pyr) } -void createLaplacePyrGpu(const Mat &img, int num_levels, vector &pyr) +void cv::createLaplacePyrGpu(const Mat &img, int num_levels, vector &pyr) { pyr.resize(num_levels + 1); @@ -363,7 +361,7 @@ void createLaplacePyrGpu(const Mat &img, int num_levels, vector &pyr) } -void restoreImageFromLaplacePyr(vector &pyr) +void cv::restoreImageFromLaplacePyr(vector &pyr) { if (pyr.size() == 0) return; @@ -374,5 +372,3 @@ void restoreImageFromLaplacePyr(vector &pyr) add(tmp, pyr[i - 1], pyr[i - 1]); } } - -} // namespace cv diff --git a/modules/stitching/src/camera.cpp b/modules/stitching/src/camera.cpp index 4c7cea6b57..18ef6d2349 100644 --- a/modules/stitching/src/camera.cpp +++ b/modules/stitching/src/camera.cpp @@ -1,20 +1,16 @@ #include "precomp.hpp" using namespace std; +using namespace cv; -namespace cv -{ - -CameraParams::CameraParams() : focal(1), R(Mat::eye(3, 3, CV_64F)), t(Mat::zeros(3, 1, CV_64F)) {} +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; } +cv::CameraParams::CameraParams(const CameraParams &other) { *this = other; } -const CameraParams& CameraParams::operator =(const CameraParams &other) +const cv::CameraParams& CameraParams::operator =(const CameraParams &other) { focal = other.focal; R = other.R.clone(); t = other.t.clone(); return *this; } - -} // namespace cv diff --git a/modules/stitching/src/exposure_compensate.cpp b/modules/stitching/src/exposure_compensate.cpp index 017b0df116..1eefe525bf 100644 --- a/modules/stitching/src/exposure_compensate.cpp +++ b/modules/stitching/src/exposure_compensate.cpp @@ -42,12 +42,10 @@ #include "precomp.hpp" using namespace std; +using namespace cv; using namespace cv::gpu; -namespace cv -{ - -Ptr ExposureCompensator::createDefault(int type) +Ptr cv::ExposureCompensator::createDefault(int type) { if (type == NO) return new NoExposureCompensator(); @@ -60,8 +58,8 @@ Ptr ExposureCompensator::createDefault(int type) } -void ExposureCompensator::feed(const vector &corners, const vector &images, - const vector &masks) +void cv::ExposureCompensator::feed(const vector &corners, const vector &images, + const vector &masks) { vector > level_masks; for (size_t i = 0; i < masks.size(); ++i) @@ -70,8 +68,8 @@ void ExposureCompensator::feed(const vector &corners, const vector & } -void GainCompensator::feed(const vector &corners, const vector &images, - const vector > &masks) +void cv::GainCompensator::feed(const vector &corners, const vector &images, + const vector > &masks) { LOGLN("Exposure compensation..."); int64 t = getTickCount(); @@ -145,13 +143,13 @@ void GainCompensator::feed(const vector &corners, const vector &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); } -vector GainCompensator::gains() const +vector cv::GainCompensator::gains() const { vector gains_vec(gains_.rows); for (int i = 0; i < gains_.rows; ++i) @@ -160,8 +158,8 @@ vector GainCompensator::gains() const } -void BlocksGainCompensator::feed(const vector &corners, const vector &images, - const vector > &masks) +void cv::BlocksGainCompensator::feed(const vector &corners, const vector &images, + const vector > &masks) { CV_Assert(corners.size() == images.size() && images.size() == masks.size()); @@ -220,7 +218,7 @@ void BlocksGainCompensator::feed(const vector &corners, const vector } -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); @@ -242,5 +240,3 @@ void BlocksGainCompensator::apply(int index, Point /*corner*/, Mat &image, const } } } - -} // namespace cv diff --git a/modules/stitching/src/matchers.cpp b/modules/stitching/src/matchers.cpp index d1ed0168a1..0b202b9718 100644 --- a/modules/stitching/src/matchers.cpp +++ b/modules/stitching/src/matchers.cpp @@ -42,25 +42,19 @@ #include "precomp.hpp" using namespace std; +using namespace cv; using namespace cv::gpu; -namespace cv -{ - -void FeaturesFinder::operator ()(const Mat &image, ImageFeatures &features) +void cv::FeaturesFinder::operator ()(const Mat &image, ImageFeatures &features) { find(image, features); features.img_size = image.size(); //features.img = image.clone(); } -} // namespace cv - namespace { - using namespace cv; - class CpuSurfFeaturesFinder : public FeaturesFinder { public: @@ -155,10 +149,7 @@ namespace } // namespace -namespace cv -{ - -SurfFeaturesFinder::SurfFeaturesFinder(bool try_use_gpu, double hess_thresh, int num_octaves, int num_layers, +cv::SurfFeaturesFinder::SurfFeaturesFinder(bool try_use_gpu, double hess_thresh, int num_octaves, int num_layers, int num_octaves_descr, int num_layers_descr) { 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); } -void SurfFeaturesFinder::releaseMemory() +void cv::SurfFeaturesFinder::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; 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 MatchPairsBody -{ - MatchPairsBody(const MatchPairsBody& other) - : matcher(other.matcher), features(other.features), - pairwise_matches(other.pairwise_matches), near_pairs(other.near_pairs) {} + struct DistIdxPair + { + bool operator<(const DistIdxPair &other) const { return dist < other.dist; } + double dist; + int idx; + }; - MatchPairsBody(FeaturesMatcher &matcher, const vector &features, - vector &pairwise_matches, vector > &near_pairs) - : matcher(matcher), features(features), - pairwise_matches(pairwise_matches), near_pairs(near_pairs) {} - void operator ()(const BlockedRange &r) const + struct MatchPairsBody { - const int num_images = static_cast(features.size()); - for (int i = r.begin(); i < r.end(); ++i) + 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 &features, + vector &pairwise_matches, vector > &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; - int to = near_pairs[i].second; - int pair_idx = from*num_images + to; + const int num_images = static_cast(features.size()); + for (int i = r.begin(); i < r.end(); ++i) + { + 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]); - pairwise_matches[pair_idx].src_img_idx = from; - pairwise_matches[pair_idx].dst_img_idx = to; + matcher(features[from], features[to], pairwise_matches[pair_idx]); + pairwise_matches[pair_idx].src_img_idx = from; + 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].src_img_idx = to; - pairwise_matches[dual_pair_idx].dst_img_idx = from; + pairwise_matches[dual_pair_idx] = pairwise_matches[pair_idx]; + pairwise_matches[dual_pair_idx].src_img_idx = to; + pairwise_matches[dual_pair_idx].dst_img_idx = from; - if (!pairwise_matches[pair_idx].H.empty()) - pairwise_matches[dual_pair_idx].H = pairwise_matches[pair_idx].H.inv(); + if (!pairwise_matches[pair_idx].H.empty()) + 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) - std::swap(pairwise_matches[dual_pair_idx].matches[j].queryIdx, - pairwise_matches[dual_pair_idx].matches[j].trainIdx); - LOG("."); + for (size_t j = 0; j < pairwise_matches[dual_pair_idx].matches.size(); ++j) + std::swap(pairwise_matches[dual_pair_idx].matches[j].queryIdx, + pairwise_matches[dual_pair_idx].matches[j].trainIdx); + LOG("."); + } } - } - FeaturesMatcher &matcher; - const vector &features; - vector &pairwise_matches; - vector > &near_pairs; + FeaturesMatcher &matcher; + const vector &features; + vector &pairwise_matches; + vector > &near_pairs; -private: - void operator =(const MatchPairsBody&); -}; + private: + void operator =(const MatchPairsBody&); + }; +} // namespace -void FeaturesMatcher::operator ()(const vector &features, vector &pairwise_matches) +void cv::FeaturesMatcher::operator ()(const vector &features, vector &pairwise_matches) { const int num_images = static_cast(features.size()); @@ -408,10 +403,10 @@ namespace vector< vector >().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) 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) { (*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); } -void BestOf2NearestMatcher::releaseMemory() +void cv::BestOf2NearestMatcher::releaseMemory() { impl_->releaseMemory(); } - -} // namespace cv diff --git a/modules/stitching/src/motion_estimators.cpp b/modules/stitching/src/motion_estimators.cpp index eb891288e6..5b29dd8548 100644 --- a/modules/stitching/src/motion_estimators.cpp +++ b/modules/stitching/src/motion_estimators.cpp @@ -42,50 +42,53 @@ #include "precomp.hpp" using namespace std; +using namespace cv; -namespace cv +namespace { -struct IncDistance -{ - IncDistance(vector &dists) : dists(&dists[0]) {} - void operator ()(const GraphEdge &edge) { dists[edge.to] = dists[edge.from] + 1; } - int* dists; -}; - + struct IncDistance + { + IncDistance(vector &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 &pairwise_matches, vector &cameras) - : num_images(num_images), pairwise_matches(&pairwise_matches[0]), cameras(&cameras[0]) {} - void operator ()(const GraphEdge &edge) + struct CalcRotation { - int pair_idx = edge.from * num_images + edge.to; + CalcRotation(int num_images, const vector &pairwise_matches, vector &cameras) + : num_images(num_images), pairwise_matches(&pairwise_matches[0]), cameras(&cameras[0]) {} - double f_from = cameras[edge.from].focal; - double f_to = cameras[edge.to].focal; + void operator ()(const GraphEdge &edge) + { + int pair_idx = edge.from * num_images + edge.to; - Mat K_from = Mat::eye(3, 3, CV_64F); - K_from.at(0, 0) = f_from; - K_from.at(1, 1) = f_from; + double f_from = cameras[edge.from].focal; + double f_to = cameras[edge.to].focal; - Mat K_to = Mat::eye(3, 3, CV_64F); - K_to.at(0, 0) = f_to; - K_to.at(1, 1) = f_to; + Mat K_from = Mat::eye(3, 3, CV_64F); + K_from.at(0, 0) = f_from; + K_from.at(1, 1) = f_from; - Mat R = K_from.inv() * pairwise_matches[pair_idx].H.inv() * K_to; - cameras[edge.to].R = cameras[edge.from].R * R; - } + Mat K_to = Mat::eye(3, 3, CV_64F); + K_to.at(0, 0) = f_to; + K_to.at(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; -}; + int num_images; + const MatchesInfo* pairwise_matches; + CameraParams* cameras; + }; +} // namespace -void HomographyBasedEstimator::estimate(const vector &features, const vector &pairwise_matches, - vector &cameras) + +void cv::HomographyBasedEstimator::estimate(const vector &features, const vector &pairwise_matches, + vector &cameras) { LOGLN("Estimating rotations..."); int64 t = getTickCount(); @@ -132,8 +135,8 @@ void HomographyBasedEstimator::estimate(const vector &features, c ////////////////////////////////////////////////////////////////////////////// -void BundleAdjuster::estimate(const vector &features, const vector &pairwise_matches, - vector &cameras) +void cv::BundleAdjuster::estimate(const vector &features, const vector &pairwise_matches, + vector &cameras) { if (cost_space_ == NO) return; @@ -247,7 +250,7 @@ void BundleAdjuster::estimate(const vector &features, const vecto } -void BundleAdjuster::calcError(Mat &err) +void cv::BundleAdjuster::calcError(Mat &err) { 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(i, 0) = (err2.at(i, 0) - err1.at(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(i, 0) = (err2.at(i, 0) - err1.at(i, 0)) / h; + } -void BundleAdjuster::calcJacobian() +} // namespace + + +void cv::BundleAdjuster::calcJacobian() { J_.create(total_num_matches_ * 3, num_images_ * 4, CV_64F); @@ -366,7 +374,7 @@ void BundleAdjuster::calcJacobian() ////////////////////////////////////////////////////////////////////////////// // TODO replace SVD with eigen -void waveCorrect(vector &rmats) +void cv::waveCorrect(vector &rmats) { LOGLN("Wave correcting..."); int64 t = getTickCount(); @@ -407,8 +415,8 @@ void waveCorrect(vector &rmats) ////////////////////////////////////////////////////////////////////////////// -string matchesGraphAsString(vector &pathes, vector &pairwise_matches, - float conf_threshold) +string cv::matchesGraphAsString(vector &pathes, vector &pairwise_matches, + float conf_threshold) { stringstream str; str << "graph matches_graph{\n"; @@ -473,8 +481,8 @@ string matchesGraphAsString(vector &pathes, vector &pairwis return str.str(); } -vector leaveBiggestComponent(vector &features, vector &pairwise_matches, - float conf_threshold) +vector cv::leaveBiggestComponent(vector &features, vector &pairwise_matches, + float conf_threshold) { const int num_images = static_cast(features.size()); @@ -531,8 +539,8 @@ vector leaveBiggestComponent(vector &features, vector &pairwise_matches, - Graph &span_tree, vector ¢ers) +void cv::findMaxSpanningTree(int num_images, const vector &pairwise_matches, + Graph &span_tree, vector ¢ers) { Graph graph(num_images); vector edges; @@ -600,5 +608,3 @@ void findMaxSpanningTree(int num_images, const vector &pairwise_mat centers.push_back(i); CV_Assert(centers.size() > 0 && centers.size() <= 2); } - -} // namespace cv diff --git a/modules/stitching/src/seam_finders.cpp b/modules/stitching/src/seam_finders.cpp index 7535ff030b..68a777c69a 100644 --- a/modules/stitching/src/seam_finders.cpp +++ b/modules/stitching/src/seam_finders.cpp @@ -42,11 +42,9 @@ #include "precomp.hpp" using namespace std; +using namespace cv; -namespace cv -{ - -Ptr SeamFinder::createDefault(int type) +Ptr cv::SeamFinder::createDefault(int type) { if (type == NO) return new NoSeamFinder(); @@ -61,8 +59,8 @@ Ptr SeamFinder::createDefault(int type) } -void PairwiseSeamFinder::find(const vector &src, const vector &corners, - vector &masks) +void cv::PairwiseSeamFinder::find(const vector &src, const vector &corners, + vector &masks) { LOGLN("Finding seams..."); int64 t = getTickCount(); @@ -88,7 +86,7 @@ void PairwiseSeamFinder::find(const vector &src, const vector &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; 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: Impl(int cost_type, float terminal_cost, float bad_region_penalty) @@ -165,8 +163,8 @@ private: }; -void GraphCutSeamFinder::Impl::find(const vector &src, const vector &corners, - vector &masks) +void cv::GraphCutSeamFinder::Impl::find(const vector &src, const vector &corners, + vector &masks) { // Compute gradients dx_.resize(src.size()); @@ -196,8 +194,8 @@ void GraphCutSeamFinder::Impl::find(const vector &src, const vector } -void GraphCutSeamFinder::Impl::setGraphWeightsColor(const Mat &img1, const Mat &img2, - const Mat &mask1, const Mat &mask2, GCGraph &graph) +void cv::GraphCutSeamFinder::Impl::setGraphWeightsColor(const Mat &img1, const Mat &img2, + const Mat &mask1, const Mat &mask2, GCGraph &graph) { 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 &dy1, const Mat &dy2, const Mat &mask1, const Mat &mask2, GCGraph &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 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)) {} -void GraphCutSeamFinder::find(const vector &src, const vector &corners, +void cv::GraphCutSeamFinder::find(const vector &src, const vector &corners, vector &masks) { impl_->find(src, corners, masks); } - -} // namespace cv diff --git a/modules/stitching/src/util.cpp b/modules/stitching/src/util.cpp index 73ba838eaf..7311b0bf10 100644 --- a/modules/stitching/src/util.cpp +++ b/modules/stitching/src/util.cpp @@ -42,11 +42,9 @@ #include "precomp.hpp" using namespace std; +using namespace cv; -namespace cv -{ - -void DisjointSets::createOneElemSets(int n) +void cv::DisjointSets::createOneElemSets(int n) { rank_.assign(n, 0); 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; 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]) { @@ -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)); } -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 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 &corners, const vector &images) +Rect cv::resultRoi(const vector &corners, const vector &images) { vector sizes(images.size()); for (size_t i = 0; i < images.size(); ++i) @@ -123,7 +121,7 @@ Rect resultRoi(const vector &corners, const vector &images) } -Rect resultRoi(const vector &corners, const vector &sizes) +Rect cv::resultRoi(const vector &corners, const vector &sizes) { CV_Assert(sizes.size() == corners.size()); Point tl(numeric_limits::max(), numeric_limits::max()); @@ -139,7 +137,7 @@ Rect resultRoi(const vector &corners, const vector &sizes) } -Point resultTl(const vector &corners) +Point cv::resultTl(const vector &corners) { Point tl(numeric_limits::max(), numeric_limits::max()); for (size_t i = 0; i < corners.size(); ++i) @@ -151,7 +149,7 @@ Point resultTl(const vector &corners) } -void selectRandomSubset(int count, int size, vector &subset) +void cv::selectRandomSubset(int count, int size, vector &subset) { subset.clear(); for (int i = 0; i < size; ++i) @@ -163,5 +161,3 @@ void selectRandomSubset(int count, int size, vector &subset) } } } - -} // namespace cv diff --git a/modules/stitching/src/warpers.cpp b/modules/stitching/src/warpers.cpp index 638f66d386..096506cf43 100644 --- a/modules/stitching/src/warpers.cpp +++ b/modules/stitching/src/warpers.cpp @@ -42,11 +42,9 @@ #include "precomp.hpp" using namespace std; +using namespace cv; -namespace cv -{ - -Ptr Warper::createByCameraFocal(float focal, int type, bool try_gpu) +Ptr cv::Warper::createByCameraFocal(float focal, int type, bool try_gpu) { bool can_use_gpu = try_gpu && gpu::getCudaEnabledDeviceCount(); if (type == PLANE) @@ -60,7 +58,7 @@ Ptr 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.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::max(); float tl_vf = numeric_limits::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(); 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); @@ -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, - int interp_mode, int border_mode) +Point cv::SphericalWarperGpu::warp(const Mat &src, float focal, const Mat &R, Mat &dst, + int interp_mode, int border_mode) { src_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, - int interp_mode, int border_mode) +Point cv::CylindricalWarperGpu::warp(const Mat &src, float focal, const Mat &R, Mat &dst, + int interp_mode, int border_mode) { src_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; } -} // namespace cv