Skip to content
Projects
Groups
Snippets
Help
Loading...
Sign in / Register
Toggle navigation
O
opencv
Project
Project
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Packages
Packages
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
submodule
opencv
Commits
d7737528
Commit
d7737528
authored
Sep 05, 2014
by
Roman Donchenko
Browse files
Options
Browse Files
Download
Plain Diff
Merge pull request #3156 from sergregory:CreateSamplesMod
parents
fd7a2def
18c0511d
Expand all
Hide whitespace changes
Inline
Side-by-side
Showing
8 changed files
with
516 additions
and
88 deletions
+516
-88
CMakeLists.txt
apps/haartraining/CMakeLists.txt
+3
-0
createsamples.cpp
apps/haartraining/createsamples.cpp
+34
-11
cvhaartraining.cpp
apps/haartraining/cvhaartraining.cpp
+109
-72
cvhaartraining.h
apps/haartraining/cvhaartraining.h
+63
-5
cvsamplesoutput.cpp
apps/haartraining/cvsamplesoutput.cpp
+227
-0
cvsamplesoutput.h
apps/haartraining/cvsamplesoutput.h
+46
-0
ioutput.h
apps/haartraining/ioutput.h
+34
-0
ug_traincascade.rst
doc/user_guide/ug_traincascade.rst
+0
-0
No files found.
apps/haartraining/CMakeLists.txt
View file @
d7737528
...
...
@@ -29,6 +29,9 @@ set(cvhaartraining_lib_src
cvhaarclassifier.cpp
cvhaartraining.cpp
cvsamples.cpp
cvsamplesoutput.cpp
cvsamplesoutput.h
ioutput.h
)
add_library
(
opencv_haartraining_engine STATIC
${
cvhaartraining_lib_src
}
)
...
...
apps/haartraining/createsamples.cpp
View file @
d7737528
...
...
@@ -50,10 +50,12 @@
#include <cstdlib>
#include <cmath>
#include <ctime>
#include <memory>
using
namespace
std
;
#include "cvhaartraining.h"
#include "ioutput.h"
int
main
(
int
argc
,
char
*
argv
[]
)
{
...
...
@@ -71,11 +73,12 @@ int main( int argc, char* argv[] )
double
maxxangle
=
1.1
;
double
maxyangle
=
1.1
;
double
maxzangle
=
0.5
;
int
showsamples
=
0
;
bool
showsamples
=
false
;
/* the samples are adjusted to this scale in the sample preview window */
double
scale
=
4.0
;
int
width
=
24
;
int
height
=
24
;
bool
pngoutput
=
false
;
/* whether to make the samples in png or in jpg*/
srand
((
unsigned
int
)
time
(
0
));
...
...
@@ -92,7 +95,8 @@ int main( int argc, char* argv[] )
" [-maxyangle <max_y_rotation_angle = %f>]
\n
"
" [-maxzangle <max_z_rotation_angle = %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
,
maxxangle
,
maxyangle
,
maxzangle
,
scale
,
width
,
height
);
...
...
@@ -155,7 +159,7 @@ int main( int argc, char* argv[] )
}
else
if
(
!
strcmp
(
argv
[
i
],
"-show"
)
)
{
showsamples
=
1
;
showsamples
=
true
;
if
(
i
+
1
<
argc
&&
strlen
(
argv
[
i
+
1
]
)
>
0
&&
argv
[
i
+
1
][
0
]
!=
'-'
)
{
double
d
;
...
...
@@ -172,6 +176,10 @@ int main( int argc, char* argv[] )
{
height
=
atoi
(
argv
[
++
i
]
);
}
else
if
(
!
strcmp
(
argv
[
i
],
"-pngoutput"
)
)
{
pngoutput
=
true
;
}
}
printf
(
"Info file name: %s
\n
"
,
((
infoname
==
NULL
)
?
nullname
:
infoname
)
);
...
...
@@ -190,10 +198,14 @@ int main( int argc, char* argv[] )
printf
(
"Show samples: %s
\n
"
,
(
showsamples
)
?
"TRUE"
:
"FALSE"
);
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
(
"
\t
Width: $backgroundWidth / %d
\n
"
,
width
);
printf
(
"
\t
Height: $backgroundHeight / %d
\n
"
,
height
);
}
printf
(
"Width: %d
\n
"
,
width
);
printf
(
"Height: %d
\n
"
,
height
);
/* determine action */
if
(
imagename
&&
vecname
)
...
...
@@ -207,13 +219,24 @@ int main( int argc, char* argv[] )
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
,
invert
,
maxintensitydev
,
maxxangle
,
maxyangle
,
maxzangle
,
showsamples
,
width
,
height
);
std
::
auto_ptr
<
DatasetGenerator
>
creator
;
if
(
pngoutput
)
{
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
"
);
}
...
...
apps/haartraining/cvhaartraining.cpp
View file @
d7737528
...
...
@@ -48,6 +48,8 @@
#include "cvhaartraining.h"
#include "_cvhaartraining.h"
#include "ioutput.h"
#include <cstdio>
#include <cstdlib>
#include <cmath>
...
...
@@ -2841,14 +2843,12 @@ void cvCreateTreeCascadeClassifier( const char* dirname,
cvReleaseMat
(
&
features_idx
);
}
void
cvCreateTrainingSamples
(
const
char
*
filename
,
const
char
*
imgfilename
,
int
bgcolor
,
int
bgthreshold
,
const
char
*
bgfilename
,
int
count
,
int
invert
,
int
maxintensitydev
,
double
maxxangle
,
double
maxyangle
,
double
maxzangle
,
int
showsamples
,
bool
showsamples
,
int
winwidth
,
int
winheight
)
{
CvSampleDistortionData
data
;
...
...
@@ -2915,7 +2915,7 @@ void cvCreateTrainingSamples( const char* filename,
cvShowImage
(
"Sample"
,
&
sample
);
if
(
cvWaitKey
(
0
)
==
27
)
{
showsamples
=
0
;
showsamples
=
false
;
}
}
...
...
@@ -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
,
const
char
*
imgfilename
,
int
bgcolor
,
int
bgthreshold
,
const
char
*
bgfilename
,
int
count
,
int
invert
,
int
maxintensitydev
,
double
maxxangle
,
double
maxyangle
,
double
maxzangle
,
int
showsamples
,
int
winwidth
,
int
winheight
)
void
DatasetGenerator
::
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
)
{
CvSampleDistortionData
data
;
assert
(
infoname
!=
NULL
);
assert
(
imgfilename
!=
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
)
)
{
char
fullname
[
PATH_MAX
];
char
*
filename
;
CvMat
win
;
FILE
*
info
;
if
(
icvInitBackgroundReaders
(
bgfilename
,
cvSize
(
10
,
10
)
)
)
{
int
i
;
int
x
,
y
,
width
,
height
;
float
scale
;
float
maxscale
;
int
inverse
;
if
(
showsamples
)
...
...
@@ -2988,73 +2986,112 @@ void cvCreateTestSamples( const char* infoname,
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
);
inverse
=
invert
;
for
(
i
=
0
;
i
<
count
;
i
++
)
{
icvGetNextFromBackgroundData
(
cvbgdata
,
cvbgreader
);
maxscale
=
MIN
(
0.7
F
*
cvbgreader
->
src
.
cols
/
winwidth
,
0.7
F
*
cvbgreader
->
src
.
rows
/
winheight
);
if
(
maxscale
<
1.0
F
)
continue
;
CvRect
boundingBox
=
getObjectPosition
(
cvSize
(
cvbgreader
->
src
.
cols
,
cvbgreader
->
src
.
rows
),
cvGetSize
(
data
.
img
),
cvSize
(
winwidth
,
winheight
)
);
if
(
boundingBox
.
width
<=
0
||
boundingBox
.
height
<=
0
)
{
continue
;
}
scale
=
(
maxscale
-
1.0
F
)
*
rand
()
/
RAND_MAX
+
1.0
F
;
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
,
boundingBox
);
cvGetSubArr
(
&
cvbgreader
->
src
,
&
win
,
cvRect
(
x
,
y
,
width
,
height
)
);
if
(
invert
==
CV_RANDOM_INVERT
)
{
inverse
=
(
rand
()
>
(
RAND_MAX
/
2
));
}
icvPlaceDistortedSample
(
&
win
,
inverse
,
maxintensitydev
,
maxxangle
,
maxyangle
,
maxzangle
,
1
,
0.0
,
0.0
,
&
data
);
writer
->
write
(
cvbgreader
->
src
,
boundingBox
);
sprintf
(
filename
,
"%04d_%04d_%04d_%04d_%04d.jpg"
,
(
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
;
}
}
showSamples
(
&
showsamples
,
&
cvbgreader
->
src
);
}
if
(
info
)
fclose
(
info
);
icvDestroyBackgroundReaders
();
}
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.7
F
*
bgImgSize
.
width
/
sampleSize
.
width
,
0.7
F
*
bgImgSize
.
height
/
sampleSize
.
height
);
if
(
maxscale
<
1.0
F
)
{
scale
=
-
1.
f
;
}
else
{
scale
=
(
maxscale
-
1.0
F
)
*
rand
()
/
RAND_MAX
+
1.0
F
;
}
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.3
F
*
bgImgSize
.
width
/
imgSize
.
width
,
0.3
F
*
bgImgSize
.
height
/
imgSize
.
height
);
int
width
=
(
int
)
(
scale
*
imgSize
.
width
);
int
height
=
(
int
)
(
scale
*
imgSize
.
height
);
return
cvSize
(
width
,
height
);
}
/* End of file. */
apps/haartraining/cvhaartraining.h
View file @
d7737528
...
...
@@ -48,6 +48,11 @@
#ifndef _CVHAARTRAINING_H_
#define _CVHAARTRAINING_H_
class
IOutput
;
struct
CvRect
;
struct
CvSize
;
struct
CvMat
;
/*
* cvCreateTrainingSamples
*
...
...
@@ -74,23 +79,30 @@
*/
#define CV_RANDOM_INVERT 0x7FFFFFFF
void
cvCreateTrainingSamples
(
const
char
*
filename
,
void
cvCreateTrainingSamples
(
const
char
*
filename
,
const
char
*
imgfilename
,
int
bgcolor
,
int
bgthreshold
,
const
char
*
bgfilename
,
int
count
,
int
invert
=
0
,
int
maxintensitydev
=
40
,
double
maxxangle
=
1
.
1
,
double
maxyangle
=
1
.
1
,
double
maxzangle
=
0
.
5
,
int
showsamples
=
0
,
bool
showsamples
=
false
,
int
winwidth
=
24
,
int
winheight
=
24
);
void
cvCreateTestSamples
(
const
char
*
infoname
,
const
char
*
imgfilename
,
int
bgcolor
,
int
bgthreshold
,
void
cvCreatePngTrainingSet
(
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
,
int
invert
,
int
maxintensitydev
,
double
maxxangle
,
double
maxyangle
,
double
maxzangle
,
int
showsamples
,
int
winwidth
,
int
winheight
);
int
winwidth
,
int
winheight
,
IOutput
*
writer
);
/*
* cvCreateTrainingSamplesFromInfo
...
...
@@ -189,4 +201,50 @@ void cvCreateTreeCascadeClassifier( const char* dirname,
int
boosttype
,
int
stumperror
,
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_ */
apps/haartraining/cvsamplesoutput.cpp
0 → 100644
View file @
d7737528
#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
;
}
apps/haartraining/cvsamplesoutput.h
0 → 100644
View file @
d7737528
#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
apps/haartraining/ioutput.h
0 → 100644
View file @
d7737528
#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
doc/user_guide/ug_traincascade.rst
View file @
d7737528
This diff is collapsed.
Click to expand it.
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment