1/*M///////////////////////////////////////////////////////////////////////////////////////
2//
3//  IMPORTANT: READ BEFORE DOWNLOADING, COPYING, INSTALLING OR USING.
4//
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.
8//
9//
10//                          License Agreement
11//                For Open Source Computer Vision Library
12//
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.
16//
17// Redistribution and use in source and binary forms, with or without modification,
18// are permitted provided that the following conditions are met:
19//
20//   * Redistribution's of source code must retain the above copyright notice,
21//     this list of conditions and the following disclaimer.
22//
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.
26//
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.
29//
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.
40//
41//M*/
42
43#include "precomp.hpp"
44
45using namespace cv;
46using namespace cv::detail;
47using namespace cv::cuda;
48
49#ifdef HAVE_OPENCV_XFEATURES2D
50#include "opencv2/xfeatures2d.hpp"
51using xfeatures2d::SURF;
52#endif
53
54namespace {
55
56struct DistIdxPair
57{
58    bool operator<(const DistIdxPair &other) const { return dist < other.dist; }
59    double dist;
60    int idx;
61};
62
63
64struct MatchPairsBody : ParallelLoopBody
65{
66    MatchPairsBody(FeaturesMatcher &_matcher, const std::vector<ImageFeatures> &_features,
67                   std::vector<MatchesInfo> &_pairwise_matches, std::vector<std::pair<int,int> > &_near_pairs)
68            : matcher(_matcher), features(_features),
69              pairwise_matches(_pairwise_matches), near_pairs(_near_pairs) {}
70
71    void operator ()(const Range &r) const
72    {
73        const int num_images = static_cast<int>(features.size());
74        for (int i = r.start; i < r.end; ++i)
75        {
76            int from = near_pairs[i].first;
77            int to = near_pairs[i].second;
78            int pair_idx = from*num_images + to;
79
80            matcher(features[from], features[to], pairwise_matches[pair_idx]);
81            pairwise_matches[pair_idx].src_img_idx = from;
82            pairwise_matches[pair_idx].dst_img_idx = to;
83
84            size_t dual_pair_idx = to*num_images + from;
85
86            pairwise_matches[dual_pair_idx] = pairwise_matches[pair_idx];
87            pairwise_matches[dual_pair_idx].src_img_idx = to;
88            pairwise_matches[dual_pair_idx].dst_img_idx = from;
89
90            if (!pairwise_matches[pair_idx].H.empty())
91                pairwise_matches[dual_pair_idx].H = pairwise_matches[pair_idx].H.inv();
92
93            for (size_t j = 0; j < pairwise_matches[dual_pair_idx].matches.size(); ++j)
94                std::swap(pairwise_matches[dual_pair_idx].matches[j].queryIdx,
95                          pairwise_matches[dual_pair_idx].matches[j].trainIdx);
96            LOG(".");
97        }
98    }
99
100    FeaturesMatcher &matcher;
101    const std::vector<ImageFeatures> &features;
102    std::vector<MatchesInfo> &pairwise_matches;
103    std::vector<std::pair<int,int> > &near_pairs;
104
105private:
106    void operator =(const MatchPairsBody&);
107};
108
109
110//////////////////////////////////////////////////////////////////////////////
111
112typedef std::set<std::pair<int,int> > MatchesSet;
113
114// These two classes are aimed to find features matches only, not to
115// estimate homography
116
117class CpuMatcher : public FeaturesMatcher
118{
119public:
120    CpuMatcher(float match_conf) : FeaturesMatcher(true), match_conf_(match_conf) {}
121    void match(const ImageFeatures &features1, const ImageFeatures &features2, MatchesInfo& matches_info);
122
123private:
124    float match_conf_;
125};
126
127#ifdef HAVE_OPENCV_CUDAFEATURES2D
128class GpuMatcher : public FeaturesMatcher
129{
130public:
131    GpuMatcher(float match_conf) : match_conf_(match_conf) {}
132    void match(const ImageFeatures &features1, const ImageFeatures &features2, MatchesInfo& matches_info);
133
134    void collectGarbage();
135
136private:
137    float match_conf_;
138    GpuMat descriptors1_, descriptors2_;
139    GpuMat train_idx_, distance_, all_dist_;
140    std::vector< std::vector<DMatch> > pair_matches;
141};
142#endif
143
144
145void CpuMatcher::match(const ImageFeatures &features1, const ImageFeatures &features2, MatchesInfo& matches_info)
146{
147    CV_Assert(features1.descriptors.type() == features2.descriptors.type());
148    CV_Assert(features2.descriptors.depth() == CV_8U || features2.descriptors.depth() == CV_32F);
149
150#ifdef HAVE_TEGRA_OPTIMIZATION
151    if (tegra::useTegra() && tegra::match2nearest(features1, features2, matches_info, match_conf_))
152        return;
153#endif
154
155    matches_info.matches.clear();
156
157    Ptr<cv::DescriptorMatcher> matcher;
158#if 0 // TODO check this
159    if (ocl::useOpenCL())
160    {
161        matcher = makePtr<BFMatcher>((int)NORM_L2);
162    }
163    else
164#endif
165    {
166        Ptr<flann::IndexParams> indexParams = makePtr<flann::KDTreeIndexParams>();
167        Ptr<flann::SearchParams> searchParams = makePtr<flann::SearchParams>();
168
169        if (features2.descriptors.depth() == CV_8U)
170        {
171            indexParams->setAlgorithm(cvflann::FLANN_INDEX_LSH);
172            searchParams->setAlgorithm(cvflann::FLANN_INDEX_LSH);
173        }
174
175        matcher = makePtr<FlannBasedMatcher>(indexParams, searchParams);
176    }
177    std::vector< std::vector<DMatch> > pair_matches;
178    MatchesSet matches;
179
180    // Find 1->2 matches
181    matcher->knnMatch(features1.descriptors, features2.descriptors, pair_matches, 2);
182    for (size_t i = 0; i < pair_matches.size(); ++i)
183    {
184        if (pair_matches[i].size() < 2)
185            continue;
186        const DMatch& m0 = pair_matches[i][0];
187        const DMatch& m1 = pair_matches[i][1];
188        if (m0.distance < (1.f - match_conf_) * m1.distance)
189        {
190            matches_info.matches.push_back(m0);
191            matches.insert(std::make_pair(m0.queryIdx, m0.trainIdx));
192        }
193    }
194    LOG("\n1->2 matches: " << matches_info.matches.size() << endl);
195
196    // Find 2->1 matches
197    pair_matches.clear();
198    matcher->knnMatch(features2.descriptors, features1.descriptors, pair_matches, 2);
199    for (size_t i = 0; i < pair_matches.size(); ++i)
200    {
201        if (pair_matches[i].size() < 2)
202            continue;
203        const DMatch& m0 = pair_matches[i][0];
204        const DMatch& m1 = pair_matches[i][1];
205        if (m0.distance < (1.f - match_conf_) * m1.distance)
206            if (matches.find(std::make_pair(m0.trainIdx, m0.queryIdx)) == matches.end())
207                matches_info.matches.push_back(DMatch(m0.trainIdx, m0.queryIdx, m0.distance));
208    }
209    LOG("1->2 & 2->1 matches: " << matches_info.matches.size() << endl);
210}
211
212#ifdef HAVE_OPENCV_CUDAFEATURES2D
213void GpuMatcher::match(const ImageFeatures &features1, const ImageFeatures &features2, MatchesInfo& matches_info)
214{
215    matches_info.matches.clear();
216
217    ensureSizeIsEnough(features1.descriptors.size(), features1.descriptors.type(), descriptors1_);
218    ensureSizeIsEnough(features2.descriptors.size(), features2.descriptors.type(), descriptors2_);
219
220    descriptors1_.upload(features1.descriptors);
221    descriptors2_.upload(features2.descriptors);
222
223    Ptr<cuda::DescriptorMatcher> matcher = cuda::DescriptorMatcher::createBFMatcher(NORM_L2);
224
225    MatchesSet matches;
226
227    // Find 1->2 matches
228    pair_matches.clear();
229    matcher->knnMatch(descriptors1_, descriptors2_, pair_matches, 2);
230    for (size_t i = 0; i < pair_matches.size(); ++i)
231    {
232        if (pair_matches[i].size() < 2)
233            continue;
234        const DMatch& m0 = pair_matches[i][0];
235        const DMatch& m1 = pair_matches[i][1];
236        if (m0.distance < (1.f - match_conf_) * m1.distance)
237        {
238            matches_info.matches.push_back(m0);
239            matches.insert(std::make_pair(m0.queryIdx, m0.trainIdx));
240        }
241    }
242
243    // Find 2->1 matches
244    pair_matches.clear();
245    matcher->knnMatch(descriptors2_, descriptors1_, pair_matches, 2);
246    for (size_t i = 0; i < pair_matches.size(); ++i)
247    {
248        if (pair_matches[i].size() < 2)
249            continue;
250        const DMatch& m0 = pair_matches[i][0];
251        const DMatch& m1 = pair_matches[i][1];
252        if (m0.distance < (1.f - match_conf_) * m1.distance)
253            if (matches.find(std::make_pair(m0.trainIdx, m0.queryIdx)) == matches.end())
254                matches_info.matches.push_back(DMatch(m0.trainIdx, m0.queryIdx, m0.distance));
255    }
256}
257
258void GpuMatcher::collectGarbage()
259{
260    descriptors1_.release();
261    descriptors2_.release();
262    train_idx_.release();
263    distance_.release();
264    all_dist_.release();
265    std::vector< std::vector<DMatch> >().swap(pair_matches);
266}
267#endif
268
269} // namespace
270
271
272namespace cv {
273namespace detail {
274
275void FeaturesFinder::operator ()(InputArray  image, ImageFeatures &features)
276{
277    find(image, features);
278    features.img_size = image.size();
279}
280
281
282void FeaturesFinder::operator ()(InputArray image, ImageFeatures &features, const std::vector<Rect> &rois)
283{
284    std::vector<ImageFeatures> roi_features(rois.size());
285    size_t total_kps_count = 0;
286    int total_descriptors_height = 0;
287
288    for (size_t i = 0; i < rois.size(); ++i)
289    {
290        find(image.getUMat()(rois[i]), roi_features[i]);
291        total_kps_count += roi_features[i].keypoints.size();
292        total_descriptors_height += roi_features[i].descriptors.rows;
293    }
294
295    features.img_size = image.size();
296    features.keypoints.resize(total_kps_count);
297    features.descriptors.create(total_descriptors_height,
298                                roi_features[0].descriptors.cols,
299                                roi_features[0].descriptors.type());
300
301    int kp_idx = 0;
302    int descr_offset = 0;
303    for (size_t i = 0; i < rois.size(); ++i)
304    {
305        for (size_t j = 0; j < roi_features[i].keypoints.size(); ++j, ++kp_idx)
306        {
307            features.keypoints[kp_idx] = roi_features[i].keypoints[j];
308            features.keypoints[kp_idx].pt.x += (float)rois[i].x;
309            features.keypoints[kp_idx].pt.y += (float)rois[i].y;
310        }
311        UMat subdescr = features.descriptors.rowRange(
312                descr_offset, descr_offset + roi_features[i].descriptors.rows);
313        roi_features[i].descriptors.copyTo(subdescr);
314        descr_offset += roi_features[i].descriptors.rows;
315    }
316}
317
318
319SurfFeaturesFinder::SurfFeaturesFinder(double hess_thresh, int num_octaves, int num_layers,
320                                       int num_octaves_descr, int num_layers_descr)
321{
322#ifdef HAVE_OPENCV_XFEATURES2D
323    if (num_octaves_descr == num_octaves && num_layers_descr == num_layers)
324    {
325        Ptr<SURF> surf_ = SURF::create();
326        if( !surf_ )
327            CV_Error( Error::StsNotImplemented, "OpenCV was built without SURF support" );
328        surf_->setHessianThreshold(hess_thresh);
329        surf_->setNOctaves(num_octaves);
330        surf_->setNOctaveLayers(num_layers);
331        surf = surf_;
332    }
333    else
334    {
335        Ptr<SURF> sdetector_ = SURF::create();
336        Ptr<SURF> sextractor_ = SURF::create();
337
338        if( !sdetector_ || !sextractor_ )
339            CV_Error( Error::StsNotImplemented, "OpenCV was built without SURF support" );
340
341        sdetector_->setHessianThreshold(hess_thresh);
342        sdetector_->setNOctaves(num_octaves);
343        sdetector_->setNOctaveLayers(num_layers);
344
345        sextractor_->setNOctaves(num_octaves_descr);
346        sextractor_->setNOctaveLayers(num_layers_descr);
347
348        detector_ = sdetector_;
349        extractor_ = sextractor_;
350    }
351#else
352    (void)hess_thresh;
353    (void)num_octaves;
354    (void)num_layers;
355    (void)num_octaves_descr;
356    (void)num_layers_descr;
357    CV_Error( Error::StsNotImplemented, "OpenCV was built without SURF support" );
358#endif
359}
360
361void SurfFeaturesFinder::find(InputArray image, ImageFeatures &features)
362{
363    UMat gray_image;
364    CV_Assert((image.type() == CV_8UC3) || (image.type() == CV_8UC1));
365    if(image.type() == CV_8UC3)
366    {
367        cvtColor(image, gray_image, COLOR_BGR2GRAY);
368    }
369    else
370    {
371        gray_image = image.getUMat();
372    }
373    if (!surf)
374    {
375        detector_->detect(gray_image, features.keypoints);
376        extractor_->compute(gray_image, features.keypoints, features.descriptors);
377    }
378    else
379    {
380        UMat descriptors;
381        surf->detectAndCompute(gray_image, Mat(), features.keypoints, descriptors);
382        features.descriptors = descriptors.reshape(1, (int)features.keypoints.size());
383    }
384}
385
386OrbFeaturesFinder::OrbFeaturesFinder(Size _grid_size, int n_features, float scaleFactor, int nlevels)
387{
388    grid_size = _grid_size;
389    orb = ORB::create(n_features * (99 + grid_size.area())/100/grid_size.area(), scaleFactor, nlevels);
390}
391
392void OrbFeaturesFinder::find(InputArray image, ImageFeatures &features)
393{
394    UMat gray_image;
395
396    CV_Assert((image.type() == CV_8UC3) || (image.type() == CV_8UC4) || (image.type() == CV_8UC1));
397
398    if (image.type() == CV_8UC3) {
399        cvtColor(image, gray_image, COLOR_BGR2GRAY);
400    } else if (image.type() == CV_8UC4) {
401        cvtColor(image, gray_image, COLOR_BGRA2GRAY);
402    } else if (image.type() == CV_8UC1) {
403        gray_image = image.getUMat();
404    } else {
405        CV_Error(Error::StsUnsupportedFormat, "");
406    }
407
408    if (grid_size.area() == 1)
409        orb->detectAndCompute(gray_image, Mat(), features.keypoints, features.descriptors);
410    else
411    {
412        features.keypoints.clear();
413        features.descriptors.release();
414
415        std::vector<KeyPoint> points;
416        Mat _descriptors;
417        UMat descriptors;
418
419        for (int r = 0; r < grid_size.height; ++r)
420            for (int c = 0; c < grid_size.width; ++c)
421            {
422                int xl = c * gray_image.cols / grid_size.width;
423                int yl = r * gray_image.rows / grid_size.height;
424                int xr = (c+1) * gray_image.cols / grid_size.width;
425                int yr = (r+1) * gray_image.rows / grid_size.height;
426
427                // LOGLN("OrbFeaturesFinder::find: gray_image.empty=" << (gray_image.empty()?"true":"false") << ", "
428                //     << " gray_image.size()=(" << gray_image.size().width << "x" << gray_image.size().height << "), "
429                //     << " yl=" << yl << ", yr=" << yr << ", "
430                //     << " xl=" << xl << ", xr=" << xr << ", gray_image.data=" << ((size_t)gray_image.data) << ", "
431                //     << "gray_image.dims=" << gray_image.dims << "\n");
432
433                UMat gray_image_part=gray_image(Range(yl, yr), Range(xl, xr));
434                // LOGLN("OrbFeaturesFinder::find: gray_image_part.empty=" << (gray_image_part.empty()?"true":"false") << ", "
435                //     << " gray_image_part.size()=(" << gray_image_part.size().width << "x" << gray_image_part.size().height << "), "
436                //     << " gray_image_part.dims=" << gray_image_part.dims << ", "
437                //     << " gray_image_part.data=" << ((size_t)gray_image_part.data) << "\n");
438
439                orb->detectAndCompute(gray_image_part, UMat(), points, descriptors);
440
441                features.keypoints.reserve(features.keypoints.size() + points.size());
442                for (std::vector<KeyPoint>::iterator kp = points.begin(); kp != points.end(); ++kp)
443                {
444                    kp->pt.x += xl;
445                    kp->pt.y += yl;
446                    features.keypoints.push_back(*kp);
447                }
448                _descriptors.push_back(descriptors.getMat(ACCESS_READ));
449            }
450
451        // TODO optimize copyTo()
452        //features.descriptors = _descriptors.getUMat(ACCESS_READ);
453        _descriptors.copyTo(features.descriptors);
454    }
455}
456
457#ifdef HAVE_OPENCV_XFEATURES2D
458SurfFeaturesFinderGpu::SurfFeaturesFinderGpu(double hess_thresh, int num_octaves, int num_layers,
459                                             int num_octaves_descr, int num_layers_descr)
460{
461    surf_.keypointsRatio = 0.1f;
462    surf_.hessianThreshold = hess_thresh;
463    surf_.extended = false;
464    num_octaves_ = num_octaves;
465    num_layers_ = num_layers;
466    num_octaves_descr_ = num_octaves_descr;
467    num_layers_descr_ = num_layers_descr;
468}
469
470
471void SurfFeaturesFinderGpu::find(InputArray image, ImageFeatures &features)
472{
473    CV_Assert(image.depth() == CV_8U);
474
475    ensureSizeIsEnough(image.size(), image.type(), image_);
476    image_.upload(image);
477
478    ensureSizeIsEnough(image.size(), CV_8UC1, gray_image_);
479    cvtColor(image_, gray_image_, COLOR_BGR2GRAY);
480
481    surf_.nOctaves = num_octaves_;
482    surf_.nOctaveLayers = num_layers_;
483    surf_.upright = false;
484    surf_(gray_image_, GpuMat(), keypoints_);
485
486    surf_.nOctaves = num_octaves_descr_;
487    surf_.nOctaveLayers = num_layers_descr_;
488    surf_.upright = true;
489    surf_(gray_image_, GpuMat(), keypoints_, descriptors_, true);
490    surf_.downloadKeypoints(keypoints_, features.keypoints);
491
492    descriptors_.download(features.descriptors);
493}
494
495void SurfFeaturesFinderGpu::collectGarbage()
496{
497    surf_.releaseMemory();
498    image_.release();
499    gray_image_.release();
500    keypoints_.release();
501    descriptors_.release();
502}
503#endif
504
505
506//////////////////////////////////////////////////////////////////////////////
507
508MatchesInfo::MatchesInfo() : src_img_idx(-1), dst_img_idx(-1), num_inliers(0), confidence(0) {}
509
510MatchesInfo::MatchesInfo(const MatchesInfo &other) { *this = other; }
511
512const MatchesInfo& MatchesInfo::operator =(const MatchesInfo &other)
513{
514    src_img_idx = other.src_img_idx;
515    dst_img_idx = other.dst_img_idx;
516    matches = other.matches;
517    inliers_mask = other.inliers_mask;
518    num_inliers = other.num_inliers;
519    H = other.H.clone();
520    confidence = other.confidence;
521    return *this;
522}
523
524
525//////////////////////////////////////////////////////////////////////////////
526
527void FeaturesMatcher::operator ()(const std::vector<ImageFeatures> &features, std::vector<MatchesInfo> &pairwise_matches,
528                                  const UMat &mask)
529{
530    const int num_images = static_cast<int>(features.size());
531
532    CV_Assert(mask.empty() || (mask.type() == CV_8U && mask.cols == num_images && mask.rows));
533    Mat_<uchar> mask_(mask.getMat(ACCESS_READ));
534    if (mask_.empty())
535        mask_ = Mat::ones(num_images, num_images, CV_8U);
536
537    std::vector<std::pair<int,int> > near_pairs;
538    for (int i = 0; i < num_images - 1; ++i)
539        for (int j = i + 1; j < num_images; ++j)
540            if (features[i].keypoints.size() > 0 && features[j].keypoints.size() > 0 && mask_(i, j))
541                near_pairs.push_back(std::make_pair(i, j));
542
543    pairwise_matches.resize(num_images * num_images);
544    MatchPairsBody body(*this, features, pairwise_matches, near_pairs);
545
546    if (is_thread_safe_)
547        parallel_for_(Range(0, static_cast<int>(near_pairs.size())), body);
548    else
549        body(Range(0, static_cast<int>(near_pairs.size())));
550    LOGLN_CHAT("");
551}
552
553
554//////////////////////////////////////////////////////////////////////////////
555
556BestOf2NearestMatcher::BestOf2NearestMatcher(bool try_use_gpu, float match_conf, int num_matches_thresh1, int num_matches_thresh2)
557{
558    (void)try_use_gpu;
559
560#ifdef HAVE_OPENCV_CUDAFEATURES2D
561    if (try_use_gpu && getCudaEnabledDeviceCount() > 0)
562    {
563        impl_ = makePtr<GpuMatcher>(match_conf);
564    }
565    else
566#endif
567    {
568        impl_ = makePtr<CpuMatcher>(match_conf);
569    }
570
571    is_thread_safe_ = impl_->isThreadSafe();
572    num_matches_thresh1_ = num_matches_thresh1;
573    num_matches_thresh2_ = num_matches_thresh2;
574}
575
576
577void BestOf2NearestMatcher::match(const ImageFeatures &features1, const ImageFeatures &features2,
578                                  MatchesInfo &matches_info)
579{
580    (*impl_)(features1, features2, matches_info);
581
582    // Check if it makes sense to find homography
583    if (matches_info.matches.size() < static_cast<size_t>(num_matches_thresh1_))
584        return;
585
586    // Construct point-point correspondences for homography estimation
587    Mat src_points(1, static_cast<int>(matches_info.matches.size()), CV_32FC2);
588    Mat dst_points(1, static_cast<int>(matches_info.matches.size()), CV_32FC2);
589    for (size_t i = 0; i < matches_info.matches.size(); ++i)
590    {
591        const DMatch& m = matches_info.matches[i];
592
593        Point2f p = features1.keypoints[m.queryIdx].pt;
594        p.x -= features1.img_size.width * 0.5f;
595        p.y -= features1.img_size.height * 0.5f;
596        src_points.at<Point2f>(0, static_cast<int>(i)) = p;
597
598        p = features2.keypoints[m.trainIdx].pt;
599        p.x -= features2.img_size.width * 0.5f;
600        p.y -= features2.img_size.height * 0.5f;
601        dst_points.at<Point2f>(0, static_cast<int>(i)) = p;
602    }
603
604    // Find pair-wise motion
605    matches_info.H = findHomography(src_points, dst_points, matches_info.inliers_mask, RANSAC);
606    if (matches_info.H.empty() || std::abs(determinant(matches_info.H)) < std::numeric_limits<double>::epsilon())
607        return;
608
609    // Find number of inliers
610    matches_info.num_inliers = 0;
611    for (size_t i = 0; i < matches_info.inliers_mask.size(); ++i)
612        if (matches_info.inliers_mask[i])
613            matches_info.num_inliers++;
614
615    // These coeffs are from paper M. Brown and D. Lowe. "Automatic Panoramic Image Stitching
616    // using Invariant Features"
617    matches_info.confidence = matches_info.num_inliers / (8 + 0.3 * matches_info.matches.size());
618
619    // Set zero confidence to remove matches between too close images, as they don't provide
620    // additional information anyway. The threshold was set experimentally.
621    matches_info.confidence = matches_info.confidence > 3. ? 0. : matches_info.confidence;
622
623    // Check if we should try to refine motion
624    if (matches_info.num_inliers < num_matches_thresh2_)
625        return;
626
627    // Construct point-point correspondences for inliers only
628    src_points.create(1, matches_info.num_inliers, CV_32FC2);
629    dst_points.create(1, matches_info.num_inliers, CV_32FC2);
630    int inlier_idx = 0;
631    for (size_t i = 0; i < matches_info.matches.size(); ++i)
632    {
633        if (!matches_info.inliers_mask[i])
634            continue;
635
636        const DMatch& m = matches_info.matches[i];
637
638        Point2f p = features1.keypoints[m.queryIdx].pt;
639        p.x -= features1.img_size.width * 0.5f;
640        p.y -= features1.img_size.height * 0.5f;
641        src_points.at<Point2f>(0, inlier_idx) = p;
642
643        p = features2.keypoints[m.trainIdx].pt;
644        p.x -= features2.img_size.width * 0.5f;
645        p.y -= features2.img_size.height * 0.5f;
646        dst_points.at<Point2f>(0, inlier_idx) = p;
647
648        inlier_idx++;
649    }
650
651    // Rerun motion estimation on inliers only
652    matches_info.H = findHomography(src_points, dst_points, RANSAC);
653}
654
655void BestOf2NearestMatcher::collectGarbage()
656{
657    impl_->collectGarbage();
658}
659
660
661BestOf2NearestRangeMatcher::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)
662{
663    range_width_ = range_width;
664}
665
666
667void BestOf2NearestRangeMatcher::operator ()(const std::vector<ImageFeatures> &features, std::vector<MatchesInfo> &pairwise_matches,
668                                  const UMat &mask)
669{
670    const int num_images = static_cast<int>(features.size());
671
672    CV_Assert(mask.empty() || (mask.type() == CV_8U && mask.cols == num_images && mask.rows));
673    Mat_<uchar> mask_(mask.getMat(ACCESS_READ));
674    if (mask_.empty())
675        mask_ = Mat::ones(num_images, num_images, CV_8U);
676
677    std::vector<std::pair<int,int> > near_pairs;
678    for (int i = 0; i < num_images - 1; ++i)
679        for (int j = i + 1; j < std::min(num_images, i + range_width_); ++j)
680            if (features[i].keypoints.size() > 0 && features[j].keypoints.size() > 0 && mask_(i, j))
681                near_pairs.push_back(std::make_pair(i, j));
682
683    pairwise_matches.resize(num_images * num_images);
684    MatchPairsBody body(*this, features, pairwise_matches, near_pairs);
685
686    if (is_thread_safe_)
687        parallel_for_(Range(0, static_cast<int>(near_pairs.size())), body);
688    else
689        body(Range(0, static_cast<int>(near_pairs.size())));
690    LOGLN_CHAT("");
691}
692
693
694} // namespace detail
695} // namespace cv
696