Path: blob/master/apps/createsamples/createsamples.cpp
16337 views
/*M///////////////////////////////////////////////////////////////////////////////////////1//2// IMPORTANT: READ BEFORE DOWNLOADING, COPYING, INSTALLING OR USING.3//4// By downloading, copying, installing or using the software you agree to this license.5// If you do not agree to this license, do not download, install,6// copy or use the software.7//8//9// Intel License Agreement10// For Open Source Computer Vision Library11//12// Copyright (C) 2000, Intel Corporation, all rights reserved.13// Third party copyrights are property of their respective owners.14//15// Redistribution and use in source and binary forms, with or without modification,16// are permitted provided that the following conditions are met:17//18// * Redistribution's of source code must retain the above copyright notice,19// this list of conditions and the following disclaimer.20//21// * Redistribution's in binary form must reproduce the above copyright notice,22// this list of conditions and the following disclaimer in the documentation23// and/or other materials provided with the distribution.24//25// * The name of Intel Corporation may not be used to endorse or promote products26// derived from this software without specific prior written permission.27//28// This software is provided by the copyright holders and contributors "as is" and29// any express or implied warranties, including, but not limited to, the implied30// warranties of merchantability and fitness for a particular purpose are disclaimed.31// In no event shall the Intel Corporation or contributors be liable for any direct,32// indirect, incidental, special, exemplary, or consequential damages33// (including, but not limited to, procurement of substitute goods or services;34// loss of use, data, or profits; or business interruption) however caused35// and on any theory of liability, whether in contract, strict liability,36// or tort (including negligence or otherwise) arising in any way out of37// the use of this software, even if advised of the possibility of such damage.38//39//M*/4041/*42* createsamples.cpp43*44* Create test/training samples45*/4647#include "opencv2/core.hpp"48#include "utility.hpp"49#include <cstdio>50#include <cstring>51#include <cstdlib>52#include <cmath>5354using namespace std;5556int main( int argc, char* argv[] )57{58int i = 0;59char* nullname = (char*)"(NULL)";60char* vecname = NULL; /* .vec file name */61char* infoname = NULL; /* file name with marked up image descriptions */62char* imagename = NULL; /* single sample image */63char* bgfilename = NULL; /* background */64int num = 1000;65int bgcolor = 0;66int bgthreshold = 80;67int invert = 0;68int maxintensitydev = 40;69double maxxangle = 1.1;70double maxyangle = 1.1;71double maxzangle = 0.5;72int showsamples = 0;73/* the samples are adjusted to this scale in the sample preview window */74double scale = 4.0;75int width = 24;76int height = 24;77double maxscale = -1.0;78int rngseed = 12345;7980if( argc == 1 )81{82printf( "Usage: %s\n [-info <collection_file_name>]\n"83" [-img <image_file_name>]\n"84" [-vec <vec_file_name>]\n"85" [-bg <background_file_name>]\n [-num <number_of_samples = %d>]\n"86" [-bgcolor <background_color = %d>]\n"87" [-inv] [-randinv] [-bgthresh <background_color_threshold = %d>]\n"88" [-maxidev <max_intensity_deviation = %d>]\n"89" [-maxxangle <max_x_rotation_angle = %f>]\n"90" [-maxyangle <max_y_rotation_angle = %f>]\n"91" [-maxzangle <max_z_rotation_angle = %f>]\n"92" [-show [<scale = %f>]]\n"93" [-w <sample_width = %d>]\n [-h <sample_height = %d>]\n"94" [-maxscale <max sample scale = %f>]\n"95" [-rngseed <rng seed = %d>]\n",96argv[0], num, bgcolor, bgthreshold, maxintensitydev,97maxxangle, maxyangle, maxzangle, scale, width, height, maxscale, rngseed );9899return 0;100}101102for( i = 1; i < argc; ++i )103{104if( !strcmp( argv[i], "-info" ) )105{106infoname = argv[++i];107}108else if( !strcmp( argv[i], "-img" ) )109{110imagename = argv[++i];111}112else if( !strcmp( argv[i], "-vec" ) )113{114vecname = argv[++i];115}116else if( !strcmp( argv[i], "-bg" ) )117{118bgfilename = argv[++i];119}120else if( !strcmp( argv[i], "-num" ) )121{122num = atoi( argv[++i] );123}124else if( !strcmp( argv[i], "-bgcolor" ) )125{126bgcolor = atoi( argv[++i] );127}128else if( !strcmp( argv[i], "-bgthresh" ) )129{130bgthreshold = atoi( argv[++i] );131}132else if( !strcmp( argv[i], "-inv" ) )133{134invert = 1;135}136else if( !strcmp( argv[i], "-randinv" ) )137{138invert = CV_RANDOM_INVERT;139}140else if( !strcmp( argv[i], "-maxidev" ) )141{142maxintensitydev = atoi( argv[++i] );143}144else if( !strcmp( argv[i], "-maxxangle" ) )145{146maxxangle = atof( argv[++i] );147}148else if( !strcmp( argv[i], "-maxyangle" ) )149{150maxyangle = atof( argv[++i] );151}152else if( !strcmp( argv[i], "-maxzangle" ) )153{154maxzangle = atof( argv[++i] );155}156else if( !strcmp( argv[i], "-show" ) )157{158showsamples = 1;159if( i+1 < argc && strlen( argv[i+1] ) > 0 && argv[i+1][0] != '-' )160{161double d;162d = strtod( argv[i+1], 0 );163if( d != -HUGE_VAL && d != HUGE_VAL && d > 0 ) scale = d;164++i;165}166}167else if( !strcmp( argv[i], "-w" ) )168{169width = atoi( argv[++i] );170}171else if( !strcmp( argv[i], "-h" ) )172{173height = atoi( argv[++i] );174}175else if( !strcmp( argv[i], "-maxscale" ) )176{177maxscale = atof( argv[++i] );178}179else if (!strcmp(argv[i], "-rngseed"))180{181rngseed = atoi(argv[++i]);182}183}184185cv::setRNGSeed( rngseed );186187printf( "Info file name: %s\n", ((infoname == NULL) ? nullname : infoname ) );188printf( "Img file name: %s\n", ((imagename == NULL) ? nullname : imagename ) );189printf( "Vec file name: %s\n", ((vecname == NULL) ? nullname : vecname ) );190printf( "BG file name: %s\n", ((bgfilename == NULL) ? nullname : bgfilename ) );191printf( "Num: %d\n", num );192printf( "BG color: %d\n", bgcolor );193printf( "BG threshold: %d\n", bgthreshold );194printf( "Invert: %s\n", (invert == CV_RANDOM_INVERT) ? "RANDOM"195: ( (invert) ? "TRUE" : "FALSE" ) );196printf( "Max intensity deviation: %d\n", maxintensitydev );197printf( "Max x angle: %g\n", maxxangle );198printf( "Max y angle: %g\n", maxyangle );199printf( "Max z angle: %g\n", maxzangle );200printf( "Show samples: %s\n", (showsamples) ? "TRUE" : "FALSE" );201if( showsamples )202{203printf( "Scale: %g\n", scale );204}205printf( "Width: %d\n", width );206printf( "Height: %d\n", height );207printf( "Max Scale: %g\n", maxscale );208printf( "RNG Seed: %d\n", rngseed );209210/* determine action */211if( imagename && vecname )212{213printf( "Create training samples from single image applying distortions...\n" );214215cvCreateTrainingSamples( vecname, imagename, bgcolor, bgthreshold, bgfilename,216num, invert, maxintensitydev,217maxxangle, maxyangle, maxzangle,218showsamples, width, height );219220printf( "Done\n" );221}222else if( imagename && bgfilename && infoname )223{224printf( "Create test samples from single image applying distortions...\n" );225226cvCreateTestSamples( infoname, imagename, bgcolor, bgthreshold, bgfilename, num,227invert, maxintensitydev,228maxxangle, maxyangle, maxzangle, showsamples, width, height, maxscale);229230printf( "Done\n" );231}232else if( infoname && vecname )233{234int total;235236printf( "Create training samples from images collection...\n" );237238total = cvCreateTrainingSamplesFromInfo( infoname, vecname, num, showsamples,239width, height );240241printf( "Done. Created %d samples\n", total );242}243else if( vecname )244{245printf( "View samples from vec file (press ESC to exit)...\n" );246247cvShowVecSamples( vecname, width, height, scale );248249printf( "Done\n" );250}251else252{253printf( "Nothing to do\n" );254}255256return 0;257}258259260