From 052bf4df73b692890d0ab4e3be6c49b791755c42 Mon Sep 17 00:00:00 2001 From: Alexey Spizhevoy Date: Wed, 25 May 2011 13:14:56 +0000 Subject: [PATCH] added number of bands cropping in multi-bands blending --- modules/stitching/blenders.cpp | 9 +++++++++ modules/stitching/blenders.hpp | 6 +++--- 2 files changed, 12 insertions(+), 3 deletions(-) diff --git a/modules/stitching/blenders.cpp b/modules/stitching/blenders.cpp index 916b5da..2033ce0 100644 --- a/modules/stitching/blenders.cpp +++ b/modules/stitching/blenders.cpp @@ -158,8 +158,15 @@ void FeatherBlender::blend(Mat &dst, Mat &dst_mask) void MultiBandBlender::prepare(Rect dst_roi) { dst_roi_final_ = dst_roi; + + // Crop unnecessary bands + double max_len = static_cast(max(dst_roi.width, dst_roi.height)); + num_bands_ = min(actual_num_bands_, static_cast(ceil(log(max_len) / log(2.0)))); + + // Add border to the final image, to ensure sizes are divided by (1 << num_bands_) dst_roi.width += ((1 << num_bands_) - dst_roi.width % (1 << num_bands_)) % (1 << num_bands_); dst_roi.height += ((1 << num_bands_) - dst_roi.height % (1 << num_bands_)) % (1 << num_bands_); + Blender::prepare(dst_roi); dst_pyr_laplace_.resize(num_bands_ + 1); @@ -186,12 +193,14 @@ void MultiBandBlender::feed(const Mat &img, const Mat &mask, Point tl) CV_Assert(img.type() == CV_16SC3); CV_Assert(mask.type() == CV_8U); + // Keep source image in memory with small border int gap = 3 * (1 << num_bands_); Point tl_new(max(dst_roi_.x, tl.x - gap), max(dst_roi_.y, tl.y - gap)); Point br_new(min(dst_roi_.br().x, tl.x + img.cols + gap), min(dst_roi_.br().y, tl.y + img.rows + gap)); + // Ensure coordinates of top-left, bootom-right corners are divided by (1 << num_bands_) tl_new.x = dst_roi_.x + (((tl_new.x - dst_roi_.x) >> num_bands_) << num_bands_); tl_new.y = dst_roi_.y + (((tl_new.y - dst_roi_.y) >> num_bands_) << num_bands_); int width = br_new.x - tl_new.x; diff --git a/modules/stitching/blenders.hpp b/modules/stitching/blenders.hpp index f65b8dd..a6fd0ec 100644 --- a/modules/stitching/blenders.hpp +++ b/modules/stitching/blenders.hpp @@ -84,15 +84,15 @@ class MultiBandBlender : public Blender { public: MultiBandBlender(int num_bands = 5) { setNumBands(num_bands); } - int numBands() const { return num_bands_; } - void setNumBands(int val) { num_bands_ = val; } + int numBands() const { return actual_num_bands_; } + void setNumBands(int val) { actual_num_bands_ = val; } void prepare(cv::Rect dst_roi); void feed(const cv::Mat &img, const cv::Mat &mask, cv::Point tl); void blend(cv::Mat &dst, cv::Mat &dst_mask); private: - int num_bands_; + int actual_num_bands_, num_bands_; std::vector dst_pyr_laplace_; std::vector dst_band_weights_; cv::Rect dst_roi_final_; -- 2.7.4