perf_stich.cpp 6.87 KB
Newer Older
wester committed
1
#include "perf_precomp.hpp"
wester committed
2 3 4
#include "opencv2/highgui/highgui.hpp"
#include "opencv2/core/internal.hpp"
#include "opencv2/flann/flann.hpp"
wester committed
5 6 7 8 9
#include "opencv2/opencv_modules.hpp"

using namespace std;
using namespace cv;
using namespace perf;
a  
Kai Westerkamp committed
10
using std::tr1::make_tuple;
wester committed
11 12 13 14 15 16
using std::tr1::get;

#define SURF_MATCH_CONFIDENCE 0.65f
#define ORB_MATCH_CONFIDENCE  0.3f
#define WORK_MEGAPIX 0.6

wester committed
17 18 19
typedef TestBaseWithParam<String> stitch;
typedef TestBaseWithParam<String> match;
typedef std::tr1::tuple<String, int> matchVector_t;
a  
Kai Westerkamp committed
20
typedef TestBaseWithParam<matchVector_t> matchVector;
wester committed
21

wester committed
22
#ifdef HAVE_OPENCV_NONFREE_TODO_FIND_WHY_SURF_IS_NOT_ABLE_TO_STITCH_PANOS
a  
Kai Westerkamp committed
23
#define TEST_DETECTORS testing::Values("surf", "orb")
wester committed
24
#else
wester committed
25
#define TEST_DETECTORS testing::Values<String>("orb")
wester committed
26 27 28 29 30 31 32 33 34 35 36
#endif

PERF_TEST_P(stitch, a123, TEST_DETECTORS)
{
    Mat pano;

    vector<Mat> imgs;
    imgs.push_back( imread( getDataPath("stitching/a1.png") ) );
    imgs.push_back( imread( getDataPath("stitching/a2.png") ) );
    imgs.push_back( imread( getDataPath("stitching/a3.png") ) );

a  
Kai Westerkamp committed
37
    Ptr<detail::FeaturesFinder> featuresFinder = GetParam() == "orb"
wester committed
38 39
            ? (detail::FeaturesFinder*)new detail::OrbFeaturesFinder()
            : (detail::FeaturesFinder*)new detail::SurfFeaturesFinder();
wester committed
40 41

    Ptr<detail::FeaturesMatcher> featuresMatcher = GetParam() == "orb"
wester committed
42 43
            ? new detail::BestOf2NearestMatcher(false, ORB_MATCH_CONFIDENCE)
            : new detail::BestOf2NearestMatcher(false, SURF_MATCH_CONFIDENCE);
wester committed
44 45 46 47 48 49 50 51

    declare.time(30 * 20).iterations(20);

    while(next())
    {
        Stitcher stitcher = Stitcher::createDefault();
        stitcher.setFeaturesFinder(featuresFinder);
        stitcher.setFeaturesMatcher(featuresMatcher);
wester committed
52
        stitcher.setWarper(new SphericalWarper());
wester committed
53 54 55 56 57 58 59
        stitcher.setRegistrationResol(WORK_MEGAPIX);

        startTimer();
        stitcher.stitch(imgs, pano);
        stopTimer();
    }

wester committed
60 61 62
    Mat pano_small;
    if (!pano.empty())
        resize(pano, pano_small, Size(320, 240), 0, 0, INTER_AREA);
wester committed
63

wester committed
64
    SANITY_CHECK(pano_small, 5);
wester committed
65 66 67 68 69 70 71 72 73 74
}

PERF_TEST_P(stitch, b12, TEST_DETECTORS)
{
    Mat pano;

    vector<Mat> imgs;
    imgs.push_back( imread( getDataPath("stitching/b1.png") ) );
    imgs.push_back( imread( getDataPath("stitching/b2.png") ) );

a  
Kai Westerkamp committed
75
    Ptr<detail::FeaturesFinder> featuresFinder = GetParam() == "orb"
wester committed
76 77
            ? (detail::FeaturesFinder*)new detail::OrbFeaturesFinder()
            : (detail::FeaturesFinder*)new detail::SurfFeaturesFinder();
wester committed
78 79

    Ptr<detail::FeaturesMatcher> featuresMatcher = GetParam() == "orb"
wester committed
80 81
            ? new detail::BestOf2NearestMatcher(false, ORB_MATCH_CONFIDENCE)
            : new detail::BestOf2NearestMatcher(false, SURF_MATCH_CONFIDENCE);
wester committed
82 83 84 85 86 87 88 89

    declare.time(30 * 20).iterations(20);

    while(next())
    {
        Stitcher stitcher = Stitcher::createDefault();
        stitcher.setFeaturesFinder(featuresFinder);
        stitcher.setFeaturesMatcher(featuresMatcher);
wester committed
90
        stitcher.setWarper(new SphericalWarper());
wester committed
91 92 93 94 95 96 97
        stitcher.setRegistrationResol(WORK_MEGAPIX);

        startTimer();
        stitcher.stitch(imgs, pano);
        stopTimer();
    }

a  
Kai Westerkamp committed
98 99 100
    Mat pano_small;
    if (!pano.empty())
        resize(pano, pano_small, Size(320, 240), 0, 0, INTER_AREA);
wester committed
101

a  
Kai Westerkamp committed
102
    SANITY_CHECK(pano_small, 5);
wester committed
103 104
}

