From a7d053f10d05c842ae56dc0453f24b7ddeba6658 Mon Sep 17 00:00:00 2001 From: Alexey Spizhevoy Date: Mon, 26 Sep 2011 09:59:13 +0000 Subject: [PATCH] Added handling of ROI in stitching features matchers --- .../opencv2/stitching/detail/matchers.hpp | 1 + .../include/opencv2/stitching/stitcher.hpp | 2 + modules/stitching/src/matchers.cpp | 37 +++++++++++++++++++ modules/stitching/src/stitcher.cpp | 12 +++++- 4 files changed, 51 insertions(+), 1 deletion(-) diff --git a/modules/stitching/include/opencv2/stitching/detail/matchers.hpp b/modules/stitching/include/opencv2/stitching/detail/matchers.hpp index 0dbd14f9ab..a25ad3e6f9 100644 --- a/modules/stitching/include/opencv2/stitching/detail/matchers.hpp +++ b/modules/stitching/include/opencv2/stitching/detail/matchers.hpp @@ -67,6 +67,7 @@ class CV_EXPORTS FeaturesFinder public: virtual ~FeaturesFinder() {} void operator ()(const Mat &image, ImageFeatures &features); + void operator ()(const Mat &image, ImageFeatures &features, const std::vector &rois); virtual void collectGarbage() {} protected: diff --git a/modules/stitching/include/opencv2/stitching/stitcher.hpp b/modules/stitching/include/opencv2/stitching/stitcher.hpp index 775c0cdb61..5cc553388c 100644 --- a/modules/stitching/include/opencv2/stitching/stitcher.hpp +++ b/modules/stitching/include/opencv2/stitching/stitcher.hpp @@ -66,6 +66,7 @@ public: // Stitches the biggest found pano. Returns status code. Status stitch(InputArray imgs, OutputArray pano); + Status stitch(InputArray imgs, const std::vector > &rois, OutputArray pano); double registrationResol() const { return registr_resol_; } void setRegistrationResol(double resol_mpx) { registr_resol_ = resol_mpx; } @@ -147,6 +148,7 @@ private: Ptr blender_; std::vector imgs_; + std::vector > rois_; std::vector full_img_sizes_; std::vector features_; std::vector pairwise_matches_; diff --git a/modules/stitching/src/matchers.cpp b/modules/stitching/src/matchers.cpp index 5875342336..57a5970234 100644 --- a/modules/stitching/src/matchers.cpp +++ b/modules/stitching/src/matchers.cpp @@ -249,10 +249,47 @@ namespace detail { void FeaturesFinder::operator ()(const Mat &image, ImageFeatures &features) { find(image, features); + cout << features.descriptors.cols << " " << features.descriptors.rows << " " << features.descriptors.type() << endl; features.img_size = image.size(); } +// TODO add tests for this function +void FeaturesFinder::operator ()(const Mat &image, ImageFeatures &features, const vector &rois) +{ + vector roi_features; + size_t total_kps_count = 0; + int total_descriptors_width = 0; + + for (size_t i = 0; i < rois.size(); ++i) + { + find(image(rois[i]), roi_features[i]); + total_kps_count += roi_features[i].keypoints.size(); + total_descriptors_width += roi_features[i].descriptors.cols; + } + + features.img_size = image.size(); + features.keypoints.resize(total_kps_count); + features.descriptors.create(1, total_descriptors_width, roi_features[0].descriptors.type()); + + int kp_idx = 0; + int descr_offset = 0; + for (size_t i = 0; i < rois.size(); ++i) + { + for (size_t j = 0; j < features.keypoints.size(); ++j, ++kp_idx) + { + features.keypoints[kp_idx] = roi_features[i].keypoints[j]; + features.keypoints[kp_idx].pt.x += (float)rois[i].x; + features.keypoints[kp_idx].pt.y += (float)rois[i].y; + } + Mat subdescr = features.descriptors.colRange( + descr_offset, descr_offset + roi_features[i].descriptors.cols); + roi_features[i].descriptors.copyTo(subdescr); + descr_offset += roi_features[i].descriptors.cols; + } +} + + SurfFeaturesFinder::SurfFeaturesFinder(double hess_thresh, int num_octaves, int num_layers, int num_octaves_descr, int num_layers_descr) { diff --git a/modules/stitching/src/stitcher.cpp b/modules/stitching/src/stitcher.cpp index 1fbfe4c564..47e3c437ba 100644 --- a/modules/stitching/src/stitcher.cpp +++ b/modules/stitching/src/stitcher.cpp @@ -100,6 +100,13 @@ Stitcher::Status Stitcher::stitch(InputArray imgs, OutputArray pano) } +Stitcher::Status Stitcher::stitch(InputArray imgs, const vector > &rois, OutputArray pano) +{ + rois_ = rois; + return stitch(imgs, pano); +} + + Stitcher::Status Stitcher::matchImages() { if ((int)imgs_.size() < 2) @@ -148,7 +155,10 @@ Stitcher::Status Stitcher::matchImages() is_seam_scale_set = true; } - (*features_finder_)(img, features_[i]); + if (rois_.empty()) + (*features_finder_)(img, features_[i]); + else + (*features_finder_)(img, features_[i], rois_[i]); features_[i].img_idx = i; LOGLN("Features in image #" << i+1 << ": " << features_[i].keypoints.size());