diff --git a/apps/createsamples/createsamples.cpp b/apps/createsamples/createsamples.cpp index 1660adee44..83a55f0a93 100644 --- a/apps/createsamples/createsamples.cpp +++ b/apps/createsamples/createsamples.cpp @@ -45,16 +45,15 @@ * Create test/training samples */ +#include "opencv2/core.hpp" +#include "utility.hpp" #include #include #include #include -#include using namespace std; -#include "utility.hpp" - int main( int argc, char* argv[] ) { int i = 0; @@ -77,8 +76,7 @@ int main( int argc, char* argv[] ) int width = 24; int height = 24; double maxscale = -1.0; - - srand((unsigned int)time(0)); + int rngseed = 12345; if( argc == 1 ) { @@ -94,9 +92,10 @@ int main( int argc, char* argv[] ) " [-maxzangle ]\n" " [-show []]\n" " [-w ]\n [-h ]\n" - " [-maxscale ]\n", + " [-maxscale ]\n" + " [-rngseed ]\n", argv[0], num, bgcolor, bgthreshold, maxintensitydev, - maxxangle, maxyangle, maxzangle, scale, width, height, maxscale ); + maxxangle, maxyangle, maxzangle, scale, width, height, maxscale, rngseed ); return 0; } @@ -178,8 +177,14 @@ int main( int argc, char* argv[] ) { maxscale = atof( argv[++i] ); } + else if (!strcmp(argv[i], "-rngseed")) + { + rngseed = atoi(argv[++i]); + } } + cv::setRNGSeed( rngseed ); + printf( "Info file name: %s\n", ((infoname == NULL) ? nullname : infoname ) ); printf( "Img file name: %s\n", ((imagename == NULL) ? nullname : imagename ) ); printf( "Vec file name: %s\n", ((vecname == NULL) ? nullname : vecname ) ); @@ -200,7 +205,8 @@ int main( int argc, char* argv[] ) } printf( "Width: %d\n", width ); printf( "Height: %d\n", height ); - printf( "Max Scale: %g\n", maxscale); + printf( "Max Scale: %g\n", maxscale ); + printf( "RNG Seed: %d\n", rngseed ); /* determine action */ if( imagename && vecname ) diff --git a/apps/createsamples/utility.cpp b/apps/createsamples/utility.cpp index ffbfe27985..76734a154a 100644 --- a/apps/createsamples/utility.cpp +++ b/apps/createsamples/utility.cpp @@ -374,23 +374,23 @@ static void cvWarpPerspective( Mat src, Mat dst, double quad[4][2] ) i00 = i10 = i01 = i11 = (int) fill_value; /* linear interpolation using 2x2 neighborhood */ - if( isrc_x >= 0 && isrc_x <= src.cols && - isrc_y >= 0 && isrc_y <= src.rows ) + if( isrc_x >= 0 && isrc_x < src.cols && + isrc_y >= 0 && isrc_y < src.rows ) { i00 = src.at(isrc_y, isrc_x); } - if( isrc_x >= -1 && isrc_x < src.cols && - isrc_y >= 0 && isrc_y <= src.rows ) + if( isrc_x >= -1 && isrc_x + 1 < src.cols && + isrc_y >= 0 && isrc_y < src.rows ) { i10 = src.at(isrc_y, isrc_x + 1); } - if( isrc_x >= 0 && isrc_x <= src.cols && - isrc_y >= -1 && isrc_y < src.rows ) + if( isrc_x >= 0 && isrc_x < src.cols && + isrc_y >= -1 && isrc_y + 1 < src.rows ) { i01 = src.at(isrc_y + 1, isrc_x); } - if( isrc_x >= -1 && isrc_x < src.cols && - isrc_y >= -1 && isrc_y < src.rows ) + if( isrc_x >= -1 && isrc_x + 1 < src.cols && + isrc_y >= -1 && isrc_y + 1 < src.rows ) { i11 = src.at(isrc_y + 1, isrc_x + 1); } @@ -458,11 +458,10 @@ void icvRandomQuad( int width, int height, double quad[4][2], Mat rotMat( 3, 3, CV_64FC1, &rotMatData[0] ); Mat vect( 3, 1, CV_64FC1, &vectData[0] ); - rotVectData[0] = maxxangle * (2.0 * rand() / RAND_MAX - 1.0); - rotVectData[1] = ( maxyangle - fabs( rotVectData[0] ) ) - * (2.0 * rand() / RAND_MAX - 1.0); - rotVectData[2] = maxzangle * (2.0 * rand() / RAND_MAX - 1.0); - d = (distfactor + distfactor2 * (2.0 * rand() / RAND_MAX - 1.0)) * width; + rotVectData[0] = theRNG().uniform( -maxxangle, maxxangle ); + rotVectData[1] = ( maxyangle - fabs( rotVectData[0] ) ) * theRNG().uniform( -1.0, 1.0 ); + rotVectData[2] = theRNG().uniform( -maxzangle, maxzangle ); + d = ( distfactor + distfactor2 * theRNG().uniform( -1.0, 1.0 ) ) * width; Rodrigues( rotVect, rotMat ); @@ -662,15 +661,15 @@ void icvPlaceDistortedSample( Mat background, cr.height = (int) (MAX( quad[2][1], quad[3][1] ) + 0.5F ) - cr.y; } - xshift = maxshiftf * rand() / RAND_MAX; - yshift = maxshiftf * rand() / RAND_MAX; + xshift = theRNG().uniform( 0., maxshiftf ); + yshift = theRNG().uniform( 0., maxshiftf ); cr.x -= (int) ( xshift * cr.width ); cr.y -= (int) ( yshift * cr.height ); cr.width = (int) ((1.0 + maxshiftf) * cr.width ); cr.height = (int) ((1.0 + maxshiftf) * cr.height); - randscale = maxscalef * rand() / RAND_MAX; + randscale = theRNG().uniform( 0., maxscalef ); cr.x -= (int) ( 0.5 * randscale * cr.width ); cr.y -= (int) ( 0.5 * randscale * cr.height ); cr.width = (int) ((1.0 + randscale) * cr.width ); @@ -689,7 +688,7 @@ void icvPlaceDistortedSample( Mat background, resize( data->img(roi), img, img.size(), 0, 0, INTER_LINEAR_EXACT); resize( data->maskimg(roi), maskimg, maskimg.size(), 0, 0, INTER_LINEAR_EXACT); - forecolordev = (int) (maxintensitydev * (2.0 * rand() / RAND_MAX - 1.0)); + forecolordev = theRNG().uniform( -maxintensitydev, maxintensitydev ); for( r = 0; r < img.rows; r++ ) { @@ -829,7 +828,7 @@ void icvGetNextFromBackgroundData( CvBackgroundData* data, { round = data->round; - data->last = rand() % data->count; + data->last = theRNG().uniform( 0, RAND_MAX ) % data->count; #ifdef CV_VERBOSE printf( "Open background image: %s\n", data->filename[data->last] ); @@ -877,7 +876,7 @@ void icvGetNextFromBackgroundData( CvBackgroundData* data, ((float) data->winsize.height + reader->point.y) / ((float) reader->src.rows) ); resize( reader->src, reader->img, - Size((int)(reader->scale * reader->src.rows + 0.5F), (int)(reader->scale * reader->src.cols + 0.5F)), 0, 0, INTER_LINEAR_EXACT ); + Size((int)(reader->scale * reader->src.cols + 0.5F), (int)(reader->scale * reader->src.rows + 0.5F)), 0, 0, INTER_LINEAR_EXACT); } /* @@ -932,7 +931,7 @@ void icvGetBackgroundImage( CvBackgroundData* data, if( reader->scale <= 1.0F ) { resize(reader->src, reader->img, - Size((int)(reader->scale * reader->src.rows), (int)(reader->scale * reader->src.cols)), 0, 0, INTER_LINEAR_EXACT); + Size((int)(reader->scale * reader->src.cols), (int)(reader->scale * reader->src.rows)), 0, 0, INTER_LINEAR_EXACT); } else { @@ -1072,7 +1071,7 @@ void cvCreateTrainingSamples( const char* filename, if( invert == CV_RANDOM_INVERT ) { - inverse = (rand() > (RAND_MAX/2)); + inverse = theRNG().uniform( 0, 2 ); } icvPlaceDistortedSample( sample, inverse, maxintensitydev, maxxangle, maxyangle, maxzangle, @@ -1182,16 +1181,16 @@ void cvCreateTestSamples( const char* infoname, if( maxscale < 1.0F ) continue; - scale = ((float)maxscale - 1.0F) * rand() / RAND_MAX + 1.0F; + scale = theRNG().uniform( 1.0F, (float)maxscale ); width = (int) (scale * winwidth); height = (int) (scale * winheight); - x = (int) ((0.1+0.8 * rand()/RAND_MAX) * (cvbgreader->src.cols - width)); - y = (int) ((0.1+0.8 * rand()/RAND_MAX) * (cvbgreader->src.rows - height)); + x = (int) ( theRNG().uniform( 0.1, 0.8 ) * (cvbgreader->src.cols - width)); + y = (int) ( theRNG().uniform( 0.1, 0.8 ) * (cvbgreader->src.rows - height)); if( invert == CV_RANDOM_INVERT ) { - inverse = (rand() > (RAND_MAX/2)); + inverse = theRNG().uniform( 0, 2 ); } icvPlaceDistortedSample( cvbgreader->src(Rect(x, y, width, height)), inverse, maxintensitydev, maxxangle, maxyangle, maxzangle, @@ -1452,9 +1451,9 @@ void cvShowVecSamples( const char* filename, int winwidth, int winheight, icvGetTraininDataFromVec( sample, file ); if( scale != 1.0 ) resize( sample, sample, - Size(MAX(1, cvCeil(scale * winheight)), MAX(1, cvCeil(scale * winwidth))), 0, 0, INTER_LINEAR_EXACT); + Size(MAX(1, cvCeil(scale * winwidth)), MAX(1, cvCeil(scale * winheight))), 0, 0, INTER_LINEAR_EXACT); imshow( "Sample", sample ); - if( (waitKey( 0 ) & 0xFF) == 27 ) break; + if( waitKey( 0 ) == 27 ) break; } } fclose( file.input );