Commit d7737528 authored by Roman Donchenko's avatar Roman Donchenko

Merge pull request #3156 from sergregory:CreateSamplesMod

parents fd7a2def 18c0511d
...@@ -29,6 +29,9 @@ set(cvhaartraining_lib_src ...@@ -29,6 +29,9 @@ set(cvhaartraining_lib_src
cvhaarclassifier.cpp cvhaarclassifier.cpp
cvhaartraining.cpp cvhaartraining.cpp
cvsamples.cpp cvsamples.cpp
cvsamplesoutput.cpp
cvsamplesoutput.h
ioutput.h
) )
add_library(opencv_haartraining_engine STATIC ${cvhaartraining_lib_src}) add_library(opencv_haartraining_engine STATIC ${cvhaartraining_lib_src})
......
...@@ -50,10 +50,12 @@ ...@@ -50,10 +50,12 @@
#include <cstdlib> #include <cstdlib>
#include <cmath> #include <cmath>
#include <ctime> #include <ctime>
#include <memory>
using namespace std; using namespace std;
#include "cvhaartraining.h" #include "cvhaartraining.h"
#include "ioutput.h"
int main( int argc, char* argv[] ) int main( int argc, char* argv[] )
{ {
...@@ -71,11 +73,12 @@ int main( int argc, char* argv[] ) ...@@ -71,11 +73,12 @@ int main( int argc, char* argv[] )
double maxxangle = 1.1; double maxxangle = 1.1;
double maxyangle = 1.1; double maxyangle = 1.1;
double maxzangle = 0.5; double maxzangle = 0.5;
int showsamples = 0; bool showsamples = false;
/* the samples are adjusted to this scale in the sample preview window */ /* the samples are adjusted to this scale in the sample preview window */
double scale = 4.0; double scale = 4.0;
int width = 24; int width = 24;
int height = 24; int height = 24;
bool pngoutput = false; /* whether to make the samples in png or in jpg*/
srand((unsigned int)time(0)); srand((unsigned int)time(0));
...@@ -92,7 +95,8 @@ int main( int argc, char* argv[] ) ...@@ -92,7 +95,8 @@ int main( int argc, char* argv[] )
" [-maxyangle <max_y_rotation_angle = %f>]\n" " [-maxyangle <max_y_rotation_angle = %f>]\n"
" [-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"
" [-pngoutput]",
argv[0], num, bgcolor, bgthreshold, maxintensitydev, argv[0], num, bgcolor, bgthreshold, maxintensitydev,
maxxangle, maxyangle, maxzangle, scale, width, height ); maxxangle, maxyangle, maxzangle, scale, width, height );
...@@ -155,7 +159,7 @@ int main( int argc, char* argv[] ) ...@@ -155,7 +159,7 @@ int main( int argc, char* argv[] )
} }
else if( !strcmp( argv[i], "-show" ) ) else if( !strcmp( argv[i], "-show" ) )
{ {
showsamples = 1; showsamples = true;
if( i+1 < argc && strlen( argv[i+1] ) > 0 && argv[i+1][0] != '-' ) if( i+1 < argc && strlen( argv[i+1] ) > 0 && argv[i+1][0] != '-' )
{ {
double d; double d;
...@@ -172,6 +176,10 @@ int main( int argc, char* argv[] ) ...@@ -172,6 +176,10 @@ int main( int argc, char* argv[] )
{ {
height = atoi( argv[++i] ); height = atoi( argv[++i] );
} }
else if( !strcmp( argv[i], "-pngoutput" ) )
{
pngoutput = true;
}
} }
printf( "Info file name: %s\n", ((infoname == NULL) ? nullname : infoname ) ); printf( "Info file name: %s\n", ((infoname == NULL) ? nullname : infoname ) );
...@@ -190,10 +198,14 @@ int main( int argc, char* argv[] ) ...@@ -190,10 +198,14 @@ int main( int argc, char* argv[] )
printf( "Show samples: %s\n", (showsamples) ? "TRUE" : "FALSE" ); printf( "Show samples: %s\n", (showsamples) ? "TRUE" : "FALSE" );
if( showsamples ) if( showsamples )
{ {
printf( "Scale: %g\n", scale ); printf( "Scale applied to display : %g\n", scale );
}
if( !pngoutput)
{
printf( "Original image will be scaled to:\n");
printf( "\tWidth: $backgroundWidth / %d\n", width );
printf( "\tHeight: $backgroundHeight / %d\n", height );
} }
printf( "Width: %d\n", width );
printf( "Height: %d\n", height );
/* determine action */ /* determine action */
if( imagename && vecname ) if( imagename && vecname )
...@@ -207,13 +219,24 @@ int main( int argc, char* argv[] ) ...@@ -207,13 +219,24 @@ int main( int argc, char* argv[] )
printf( "Done\n" ); printf( "Done\n" );
} }
else if( imagename && bgfilename && infoname ) else if( imagename && bgfilename && infoname)
{ {
printf( "Create test samples from single image applying distortions...\n" ); printf( "Create data set from single image applying distortions...\n"
"Output format: %s\n",
(( pngoutput ) ? "PNG" : "JPG") );
cvCreateTestSamples( infoname, imagename, bgcolor, bgthreshold, bgfilename, num, std::auto_ptr<DatasetGenerator> creator;
invert, maxintensitydev, if( pngoutput )
maxxangle, maxyangle, maxzangle, showsamples, width, height ); {
creator = std::auto_ptr<DatasetGenerator>( new PngDatasetGenerator( infoname ) );
}
else
{
creator = std::auto_ptr<DatasetGenerator>( new JpgDatasetGenerator( infoname ) );
}
creator->create( imagename, bgcolor, bgthreshold, bgfilename, num,
invert, maxintensitydev, maxxangle, maxyangle, maxzangle,
showsamples, width, height );
printf( "Done\n" ); printf( "Done\n" );
} }
......
...@@ -48,6 +48,8 @@ ...@@ -48,6 +48,8 @@
#include "cvhaartraining.h" #include "cvhaartraining.h"
#include "_cvhaartraining.h" #include "_cvhaartraining.h"
#include "ioutput.h"
#include <cstdio> #include <cstdio>
#include <cstdlib> #include <cstdlib>
#include <cmath> #include <cmath>
...@@ -2841,14 +2843,12 @@ void cvCreateTreeCascadeClassifier( const char* dirname, ...@@ -2841,14 +2843,12 @@ void cvCreateTreeCascadeClassifier( const char* dirname,
cvReleaseMat( &features_idx ); cvReleaseMat( &features_idx );
} }
void cvCreateTrainingSamples( const char* filename, void cvCreateTrainingSamples( const char* filename,
const char* imgfilename, int bgcolor, int bgthreshold, const char* imgfilename, int bgcolor, int bgthreshold,
const char* bgfilename, int count, const char* bgfilename, int count,
int invert, int maxintensitydev, int invert, int maxintensitydev,
double maxxangle, double maxyangle, double maxzangle, double maxxangle, double maxyangle, double maxzangle,
int showsamples, bool showsamples,
int winwidth, int winheight ) int winwidth, int winheight )
{ {
CvSampleDistortionData data; CvSampleDistortionData data;
...@@ -2915,7 +2915,7 @@ void cvCreateTrainingSamples( const char* filename, ...@@ -2915,7 +2915,7 @@ void cvCreateTrainingSamples( const char* filename,
cvShowImage( "Sample", &sample ); cvShowImage( "Sample", &sample );
if( cvWaitKey( 0 ) == 27 ) if( cvWaitKey( 0 ) == 27 )
{ {
showsamples = 0; showsamples = false;
} }
} }
...@@ -2942,45 +2942,43 @@ void cvCreateTrainingSamples( const char* filename, ...@@ -2942,45 +2942,43 @@ void cvCreateTrainingSamples( const char* filename,
} }
#define CV_INFO_FILENAME "info.dat" DatasetGenerator::DatasetGenerator( IOutput* _writer )
:writer(_writer)
{
}
void DatasetGenerator::showSamples(bool* show, CvMat *img) const
{
if( *show )
{
cvShowImage( "Image", img);
if( cvWaitKey( 0 ) == 27 )
{
*show = false;
}
}
}
void cvCreateTestSamples( const char* infoname, void DatasetGenerator::create(const char* imgfilename, int bgcolor, int bgthreshold,
const char* imgfilename, int bgcolor, int bgthreshold, const char* bgfilename, int count,
const char* bgfilename, int count, int invert, int maxintensitydev,
int invert, int maxintensitydev, double maxxangle, double maxyangle, double maxzangle,
double maxxangle, double maxyangle, double maxzangle, bool showsamples,
int showsamples, int winwidth, int winheight )
int winwidth, int winheight )
{ {
CvSampleDistortionData data; CvSampleDistortionData data;
assert( infoname != NULL );
assert( imgfilename != NULL ); assert( imgfilename != NULL );
assert( bgfilename != NULL ); assert( bgfilename != NULL );
if( !icvMkDir( infoname ) )
{
#if CV_VERBOSE
fprintf( stderr, "Unable to create directory hierarchy: %s\n", infoname );
#endif /* CV_VERBOSE */
return;
}
if( icvStartSampleDistortion( imgfilename, bgcolor, bgthreshold, &data ) ) if( icvStartSampleDistortion( imgfilename, bgcolor, bgthreshold, &data ) )
{ {
char fullname[PATH_MAX];
char* filename;
CvMat win; CvMat win;
FILE* info;
if( icvInitBackgroundReaders( bgfilename, cvSize( 10, 10 ) ) ) if( icvInitBackgroundReaders( bgfilename, cvSize( 10, 10 ) ) )
{ {
int i; int i;
int x, y, width, height;
float scale;
float maxscale;
int inverse; int inverse;
if( showsamples ) if( showsamples )
...@@ -2988,73 +2986,112 @@ void cvCreateTestSamples( const char* infoname, ...@@ -2988,73 +2986,112 @@ void cvCreateTestSamples( const char* infoname,
cvNamedWindow( "Image", CV_WINDOW_AUTOSIZE ); cvNamedWindow( "Image", CV_WINDOW_AUTOSIZE );
} }
info = fopen( infoname, "w" );
strcpy( fullname, infoname );
filename = strrchr( fullname, '\\' );
if( filename == NULL )
{
filename = strrchr( fullname, '/' );
}
if( filename == NULL )
{
filename = fullname;
}
else
{
filename++;
}
count = MIN( count, cvbgdata->count ); count = MIN( count, cvbgdata->count );
inverse = invert; inverse = invert;
for( i = 0; i < count; i++ ) for( i = 0; i < count; i++ )
{ {
icvGetNextFromBackgroundData( cvbgdata, cvbgreader ); icvGetNextFromBackgroundData( cvbgdata, cvbgreader );
maxscale = MIN( 0.7F * cvbgreader->src.cols / winwidth, CvRect boundingBox = getObjectPosition( cvSize( cvbgreader->src.cols,
0.7F * cvbgreader->src.rows / winheight ); cvbgreader->src.rows ),
if( maxscale < 1.0F ) continue; cvGetSize(data.img),
cvSize( winwidth, winheight ) );
if(boundingBox.width <= 0 || boundingBox.height <= 0)
{
continue;
}
scale = (maxscale - 1.0F) * rand() / RAND_MAX + 1.0F; cvGetSubArr( &cvbgreader->src, &win, boundingBox );
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));
cvGetSubArr( &cvbgreader->src, &win, cvRect( x, y ,width, height ) );
if( invert == CV_RANDOM_INVERT ) if( invert == CV_RANDOM_INVERT )
{ {
inverse = (rand() > (RAND_MAX/2)); inverse = (rand() > (RAND_MAX/2));
} }
icvPlaceDistortedSample( &win, inverse, maxintensitydev, icvPlaceDistortedSample( &win, inverse, maxintensitydev,
maxxangle, maxyangle, maxzangle, maxxangle, maxyangle, maxzangle,
1, 0.0, 0.0, &data ); 1, 0.0, 0.0, &data );
writer->write( cvbgreader->src, boundingBox );
sprintf( filename, "%04d_%04d_%04d_%04d_%04d.jpg", showSamples(&showsamples, &cvbgreader->src);
(i + 1), x, y, width, height );
if( info )
{
fprintf( info, "%s %d %d %d %d %d\n",
filename, 1, x, y, width, height );
}
cvSaveImage( fullname, &cvbgreader->src );
if( showsamples )
{
cvShowImage( "Image", &cvbgreader->src );
if( cvWaitKey( 0 ) == 27 )
{
showsamples = 0;
}
}
} }
if( info ) fclose( info );
icvDestroyBackgroundReaders(); icvDestroyBackgroundReaders();
} }
icvEndSampleDistortion( &data ); icvEndSampleDistortion( &data );
} }
} }
DatasetGenerator::~DatasetGenerator()
{
delete writer;
}
JpgDatasetGenerator::JpgDatasetGenerator( const char* filename )
:DatasetGenerator( IOutput::createOutput( filename, IOutput::JPG_DATASET ) )
{
}
CvSize JpgDatasetGenerator::scaleObjectSize( const CvSize& bgImgSize,
const CvSize& ,
const CvSize& sampleSize) const
{
float scale;
float maxscale;
maxscale = MIN( 0.7F * bgImgSize.width / sampleSize.width,
0.7F * bgImgSize.height / sampleSize.height );
if( maxscale < 1.0F )
{
scale = -1.f;
}
else
{
scale = (maxscale - 1.0F) * rand() / RAND_MAX + 1.0F;
}
int width = (int) (scale * sampleSize.width);
int height = (int) (scale * sampleSize.height);
return cvSize( width, height );
}
CvRect DatasetGenerator::getObjectPosition(const CvSize& bgImgSize,
const CvSize& imgSize,
const CvSize& sampleSize) const
{
CvSize size = scaleObjectSize( bgImgSize, imgSize, sampleSize );
int width = size.width;
int height = size.height;
int x = (int) ((0.1 + 0.8 * rand() / RAND_MAX) * (bgImgSize.width - width));
int y = (int) ((0.1 + 0.8 * rand() / RAND_MAX) * (bgImgSize.height - height));
return cvRect( x, y, width, height );
}
PngDatasetGenerator::PngDatasetGenerator(const char* filename)
:DatasetGenerator( IOutput::createOutput( filename, IOutput::PNG_DATASET ) )
{
}
CvSize PngDatasetGenerator::scaleObjectSize( const CvSize& bgImgSize,
const CvSize& imgSize,
const CvSize& ) const
{
float scale;
scale = MIN( 0.3F * bgImgSize.width / imgSize.width,
0.3F * bgImgSize.height / imgSize.height );
int width = (int) (scale * imgSize.width);
int height = (int) (scale * imgSize.height);
return cvSize( width, height );
}
/* End of file. */ /* End of file. */
...@@ -48,6 +48,11 @@ ...@@ -48,6 +48,11 @@
#ifndef _CVHAARTRAINING_H_ #ifndef _CVHAARTRAINING_H_
#define _CVHAARTRAINING_H_ #define _CVHAARTRAINING_H_
class IOutput;
struct CvRect;
struct CvSize;
struct CvMat;
/* /*
* cvCreateTrainingSamples * cvCreateTrainingSamples
* *
...@@ -74,23 +79,30 @@ ...@@ -74,23 +79,30 @@
*/ */
#define CV_RANDOM_INVERT 0x7FFFFFFF #define CV_RANDOM_INVERT 0x7FFFFFFF
void cvCreateTrainingSamples( const char* filename, void cvCreateTrainingSamples(const char* filename,
const char* imgfilename, int bgcolor, int bgthreshold, const char* imgfilename, int bgcolor, int bgthreshold,
const char* bgfilename, int count, const char* bgfilename, int count,
int invert = 0, int maxintensitydev = 40, int invert = 0, int maxintensitydev = 40,
double maxxangle = 1.1, double maxxangle = 1.1,
double maxyangle = 1.1, double maxyangle = 1.1,
double maxzangle = 0.5, double maxzangle = 0.5,
int showsamples = 0, bool showsamples = false,
int winwidth = 24, int winheight = 24 ); int winwidth = 24, int winheight = 24 );
void cvCreateTestSamples( const char* infoname, void cvCreatePngTrainingSet(const char* imgfilename, int bgcolor, int bgthreshold,
const char* imgfilename, int bgcolor, int bgthreshold, const char* bgfilename, int count,
int invert, int maxintensitydev,
double maxxangle, double maxyangle, double maxzangle,
int winwidth, int winheight,
IOutput *writer );
void cvCreateTestSamples(const char* imgfilename, int bgcolor, int bgthreshold,
const char* bgfilename, int count, const char* bgfilename, int count,
int invert, int maxintensitydev, int invert, int maxintensitydev,
double maxxangle, double maxyangle, double maxzangle, double maxxangle, double maxyangle, double maxzangle,
int showsamples, int showsamples,
int winwidth, int winheight ); int winwidth, int winheight,
IOutput* writer);
/* /*
* cvCreateTrainingSamplesFromInfo * cvCreateTrainingSamplesFromInfo
...@@ -189,4 +201,50 @@ void cvCreateTreeCascadeClassifier( const char* dirname, ...@@ -189,4 +201,50 @@ void cvCreateTreeCascadeClassifier( const char* dirname,
int boosttype, int stumperror, int boosttype, int stumperror,
int maxtreesplits, int minpos, bool bg_vecfile = false ); int maxtreesplits, int minpos, bool bg_vecfile = false );
class DatasetGenerator
{
public:
DatasetGenerator( IOutput* _writer );
void create( const char* imgfilename, int bgcolor, int bgthreshold,
const char* bgfilename, int count,
int invert, int maxintensitydev,
double maxxangle, double maxyangle, double maxzangle,
bool showsamples,
int winwidth, int winheight);
virtual ~DatasetGenerator();
private:
virtual void showSamples( bool* showSamples, CvMat* img ) const;
CvRect getObjectPosition( const CvSize& bgImgSize,
const CvSize& imgSize,
const CvSize& sampleSize ) const;
virtual CvSize scaleObjectSize(const CvSize& bgImgSize,
const CvSize& imgSize ,
const CvSize& sampleSize) const =0 ;
private:
IOutput* writer;
};
/* Provides the functionality of test set generating */
class JpgDatasetGenerator: public DatasetGenerator
{
public:
JpgDatasetGenerator(const char* filename);
private:
CvSize scaleObjectSize(const CvSize& bgImgSize,
const CvSize& ,
const CvSize& sampleSize) const;
};
class PngDatasetGenerator: public DatasetGenerator
{
public:
PngDatasetGenerator(const char *filename);
private:
CvSize scaleObjectSize(const CvSize& bgImgSize,
const CvSize& imgSize ,
const CvSize& ) const;
};
#endif /* _CVHAARTRAINING_H_ */ #endif /* _CVHAARTRAINING_H_ */
#include "cvsamplesoutput.h"
#include <cstdio>
#include "_cvcommon.h"
#include "highgui.h"
/* print statistic info */
#define CV_VERBOSE 1
IOutput::IOutput()
: currentIdx(0)
{}
void IOutput::findFilePathPart(char **partOfPath, char *fullPath)
{
*partOfPath = strrchr( fullPath, '\\' );
if( *partOfPath == NULL )
{
*partOfPath = strrchr( fullPath, '/' );
}
if( *partOfPath == NULL )
{
*partOfPath = fullPath;
}
else
{
*partOfPath += 1;
}
}
IOutput* IOutput::createOutput(const char *filename,
IOutput::OutputType type)
{
IOutput* output = 0;
switch (type) {
case IOutput::PNG_DATASET:
output = new PngDatasetOutput();
break;
case IOutput::JPG_DATASET:
output = new JpgDatasetOutput();
break;
default:
#if CV_VERBOSE
fprintf( stderr, "Invalid output type, valid types are: PNG_TRAINING_SET, JPG_TEST_SET");
#endif /* CV_VERBOSE */
return 0;
}
if ( output->init( filename ) )
return output;
else
return 0;
}
bool PngDatasetOutput::init( const char* annotationsListFileName )
{
IOutput::init( annotationsListFileName );
if(imgFileName == imgFullPath)
{
#if CV_VERBOSE
fprintf( stderr, "Invalid path to annotations file: %s\n"
"It should contain a parent directory name\n", imgFullPath );
#endif /* CV_VERBOSE */
return false;
}
const char* annotationsdirname = "/annotations/";
const char* positivesdirname = "/pos/";
imgFileName[-1] = '\0'; //erase slash at the end of the path
imgFileName -= 1;
//copy path to dataset top-level dir
strcpy(annotationFullPath, imgFullPath);
//find the name of annotation starting from the top-level dataset dir
findFilePathPart(&annotationRelativePath, annotationFullPath);
if( !strcmp( annotationRelativePath, ".." ) || !strcmp( annotationRelativePath, "." ) )
{
#if CV_VERBOSE
fprintf( stderr, "Invalid path to annotations file: %s\n"
"It should contain a parent directory name\n", annotationsListFileName );
#endif /* CV_VERBOSE */
return false;
}
//find the name of output image starting from the top-level dataset dir
findFilePathPart(&imgRelativePath, imgFullPath);
annotationFileName = annotationFullPath + strlen(annotationFullPath);
sprintf(annotationFileName, "%s", annotationsdirname);
annotationFileName += strlen(annotationFileName);
sprintf(imgFileName, "%s", positivesdirname);
imgFileName += strlen(imgFileName);
if( !icvMkDir( annotationFullPath ) )
{
#if CV_VERBOSE
fprintf( stderr, "Unable to create directory hierarchy: %s\n", annotationFullPath );
#endif /* CV_VERBOSE */
return false;
}
if( !icvMkDir( imgFullPath ) )
{
#if CV_VERBOSE
fprintf( stderr, "Unable to create directory hierarchy: %s\n", imgFullPath );
#endif /* CV_VERBOSE */
return false;
}
return true;
}
bool PngDatasetOutput::write( const CvMat& img,
const CvRect& boundingBox )
{
CvRect bbox = addBoundingboxBorder(boundingBox);
sprintf( imgFileName,
"%04d_%04d_%04d_%04d_%04d",
++currentIdx,
bbox.x,
bbox.y,
bbox.width,
bbox.height );
sprintf( annotationFileName, "%s.txt", imgFileName );
fprintf( annotationsList, "%s\n", annotationRelativePath );
FILE* annotationFile = fopen( annotationFullPath, "w" );
if(annotationFile == 0)
{
return false;
}
sprintf( imgFileName + strlen(imgFileName), ".%s", extension );
fprintf( annotationFile,
"Image filename : \"%s\"\n"
"Bounding box for object 1 \"PASperson\" (Xmin, Ymin) - (Xmax, Ymax) : (%d, %d) - (%d, %d)",
imgRelativePath,
bbox.x,
bbox.y,
bbox.x + bbox.width,
bbox.y + bbox.height );
fclose( annotationFile );
cvSaveImage( imgFullPath, &img);
return true;
}
CvRect PngDatasetOutput::addBoundingboxBorder(const CvRect& bbox) const
{
CvRect boundingBox = bbox;
int border = 5;
boundingBox.x -= border;
boundingBox.y -= border;
boundingBox.width += 2*border;
boundingBox.height += 2*border;
return boundingBox;
}
IOutput::~IOutput()
{
if(annotationsList)
{
fclose(annotationsList);
}
}
bool IOutput::init(const char *filename)
{
assert( filename != NULL );
if( !icvMkDir( filename ) )
{
#if CV_VERBOSE
fprintf( stderr, "Unable to create directory hierarchy: %s\n", filename );
#endif /* CV_VERBOSE */
return false;
}
annotationsList = fopen( filename, "w" );
if( annotationsList == NULL )
{
#if CV_VERBOSE
fprintf( stderr, "Unable to create info file: %s\n", filename );
#endif /* CV_VERBOSE */
return false;
}
strcpy( imgFullPath, filename );
findFilePathPart( &imgFileName, imgFullPath );
return true;
}
bool JpgDatasetOutput::write( const CvMat& img,
const CvRect& boundingBox )
{
sprintf( imgFileName, "%04d_%04d_%04d_%04d_%04d.jpg",
++currentIdx,
boundingBox.x,
boundingBox.y,
boundingBox.width,
boundingBox.height );
fprintf( annotationsList, "%s %d %d %d %d %d\n",
imgFileName,
1,
boundingBox.x,
boundingBox.y,
boundingBox.width,
boundingBox.height );
cvSaveImage( imgFullPath, &img);
return true;
}
#ifndef CVSAMPLESOUTPUT_H
#define CVSAMPLESOUTPUT_H
#include "ioutput.h"
class PngDatasetOutput: public IOutput
{
friend IOutput* IOutput::createOutput(const char *filename, OutputType type);
public:
virtual bool write( const CvMat& img,
const CvRect& boundingBox);
virtual ~PngDatasetOutput(){}
private:
PngDatasetOutput()
: extension("png")
, destImgWidth(640)
, destImgHeight(480)
{}
virtual bool init(const char* annotationsListFileName );
CvRect addBoundingboxBorder(const CvRect& bbox) const;
private:
char annotationFullPath[PATH_MAX];
char* annotationFileName;
char* annotationRelativePath;
char* imgRelativePath;
const char* extension;
int destImgWidth;
int destImgHeight ;
};
class JpgDatasetOutput: public IOutput
{
friend IOutput* IOutput::createOutput(const char *filename, OutputType type);
public:
virtual bool write( const CvMat& img,
const CvRect& boundingBox );
virtual ~JpgDatasetOutput(){}
private:
JpgDatasetOutput(){}
};
#endif // CVSAMPLESOUTPUT_H
#ifndef IOUTPUT_H
#define IOUTPUT_H
#include <cstdio>
#include "_cvcommon.h"
struct CvMat;
struct CvRect;
class IOutput
{
public:
enum OutputType {PNG_DATASET, JPG_DATASET};
public:
virtual bool write( const CvMat& img,
const CvRect& boundingBox ) =0;
virtual ~IOutput();
static IOutput* createOutput( const char *filename, OutputType type );
protected:
IOutput();
/* finds the beginning of the last token in the path */
void findFilePathPart( char **partOfPath, char *fullPath );
virtual bool init( const char* filename );
protected:
int currentIdx;
char imgFullPath[PATH_MAX];
char* imgFileName;
FILE* annotationsList;
};
#endif // IOUTPUT_H
This diff is collapsed.
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment