From a7fbcad283e8e072e5050a31be478e999fd9f0c7 Mon Sep 17 00:00:00 2001 From: Alexey Spizhevoy Date: Fri, 7 Oct 2011 08:17:55 +0000 Subject: [PATCH] Added warp method into the RotationWarper interface, added find() into VoronoiSeamFinder which uses only source image sizes --- .../opencv2/stitching/detail/seam_finders.hpp | 5 +++ .../opencv2/stitching/detail/warpers.hpp | 7 +++ .../opencv2/stitching/detail/warpers_inl.hpp | 10 +++++ modules/stitching/src/seam_finders.cpp | 44 +++++++++++++++---- modules/stitching/src/warpers.cpp | 9 ++++ 5 files changed, 66 insertions(+), 9 deletions(-) diff --git a/modules/stitching/include/opencv2/stitching/detail/seam_finders.hpp b/modules/stitching/include/opencv2/stitching/detail/seam_finders.hpp index bd21958459..c52d1cdb4c 100644 --- a/modules/stitching/include/opencv2/stitching/detail/seam_finders.hpp +++ b/modules/stitching/include/opencv2/stitching/detail/seam_finders.hpp @@ -71,9 +71,11 @@ public: std::vector &masks); protected: + void run(); virtual void findInPair(size_t first, size_t second, Rect roi) = 0; std::vector images_; + std::vector sizes_; std::vector corners_; std::vector masks_; }; @@ -81,6 +83,9 @@ protected: class CV_EXPORTS VoronoiSeamFinder : public PairwiseSeamFinder { +public: + virtual void find(const std::vector &size, const std::vector &corners, + std::vector &masks); private: void findInPair(size_t first, size_t second, Rect roi); }; diff --git a/modules/stitching/include/opencv2/stitching/detail/warpers.hpp b/modules/stitching/include/opencv2/stitching/detail/warpers.hpp index 0f168a4455..5e36a6e679 100644 --- a/modules/stitching/include/opencv2/stitching/detail/warpers.hpp +++ b/modules/stitching/include/opencv2/stitching/detail/warpers.hpp @@ -57,11 +57,14 @@ class CV_EXPORTS RotationWarper public: virtual ~RotationWarper() {} + virtual Point2f warp(const Point2f &pt, const Mat &K, const Mat &R) = 0; + virtual Rect buildMaps(Size src_size, const Mat &K, const Mat &R, Mat &xmap, Mat &ymap) = 0; virtual Point warp(const Mat &src, const Mat &K, const Mat &R, int interp_mode, int border_mode, Mat &dst) = 0; + // TODO add other backward functions for consistency or move this into a separated interface virtual void warpBackward(const Mat &src, const Mat &K, const Mat &R, int interp_mode, int border_mode, Size dst_size, Mat &dst) = 0; @@ -88,6 +91,8 @@ template class CV_EXPORTS RotationWarperBase : public RotationWarper { public: + Point2f warp(const Point2f &pt, const Mat &K, const Mat &R); + Rect buildMaps(Size src_size, const Mat &K, const Mat &R, Mat &xmap, Mat &ymap); Point warp(const Mat &src, const Mat &K, const Mat &R, int interp_mode, int border_mode, @@ -126,6 +131,8 @@ public: void setScale(float scale) { projector_.scale = scale; } + Point2f warp(const Point2f &pt, const Mat &K, const Mat &R, const Mat &T); + Rect buildMaps(Size src_size, const Mat &K, const Mat &R, const Mat &T, Mat &xmap, Mat &ymap); Point warp(const Mat &src, const Mat &K, const Mat &R, const Mat &T, int interp_mode, int border_mode, diff --git a/modules/stitching/include/opencv2/stitching/detail/warpers_inl.hpp b/modules/stitching/include/opencv2/stitching/detail/warpers_inl.hpp index 389deda140..65532bbe47 100644 --- a/modules/stitching/include/opencv2/stitching/detail/warpers_inl.hpp +++ b/modules/stitching/include/opencv2/stitching/detail/warpers_inl.hpp @@ -49,6 +49,16 @@ namespace cv { namespace detail { +template +Point2f RotationWarperBase

::warp(const Point2f &pt, const Mat &K, const Mat &R) +{ + projector_.setCameraParams(K, R); + Point2f uv; + projector_.mapForward(pt.x, pt.y, uv.x, uv.y); + return uv; +} + + template Rect RotationWarperBase

::buildMaps(Size src_size, const Mat &K, const Mat &R, Mat &xmap, Mat &ymap) { diff --git a/modules/stitching/src/seam_finders.cpp b/modules/stitching/src/seam_finders.cpp index ef854e4e74..d2d50fd620 100644 --- a/modules/stitching/src/seam_finders.cpp +++ b/modules/stitching/src/seam_finders.cpp @@ -49,24 +49,50 @@ void PairwiseSeamFinder::find(const vector &src, const vector &corne vector &masks) { LOGLN("Finding seams..."); - int64 t = getTickCount(); - - if (src.size() == 0) + if (src.size() == 0) return; + int64 t = getTickCount(); + images_ = src; + sizes_.resize(src.size()); + for (size_t i = 0; i < src.size(); ++i) + sizes_[i] = src[i].size(); corners_ = corners; masks_ = masks; + run(); + + LOGLN("Finding seams, time: " << ((getTickCount() - t) / getTickFrequency()) << " sec"); +} + - for (size_t i = 0; i < src.size() - 1; ++i) +void PairwiseSeamFinder::run() +{ + for (size_t i = 0; i < sizes_.size() - 1; ++i) { - for (size_t j = i + 1; j < src.size(); ++j) + for (size_t j = i + 1; j < sizes_.size(); ++j) { Rect roi; - if (overlapRoi(corners[i], corners[j], src[i].size(), src[j].size(), roi)) + if (overlapRoi(corners_[i], corners_[j], sizes_[i], sizes_[j], roi)) findInPair(i, j, roi); } } +} + + +void VoronoiSeamFinder::find(const vector &sizes, const vector &corners, + vector &masks) +{ + LOGLN("Finding seams..."); + if (sizes.size() == 0) + return; + + int64 t = getTickCount(); + + sizes_ = sizes; + corners_ = corners; + masks_ = masks; + run(); LOGLN("Finding seams, time: " << ((getTickCount() - t) / getTickFrequency()) << " sec"); } @@ -78,7 +104,7 @@ void VoronoiSeamFinder::findInPair(size_t first, size_t second, Rect roi) Mat submask1(roi.height + 2 * gap, roi.width + 2 * gap, CV_8U); Mat submask2(roi.height + 2 * gap, roi.width + 2 * gap, CV_8U); - Mat img1 = images_[first], img2 = images_[second]; + Size img1 = sizes_[first], img2 = sizes_[second]; Mat mask1 = masks_[first], mask2 = masks_[second]; Point tl1 = corners_[first], tl2 = corners_[second]; @@ -89,14 +115,14 @@ void VoronoiSeamFinder::findInPair(size_t first, size_t second, Rect roi) { int y1 = roi.y - tl1.y + y; int x1 = roi.x - tl1.x + x; - if (y1 >= 0 && x1 >= 0 && y1 < img1.rows && x1 < img1.cols) + if (y1 >= 0 && x1 >= 0 && y1 < img1.height && x1 < img1.width) submask1.at(y + gap, x + gap) = mask1.at(y1, x1); else submask1.at(y + gap, x + gap) = 0; int y2 = roi.y - tl2.y + y; int x2 = roi.x - tl2.x + x; - if (y2 >= 0 && x2 >= 0 && y2 < img2.rows && x2 < img2.cols) + if (y2 >= 0 && x2 >= 0 && y2 < img2.height && x2 < img2.width) submask2.at(y + gap, x + gap) = mask2.at(y2, x2); else submask2.at(y + gap, x + gap) = 0; diff --git a/modules/stitching/src/warpers.cpp b/modules/stitching/src/warpers.cpp index 3bd04979af..539bb08ad9 100644 --- a/modules/stitching/src/warpers.cpp +++ b/modules/stitching/src/warpers.cpp @@ -78,6 +78,15 @@ void ProjectorBase::setCameraParams(const Mat &K, const Mat &R, const Mat &T) } +Point2f PlaneWarper::warp(const Point2f &pt, const Mat &K, const Mat &R, const Mat &T) +{ + projector_.setCameraParams(K, R, T); + Point2f uv; + projector_.mapForward(pt.x, pt.y, uv.x, uv.y); + return uv; +} + + Rect PlaneWarper::buildMaps(Size src_size, const Mat &K, const Mat &R, const Mat &T, Mat &xmap, Mat &ymap) { projector_.setCameraParams(K, R, T);