Skip to content

Commit 6fe1898

Browse files
committed
Merge pull request opencv#8294 from alalek:fix_stitching_failure
2 parents 990e87e + e65c627 commit 6fe1898

File tree

5 files changed

+42
-3
lines changed

5 files changed

+42
-3
lines changed

modules/flann/include/opencv2/flann/kdtree_index.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,12 @@ class KDTreeIndex : public NNIndex<Distance>
125125
/* Construct the randomized trees. */
126126
for (int i = 0; i < trees_; i++) {
127127
/* Randomize the order of vectors to allow for unbiased sampling. */
128+
#ifndef OPENCV_FLANN_USE_STD_RAND
129+
cv::randShuffle(vind_);
130+
#else
128131
std::random_shuffle(vind_.begin(), vind_.end());
132+
#endif
133+
129134
tree_roots_[i] = divideTree(&vind_[0], int(size_) );
130135
}
131136
}

modules/flann/include/opencv2/flann/lsh_table.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -350,7 +350,11 @@ inline LshTable<unsigned char>::LshTable(unsigned int feature_size, unsigned int
350350
// A bit brutal but fast to code
351351
std::vector<size_t> indices(feature_size * CHAR_BIT);
352352
for (size_t i = 0; i < feature_size * CHAR_BIT; ++i) indices[i] = i;
353+
#ifndef OPENCV_FLANN_USE_STD_RAND
354+
cv::randShuffle(indices);
355+
#else
353356
std::random_shuffle(indices.begin(), indices.end());
357+
#endif
354358

355359
// Generate a random set of order of subsignature_size_ bits
356360
for (unsigned int i = 0; i < key_size_; ++i) {

modules/flann/include/opencv2/flann/random.h

Lines changed: 25 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -40,13 +40,31 @@
4040
namespace cvflann
4141
{
4242

43+
inline int rand()
44+
{
45+
#ifndef OPENCV_FLANN_USE_STD_RAND
46+
# if INT_MAX == RAND_MAX
47+
int v = cv::theRNG().next() & INT_MAX;
48+
# else
49+
int v = cv::theRNG().uniform(0, RAND_MAX + 1);
50+
# endif
51+
#else
52+
int v = std::rand();
53+
#endif // OPENCV_FLANN_USE_STD_RAND
54+
return v;
55+
}
56+
4357
/**
4458
* Seeds the random number generator
4559
* @param seed Random seed
4660
*/
4761
inline void seed_random(unsigned int seed)
4862
{
49-
srand(seed);
63+
#ifndef OPENCV_FLANN_USE_STD_RAND
64+
cv::theRNG() = cv::RNG(seed);
65+
#else
66+
std::srand(seed);
67+
#endif
5068
}
5169

5270
/*
@@ -60,7 +78,7 @@ inline void seed_random(unsigned int seed)
6078
*/
6179
inline double rand_double(double high = 1.0, double low = 0)
6280
{
63-
return low + ((high-low) * (std::rand() / (RAND_MAX + 1.0)));
81+
return low + ((high-low) * (rand() / (RAND_MAX + 1.0)));
6482
}
6583

6684
/**
@@ -71,7 +89,7 @@ inline double rand_double(double high = 1.0, double low = 0)
7189
*/
7290
inline int rand_int(int high = RAND_MAX, int low = 0)
7391
{
74-
return low + (int) ( double(high-low) * (std::rand() / (RAND_MAX + 1.0)));
92+
return low + (int) ( double(high-low) * (rand() / (RAND_MAX + 1.0)));
7593
}
7694

7795
/**
@@ -107,7 +125,11 @@ class UniqueRandom
107125
for (int i = 0; i < size_; ++i) vals_[i] = i;
108126

109127
// shuffle the elements in the array
128+
#ifndef OPENCV_FLANN_USE_STD_RAND
129+
cv::randShuffle(vals_);
130+
#else
110131
std::random_shuffle(vals_.begin(), vals_.end());
132+
#endif
111133

112134
counter_ = 0;
113135
}

modules/stitching/perf/perf_stich.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22
#include "opencv2/imgcodecs.hpp"
33
#include "opencv2/opencv_modules.hpp"
44

5+
#include "opencv2/core/ocl.hpp"
6+
57
using namespace std;
68
using namespace cv;
79
using namespace perf;
@@ -161,6 +163,9 @@ PERF_TEST_P(stitchDatasets, affine, testing::Combine(AFFINE_DATASETS, TEST_DETEC
161163
Ptr<Stitcher> stitcher = Stitcher::create(Stitcher::SCANS, false);
162164
stitcher->setFeaturesFinder(featuresFinder);
163165

166+
if (cv::ocl::useOpenCL())
167+
cv::theRNG() = cv::RNG(12345); // prevent fails of Windows OpenCL builds (see #8294)
168+
164169
startTimer();
165170
stitcher->stitch(imgs, pano);
166171
stopTimer();

modules/stitching/src/matchers.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,9 +70,12 @@ struct MatchPairsBody : ParallelLoopBody
7070

7171
void operator ()(const Range &r) const
7272
{
73+
cv::RNG rng = cv::theRNG(); // save entry rng state
7374
const int num_images = static_cast<int>(features.size());
7475
for (int i = r.start; i < r.end; ++i)
7576
{
77+
cv::theRNG() = cv::RNG(rng.state + i); // force "stable" RNG seed for each processed pair
78+
7679
int from = near_pairs[i].first;
7780
int to = near_pairs[i].second;
7881
int pair_idx = from*num_images + to;

0 commit comments

Comments
 (0)