1 /*M///////////////////////////////////////////////////////////////////////////////////////
3 // IMPORTANT: READ BEFORE DOWNLOADING, COPYING, INSTALLING OR USING.
5 // By downloading, copying, installing or using the software you agree to this license.
6 // If you do not agree to this license, do not download, install,
7 // copy or use the software.
11 // For Open Source Computer Vision Library
13 // Copyright (C) 2000-2008, Intel Corporation, all rights reserved.
14 // Copyright (C) 2009, Willow Garage Inc., all rights reserved.
15 // Third party copyrights are property of their respective owners.
17 // Redistribution and use in source and binary forms, with or without modification,
18 // are permitted provided that the following conditions are met:
20 // * Redistribution's of source code must retain the above copyright notice,
21 // this list of conditions and the following disclaimer.
23 // * Redistribution's in binary form must reproduce the above copyright notice,
24 // this list of conditions and the following disclaimer in the documentation
25 // and/or other materials provided with the distribution.
27 // * The name of the copyright holders may not be used to endorse or promote products
28 // derived from this software without specific prior written permission.
30 // This software is provided by the copyright holders and contributors "as is" and
31 // any express or implied warranties, including, but not limited to, the implied
32 // warranties of merchantability and fitness for a particular purpose are disclaimed.
33 // In no event shall the Intel Corporation or contributors be liable for any direct,
34 // indirect, incidental, special, exemplary, or consequential damages
35 // (including, but not limited to, procurement of substitute goods or services;
36 // loss of use, data, or profits; or business interruption) however caused
37 // and on any theory of liability, whether in contract, strict liability,
38 // or tort (including negligence or otherwise) arising in any way out of
39 // the use of this software, even if advised of the possibility of such damage.
43 #include "precomp.hpp"
45 #include "opencv2/core/opencl/ocl_defs.hpp"
48 using namespace cv::detail;
49 using namespace cv::cuda;
51 #ifdef HAVE_OPENCV_XFEATURES2D
52 #include "opencv2/xfeatures2d.hpp"
53 using xfeatures2d::SURF;
56 #ifdef HAVE_OPENCV_CUDAIMGPROC
57 # include "opencv2/cudaimgproc.hpp"
64 bool operator<(const DistIdxPair &other) const { return dist < other.dist; }
70 struct MatchPairsBody : ParallelLoopBody
72 MatchPairsBody(FeaturesMatcher &_matcher, const std::vector<ImageFeatures> &_features,
73 std::vector<MatchesInfo> &_pairwise_matches, std::vector<std::pair<int,int> > &_near_pairs)
74 : matcher(_matcher), features(_features),
75 pairwise_matches(_pairwise_matches), near_pairs(_near_pairs) {}
77 void operator ()(const Range &r) const CV_OVERRIDE
79 cv::RNG rng = cv::theRNG(); // save entry rng state
80 const int num_images = static_cast<int>(features.size());
81 for (int i = r.start; i < r.end; ++i)
83 cv::theRNG() = cv::RNG(rng.state + i); // force "stable" RNG seed for each processed pair
85 int from = near_pairs[i].first;
86 int to = near_pairs[i].second;
87 int pair_idx = from*num_images + to;
89 matcher(features[from], features[to], pairwise_matches[pair_idx]);
90 pairwise_matches[pair_idx].src_img_idx = from;
91 pairwise_matches[pair_idx].dst_img_idx = to;
93 size_t dual_pair_idx = to*num_images + from;
95 pairwise_matches[dual_pair_idx] = pairwise_matches[pair_idx];
96 pairwise_matches[dual_pair_idx].src_img_idx = to;
97 pairwise_matches[dual_pair_idx].dst_img_idx = from;
99 if (!pairwise_matches[pair_idx].H.empty())
100 pairwise_matches[dual_pair_idx].H = pairwise_matches[pair_idx].H.inv();
102 for (size_t j = 0; j < pairwise_matches[dual_pair_idx].matches.size(); ++j)
103 std::swap(pairwise_matches[dual_pair_idx].matches[j].queryIdx,
104 pairwise_matches[dual_pair_idx].matches[j].trainIdx);
109 FeaturesMatcher &matcher;
110 const std::vector<ImageFeatures> &features;
111 std::vector<MatchesInfo> &pairwise_matches;
112 std::vector<std::pair<int,int> > &near_pairs;
115 void operator =(const MatchPairsBody&);
119 struct FindFeaturesBody : ParallelLoopBody
121 FindFeaturesBody(FeaturesFinder &finder, InputArrayOfArrays images,
122 std::vector<ImageFeatures> &features, const std::vector<std::vector<cv::Rect> > *rois)
123 : finder_(finder), images_(images), features_(features), rois_(rois) {}
125 void operator ()(const Range &r) const CV_OVERRIDE
127 for (int i = r.start; i < r.end; ++i)
129 Mat image = images_.getMat(i);
131 finder_(image, features_[i], (*rois_)[i]);
133 finder_(image, features_[i]);
138 FeaturesFinder &finder_;
139 InputArrayOfArrays images_;
140 std::vector<ImageFeatures> &features_;
141 const std::vector<std::vector<cv::Rect> > *rois_;
143 // to cease visual studio warning
144 void operator =(const FindFeaturesBody&);
148 //////////////////////////////////////////////////////////////////////////////
150 typedef std::set<std::pair<int,int> > MatchesSet;
152 // These two classes are aimed to find features matches only, not to
153 // estimate homography
155 class CpuMatcher CV_FINAL : public FeaturesMatcher
158 CpuMatcher(float match_conf) : FeaturesMatcher(true), match_conf_(match_conf) {}
159 void match(const ImageFeatures &features1, const ImageFeatures &features2, MatchesInfo& matches_info) CV_OVERRIDE;
165 #ifdef HAVE_OPENCV_CUDAFEATURES2D
166 class GpuMatcher CV_FINAL : public FeaturesMatcher
169 GpuMatcher(float match_conf) : match_conf_(match_conf) {}
170 void match(const ImageFeatures &features1, const ImageFeatures &features2, MatchesInfo& matches_info);
172 void collectGarbage();
176 GpuMat descriptors1_, descriptors2_;
177 GpuMat train_idx_, distance_, all_dist_;
178 std::vector< std::vector<DMatch> > pair_matches;
183 void CpuMatcher::match(const ImageFeatures &features1, const ImageFeatures &features2, MatchesInfo& matches_info)
185 CV_INSTRUMENT_REGION()
187 CV_Assert(features1.descriptors.type() == features2.descriptors.type());
188 CV_Assert(features2.descriptors.depth() == CV_8U || features2.descriptors.depth() == CV_32F);
190 matches_info.matches.clear();
192 Ptr<cv::DescriptorMatcher> matcher;
193 #if 0 // TODO check this
194 if (ocl::isOpenCLActivated())
196 matcher = makePtr<BFMatcher>((int)NORM_L2);
201 Ptr<flann::IndexParams> indexParams = makePtr<flann::KDTreeIndexParams>();
202 Ptr<flann::SearchParams> searchParams = makePtr<flann::SearchParams>();
204 if (features2.descriptors.depth() == CV_8U)
206 indexParams->setAlgorithm(cvflann::FLANN_INDEX_LSH);
207 searchParams->setAlgorithm(cvflann::FLANN_INDEX_LSH);
210 matcher = makePtr<FlannBasedMatcher>(indexParams, searchParams);
212 std::vector< std::vector<DMatch> > pair_matches;
216 matcher->knnMatch(features1.descriptors, features2.descriptors, pair_matches, 2);
217 for (size_t i = 0; i < pair_matches.size(); ++i)
219 if (pair_matches[i].size() < 2)
221 const DMatch& m0 = pair_matches[i][0];
222 const DMatch& m1 = pair_matches[i][1];
223 if (m0.distance < (1.f - match_conf_) * m1.distance)
225 matches_info.matches.push_back(m0);
226 matches.insert(std::make_pair(m0.queryIdx, m0.trainIdx));
229 LOG("\n1->2 matches: " << matches_info.matches.size() << endl);
232 pair_matches.clear();
233 matcher->knnMatch(features2.descriptors, features1.descriptors, pair_matches, 2);
234 for (size_t i = 0; i < pair_matches.size(); ++i)
236 if (pair_matches[i].size() < 2)
238 const DMatch& m0 = pair_matches[i][0];
239 const DMatch& m1 = pair_matches[i][1];
240 if (m0.distance < (1.f - match_conf_) * m1.distance)
241 if (matches.find(std::make_pair(m0.trainIdx, m0.queryIdx)) == matches.end())
242 matches_info.matches.push_back(DMatch(m0.trainIdx, m0.queryIdx, m0.distance));
244 LOG("1->2 & 2->1 matches: " << matches_info.matches.size() << endl);
247 #ifdef HAVE_OPENCV_CUDAFEATURES2D
248 void GpuMatcher::match(const ImageFeatures &features1, const ImageFeatures &features2, MatchesInfo& matches_info)
250 CV_INSTRUMENT_REGION()
252 matches_info.matches.clear();
254 ensureSizeIsEnough(features1.descriptors.size(), features1.descriptors.type(), descriptors1_);
255 ensureSizeIsEnough(features2.descriptors.size(), features2.descriptors.type(), descriptors2_);
257 descriptors1_.upload(features1.descriptors);
258 descriptors2_.upload(features2.descriptors);
260 //TODO: NORM_L1 allows to avoid matcher crashes for ORB features, but is not absolutely correct for them.
261 // The best choice for ORB features is NORM_HAMMING, but it is incorrect for SURF features.
262 // More accurate fix in this place should be done in the future -- the type of the norm
263 // should be either a parameter of this method, or a field of the class.
264 Ptr<cuda::DescriptorMatcher> matcher = cuda::DescriptorMatcher::createBFMatcher(NORM_L1);
269 pair_matches.clear();
270 matcher->knnMatch(descriptors1_, descriptors2_, pair_matches, 2);
271 for (size_t i = 0; i < pair_matches.size(); ++i)
273 if (pair_matches[i].size() < 2)
275 const DMatch& m0 = pair_matches[i][0];
276 const DMatch& m1 = pair_matches[i][1];
277 if (m0.distance < (1.f - match_conf_) * m1.distance)
279 matches_info.matches.push_back(m0);
280 matches.insert(std::make_pair(m0.queryIdx, m0.trainIdx));
285 pair_matches.clear();
286 matcher->knnMatch(descriptors2_, descriptors1_, pair_matches, 2);
287 for (size_t i = 0; i < pair_matches.size(); ++i)
289 if (pair_matches[i].size() < 2)
291 const DMatch& m0 = pair_matches[i][0];
292 const DMatch& m1 = pair_matches[i][1];
293 if (m0.distance < (1.f - match_conf_) * m1.distance)
294 if (matches.find(std::make_pair(m0.trainIdx, m0.queryIdx)) == matches.end())
295 matches_info.matches.push_back(DMatch(m0.trainIdx, m0.queryIdx, m0.distance));
299 void GpuMatcher::collectGarbage()
301 descriptors1_.release();
302 descriptors2_.release();
303 train_idx_.release();
306 std::vector< std::vector<DMatch> >().swap(pair_matches);
316 void FeaturesFinder::operator ()(InputArray image, ImageFeatures &features)
318 find(image, features);
319 features.img_size = image.size();
323 void FeaturesFinder::operator ()(InputArray image, ImageFeatures &features, const std::vector<Rect> &rois)
325 std::vector<ImageFeatures> roi_features(rois.size());
326 size_t total_kps_count = 0;
327 int total_descriptors_height = 0;
329 for (size_t i = 0; i < rois.size(); ++i)
331 find(image.getUMat()(rois[i]), roi_features[i]);
332 total_kps_count += roi_features[i].keypoints.size();
333 total_descriptors_height += roi_features[i].descriptors.rows;
336 features.img_size = image.size();
337 features.keypoints.resize(total_kps_count);
338 features.descriptors.create(total_descriptors_height,
339 roi_features[0].descriptors.cols,
340 roi_features[0].descriptors.type());
343 int descr_offset = 0;
344 for (size_t i = 0; i < rois.size(); ++i)
346 for (size_t j = 0; j < roi_features[i].keypoints.size(); ++j, ++kp_idx)
348 features.keypoints[kp_idx] = roi_features[i].keypoints[j];
349 features.keypoints[kp_idx].pt.x += (float)rois[i].x;
350 features.keypoints[kp_idx].pt.y += (float)rois[i].y;
352 UMat subdescr = features.descriptors.rowRange(
353 descr_offset, descr_offset + roi_features[i].descriptors.rows);
354 roi_features[i].descriptors.copyTo(subdescr);
355 descr_offset += roi_features[i].descriptors.rows;
360 void FeaturesFinder::operator ()(InputArrayOfArrays images, std::vector<ImageFeatures> &features)
362 size_t count = images.total();
363 features.resize(count);
365 FindFeaturesBody body(*this, images, features, NULL);
367 parallel_for_(Range(0, static_cast<int>(count)), body);
369 body(Range(0, static_cast<int>(count)));
373 void FeaturesFinder::operator ()(InputArrayOfArrays images, std::vector<ImageFeatures> &features,
374 const std::vector<std::vector<cv::Rect> > &rois)
376 CV_Assert(rois.size() == images.total());
377 size_t count = images.total();
378 features.resize(count);
380 FindFeaturesBody body(*this, images, features, &rois);
382 parallel_for_(Range(0, static_cast<int>(count)), body);
384 body(Range(0, static_cast<int>(count)));
388 bool FeaturesFinder::isThreadSafe() const
391 if (ocl::isOpenCLActivated())
396 if (dynamic_cast<const SurfFeaturesFinder*>(this))
400 else if (dynamic_cast<const OrbFeaturesFinder*>(this))
411 SurfFeaturesFinder::SurfFeaturesFinder(double hess_thresh, int num_octaves, int num_layers,
412 int num_octaves_descr, int num_layers_descr)
414 #ifdef HAVE_OPENCV_XFEATURES2D
415 if (num_octaves_descr == num_octaves && num_layers_descr == num_layers)
417 Ptr<SURF> surf_ = SURF::create();
419 CV_Error( Error::StsNotImplemented, "OpenCV was built without SURF support" );
420 surf_->setHessianThreshold(hess_thresh);
421 surf_->setNOctaves(num_octaves);
422 surf_->setNOctaveLayers(num_layers);
427 Ptr<SURF> sdetector_ = SURF::create();
428 Ptr<SURF> sextractor_ = SURF::create();
430 if( !sdetector_ || !sextractor_ )
431 CV_Error( Error::StsNotImplemented, "OpenCV was built without SURF support" );
433 sdetector_->setHessianThreshold(hess_thresh);
434 sdetector_->setNOctaves(num_octaves);
435 sdetector_->setNOctaveLayers(num_layers);
437 sextractor_->setNOctaves(num_octaves_descr);
438 sextractor_->setNOctaveLayers(num_layers_descr);
440 detector_ = sdetector_;
441 extractor_ = sextractor_;
447 (void)num_octaves_descr;
448 (void)num_layers_descr;
449 CV_Error( Error::StsNotImplemented, "OpenCV was built without SURF support" );
453 void SurfFeaturesFinder::find(InputArray image, ImageFeatures &features)
456 CV_Assert((image.type() == CV_8UC3) || (image.type() == CV_8UC1));
457 if(image.type() == CV_8UC3)
459 cvtColor(image, gray_image, COLOR_BGR2GRAY);
463 gray_image = image.getUMat();
467 detector_->detect(gray_image, features.keypoints);
468 extractor_->compute(gray_image, features.keypoints, features.descriptors);
473 surf->detectAndCompute(gray_image, Mat(), features.keypoints, descriptors);
474 features.descriptors = descriptors.reshape(1, (int)features.keypoints.size());
478 OrbFeaturesFinder::OrbFeaturesFinder(Size _grid_size, int n_features, float scaleFactor, int nlevels)
480 grid_size = _grid_size;
481 orb = ORB::create(n_features * (99 + grid_size.area())/100/grid_size.area(), scaleFactor, nlevels);
484 void OrbFeaturesFinder::find(InputArray image, ImageFeatures &features)
488 CV_Assert((image.type() == CV_8UC3) || (image.type() == CV_8UC4) || (image.type() == CV_8UC1));
490 if (image.type() == CV_8UC3) {
491 cvtColor(image, gray_image, COLOR_BGR2GRAY);
492 } else if (image.type() == CV_8UC4) {
493 cvtColor(image, gray_image, COLOR_BGRA2GRAY);
494 } else if (image.type() == CV_8UC1) {
495 gray_image = image.getUMat();
497 CV_Error(Error::StsUnsupportedFormat, "");
500 if (grid_size.area() == 1)
501 orb->detectAndCompute(gray_image, Mat(), features.keypoints, features.descriptors);
504 features.keypoints.clear();
505 features.descriptors.release();
507 std::vector<KeyPoint> points;
511 for (int r = 0; r < grid_size.height; ++r)
512 for (int c = 0; c < grid_size.width; ++c)
514 int xl = c * gray_image.cols / grid_size.width;
515 int yl = r * gray_image.rows / grid_size.height;
516 int xr = (c+1) * gray_image.cols / grid_size.width;
517 int yr = (r+1) * gray_image.rows / grid_size.height;
519 // LOGLN("OrbFeaturesFinder::find: gray_image.empty=" << (gray_image.empty()?"true":"false") << ", "
520 // << " gray_image.size()=(" << gray_image.size().width << "x" << gray_image.size().height << "), "
521 // << " yl=" << yl << ", yr=" << yr << ", "
522 // << " xl=" << xl << ", xr=" << xr << ", gray_image.data=" << ((size_t)gray_image.data) << ", "
523 // << "gray_image.dims=" << gray_image.dims << "\n");
525 UMat gray_image_part=gray_image(Range(yl, yr), Range(xl, xr));
526 // LOGLN("OrbFeaturesFinder::find: gray_image_part.empty=" << (gray_image_part.empty()?"true":"false") << ", "
527 // << " gray_image_part.size()=(" << gray_image_part.size().width << "x" << gray_image_part.size().height << "), "
528 // << " gray_image_part.dims=" << gray_image_part.dims << ", "
529 // << " gray_image_part.data=" << ((size_t)gray_image_part.data) << "\n");
531 orb->detectAndCompute(gray_image_part, UMat(), points, descriptors);
533 features.keypoints.reserve(features.keypoints.size() + points.size());
534 for (std::vector<KeyPoint>::iterator kp = points.begin(); kp != points.end(); ++kp)
538 features.keypoints.push_back(*kp);
540 _descriptors.push_back(descriptors.getMat(ACCESS_READ));
543 // TODO optimize copyTo()
544 //features.descriptors = _descriptors.getUMat(ACCESS_READ);
545 _descriptors.copyTo(features.descriptors);
549 AKAZEFeaturesFinder::AKAZEFeaturesFinder(int descriptor_type,
551 int descriptor_channels,
557 akaze = AKAZE::create(descriptor_type, descriptor_size, descriptor_channels,
558 threshold, nOctaves, nOctaveLayers, diffusivity);
561 void AKAZEFeaturesFinder::find(InputArray image, detail::ImageFeatures &features)
563 CV_Assert((image.type() == CV_8UC3) || (image.type() == CV_8UC1));
564 akaze->detectAndCompute(image, noArray(), features.keypoints, features.descriptors);
567 #ifdef HAVE_OPENCV_XFEATURES2D
568 SurfFeaturesFinderGpu::SurfFeaturesFinderGpu(double hess_thresh, int num_octaves, int num_layers,
569 int num_octaves_descr, int num_layers_descr)
571 surf_.keypointsRatio = 0.1f;
572 surf_.hessianThreshold = hess_thresh;
573 surf_.extended = false;
574 num_octaves_ = num_octaves;
575 num_layers_ = num_layers;
576 num_octaves_descr_ = num_octaves_descr;
577 num_layers_descr_ = num_layers_descr;
581 void SurfFeaturesFinderGpu::find(InputArray image, ImageFeatures &features)
583 CV_Assert(image.depth() == CV_8U);
585 ensureSizeIsEnough(image.size(), image.type(), image_);
586 image_.upload(image);
588 ensureSizeIsEnough(image.size(), CV_8UC1, gray_image_);
590 #ifdef HAVE_OPENCV_CUDAIMGPROC
591 cv::cuda::cvtColor(image_, gray_image_, COLOR_BGR2GRAY);
593 cvtColor(image_, gray_image_, COLOR_BGR2GRAY);
596 surf_.nOctaves = num_octaves_;
597 surf_.nOctaveLayers = num_layers_;
598 surf_.upright = false;
599 surf_(gray_image_, GpuMat(), keypoints_);
601 surf_.nOctaves = num_octaves_descr_;
602 surf_.nOctaveLayers = num_layers_descr_;
603 surf_.upright = true;
604 surf_(gray_image_, GpuMat(), keypoints_, descriptors_, true);
605 surf_.downloadKeypoints(keypoints_, features.keypoints);
607 descriptors_.download(features.descriptors);
610 void SurfFeaturesFinderGpu::collectGarbage()
612 surf_.releaseMemory();
614 gray_image_.release();
615 keypoints_.release();
616 descriptors_.release();
621 //////////////////////////////////////////////////////////////////////////////
623 MatchesInfo::MatchesInfo() : src_img_idx(-1), dst_img_idx(-1), num_inliers(0), confidence(0) {}
625 MatchesInfo::MatchesInfo(const MatchesInfo &other) { *this = other; }
627 MatchesInfo& MatchesInfo::operator =(const MatchesInfo &other)
629 src_img_idx = other.src_img_idx;
630 dst_img_idx = other.dst_img_idx;
631 matches = other.matches;
632 inliers_mask = other.inliers_mask;
633 num_inliers = other.num_inliers;
635 confidence = other.confidence;
640 //////////////////////////////////////////////////////////////////////////////
642 void FeaturesMatcher::operator ()(const std::vector<ImageFeatures> &features, std::vector<MatchesInfo> &pairwise_matches,
645 const int num_images = static_cast<int>(features.size());
647 CV_Assert(mask.empty() || (mask.type() == CV_8U && mask.cols == num_images && mask.rows));
648 Mat_<uchar> mask_(mask.getMat(ACCESS_READ));
650 mask_ = Mat::ones(num_images, num_images, CV_8U);
652 std::vector<std::pair<int,int> > near_pairs;
653 for (int i = 0; i < num_images - 1; ++i)
654 for (int j = i + 1; j < num_images; ++j)
655 if (features[i].keypoints.size() > 0 && features[j].keypoints.size() > 0 && mask_(i, j))
656 near_pairs.push_back(std::make_pair(i, j));
658 pairwise_matches.resize(num_images * num_images);
659 MatchPairsBody body(*this, features, pairwise_matches, near_pairs);
662 parallel_for_(Range(0, static_cast<int>(near_pairs.size())), body);
664 body(Range(0, static_cast<int>(near_pairs.size())));
669 //////////////////////////////////////////////////////////////////////////////
671 BestOf2NearestMatcher::BestOf2NearestMatcher(bool try_use_gpu, float match_conf, int num_matches_thresh1, int num_matches_thresh2)
675 #ifdef HAVE_OPENCV_CUDAFEATURES2D
676 if (try_use_gpu && getCudaEnabledDeviceCount() > 0)
678 impl_ = makePtr<GpuMatcher>(match_conf);
683 impl_ = makePtr<CpuMatcher>(match_conf);
686 is_thread_safe_ = impl_->isThreadSafe();
687 num_matches_thresh1_ = num_matches_thresh1;
688 num_matches_thresh2_ = num_matches_thresh2;
692 void BestOf2NearestMatcher::match(const ImageFeatures &features1, const ImageFeatures &features2,
693 MatchesInfo &matches_info)
695 CV_INSTRUMENT_REGION()
697 (*impl_)(features1, features2, matches_info);
699 // Check if it makes sense to find homography
700 if (matches_info.matches.size() < static_cast<size_t>(num_matches_thresh1_))
703 // Construct point-point correspondences for homography estimation
704 Mat src_points(1, static_cast<int>(matches_info.matches.size()), CV_32FC2);
705 Mat dst_points(1, static_cast<int>(matches_info.matches.size()), CV_32FC2);
706 for (size_t i = 0; i < matches_info.matches.size(); ++i)
708 const DMatch& m = matches_info.matches[i];
710 Point2f p = features1.keypoints[m.queryIdx].pt;
711 p.x -= features1.img_size.width * 0.5f;
712 p.y -= features1.img_size.height * 0.5f;
713 src_points.at<Point2f>(0, static_cast<int>(i)) = p;
715 p = features2.keypoints[m.trainIdx].pt;
716 p.x -= features2.img_size.width * 0.5f;
717 p.y -= features2.img_size.height * 0.5f;
718 dst_points.at<Point2f>(0, static_cast<int>(i)) = p;
721 // Find pair-wise motion
722 matches_info.H = findHomography(src_points, dst_points, matches_info.inliers_mask, RANSAC);
723 if (matches_info.H.empty() || std::abs(determinant(matches_info.H)) < std::numeric_limits<double>::epsilon())
726 // Find number of inliers
727 matches_info.num_inliers = 0;
728 for (size_t i = 0; i < matches_info.inliers_mask.size(); ++i)
729 if (matches_info.inliers_mask[i])
730 matches_info.num_inliers++;
732 // These coeffs are from paper M. Brown and D. Lowe. "Automatic Panoramic Image Stitching
733 // using Invariant Features"
734 matches_info.confidence = matches_info.num_inliers / (8 + 0.3 * matches_info.matches.size());
736 // Set zero confidence to remove matches between too close images, as they don't provide
737 // additional information anyway. The threshold was set experimentally.
738 matches_info.confidence = matches_info.confidence > 3. ? 0. : matches_info.confidence;
740 // Check if we should try to refine motion
741 if (matches_info.num_inliers < num_matches_thresh2_)
744 // Construct point-point correspondences for inliers only
745 src_points.create(1, matches_info.num_inliers, CV_32FC2);
746 dst_points.create(1, matches_info.num_inliers, CV_32FC2);
748 for (size_t i = 0; i < matches_info.matches.size(); ++i)
750 if (!matches_info.inliers_mask[i])
753 const DMatch& m = matches_info.matches[i];
755 Point2f p = features1.keypoints[m.queryIdx].pt;
756 p.x -= features1.img_size.width * 0.5f;
757 p.y -= features1.img_size.height * 0.5f;
758 src_points.at<Point2f>(0, inlier_idx) = p;
760 p = features2.keypoints[m.trainIdx].pt;
761 p.x -= features2.img_size.width * 0.5f;
762 p.y -= features2.img_size.height * 0.5f;
763 dst_points.at<Point2f>(0, inlier_idx) = p;
768 // Rerun motion estimation on inliers only
769 matches_info.H = findHomography(src_points, dst_points, RANSAC);
772 void BestOf2NearestMatcher::collectGarbage()
774 impl_->collectGarbage();
778 BestOf2NearestRangeMatcher::BestOf2NearestRangeMatcher(int range_width, bool try_use_gpu, float match_conf, int num_matches_thresh1, int num_matches_thresh2): BestOf2NearestMatcher(try_use_gpu, match_conf, num_matches_thresh1, num_matches_thresh2)
780 range_width_ = range_width;
784 void BestOf2NearestRangeMatcher::operator ()(const std::vector<ImageFeatures> &features, std::vector<MatchesInfo> &pairwise_matches,
787 const int num_images = static_cast<int>(features.size());
789 CV_Assert(mask.empty() || (mask.type() == CV_8U && mask.cols == num_images && mask.rows));
790 Mat_<uchar> mask_(mask.getMat(ACCESS_READ));
792 mask_ = Mat::ones(num_images, num_images, CV_8U);
794 std::vector<std::pair<int,int> > near_pairs;
795 for (int i = 0; i < num_images - 1; ++i)
796 for (int j = i + 1; j < std::min(num_images, i + range_width_); ++j)
797 if (features[i].keypoints.size() > 0 && features[j].keypoints.size() > 0 && mask_(i, j))
798 near_pairs.push_back(std::make_pair(i, j));
800 pairwise_matches.resize(num_images * num_images);
801 MatchPairsBody body(*this, features, pairwise_matches, near_pairs);
804 parallel_for_(Range(0, static_cast<int>(near_pairs.size())), body);
806 body(Range(0, static_cast<int>(near_pairs.size())));
811 void AffineBestOf2NearestMatcher::match(const ImageFeatures &features1, const ImageFeatures &features2,
812 MatchesInfo &matches_info)
814 (*impl_)(features1, features2, matches_info);
816 // Check if it makes sense to find transform
817 if (matches_info.matches.size() < static_cast<size_t>(num_matches_thresh1_))
820 // Construct point-point correspondences for transform estimation
821 Mat src_points(1, static_cast<int>(matches_info.matches.size()), CV_32FC2);
822 Mat dst_points(1, static_cast<int>(matches_info.matches.size()), CV_32FC2);
823 for (size_t i = 0; i < matches_info.matches.size(); ++i)
825 const cv::DMatch &m = matches_info.matches[i];
826 src_points.at<Point2f>(0, static_cast<int>(i)) = features1.keypoints[m.queryIdx].pt;
827 dst_points.at<Point2f>(0, static_cast<int>(i)) = features2.keypoints[m.trainIdx].pt;
830 // Find pair-wise motion
832 matches_info.H = estimateAffine2D(src_points, dst_points, matches_info.inliers_mask);
834 matches_info.H = estimateAffinePartial2D(src_points, dst_points, matches_info.inliers_mask);
836 if (matches_info.H.empty()) {
837 // could not find transformation
838 matches_info.confidence = 0;
839 matches_info.num_inliers = 0;
843 // Find number of inliers
844 matches_info.num_inliers = 0;
845 for (size_t i = 0; i < matches_info.inliers_mask.size(); ++i)
846 if (matches_info.inliers_mask[i])
847 matches_info.num_inliers++;
849 // These coeffs are from paper M. Brown and D. Lowe. "Automatic Panoramic
850 // Image Stitching using Invariant Features"
851 matches_info.confidence =
852 matches_info.num_inliers / (8 + 0.3 * matches_info.matches.size());
854 /* should we remove matches between too close images? */
855 // matches_info.confidence = matches_info.confidence > 3. ? 0. : matches_info.confidence;
857 // extend H to represent linear transformation in homogeneous coordinates
858 matches_info.H.push_back(Mat::zeros(1, 3, CV_64F));
859 matches_info.H.at<double>(2, 2) = 1;
863 } // namespace detail