Skip to content

Commit c5ed507

Browse files
sturkmen72alalek
authored andcommitted
Merge pull request opencv#10287 from sturkmen72:update_createsamples_cpp
* update createsamples adds command-line option -rngseed replaces rand() -> theRNG() * Update utility.cpp * apps(createsamples): fix warpPerspective pixels access bug
1 parent 7d4a67f commit c5ed507

File tree

2 files changed

+40
-35
lines changed

2 files changed

+40
-35
lines changed

apps/createsamples/createsamples.cpp

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -45,16 +45,15 @@
4545
* Create test/training samples
4646
*/
4747

48+
#include "opencv2/core.hpp"
49+
#include "utility.hpp"
4850
#include <cstdio>
4951
#include <cstring>
5052
#include <cstdlib>
5153
#include <cmath>
52-
#include <ctime>
5354

5455
using namespace std;
5556

56-
#include "utility.hpp"
57-
5857
int main( int argc, char* argv[] )
5958
{
6059
int i = 0;
@@ -77,8 +76,7 @@ int main( int argc, char* argv[] )
7776
int width = 24;
7877
int height = 24;
7978
double maxscale = -1.0;
80-
81-
srand((unsigned int)time(0));
79+
int rngseed = 12345;
8280

8381
if( argc == 1 )
8482
{
@@ -94,9 +92,10 @@ int main( int argc, char* argv[] )
9492
" [-maxzangle <max_z_rotation_angle = %f>]\n"
9593
" [-show [<scale = %f>]]\n"
9694
" [-w <sample_width = %d>]\n [-h <sample_height = %d>]\n"
97-
" [-maxscale <max sample scale = %f>]\n",
95+
" [-maxscale <max sample scale = %f>]\n"
96+
" [-rngseed <rng seed = %d>]\n",
9897
argv[0], num, bgcolor, bgthreshold, maxintensitydev,
99-
maxxangle, maxyangle, maxzangle, scale, width, height, maxscale );
98+
maxxangle, maxyangle, maxzangle, scale, width, height, maxscale, rngseed );
10099

101100
return 0;
102101
}
@@ -178,8 +177,14 @@ int main( int argc, char* argv[] )
178177
{
179178
maxscale = atof( argv[++i] );
180179
}
180+
else if (!strcmp(argv[i], "-rngseed"))
181+
{
182+
rngseed = atoi(argv[++i]);
183+
}
181184
}
182185

186+
cv::setRNGSeed( rngseed );
187+
183188
printf( "Info file name: %s\n", ((infoname == NULL) ? nullname : infoname ) );
184189
printf( "Img file name: %s\n", ((imagename == NULL) ? nullname : imagename ) );
185190
printf( "Vec file name: %s\n", ((vecname == NULL) ? nullname : vecname ) );
@@ -200,7 +205,8 @@ int main( int argc, char* argv[] )
200205
}
201206
printf( "Width: %d\n", width );
202207
printf( "Height: %d\n", height );
203-
printf( "Max Scale: %g\n", maxscale);
208+
printf( "Max Scale: %g\n", maxscale );
209+
printf( "RNG Seed: %d\n", rngseed );
204210

205211
/* determine action */
206212
if( imagename && vecname )

apps/createsamples/utility.cpp

Lines changed: 26 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -374,23 +374,23 @@ static void cvWarpPerspective( Mat src, Mat dst, double quad[4][2] )
374374
i00 = i10 = i01 = i11 = (int) fill_value;
375375

