mirror of
https://github.com/opencv/opencv.git
synced 2025-06-08 01:53:19 +08:00
Merge pull request #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
This commit is contained in:
parent
7d4a67f2a8
commit
c5ed507737
@ -45,16 +45,15 @@
|
|||||||
* Create test/training samples
|
* Create test/training samples
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
#include "opencv2/core.hpp"
|
||||||
|
#include "utility.hpp"
|
||||||
#include <cstdio>
|
#include <cstdio>
|
||||||
#include <cstring>
|
#include <cstring>
|
||||||
#include <cstdlib>
|
#include <cstdlib>
|
||||||
#include <cmath>
|
#include <cmath>
|
||||||
#include <ctime>
|
|
||||||
|
|
||||||
using namespace std;
|
using namespace std;
|
||||||
|
|
||||||
#include "utility.hpp"
|
|
||||||
|
|
||||||
int main( int argc, char* argv[] )
|
int main( int argc, char* argv[] )
|
||||||
{
|
{
|
||||||
int i = 0;
|
int i = 0;
|
||||||
@ -77,8 +76,7 @@ int main( int argc, char* argv[] )
|
|||||||
int width = 24;
|
int width = 24;
|
||||||
int height = 24;
|
int height = 24;
|
||||||
double maxscale = -1.0;
|
double maxscale = -1.0;
|
||||||
|
int rngseed = 12345;
|
||||||
srand((unsigned int)time(0));
|
|
||||||
|
|
||||||
if( argc == 1 )
|
if( argc == 1 )
|
||||||
{
|
{
|
||||||
@ -94,9 +92,10 @@ int main( int argc, char* argv[] )
|
|||||||
" [-maxzangle <max_z_rotation_angle = %f>]\n"
|
" [-maxzangle <max_z_rotation_angle = %f>]\n"
|
||||||
" [-show [<scale = %f>]]\n"
|
" [-show [<scale = %f>]]\n"
|
||||||
" [-w <sample_width = %d>]\n [-h <sample_height = %d>]\n"
|
" [-w <sample_width = %d>]\n [-h <sample_height = %d>]\n"
|
||||||
" [-maxscale <max sample scale = %f>]\n",
|
" [-maxscale <max sample scale = %f>]\n"
|
||||||
|
" [-rngseed <rng seed = %d>]\n",
|
||||||
argv[0], num, bgcolor, bgthreshold, maxintensitydev,
|
argv[0], num, bgcolor, bgthreshold, maxintensitydev,
|
||||||
maxxangle, maxyangle, maxzangle, scale, width, height, maxscale );
|
maxxangle, maxyangle, maxzangle, scale, width, height, maxscale, rngseed );
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
@ -178,7 +177,13 @@ int main( int argc, char* argv[] )
|
|||||||
{
|
{
|
||||||
maxscale = atof( argv[++i] );
|
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( "Info file name: %s\n", ((infoname == NULL) ? nullname : infoname ) );
|
||||||
printf( "Img file name: %s\n", ((imagename == NULL) ? nullname : imagename ) );
|
printf( "Img file name: %s\n", ((imagename == NULL) ? nullname : imagename ) );
|
||||||
@ -201,6 +206,7 @@ int main( int argc, char* argv[] )
|
|||||||
printf( "Width: %d\n", width );
|
printf( "Width: %d\n", width );
|
||||||
printf( "Height: %d\n", height );
|
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 */
|
/* determine action */
|
||||||
if( imagename && vecname )
|
if( imagename && vecname )
|
||||||
|
@ -374,23 +374,23 @@ static void cvWarpPerspective( Mat src, Mat dst, double quad[4][2] )
|
|||||||
i00 = i10 = i01 = i11 = (int) fill_value;
|
i00 = i10 = i01 = i11 = (int) fill_value;
|
||||||
|
|
||||||
/* linear interpolation using 2x2 neighborhood */
|
/* linear interpolation using 2x2 neighborhood */
|
||||||
if( isrc_x >= 0 && isrc_x <= src.cols &&
|
if( isrc_x >= 0 && isrc_x < src.cols &&
|
||||||
isrc_y >= 0 && isrc_y <= src.rows )
|
isrc_y >= 0 && isrc_y < src.rows )
|
||||||
{
|
{
|
||||||
i00 = src.at<uchar>(isrc_y, isrc_x);
|
i00 = src.at<uchar>(isrc_y, isrc_x);
|
||||||
}
|
}
|
||||||
if( isrc_x >= -1 && isrc_x < src.cols &&
|
if( isrc_x >= -1 && isrc_x + 1 < src.cols &&
|
||||||
isrc_y >= 0 && isrc_y <= src.rows )
|
isrc_y >= 0 && isrc_y < src.rows )
|
||||||
{
|
{
|
||||||
i10 = src.at<uchar>(isrc_y, isrc_x + 1);
|
i10 = src.at<uchar>(isrc_y, isrc_x + 1);
|
||||||
}
|
}
|
||||||
if( isrc_x >= 0 && isrc_x <= src.cols &&
|
if( isrc_x >= 0 && isrc_x < src.cols &&
|
||||||
isrc_y >= -1 && isrc_y < src.rows )
|
isrc_y >= -1 && isrc_y + 1 < src.rows )
|
||||||
{
|
{
|
||||||
i01 = src.at<uchar>(isrc_y + 1, isrc_x);
|
i01 = src.at<uchar>(isrc_y + 1, isrc_x);
|
||||||
}
|
}
|
||||||
if( isrc_x >= -1 && isrc_x < src.cols &&
|
if( isrc_x >= -1 && isrc_x + 1 < src.cols &&
|
||||||
isrc_y >= -1 && isrc_y < src.rows )
|
isrc_y >= -1 && isrc_y + 1 < src.rows )
|
||||||
{
|
{
|
||||||
i11 = src.at<uchar>(isrc_y + 1, isrc_x + 1);
|
i11 = src.at<uchar>(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 rotMat( 3, 3, CV_64FC1, &rotMatData[0] );
|
||||||
Mat vect( 3, 1, CV_64FC1, &vectData[0] );
|
Mat vect( 3, 1, CV_64FC1, &vectData[0] );
|
||||||
|
|
||||||
rotVectData[0] = maxxangle * (2.0 * rand() / RAND_MAX - 1.0);
|
rotVectData[0] = theRNG().uniform( -maxxangle, maxxangle );
|
||||||
rotVectData[1] = ( maxyangle - fabs( rotVectData[0] ) )
|
rotVectData[1] = ( maxyangle - fabs( rotVectData[0] ) ) * theRNG().uniform( -1.0, 1.0 );
|
||||||
* (2.0 * rand() / RAND_MAX - 1.0);
|
rotVectData[2] = theRNG().uniform( -maxzangle, maxzangle );
|
||||||
rotVectData[2] = maxzangle * (2.0 * rand() / RAND_MAX - 1.0);
|
d = ( distfactor + distfactor2 * theRNG().uniform( -1.0, 1.0 ) ) * width;
|
||||||
d = (distfactor + distfactor2 * (2.0 * rand() / RAND_MAX - 1.0)) * width;
|
|
||||||
|
|
||||||
Rodrigues( rotVect, rotMat );
|
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;
|
cr.height = (int) (MAX( quad[2][1], quad[3][1] ) + 0.5F ) - cr.y;
|
||||||
}
|
}
|
||||||
|
|
||||||
xshift = maxshiftf * rand() / RAND_MAX;
|
xshift = theRNG().uniform( 0., maxshiftf );
|
||||||
yshift = maxshiftf * rand() / RAND_MAX;
|
yshift = theRNG().uniform( 0., maxshiftf );
|
||||||
|
|
||||||
cr.x -= (int) ( xshift * cr.width );
|
cr.x -= (int) ( xshift * cr.width );
|
||||||
cr.y -= (int) ( yshift * cr.height );
|
cr.y -= (int) ( yshift * cr.height );
|
||||||
cr.width = (int) ((1.0 + maxshiftf) * cr.width );
|
cr.width = (int) ((1.0 + maxshiftf) * cr.width );
|
||||||
cr.height = (int) ((1.0 + maxshiftf) * cr.height);
|
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.x -= (int) ( 0.5 * randscale * cr.width );
|
||||||
cr.y -= (int) ( 0.5 * randscale * cr.height );
|
cr.y -= (int) ( 0.5 * randscale * cr.height );
|
||||||
cr.width = (int) ((1.0 + randscale) * cr.width );
|
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->img(roi), img, img.size(), 0, 0, INTER_LINEAR_EXACT);
|
||||||
resize( data->maskimg(roi), maskimg, maskimg.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++ )
|
for( r = 0; r < img.rows; r++ )
|
||||||
{
|
{
|
||||||
@ -829,7 +828,7 @@ void icvGetNextFromBackgroundData( CvBackgroundData* data,
|
|||||||
{
|
{
|
||||||
round = data->round;
|
round = data->round;
|
||||||
|
|
||||||
data->last = rand() % data->count;
|
data->last = theRNG().uniform( 0, RAND_MAX ) % data->count;
|
||||||
|
|
||||||
#ifdef CV_VERBOSE
|
#ifdef CV_VERBOSE
|
||||||
printf( "Open background image: %s\n", data->filename[data->last] );
|
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) );
|
((float) data->winsize.height + reader->point.y) / ((float) reader->src.rows) );
|
||||||
|
|
||||||
resize( reader->src, reader->img,
|
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 )
|
if( reader->scale <= 1.0F )
|
||||||
{
|
{
|
||||||
resize(reader->src, reader->img,
|
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
|
else
|
||||||
{
|
{
|
||||||
@ -1072,7 +1071,7 @@ void cvCreateTrainingSamples( const char* filename,
|
|||||||
|
|
||||||
if( invert == CV_RANDOM_INVERT )
|
if( invert == CV_RANDOM_INVERT )
|
||||||
{
|
{
|
||||||
inverse = (rand() > (RAND_MAX/2));
|
inverse = theRNG().uniform( 0, 2 );
|
||||||
}
|
}
|
||||||
icvPlaceDistortedSample( sample, inverse, maxintensitydev,
|
icvPlaceDistortedSample( sample, inverse, maxintensitydev,
|
||||||
maxxangle, maxyangle, maxzangle,
|
maxxangle, maxyangle, maxzangle,
|
||||||
@ -1182,16 +1181,16 @@ void cvCreateTestSamples( const char* infoname,
|
|||||||
|
|
||||||
if( maxscale < 1.0F ) continue;
|
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);
|
width = (int) (scale * winwidth);
|
||||||
height = (int) (scale * winheight);
|
height = (int) (scale * winheight);
|
||||||
x = (int) ((0.1+0.8 * rand()/RAND_MAX) * (cvbgreader->src.cols - width));
|
x = (int) ( theRNG().uniform( 0.1, 0.8 ) * (cvbgreader->src.cols - width));
|
||||||
y = (int) ((0.1+0.8 * rand()/RAND_MAX) * (cvbgreader->src.rows - height));
|
y = (int) ( theRNG().uniform( 0.1, 0.8 ) * (cvbgreader->src.rows - height));
|
||||||
|
|
||||||
if( invert == CV_RANDOM_INVERT )
|
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,
|
icvPlaceDistortedSample( cvbgreader->src(Rect(x, y, width, height)), inverse, maxintensitydev,
|
||||||
maxxangle, maxyangle, maxzangle,
|
maxxangle, maxyangle, maxzangle,
|
||||||
@ -1452,9 +1451,9 @@ void cvShowVecSamples( const char* filename, int winwidth, int winheight,
|
|||||||
icvGetTraininDataFromVec( sample, file );
|
icvGetTraininDataFromVec( sample, file );
|
||||||
if( scale != 1.0 )
|
if( scale != 1.0 )
|
||||||
resize( sample, sample,
|
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 );
|
imshow( "Sample", sample );
|
||||||
if( (waitKey( 0 ) & 0xFF) == 27 ) break;
|
if( waitKey( 0 ) == 27 ) break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
fclose( file.input );
|
fclose( file.input );
|
||||||
|
Loading…
Reference in New Issue
Block a user