Merge pull request #796 from SpecLad:more-backports

pull/802/head
Andrey Kamaev 12 years ago committed by OpenCV Buildbot
commit ca56e99a62
  1. 36
      modules/core/include/opencv2/core/core.hpp
  2. 244
      modules/core/src/glob.cpp
  3. 125
      modules/core/src/rand.cpp
  4. 17
      modules/core/test/test_io.cpp
  5. 26
      modules/core/test/test_rand.cpp

@ -225,6 +225,8 @@ CV_EXPORTS ErrorCallback redirectError( ErrorCallback errCallback,
#define CV_DbgAssert(expr) #define CV_DbgAssert(expr)
#endif #endif
CV_EXPORTS void glob(String pattern, std::vector<String>& result, bool recursive = false);
CV_EXPORTS void setNumThreads(int nthreads); CV_EXPORTS void setNumThreads(int nthreads);
CV_EXPORTS int getNumThreads(); CV_EXPORTS int getNumThreads();
CV_EXPORTS int getThreadNum(); CV_EXPORTS int getThreadNum();
@ -2042,6 +2044,40 @@ public:
uint64 state; uint64 state;
}; };
/*!
Random Number Generator - MT
The class implements RNG using the Mersenne Twister algorithm
*/
class CV_EXPORTS RNG_MT19937
{
public:
RNG_MT19937();
RNG_MT19937(unsigned s);
void seed(unsigned s);
unsigned next();
operator int();
operator unsigned();
operator float();
operator double();
unsigned operator ()(unsigned N);
unsigned operator ()();
//! returns uniformly distributed integer random number from [a,b) range
int uniform(int a, int b);
//! returns uniformly distributed floating-point random number from [a,b) range
float uniform(float a, float b);
//! returns uniformly distributed double-precision floating-point random number from [a,b) range
double uniform(double a, double b);
private:
enum PeriodParameters {N = 624, M = 397};
unsigned state[N];
int mti;
};
/*! /*!
Termination criteria in iterative algorithms Termination criteria in iterative algorithms

@ -0,0 +1,244 @@
/*M///////////////////////////////////////////////////////////////////////////////////////
//
// IMPORTANT: READ BEFORE DOWNLOADING, COPYING, INSTALLING OR USING.
//
// By downloading, copying, installing or using the software you agree to this license.
// If you do not agree to this license, do not download, install,
// copy or use the software.
//
//
// License Agreement
// For Open Source Computer Vision Library
//
// Copyright (C) 2000-2008, Intel Corporation, all rights reserved.
// Copyright (C) 2008-2013, Willow Garage Inc., all rights reserved.
// Third party copyrights are property of their respective owners.
//
// Redistribution and use in source and binary forms, with or without modification,
// are permitted provided that the following conditions are met:
//
// * Redistribution's of source code must retain the above copyright notice,
// this list of conditions and the following disclaimer.
//
// * Redistribution's in binary form must reproduce the above copyright notice,
// this list of conditions and the following disclaimer in the documentation
// and / or other materials provided with the distribution.
//
// * The name of the copyright holders may not be used to endorse or promote products
// derived from this software without specific prior written permission.
//
// This software is provided by the copyright holders and contributors "as is" and
// any express or implied warranties, including, but not limited to, the implied
// warranties of merchantability and fitness for a particular purpose are disclaimed.
// In no event shall the Intel Corporation or contributors be liable for any direct,
// indirect, incidental, special, exemplary, or consequential damages
// (including, but not limited to, procurement of substitute goods or services;
// loss of use, data, or profits; or business interruption) however caused
// and on any theory of liability, whether in contract, strict liability,
// or tort (including negligence or otherwise) arising in any way out of
// the use of this software, even if advised of the possibility of such damage.
//
//M*/
#include "precomp.hpp"
#if defined WIN32 || defined _WIN32 || defined WINCE
# include <windows.h>
const char dir_separators[] = "/\\";
const char native_separator = '\\';
namespace
{
struct dirent
{
const char* d_name;
};
struct DIR
{
WIN32_FIND_DATA data;
HANDLE handle;
dirent ent;
};
DIR* opendir(const char* path)
{
DIR* dir = new DIR;
dir->ent.d_name = 0;
dir->handle = ::FindFirstFileA((cv::String(path) + "\\*").c_str(), &dir->data);
if(dir->handle == INVALID_HANDLE_VALUE)
{
/*closedir will do all cleanup*/
return 0;
}
return dir;
}
dirent* readdir(DIR* dir)
{
if (dir->ent.d_name != 0)
{
if (::FindNextFile(dir->handle, &dir->data) != TRUE)
return 0;
}
dir->ent.d_name = dir->data.cFileName;
return &dir->ent;
}
void closedir(DIR* dir)
{
::FindClose(dir->handle);
delete dir;
}
}
#else
# include <dirent.h>
# include <sys/stat.h>
const char dir_separators[] = "/";
const char native_separator = '/';
#endif
static bool isDir(const cv::String& path, DIR* dir)
{
#if defined WIN32 || defined _WIN32 || defined WINCE
DWORD attributes;
if (dir)
attributes = dir->data.dwFileAttributes;
else
attributes = ::GetFileAttributes(path.c_str());
return (attributes != INVALID_FILE_ATTRIBUTES) && ((attributes & FILE_ATTRIBUTE_DIRECTORY) != 0);
#else
(void)dir;
struct stat stat_buf;
if (0 != stat( path.c_str(), &stat_buf))
return false;
int is_dir = S_ISDIR( stat_buf.st_mode);
return is_dir != 0;
#endif
}
static bool wildcmp(const char *string, const char *wild)
{
// Based on wildcmp written by Jack Handy - <A href="mailto:jakkhandy@hotmail.com">jakkhandy@hotmail.com</A>
const char *cp = 0, *mp = 0;
while ((*string) && (*wild != '*'))
{
if ((*wild != *string) && (*wild != '?'))
{
return false;
}
wild++;
string++;
}
while (*string)
{
if (*wild == '*')
{
if (!*++wild)
{
return true;
}
mp = wild;
cp = string + 1;
}
else if ((*wild == *string) || (*wild == '?'))
{
wild++;
string++;
}
else
{
wild = mp;
string = cp++;
}
}
while (*wild == '*')
{
wild++;
}
return *wild == 0;
}
static void glob_rec(const cv::String& directory, const cv::String& wildchart, std::vector<cv::String>& result, bool recursive)
{
DIR *dir;
struct dirent *ent;
if ((dir = opendir (directory.c_str())) != 0)
{
/* find all the files and directories within directory */
try
{
while ((ent = readdir (dir)) != 0)
{
const char* name = ent->d_name;
if((name[0] == 0) || (name[0] == '.' && name[1] == 0) || (name[0] == '.' && name[1] == '.' && name[2] == 0))
continue;
cv::String path = directory + native_separator + name;
if (isDir(path, dir))
{
if (recursive)
glob_rec(path, wildchart, result, recursive);
}
else
{
if (wildchart.empty() || wildcmp(name, wildchart.c_str()))
result.push_back(path);
}
}
}
catch (...)
{
closedir(dir);
throw;
}
closedir(dir);
}
else CV_Error(CV_StsObjectNotFound, cv::format("could not open directory: %s", directory.c_str()));
}
void cv::glob(String pattern, std::vector<String>& result, bool recursive)
{
result.clear();
String path, wildchart;
if (isDir(pattern, 0))
{
if(strchr(dir_separators, pattern[pattern.size() - 1]) != 0)
{
path = pattern.substr(0, pattern.size() - 1);
}
else
{
path = pattern;
}
}
else
{
size_t pos = pattern.find_last_of(dir_separators);
if (pos == String::npos)
{
wildchart = pattern;
path = ".";
}
else
{
path = pattern.substr(0, pos);
wildchart = pattern.substr(pos + 1);
}
}
glob_rec(path, wildchart, result, recursive);
std::sort(result.begin(), result.end());
}