376376
/* linear interpolation using 2x2 neighborhood */
377-
if( isrc_x >= 0 && isrc_x <= src.cols &&
378-
isrc_y >= 0 && isrc_y <= src.rows )
377+
if( isrc_x >= 0 && isrc_x < src.cols &&
378+
isrc_y >= 0 && isrc_y < src.rows )
379379
{
380380
i00 = src.at<uchar>(isrc_y, isrc_x);
381381
}
382-
if( isrc_x >= -1 && isrc_x < src.cols &&
383-
isrc_y >= 0 && isrc_y <= src.rows )
382+
if( isrc_x >= -1 && isrc_x + 1 < src.cols &&
383+
isrc_y >= 0 && isrc_y < src.rows )
384384
{
385385
i10 = src.at<uchar>(isrc_y, isrc_x + 1);
386386
}
387-
if( isrc_x >= 0 && isrc_x <= src.cols &&
388-
isrc_y >= -1 && isrc_y < src.rows )
387+
if( isrc_x >= 0 && isrc_x < src.cols &&
388+
isrc_y >= -1 && isrc_y + 1 < src.rows )
389389
{
390390
i01 = src.at<uchar>(isrc_y + 1, isrc_x);
391391
}
392-
if( isrc_x >= -1 && isrc_x < src.cols &&
393-
isrc_y >= -1 && isrc_y < src.rows )
392+
if( isrc_x >= -1 && isrc_x + 1 < src.cols &&
393+
isrc_y >= -1 && isrc_y + 1 < src.rows )
394394
{
395395
i11 = src.at<uchar>(isrc_y + 1, isrc_x + 1);
396396
}
@@ -458,11 +458,10 @@ void icvRandomQuad( int width, int height, double quad[4][2],
458458
Mat rotMat( 3, 3, CV_64FC1, &rotMatData[0] );
459459
Mat vect( 3, 1, CV_64FC1, &vectData[0] );
460460

461-
rotVectData[0] = maxxangle * (2.0 * rand() / RAND_MAX - 1.0);
462-
rotVectData[1] = ( maxyangle - fabs( rotVectData[0] ) )
463-
* (2.0 * rand() / RAND_MAX - 1.0);
464-
rotVectData[2] = maxzangle * (2.0 * rand() / RAND_MAX - 1.0);
465-
d = (distfactor + distfactor2 * (2.0 * rand() / RAND_MAX - 1.0)) * width;
461+
rotVectData[0] = theRNG().uniform( -maxxangle, maxxangle );
462+
rotVectData[1] = ( maxyangle - fabs( rotVectData[0] ) ) * theRNG().uniform( -1.0, 1.0 );
463+
rotVectData[2] = theRNG().uniform( -maxzangle, maxzangle );
464+
d = ( distfactor + distfactor2 * theRNG().uniform( -1.0, 1.0 ) ) * width;
466465

467466
Rodrigues( rotVect, rotMat );
468467

@@ -662,15 +661,15 @@ void icvPlaceDistortedSample( Mat background,
662661
cr.height = (int) (MAX( quad[2][1], quad[3][1] ) + 0.5F ) - cr.y;
663662
}
664663

665-
xshift = maxshiftf * rand() / RAND_MAX;
666-
yshift = maxshiftf * rand() / RAND_MAX;
664+
xshift = theRNG().uniform( 0., maxshiftf );
665+
yshift = theRNG().uniform( 0., maxshiftf );
667666

668667
cr.x -= (int) ( xshift * cr.width );
669668
cr.y -= (int) ( yshift * cr.height );
670669
cr.width = (int) ((1.0 + maxshiftf) * cr.width );
671670
cr.height = (int) ((1.0 + maxshiftf) * cr.height);
672671

673-
randscale = maxscalef * rand() / RAND_MAX;
672+
randscale = theRNG().uniform( 0., maxscalef );
674673
cr.x -= (int) ( 0.5 * randscale * cr.width );
675674
cr.y -= (int) ( 0.5 * randscale * cr.height );
676675
cr.width = (int) ((1.0 + randscale) * cr.width );
@@ -689,7 +688,7 @@ void icvPlaceDistortedSample( Mat background,
689688
resize( data->img(roi), img, img.size(), 0, 0, INTER_LINEAR_EXACT);
690689
resize( data->maskimg(roi), maskimg, maskimg.size(), 0, 0, INTER_LINEAR_EXACT);
691690

692-
forecolordev = (int) (maxintensitydev * (2.0 * rand() / RAND_MAX - 1.0));
691+
forecolordev = theRNG().uniform( -maxintensitydev, maxintensitydev );
693692

694693
for( r = 0; r < img.rows; r++ )
695694
{
@@ -829,7 +828,7 @@ void icvGetNextFromBackgroundData( CvBackgroundData* data,
829828
{
830829
round = data->round;
831830

832-
data->last = rand() % data->count;
831+
data->last = theRNG().uniform( 0, RAND_MAX ) % data->count;
833832

834833
#ifdef CV_VERBOSE
835834
printf( "Open background image: %s\n", data->filename[data->last] );
@@ -877,7 +876,7 @@ void icvGetNextFromBackgroundData( CvBackgroundData* data,
877876
((float) data->winsize.height + reader->point.y) / ((float) reader->src.rows) );
878877

879878
resize( reader->src, reader->img,
880-
Size((int)(reader->scale * reader->src.rows + 0.5F), (int)(reader->scale * reader->src.cols + 0.5F)), 0, 0, INTER_LINEAR_EXACT );
879+
Size((int)(reader->scale * reader->src.cols + 0.5F), (int)(reader->scale * reader->src.rows + 0.5F)), 0, 0, INTER_LINEAR_EXACT);
881880
}
882881

883882
/*
@@ -932,7 +931,7 @@ void icvGetBackgroundImage( CvBackgroundData* data,
932931
if( reader->scale <= 1.0F )
933932
{
934933
resize(reader->src, reader->img,
935-
Size((int)(reader->scale * reader->src.rows), (int)(reader->scale * reader->src.cols)), 0, 0, INTER_LINEAR_EXACT);
934+
Size((int)(reader->scale * reader->src.cols), (int)(reader->scale * reader->src.rows)), 0, 0, INTER_LINEAR_EXACT);
936935
}
937936
else
938937
{
@@ -1072,7 +1071,7 @@ void cvCreateTrainingSamples( const char* filename,
10721071

10731072
if( invert == CV_RANDOM_INVERT )
10741073
{
1075-
inverse = (rand() > (RAND_MAX/2));
1074+
inverse = theRNG().uniform( 0, 2 );
10761075
}
10771076
icvPlaceDistortedSample( sample, inverse, maxintensitydev,
10781077
maxxangle, maxyangle, maxzangle,
@@ -1182,16 +1181,16 @@ void cvCreateTestSamples( const char* infoname,
11821181

11831182
if( maxscale < 1.0F ) continue;
11841183

1185-
scale = ((float)maxscale - 1.0F) * rand() / RAND_MAX + 1.0F;
1184+
scale = theRNG().uniform( 1.0F, (float)maxscale );
11861185

11871186
width = (int) (scale * winwidth);
11881187
height = (int) (scale * winheight);
1189-
x = (int) ((0.1+0.8 * rand()/RAND_MAX) * (cvbgreader->src.cols - width));
1190-
y = (int) ((0.1+0.8 * rand()/RAND_MAX) * (cvbgreader->src.rows - height));
1188+
x = (int) ( theRNG().uniform( 0.1, 0.8 ) * (cvbgreader->src.cols - width));
1189+
y = (int) ( theRNG().uniform( 0.1, 0.8 ) * (cvbgreader->src.rows - height));
11911190

11921191
if( invert == CV_RANDOM_INVERT )
11931192
{
1194-
inverse = (rand() > (RAND_MAX/2));
1193+
inverse = theRNG().uniform( 0, 2 );
11951194
}
11961195
icvPlaceDistortedSample( cvbgreader->src(Rect(x, y, width, height)), inverse, maxintensitydev,
11971196
maxxangle, maxyangle, maxzangle,
@@ -1452,9 +1451,9 @@ void cvShowVecSamples( const char* filename, int winwidth, int winheight,
14521451
icvGetTraininDataFromVec( sample, file );
14531452
if( scale != 1.0 )
14541453
resize( sample, sample,
1455-
Size(MAX(1, cvCeil(scale * winheight)), MAX(1, cvCeil(scale * winwidth))), 0, 0, INTER_LINEAR_EXACT);
1454+
Size(MAX(1, cvCeil(scale * winwidth)), MAX(1, cvCeil(scale * winheight))), 0, 0, INTER_LINEAR_EXACT);
14561455
imshow( "Sample", sample );
1457-
if( (waitKey( 0 ) & 0xFF) == 27 ) break;
1456+
if( waitKey( 0 ) == 27 ) break;
14581457
}
14591458
}
14601459
fclose( file.input );

0 commit comments

Comments
 (0)