a  
Kai Westerkamp committed
105
PERF_TEST_P( match, bestOf2Nearest, TEST_DETECTORS)
wester committed
106
{
a  
Kai Westerkamp committed
107 108 109 110 111 112 113 114 115 116 117
    Mat img1, img1_full = imread( getDataPath("stitching/b1.png") );
    Mat img2, img2_full = imread( getDataPath("stitching/b2.png") );
    float scale1 = (float)std::min(1.0, sqrt(WORK_MEGAPIX * 1e6 / img1_full.total()));
    float scale2 = (float)std::min(1.0, sqrt(WORK_MEGAPIX * 1e6 / img2_full.total()));
    resize(img1_full, img1, Size(), scale1, scale1);
    resize(img2_full, img2, Size(), scale2, scale2);

    Ptr<detail::FeaturesFinder> finder;
    Ptr<detail::FeaturesMatcher> matcher;
    if (GetParam() == "surf")
    {
wester committed
118 119
        finder = new detail::SurfFeaturesFinder();
        matcher = new detail::BestOf2NearestMatcher(false, SURF_MATCH_CONFIDENCE);
a  
Kai Westerkamp committed
120 121 122
    }
    else if (GetParam() == "orb")
    {
wester committed
123 124
        finder = new detail::OrbFeaturesFinder();
        matcher = new detail::BestOf2NearestMatcher(false, ORB_MATCH_CONFIDENCE);
a  
Kai Westerkamp committed
125 126 127 128 129
    }
    else
    {
        FAIL() << "Unknown 2D features type: " << GetParam();
    }
wester committed
130

a  
Kai Westerkamp committed
131 132 133
    detail::ImageFeatures features1, features2;
    (*finder)(img1, features1);
    (*finder)(img2, features2);
wester committed
134

a  
Kai Westerkamp committed
135 136 137 138 139
    detail::MatchesInfo pairwise_matches;

    declare.in(features1.descriptors, features2.descriptors);

    while(next())
wester committed
140
    {
a  
Kai Westerkamp committed
141 142 143 144 145
        cvflann::seed_random(42);//for predictive FlannBasedMatcher
        startTimer();
        (*matcher)(features1, features2, pairwise_matches);
        stopTimer();
        matcher->collectGarbage();
wester committed
146
    }
a  
Kai Westerkamp committed
147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168

    std::vector<DMatch>& matches = pairwise_matches.matches;
    if (GetParam() == "orb") matches.resize(0);
    for(size_t q = 0; q < matches.size(); ++q)
        if (matches[q].imgIdx < 0) { matches.resize(q); break;}
    SANITY_CHECK_MATCHES(matches);
}

PERF_TEST_P( matchVector, bestOf2NearestVectorFeatures, testing::Combine(
                 TEST_DETECTORS,
                 testing::Values(2, 4, 8))
             )
{
    Mat img1, img1_full = imread( getDataPath("stitching/b1.png") );
    Mat img2, img2_full = imread( getDataPath("stitching/b2.png") );
    float scale1 = (float)std::min(1.0, sqrt(WORK_MEGAPIX * 1e6 / img1_full.total()));
    float scale2 = (float)std::min(1.0, sqrt(WORK_MEGAPIX * 1e6 / img2_full.total()));
    resize(img1_full, img1, Size(), scale1, scale1);
    resize(img2_full, img2, Size(), scale2, scale2);

    Ptr<detail::FeaturesFinder> finder;
    Ptr<detail::FeaturesMatcher> matcher;
wester committed
169
    String detectorName = get<0>(GetParam());
a  
Kai Westerkamp committed
170 171
    int featuresVectorSize = get<1>(GetParam());
    if (detectorName == "surf")
wester committed
172
    {
wester committed
173 174
        finder = new detail::SurfFeaturesFinder();
        matcher = new detail::BestOf2NearestMatcher(false, SURF_MATCH_CONFIDENCE);
wester committed
175
    }
a  
Kai Westerkamp committed
176
    else if (detectorName == "orb")
wester committed
177
    {
wester committed
178 179
        finder = new detail::OrbFeaturesFinder();
        matcher = new detail::BestOf2NearestMatcher(false, ORB_MATCH_CONFIDENCE);
wester committed
180
    }
a  
Kai Westerkamp committed
181
    else
wester committed
182
    {
a  
Kai Westerkamp committed
183
        FAIL() << "Unknown 2D features type: " << get<0>(GetParam());
wester committed
184 185
    }

a  
Kai Westerkamp committed
186 187 188 189 190 191 192 193 194 195
    detail::ImageFeatures features1, features2;
    (*finder)(img1, features1);
    (*finder)(img2, features2);
    vector<detail::ImageFeatures> features;
    vector<detail::MatchesInfo> pairwise_matches;
    for(int i = 0; i < featuresVectorSize/2; i++)
    {
        features.push_back(features1);
        features.push_back(features2);
    }
wester committed
196

a  
Kai Westerkamp committed
197
    declare.time(200);
wester committed
198 199
    while(next())
    {
a  
Kai Westerkamp committed
200
        cvflann::seed_random(42);//for predictive FlannBasedMatcher
wester committed
201
        startTimer();
a  
Kai Westerkamp committed
202
        (*matcher)(features, pairwise_matches);
wester committed
203
        stopTimer();
a  
Kai Westerkamp committed
204
        matcher->collectGarbage();
wester committed
205 206 207
    }


a  
Kai Westerkamp committed
208 209 210 211
    std::vector<DMatch>& matches = pairwise_matches[detectorName == "surf" ? 1 : 0].matches;
    for(size_t q = 0; q < matches.size(); ++q)
        if (matches[q].imgIdx < 0) { matches.resize(q); break;}
    SANITY_CHECK_MATCHES(matches);
wester committed
212
}