@ -883,4 +883,129 @@ CV_IMPL void cvRandShuffle( CvArr* arr, CvRNG* _rng, double iter_factor )
cv::randShuffle( dst, iter_factor, &rng ); cv::randShuffle( dst, iter_factor, &rng );
} }
// Mersenne Twister random number generator.
// Inspired by http://www.math.sci.hiroshima-u.ac.jp/~m-mat/MT/MT2002/CODES/mt19937ar.c
/*
A C-program for MT19937, with initialization improved 2002/1/26.
Coded by Takuji Nishimura and Makoto Matsumoto.
Before using, initialize the state by using init_genrand(seed)
or init_by_array(init_key, key_length).
Copyright (C) 1997 - 2002, Makoto Matsumoto and Takuji Nishimura,
All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions
are met:
1. Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in the
documentation and/or other materials provided with the distribution.
3. The names of its contributors may not be used to endorse or promote
products derived from this software without specific prior written
permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
Any feedback is very welcome.
http://www.math.sci.hiroshima-u.ac.jp/~m-mat/MT/emt.html
email: m-mat @ math.sci.hiroshima-u.ac.jp (remove space)
*/
cv::RNG_MT19937::RNG_MT19937(unsigned s) { seed(s); }
cv::RNG_MT19937::RNG_MT19937() { seed(5489U); }
void cv::RNG_MT19937::seed(unsigned s)
{
state[0]= s;
for (mti = 1; mti < N; mti++)
{
/* See Knuth TAOCP Vol2. 3rd Ed. P.106 for multiplier. */
state[mti] = (1812433253U * (state[mti - 1] ^ (state[mti - 1] >> 30)) + mti);
}
}
unsigned cv::RNG_MT19937::next()
{
/* mag01[x] = x * MATRIX_A for x=0,1 */
static unsigned mag01[2] = { 0x0U, /*MATRIX_A*/ 0x9908b0dfU};
const unsigned UPPER_MASK = 0x80000000U;
const unsigned LOWER_MASK = 0x7fffffffU;
/* generate N words at one time */
if (mti >= N)
{
int kk = 0;
for (; kk < N - M; ++kk)
{
unsigned y = (state[kk] & UPPER_MASK) | (state[kk + 1] & LOWER_MASK);
state[kk] = state[kk + M] ^ (y >> 1) ^ mag01[y & 0x1U];
}
for (; kk < N - 1; ++kk)
{
unsigned y = (state[kk] & UPPER_MASK) | (state[kk + 1] & LOWER_MASK);
state[kk] = state[kk + (M - N)] ^ (y >> 1) ^ mag01[y & 0x1U];
}
unsigned y = (state[N - 1] & UPPER_MASK) | (state[0] & LOWER_MASK);
state[N - 1] = state[M - 1] ^ (y >> 1) ^ mag01[y & 0x1U];
mti = 0;
}
unsigned y = state[mti++];
/* Tempering */
y ^= (y >> 11);
y ^= (y << 7) & 0x9d2c5680U;
y ^= (y << 15) & 0xefc60000U;
y ^= (y >> 18);
return y;
}
cv::RNG_MT19937::operator unsigned() { return next(); }
cv::RNG_MT19937::operator int() { return (int)next();}
cv::RNG_MT19937::operator float() { return next() * (1.f / 4294967296.f); }
cv::RNG_MT19937::operator double()
{
unsigned a = next() >> 5;
unsigned b = next() >> 6;
return (a * 67108864.0 + b) * (1.0 / 9007199254740992.0);
}
int cv::RNG_MT19937::uniform(int a, int b) { return (int)(next() % (b - a) + a); }
float cv::RNG_MT19937::uniform(float a, float b) { return ((float)*this)*(b - a) + a; }
double cv::RNG_MT19937::uniform(double a, double b) { return ((double)*this)*(b - a) + a; }
unsigned cv::RNG_MT19937::operator ()(unsigned b) { return next() % b; }
unsigned cv::RNG_MT19937::operator ()() { return next(); }
/* End of file. */ /* End of file. */

@ -454,6 +454,23 @@ protected:
TEST(Core_InputOutput, huge) { CV_BigMatrixIOTest test; test.safe_run(); } TEST(Core_InputOutput, huge) { CV_BigMatrixIOTest test; test.safe_run(); }
*/ */
TEST(Core_globbing, accuracy)
{
std::string patternLena = cvtest::TS::ptr()->get_data_path() + "lena*.*";
std::string patternLenaPng = cvtest::TS::ptr()->get_data_path() + "lena.png";
std::vector<String> lenas, pngLenas;
cv::glob(patternLena, lenas, true);
cv::glob(patternLenaPng, pngLenas, true);
ASSERT_GT(lenas.size(), pngLenas.size());
for (size_t i = 0; i < pngLenas.size(); ++i)
{
ASSERT_NE(std::find(lenas.begin(), lenas.end(), pngLenas[i]), lenas.end());
}
}
TEST(Core_InputOutput, FileStorage) TEST(Core_InputOutput, FileStorage)
{ {
std::string file = cv::tempfile(".xml"); std::string file = cv::tempfile(".xml");

@ -339,3 +339,29 @@ protected:
TEST(Core_Rand, range) { Core_RandRangeTest test; test.safe_run(); } TEST(Core_Rand, range) { Core_RandRangeTest test; test.safe_run(); }
TEST(Core_RNG_MT19937, regression)
{
cv::RNG_MT19937 rng;
int actual[61] = {0, };
const size_t length = (sizeof(actual) / sizeof(actual[0]));
for (int i = 0; i < 10000; ++i )
{
actual[(unsigned)(rng.next() ^ i) % length]++;
}
int expected[length] = {
177, 158, 180, 177, 160, 179, 143, 162,
177, 144, 170, 174, 165, 168, 168, 156,
177, 157, 159, 169, 177, 182, 166, 154,
144, 180, 168, 152, 170, 187, 160, 145,
139, 164, 157, 179, 148, 183, 159, 160,
196, 184, 149, 142, 162, 148, 163, 152,
168, 173, 160, 181, 172, 181, 155, 153,
158, 171, 138, 150, 150 };
for (size_t i = 0; i < length; ++i)
{
ASSERT_EQ(expected[i], actual[i]);
}
}

Loading…
Cancel
Save