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
70c1b700
Commit
70c1b700
authored
Nov 28, 2013
by
Roman Donchenko
Committed by
OpenCV Buildbot
Nov 28, 2013
Browse files
Options
Browse Files
Download
Plain Diff
Merge pull request #1879 from LeszekSwirski:traincascade-2.4
parents
19b88a17
37a75462
Hide whitespace changes
Inline
Side-by-side
Showing
15 changed files
with
148 additions
and
124 deletions
+148
-124
HOGfeatures.cpp
apps/traincascade/HOGfeatures.cpp
+2
-0
HOGfeatures.h
apps/traincascade/HOGfeatures.h
+12
-12
boost.cpp
apps/traincascade/boost.cpp
+19
-6
boost.h
apps/traincascade/boost.h
+10
-10
cascadeclassifier.cpp
apps/traincascade/cascadeclassifier.cpp
+10
-9
cascadeclassifier.h
apps/traincascade/cascadeclassifier.h
+19
-19
features.cpp
apps/traincascade/features.cpp
+2
-1
haarfeatures.cpp
apps/traincascade/haarfeatures.cpp
+6
-5
haarfeatures.h
apps/traincascade/haarfeatures.h
+15
-15
imagestorage.cpp
apps/traincascade/imagestorage.cpp
+11
-8
imagestorage.h
apps/traincascade/imagestorage.h
+12
-12
lbpfeatures.cpp
apps/traincascade/lbpfeatures.cpp
+2
-0
lbpfeatures.h
apps/traincascade/lbpfeatures.h
+9
-9
traincascade.cpp
apps/traincascade/traincascade.cpp
+2
-1
traincascade_features.h
apps/traincascade/traincascade_features.h
+17
-17
No files found.
apps/traincascade/HOGfeatures.cpp
View file @
70c1b700
...
...
@@ -4,6 +4,8 @@
#include "HOGfeatures.h"
#include "cascadeclassifier.h"
using
namespace
std
;
using
namespace
cv
;
CvHOGFeatureParams
::
CvHOGFeatureParams
()
{
...
...
apps/traincascade/HOGfeatures.h
View file @
70c1b700
...
...
@@ -20,33 +20,33 @@ class CvHOGEvaluator : public CvFeatureEvaluator
public
:
virtual
~
CvHOGEvaluator
()
{}
virtual
void
init
(
const
CvFeatureParams
*
_featureParams
,
int
_maxSampleCount
,
Size
_winSize
);
virtual
void
setImage
(
const
Mat
&
img
,
uchar
clsLabel
,
int
idx
);
int
_maxSampleCount
,
cv
::
Size
_winSize
);
virtual
void
setImage
(
const
cv
::
Mat
&
img
,
uchar
clsLabel
,
int
idx
);
virtual
float
operator
()(
int
varIdx
,
int
sampleIdx
)
const
;
virtual
void
writeFeatures
(
FileStorage
&
fs
,
const
Mat
&
featureMap
)
const
;
virtual
void
writeFeatures
(
cv
::
FileStorage
&
fs
,
const
cv
::
Mat
&
featureMap
)
const
;
protected
:
virtual
void
generateFeatures
();
virtual
void
integralHistogram
(
const
Mat
&
img
,
vector
<
Mat
>
&
histogram
,
Mat
&
norm
,
int
nbins
)
const
;
virtual
void
integralHistogram
(
const
cv
::
Mat
&
img
,
std
::
vector
<
cv
::
Mat
>
&
histogram
,
cv
::
Mat
&
norm
,
int
nbins
)
const
;
class
Feature
{
public
:
Feature
();
Feature
(
int
offset
,
int
x
,
int
y
,
int
cellW
,
int
cellH
);
float
calc
(
const
vector
<
Mat
>
&
_hists
,
const
Mat
&
_normSum
,
size_t
y
,
int
featComponent
)
const
;
void
write
(
FileStorage
&
fs
)
const
;
void
write
(
FileStorage
&
fs
,
int
varIdx
)
const
;
float
calc
(
const
std
::
vector
<
cv
::
Mat
>
&
_hists
,
const
cv
::
Mat
&
_normSum
,
size_t
y
,
int
featComponent
)
const
;
void
write
(
cv
::
FileStorage
&
fs
)
const
;
void
write
(
cv
::
FileStorage
&
fs
,
int
varIdx
)
const
;
Rect
rect
[
N_CELLS
];
//cells
cv
::
Rect
rect
[
N_CELLS
];
//cells
struct
{
int
p0
,
p1
,
p2
,
p3
;
}
fastRect
[
N_CELLS
];
};
vector
<
Feature
>
features
;
std
::
vector
<
Feature
>
features
;
Mat
normSum
;
//for nomalization calculation (L1 or L2)
vector
<
Mat
>
hist
;
cv
::
Mat
normSum
;
//for nomalization calculation (L1 or L2)
std
::
vector
<
cv
::
Mat
>
hist
;
};
inline
float
CvHOGEvaluator
::
operator
()(
int
varIdx
,
int
sampleIdx
)
const
...
...
@@ -57,7 +57,7 @@ inline float CvHOGEvaluator::operator()(int varIdx, int sampleIdx) const
return
features
[
featureIdx
].
calc
(
hist
,
normSum
,
sampleIdx
,
componentIdx
);
}
inline
float
CvHOGEvaluator
::
Feature
::
calc
(
const
vector
<
Mat
>&
_hists
,
const
Mat
&
_normSum
,
size_t
y
,
int
featComponent
)
const
inline
float
CvHOGEvaluator
::
Feature
::
calc
(
const
std
::
vector
<
cv
::
Mat
>&
_hists
,
const
cv
::
Mat
&
_normSum
,
size_t
y
,
int
featComponent
)
const
{
float
normFactor
;
float
res
;
...
...
apps/traincascade/boost.cpp
View file @
70c1b700
#include "opencv2/core/core.hpp"
#include "opencv2/core/internal.hpp"
using
cv
::
Size
;
using
cv
::
Mat
;
using
cv
::
Point
;
using
cv
::
FileStorage
;
using
cv
::
Rect
;
using
cv
::
Ptr
;
using
cv
::
FileNode
;
using
cv
::
Mat_
;
using
cv
::
Range
;
using
cv
::
FileNodeIterator
;
using
cv
::
ParallelLoopBody
;
#include "boost.h"
#include "cascadeclassifier.h"
#include <queue>
...
...
@@ -160,10 +173,10 @@ CvCascadeBoostParams::CvCascadeBoostParams( int _boostType,
void
CvCascadeBoostParams
::
write
(
FileStorage
&
fs
)
const
{
S
tring
boostTypeStr
=
boost_type
==
CvBoost
::
DISCRETE
?
CC_DISCRETE_BOOST
:
s
tring
boostTypeStr
=
boost_type
==
CvBoost
::
DISCRETE
?
CC_DISCRETE_BOOST
:
boost_type
==
CvBoost
::
REAL
?
CC_REAL_BOOST
:
boost_type
==
CvBoost
::
LOGIT
?
CC_LOGIT_BOOST
:
boost_type
==
CvBoost
::
GENTLE
?
CC_GENTLE_BOOST
:
S
tring
();
boost_type
==
CvBoost
::
GENTLE
?
CC_GENTLE_BOOST
:
s
tring
();
CV_Assert
(
!
boostTypeStr
.
empty
()
);
fs
<<
CC_BOOST_TYPE
<<
boostTypeStr
;
fs
<<
CC_MINHITRATE
<<
minHitRate
;
...
...
@@ -175,7 +188,7 @@ void CvCascadeBoostParams::write( FileStorage &fs ) const
bool
CvCascadeBoostParams
::
read
(
const
FileNode
&
node
)
{
S
tring
boostTypeStr
;
s
tring
boostTypeStr
;
FileNode
rnode
=
node
[
CC_BOOST_TYPE
];
rnode
>>
boostTypeStr
;
boost_type
=
!
boostTypeStr
.
compare
(
CC_DISCRETE_BOOST
)
?
CvBoost
::
DISCRETE
:
...
...
@@ -213,10 +226,10 @@ void CvCascadeBoostParams::printDefaults() const
void
CvCascadeBoostParams
::
printAttrs
()
const
{
S
tring
boostTypeStr
=
boost_type
==
CvBoost
::
DISCRETE
?
CC_DISCRETE_BOOST
:
s
tring
boostTypeStr
=
boost_type
==
CvBoost
::
DISCRETE
?
CC_DISCRETE_BOOST
:
boost_type
==
CvBoost
::
REAL
?
CC_REAL_BOOST
:
boost_type
==
CvBoost
::
LOGIT
?
CC_LOGIT_BOOST
:
boost_type
==
CvBoost
::
GENTLE
?
CC_GENTLE_BOOST
:
S
tring
();
boost_type
==
CvBoost
::
GENTLE
?
CC_GENTLE_BOOST
:
s
tring
();
CV_Assert
(
!
boostTypeStr
.
empty
()
);
cout
<<
"boostType: "
<<
boostTypeStr
<<
endl
;
cout
<<
"minHitRate: "
<<
minHitRate
<<
endl
;
...
...
@@ -226,7 +239,7 @@ void CvCascadeBoostParams::printAttrs() const
cout
<<
"maxWeakCount: "
<<
weak_count
<<
endl
;
}
bool
CvCascadeBoostParams
::
scanAttr
(
const
String
prmName
,
const
S
tring
val
)
bool
CvCascadeBoostParams
::
scanAttr
(
const
string
prmName
,
const
s
tring
val
)
{
bool
res
=
true
;
...
...
apps/traincascade/boost.h
View file @
70c1b700
...
...
@@ -13,11 +13,11 @@ struct CvCascadeBoostParams : CvBoostParams
CvCascadeBoostParams
(
int
_boostType
,
float
_minHitRate
,
float
_maxFalseAlarm
,
double
_weightTrimRate
,
int
_maxDepth
,
int
_maxWeakCount
);
virtual
~
CvCascadeBoostParams
()
{}
void
write
(
FileStorage
&
fs
)
const
;
bool
read
(
const
FileNode
&
node
);
void
write
(
cv
::
FileStorage
&
fs
)
const
;
bool
read
(
const
cv
::
FileNode
&
node
);
virtual
void
printDefaults
()
const
;
virtual
void
printAttrs
()
const
;
virtual
bool
scanAttr
(
const
String
prmName
,
const
S
tring
val
);
virtual
bool
scanAttr
(
const
std
::
string
prmName
,
const
std
::
s
tring
val
);
};
struct
CvCascadeBoostTrainData
:
CvDTreeTrainData
...
...
@@ -45,7 +45,7 @@ struct CvCascadeBoostTrainData : CvDTreeTrainData
virtual
void
free_train_data
();
const
CvFeatureEvaluator
*
featureEvaluator
;
Mat
valCache
;
// precalculated feature values (CV_32FC1)
cv
::
Mat
valCache
;
// precalculated feature values (CV_32FC1)
CvMat
_resp
;
// for casting
int
numPrecalcVal
,
numPrecalcIdx
;
};
...
...
@@ -54,9 +54,9 @@ class CvCascadeBoostTree : public CvBoostTree
{
public
:
virtual
CvDTreeNode
*
predict
(
int
sampleIdx
)
const
;
void
write
(
FileStorage
&
fs
,
const
Mat
&
featureMap
);
void
read
(
const
FileNode
&
node
,
CvBoost
*
_ensemble
,
CvDTreeTrainData
*
_data
);
void
markFeaturesInMap
(
Mat
&
featureMap
);
void
write
(
cv
::
FileStorage
&
fs
,
const
cv
::
Mat
&
featureMap
);
void
read
(
const
cv
::
FileNode
&
node
,
CvBoost
*
_ensemble
,
CvDTreeTrainData
*
_data
);
void
markFeaturesInMap
(
cv
::
Mat
&
featureMap
);
protected
:
virtual
void
split_node_data
(
CvDTreeNode
*
n
);
};
...
...
@@ -70,10 +70,10 @@ public:
virtual
float
predict
(
int
sampleIdx
,
bool
returnSum
=
false
)
const
;
float
getThreshold
()
const
{
return
threshold
;
}
void
write
(
FileStorage
&
fs
,
const
Mat
&
featureMap
)
const
;
bool
read
(
const
FileNode
&
node
,
const
CvFeatureEvaluator
*
_featureEvaluator
,
void
write
(
cv
::
FileStorage
&
fs
,
const
cv
::
Mat
&
featureMap
)
const
;
bool
read
(
const
cv
::
FileNode
&
node
,
const
CvFeatureEvaluator
*
_featureEvaluator
,
const
CvCascadeBoostParams
&
_params
);
void
markUsedFeaturesInMap
(
Mat
&
featureMap
);
void
markUsedFeaturesInMap
(
cv
::
Mat
&
featureMap
);
protected
:
virtual
bool
set_params
(
const
CvBoostParams
&
_params
);
virtual
void
update_weights
(
CvBoostTree
*
tree
);
...
...
apps/traincascade/cascadeclassifier.cpp
View file @
70c1b700
...
...
@@ -5,6 +5,7 @@
#include <queue>
using
namespace
std
;
using
namespace
cv
;
static
const
char
*
stageTypes
[]
=
{
CC_BOOST
};
static
const
char
*
featureTypes
[]
=
{
CC_HAAR
,
CC_LBP
,
CC_HOG
};
...
...
@@ -24,10 +25,10 @@ CvCascadeParams::CvCascadeParams( int _stageType, int _featureType ) : stageType
void
CvCascadeParams
::
write
(
FileStorage
&
fs
)
const
{
String
stageTypeStr
=
stageType
==
BOOST
?
CC_BOOST
:
S
tring
();
string
stageTypeStr
=
stageType
==
BOOST
?
CC_BOOST
:
s
tring
();
CV_Assert
(
!
stageTypeStr
.
empty
()
);
fs
<<
CC_STAGE_TYPE
<<
stageTypeStr
;
S
tring
featureTypeStr
=
featureType
==
CvFeatureParams
::
HAAR
?
CC_HAAR
:
s
tring
featureTypeStr
=
featureType
==
CvFeatureParams
::
HAAR
?
CC_HAAR
:
featureType
==
CvFeatureParams
::
LBP
?
CC_LBP
:
featureType
==
CvFeatureParams
::
HOG
?
CC_HOG
:
0
;
...
...
@@ -41,7 +42,7 @@ bool CvCascadeParams::read( const FileNode &node )
{
if
(
node
.
empty
()
)
return
false
;
S
tring
stageTypeStr
,
featureTypeStr
;
s
tring
stageTypeStr
,
featureTypeStr
;
FileNode
rnode
=
node
[
CC_STAGE_TYPE
];
if
(
!
rnode
.
isString
()
)
return
false
;
...
...
@@ -96,7 +97,7 @@ void CvCascadeParams::printAttrs() const
cout
<<
"sampleHeight: "
<<
winSize
.
height
<<
endl
;
}
bool
CvCascadeParams
::
scanAttr
(
const
String
prmName
,
const
S
tring
val
)
bool
CvCascadeParams
::
scanAttr
(
const
string
prmName
,
const
s
tring
val
)
{
bool
res
=
true
;
if
(
!
prmName
.
compare
(
"-stageType"
)
)
...
...
@@ -126,9 +127,9 @@ bool CvCascadeParams::scanAttr( const String prmName, const String val )
//---------------------------- CascadeClassifier --------------------------------------
bool
CvCascadeClassifier
::
train
(
const
S
tring
_cascadeDirName
,
const
S
tring
_posFilename
,
const
S
tring
_negFilename
,
bool
CvCascadeClassifier
::
train
(
const
s
tring
_cascadeDirName
,
const
s
tring
_posFilename
,
const
s
tring
_negFilename
,
int
_numPos
,
int
_numNeg
,
int
_precalcValBufSize
,
int
_precalcIdxBufSize
,
int
_numStages
,
...
...
@@ -411,7 +412,7 @@ bool CvCascadeClassifier::readStages( const FileNode &node)
#define ICV_HAAR_PARENT_NAME "parent"
#define ICV_HAAR_NEXT_NAME "next"
void
CvCascadeClassifier
::
save
(
const
S
tring
filename
,
bool
baseFormat
)
void
CvCascadeClassifier
::
save
(
const
s
tring
filename
,
bool
baseFormat
)
{
FileStorage
fs
(
filename
,
FileStorage
::
WRITE
);
...
...
@@ -503,7 +504,7 @@ void CvCascadeClassifier::save( const String filename, bool baseFormat )
fs
<<
"}"
;
}
bool
CvCascadeClassifier
::
load
(
const
S
tring
cascadeDirName
)
bool
CvCascadeClassifier
::
load
(
const
s
tring
cascadeDirName
)
{
FileStorage
fs
(
cascadeDirName
+
CC_PARAMS_FILENAME
,
FileStorage
::
READ
);
if
(
!
fs
.
isOpened
()
)
...
...
apps/traincascade/cascadeclassifier.h
View file @
70c1b700
...
...
@@ -72,24 +72,24 @@ public:
CvCascadeParams
();
CvCascadeParams
(
int
_stageType
,
int
_featureType
);
void
write
(
FileStorage
&
fs
)
const
;
bool
read
(
const
FileNode
&
node
);
void
write
(
cv
::
FileStorage
&
fs
)
const
;
bool
read
(
const
cv
::
FileNode
&
node
);
void
printDefaults
()
const
;
void
printAttrs
()
const
;
bool
scanAttr
(
const
String
prmName
,
const
S
tring
val
);
bool
scanAttr
(
const
std
::
string
prmName
,
const
std
::
s
tring
val
);
int
stageType
;
int
featureType
;
Size
winSize
;
cv
::
Size
winSize
;
};
class
CvCascadeClassifier
{
public
:
bool
train
(
const
S
tring
_cascadeDirName
,
const
S
tring
_posFilename
,
const
S
tring
_negFilename
,
bool
train
(
const
std
::
s
tring
_cascadeDirName
,
const
std
::
s
tring
_posFilename
,
const
std
::
s
tring
_negFilename
,
int
_numPos
,
int
_numNeg
,
int
_precalcValBufSize
,
int
_precalcIdxBufSize
,
int
_numStages
,
...
...
@@ -99,25 +99,25 @@ public:
bool
baseFormatSave
=
false
);
private
:
int
predict
(
int
sampleIdx
);
void
save
(
const
S
tring
cascadeDirName
,
bool
baseFormat
=
false
);
bool
load
(
const
S
tring
cascadeDirName
);
void
save
(
const
std
::
s
tring
cascadeDirName
,
bool
baseFormat
=
false
);
bool
load
(
const
std
::
s
tring
cascadeDirName
);
bool
updateTrainingSet
(
double
&
acceptanceRatio
);
int
fillPassedSamples
(
int
first
,
int
count
,
bool
isPositive
,
int64
&
consumed
);
void
writeParams
(
FileStorage
&
fs
)
const
;
void
writeStages
(
FileStorage
&
fs
,
const
Mat
&
featureMap
)
const
;
void
writeFeatures
(
FileStorage
&
fs
,
const
Mat
&
featureMap
)
const
;
bool
readParams
(
const
FileNode
&
node
);
bool
readStages
(
const
FileNode
&
node
);
void
writeParams
(
cv
::
FileStorage
&
fs
)
const
;
void
writeStages
(
cv
::
FileStorage
&
fs
,
const
cv
::
Mat
&
featureMap
)
const
;
void
writeFeatures
(
cv
::
FileStorage
&
fs
,
const
cv
::
Mat
&
featureMap
)
const
;
bool
readParams
(
const
cv
::
FileNode
&
node
);
bool
readStages
(
const
cv
::
FileNode
&
node
);
void
getUsedFeaturesIdxMap
(
Mat
&
featureMap
);
void
getUsedFeaturesIdxMap
(
cv
::
Mat
&
featureMap
);
CvCascadeParams
cascadeParams
;
Ptr
<
CvFeatureParams
>
featureParams
;
Ptr
<
CvCascadeBoostParams
>
stageParams
;
cv
::
Ptr
<
CvFeatureParams
>
featureParams
;
cv
::
Ptr
<
CvCascadeBoostParams
>
stageParams
;
Ptr
<
CvFeatureEvaluator
>
featureEvaluator
;
vector
<
Ptr
<
CvCascadeBoost
>
>
stageClassifiers
;
cv
::
Ptr
<
CvFeatureEvaluator
>
featureEvaluator
;
std
::
vector
<
cv
::
Ptr
<
CvCascadeBoost
>
>
stageClassifiers
;
CvCascadeImageReader
imgReader
;
int
numStages
,
curNumSamples
;
int
numPos
,
numNeg
;
...
...
apps/traincascade/features.cpp
View file @
70c1b700
...
...
@@ -5,6 +5,7 @@
#include "cascadeclassifier.h"
using
namespace
std
;
using
namespace
cv
;
float
calcNormFactor
(
const
Mat
&
sum
,
const
Mat
&
sqSum
)
{
...
...
@@ -24,7 +25,7 @@ CvParams::CvParams() : name( "params" ) {}
void
CvParams
::
printDefaults
()
const
{
cout
<<
"--"
<<
name
<<
"--"
<<
endl
;
}
void
CvParams
::
printAttrs
()
const
{}
bool
CvParams
::
scanAttr
(
const
String
,
const
S
tring
)
{
return
false
;
}
bool
CvParams
::
scanAttr
(
const
string
,
const
s
tring
)
{
return
false
;
}
//---------------------------- FeatureParams --------------------------------------
...
...
apps/traincascade/haarfeatures.cpp
View file @
70c1b700
...
...
@@ -5,6 +5,7 @@
#include "cascadeclassifier.h"
using
namespace
std
;
using
namespace
cv
;
CvHaarFeatureParams
::
CvHaarFeatureParams
()
:
mode
(
BASIC
)
{
...
...
@@ -25,9 +26,9 @@ void CvHaarFeatureParams::init( const CvFeatureParams& fp )
void
CvHaarFeatureParams
::
write
(
FileStorage
&
fs
)
const
{
CvFeatureParams
::
write
(
fs
);
S
tring
modeStr
=
mode
==
BASIC
?
CC_MODE_BASIC
:
s
tring
modeStr
=
mode
==
BASIC
?
CC_MODE_BASIC
:
mode
==
CORE
?
CC_MODE_CORE
:
mode
==
ALL
?
CC_MODE_ALL
:
S
tring
();
mode
==
ALL
?
CC_MODE_ALL
:
s
tring
();
CV_Assert
(
!
modeStr
.
empty
()
);
fs
<<
CC_MODE
<<
modeStr
;
}
...
...
@@ -40,7 +41,7 @@ bool CvHaarFeatureParams::read( const FileNode &node )
FileNode
rnode
=
node
[
CC_MODE
];
if
(
!
rnode
.
isString
()
)
return
false
;
S
tring
modeStr
;
s
tring
modeStr
;
rnode
>>
modeStr
;
mode
=
!
modeStr
.
compare
(
CC_MODE_BASIC
)
?
BASIC
:
!
modeStr
.
compare
(
CC_MODE_CORE
)
?
CORE
:
...
...
@@ -58,13 +59,13 @@ void CvHaarFeatureParams::printDefaults() const
void
CvHaarFeatureParams
::
printAttrs
()
const
{
CvFeatureParams
::
printAttrs
();
S
tring
mode_str
=
mode
==
BASIC
?
CC_MODE_BASIC
:
s
tring
mode_str
=
mode
==
BASIC
?
CC_MODE_BASIC
:
mode
==
CORE
?
CC_MODE_CORE
:
mode
==
ALL
?
CC_MODE_ALL
:
0
;
cout
<<
"mode: "
<<
mode_str
<<
endl
;
}
bool
CvHaarFeatureParams
::
scanAttr
(
const
String
prmName
,
const
S
tring
val
)
bool
CvHaarFeatureParams
::
scanAttr
(
const
string
prmName
,
const
s
tring
val
)
{
if
(
!
CvFeatureParams
::
scanAttr
(
prmName
,
val
)
)
{
...
...
apps/traincascade/haarfeatures.h
View file @
70c1b700
...
...
@@ -18,12 +18,12 @@ public:
CvHaarFeatureParams
(
int
_mode
);
virtual
void
init
(
const
CvFeatureParams
&
fp
);
virtual
void
write
(
FileStorage
&
fs
)
const
;
virtual
bool
read
(
const
FileNode
&
node
);
virtual
void
write
(
cv
::
FileStorage
&
fs
)
const
;
virtual
bool
read
(
const
cv
::
FileNode
&
node
);
virtual
void
printDefaults
()
const
;
virtual
void
printAttrs
()
const
;
virtual
bool
scanAttr
(
const
String
prm
,
const
S
tring
val
);
virtual
bool
scanAttr
(
const
std
::
string
prm
,
const
std
::
s
tring
val
);
int
mode
;
};
...
...
@@ -32,11 +32,11 @@ class CvHaarEvaluator : public CvFeatureEvaluator
{
public
:
virtual
void
init
(
const
CvFeatureParams
*
_featureParams
,
int
_maxSampleCount
,
Size
_winSize
);
virtual
void
setImage
(
const
Mat
&
img
,
uchar
clsLabel
,
int
idx
);
int
_maxSampleCount
,
cv
::
Size
_winSize
);
virtual
void
setImage
(
const
cv
::
Mat
&
img
,
uchar
clsLabel
,
int
idx
);
virtual
float
operator
()(
int
featureIdx
,
int
sampleIdx
)
const
;
virtual
void
writeFeatures
(
FileStorage
&
fs
,
const
Mat
&
featureMap
)
const
;
void
writeFeature
(
FileStorage
&
fs
,
int
fi
)
const
;
// for old file fornat
virtual
void
writeFeatures
(
cv
::
FileStorage
&
fs
,
const
cv
::
Mat
&
featureMap
)
const
;
void
writeFeature
(
cv
::
FileStorage
&
fs
,
int
fi
)
const
;
// for old file fornat
protected
:
virtual
void
generateFeatures
();
...
...
@@ -48,13 +48,13 @@ protected:
int
x0
,
int
y0
,
int
w0
,
int
h0
,
float
wt0
,
int
x1
,
int
y1
,
int
w1
,
int
h1
,
float
wt1
,
int
x2
=
0
,
int
y2
=
0
,
int
w2
=
0
,
int
h2
=
0
,
float
wt2
=
0
.
0
F
);
float
calc
(
const
Mat
&
sum
,
const
Mat
&
tilted
,
size_t
y
)
const
;
void
write
(
FileStorage
&
fs
)
const
;
float
calc
(
const
cv
::
Mat
&
sum
,
const
cv
::
Mat
&
tilted
,
size_t
y
)
const
;
void
write
(
cv
::
FileStorage
&
fs
)
const
;
bool
tilted
;
struct
{
Rect
r
;
cv
::
Rect
r
;
float
weight
;
}
rect
[
CV_HAAR_FEATURE_MAX
];
...
...
@@ -64,10 +64,10 @@ protected:
}
fastRect
[
CV_HAAR_FEATURE_MAX
];
};
vector
<
Feature
>
features
;
Mat
sum
;
/* sum images (each row represents image) */
Mat
tilted
;
/* tilted sum images (each row represents image) */
Mat
normfactor
;
/* normalization factor */
std
::
vector
<
Feature
>
features
;
cv
::
Mat
sum
;
/* sum images (each row represents image) */
cv
::
Mat
tilted
;
/* tilted sum images (each row represents image) */
cv
::
Mat
normfactor
;
/* normalization factor */
};
inline
float
CvHaarEvaluator
::
operator
()(
int
featureIdx
,
int
sampleIdx
)
const
...
...
@@ -76,7 +76,7 @@ inline float CvHaarEvaluator::operator()(int featureIdx, int sampleIdx) const
return
!
nf
?
0
.
0
f
:
(
features
[
featureIdx
].
calc
(
sum
,
tilted
,
sampleIdx
)
/
nf
);
}
inline
float
CvHaarEvaluator
::
Feature
::
calc
(
const
Mat
&
_sum
,
const
Mat
&
_tilted
,
size_t
y
)
const
inline
float
CvHaarEvaluator
::
Feature
::
calc
(
const
cv
::
Mat
&
_sum
,
const
cv
::
Mat
&
_tilted
,
size_t
y
)
const
{
const
int
*
img
=
tilted
?
_tilted
.
ptr
<
int
>
((
int
)
y
)
:
_sum
.
ptr
<
int
>
((
int
)
y
);
float
ret
=
rect
[
0
].
weight
*
(
img
[
fastRect
[
0
].
p0
]
-
img
[
fastRect
[
0
].
p1
]
-
img
[
fastRect
[
0
].
p2
]
+
img
[
fastRect
[
0
].
p3
]
)
+
...
...
apps/traincascade/imagestorage.cpp
View file @
70c1b700
...
...
@@ -7,7 +7,10 @@
#include <iostream>
#include <fstream>
bool
CvCascadeImageReader
::
create
(
const
String
_posFilename
,
const
String
_negFilename
,
Size
_winSize
)
using
namespace
std
;
using
namespace
cv
;
bool
CvCascadeImageReader
::
create
(
const
string
_posFilename
,
const
string
_negFilename
,
Size
_winSize
)
{
return
posReader
.
create
(
_posFilename
)
&&
negReader
.
create
(
_negFilename
,
_winSize
);
}
...
...
@@ -22,21 +25,21 @@ CvCascadeImageReader::NegReader::NegReader()
stepFactor
=
0.5
F
;
}
bool
CvCascadeImageReader
::
NegReader
::
create
(
const
S
tring
_filename
,
Size
_winSize
)
bool
CvCascadeImageReader
::
NegReader
::
create
(
const
s
tring
_filename
,
Size
_winSize
)
{
S
tring
dirname
,
str
;
s
tring
dirname
,
str
;
std
::
ifstream
file
(
_filename
.
c_str
());
if
(
!
file
.
is_open
()
)
return
false
;
size_t
pos
=
_filename
.
rfind
(
'\\'
);
char
dlmrt
=
'\\'
;
if
(
pos
==
S
tring
::
npos
)
if
(
pos
==
s
tring
::
npos
)
{
pos
=
_filename
.
rfind
(
'/'
);
dlmrt
=
'/'
;
}
dirname
=
pos
==
S
tring
::
npos
?
""
:
_filename
.
substr
(
0
,
pos
)
+
dlmrt
;
dirname
=
pos
==
s
tring
::
npos
?
""
:
_filename
.
substr
(
0
,
pos
)
+
dlmrt
;
while
(
!
file
.
eof
()
)
{
std
::
getline
(
file
,
str
);
...
...
@@ -64,8 +67,8 @@ bool CvCascadeImageReader::NegReader::nextImg()
round
=
round
%
(
winSize
.
width
*
winSize
.
height
);
last
%=
count
;
_offset
.
x
=
min
(
(
int
)
round
%
winSize
.
width
,
src
.
cols
-
winSize
.
width
);
_offset
.
y
=
min
(
(
int
)
round
/
winSize
.
width
,
src
.
rows
-
winSize
.
height
);
_offset
.
x
=
std
::
min
(
(
int
)
round
%
winSize
.
width
,
src
.
cols
-
winSize
.
width
);
_offset
.
y
=
std
::
min
(
(
int
)
round
/
winSize
.
width
,
src
.
rows
-
winSize
.
height
);
if
(
!
src
.
empty
()
&&
src
.
type
()
==
CV_8UC1
&&
offset
.
x
>=
0
&&
offset
.
y
>=
0
)
break
;
...
...
@@ -126,7 +129,7 @@ CvCascadeImageReader::PosReader::PosReader()
vec
=
0
;
}
bool
CvCascadeImageReader
::
PosReader
::
create
(
const
S
tring
_filename
)
bool
CvCascadeImageReader
::
PosReader
::
create
(
const
s
tring
_filename
)
{
if
(
file
)
fclose
(
file
);
...
...
apps/traincascade/imagestorage.h
View file @
70c1b700
...
...
@@ -3,15 +3,15 @@
#include "highgui.h"
using
namespace
cv
;
class
CvCascadeImageReader
{
public
:
bool
create
(
const
String
_posFilename
,
const
String
_negFilename
,
Size
_winSize
);
bool
create
(
const
std
::
string
_posFilename
,
const
std
::
string
_negFilename
,
cv
::
Size
_winSize
);
void
restart
()
{
posReader
.
restart
();
}
bool
getNeg
(
Mat
&
_img
)
{
return
negReader
.
get
(
_img
);
}
bool
getPos
(
Mat
&
_img
)
{
return
posReader
.
get
(
_img
);
}
bool
getNeg
(
cv
::
Mat
&
_img
)
{
return
negReader
.
get
(
_img
);
}
bool
getPos
(
cv
::
Mat
&
_img
)
{
return
posReader
.
get
(
_img
);
}
private
:
class
PosReader
...
...
@@ -19,8 +19,8 @@ private:
public
:
PosReader
();
virtual
~
PosReader
();
bool
create
(
const
S
tring
_filename
);
bool
get
(
Mat
&
_img
);
bool
create
(
const
std
::
s
tring
_filename
);
bool
get
(
cv
::
Mat
&
_img
);
void
restart
();
short
*
vec
;
...
...
@@ -35,18 +35,18 @@ private:
{
public
:
NegReader
();
bool
create
(
const
String
_filename
,
Size
_winSize
);
bool
get
(
Mat
&
_img
);
bool
create
(
const
std
::
string
_filename
,
cv
::
Size
_winSize
);
bool
get
(
cv
::
Mat
&
_img
);
bool
nextImg
();
Mat
src
,
img
;
vector
<
S
tring
>
imgFilenames
;
Point
offset
,
point
;
cv
::
Mat
src
,
img
;
std
::
vector
<
std
::
s
tring
>
imgFilenames
;
cv
::
Point
offset
,
point
;
float
scale
;
float
scaleFactor
;
float
stepFactor
;
size_t
last
,
round
;
Size
winSize
;
cv
::
Size
winSize
;
}
negReader
;
};
...
...
apps/traincascade/lbpfeatures.cpp
View file @
70c1b700
...
...
@@ -4,6 +4,8 @@
#include "lbpfeatures.h"
#include "cascadeclassifier.h"
using
namespace
cv
;
CvLBPFeatureParams
::
CvLBPFeatureParams
()
{
maxCatCount
=
256
;
...
...
apps/traincascade/lbpfeatures.h
View file @
70c1b700
...
...
@@ -15,11 +15,11 @@ class CvLBPEvaluator : public CvFeatureEvaluator
public
:
virtual
~
CvLBPEvaluator
()
{}
virtual
void
init
(
const
CvFeatureParams
*
_featureParams
,
int
_maxSampleCount
,
Size
_winSize
);
virtual
void
setImage
(
const
Mat
&
img
,
uchar
clsLabel
,
int
idx
);
int
_maxSampleCount
,
cv
::
Size
_winSize
);
virtual
void
setImage
(
const
cv
::
Mat
&
img
,
uchar
clsLabel
,
int
idx
);
virtual
float
operator
()(
int
featureIdx
,
int
sampleIdx
)
const
{
return
(
float
)
features
[
featureIdx
].
calc
(
sum
,
sampleIdx
);
}
virtual
void
writeFeatures
(
FileStorage
&
fs
,
const
Mat
&
featureMap
)
const
;
virtual
void
writeFeatures
(
cv
::
FileStorage
&
fs
,
const
cv
::
Mat
&
featureMap
)
const
;
protected
:
virtual
void
generateFeatures
();
...
...
@@ -28,18 +28,18 @@ protected:
public
:
Feature
();
Feature
(
int
offset
,
int
x
,
int
y
,
int
_block_w
,
int
_block_h
);
uchar
calc
(
const
Mat
&
_sum
,
size_t
y
)
const
;
void
write
(
FileStorage
&
fs
)
const
;
uchar
calc
(
const
cv
::
Mat
&
_sum
,
size_t
y
)
const
;
void
write
(
cv
::
FileStorage
&
fs
)
const
;
Rect
rect
;
cv
::
Rect
rect
;
int
p
[
16
];
};
vector
<
Feature
>
features
;
std
::
vector
<
Feature
>
features
;
Mat
sum
;
cv
::
Mat
sum
;
};
inline
uchar
CvLBPEvaluator
::
Feature
::
calc
(
const
Mat
&
_sum
,
size_t
y
)
const
inline
uchar
CvLBPEvaluator
::
Feature
::
calc
(
const
cv
::
Mat
&
_sum
,
size_t
y
)
const
{
const
int
*
psum
=
_sum
.
ptr
<
int
>
((
int
)
y
);
int
cval
=
psum
[
p
[
5
]]
-
psum
[
p
[
6
]]
-
psum
[
p
[
9
]]
+
psum
[
p
[
10
]];
...
...
apps/traincascade/traincascade.cpp
View file @
70c1b700
...
...
@@ -5,11 +5,12 @@
#include "cascadeclassifier.h"
using
namespace
std
;
using
namespace
cv
;
int
main
(
int
argc
,
char
*
argv
[]
)
{
CvCascadeClassifier
classifier
;
S
tring
cascadeDirName
,
vecName
,
bgName
;
s
tring
cascadeDirName
,
vecName
,
bgName
;
int
numPos
=
2000
;
int
numNeg
=
1000
;
int
numStages
=
20
;
...
...
apps/traincascade/traincascade_features.h
View file @
70c1b700
...
...
@@ -30,13 +30,13 @@
(p3) = (rect).x + (rect).width - (rect).height \
+ (step) * ((rect).y + (rect).width + (rect).height);
float
calcNormFactor
(
const
Mat
&
sum
,
const
Mat
&
sqSum
);
float
calcNormFactor
(
const
cv
::
Mat
&
sum
,
const
cv
::
Mat
&
sqSum
);
template
<
class
Feature
>
void
_writeFeatures
(
const
vector
<
Feature
>
features
,
FileStorage
&
fs
,
const
Mat
&
featureMap
)
void
_writeFeatures
(
const
std
::
vector
<
Feature
>
features
,
cv
::
FileStorage
&
fs
,
const
cv
::
Mat
&
featureMap
)
{
fs
<<
FEATURES
<<
"["
;
const
Mat_
<
int
>&
featureMap_
=
(
const
Mat_
<
int
>&
)
featureMap
;
const
cv
::
Mat_
<
int
>&
featureMap_
=
(
const
cv
::
Mat_
<
int
>&
)
featureMap
;
for
(
int
fi
=
0
;
fi
<
featureMap
.
cols
;
fi
++
)
if
(
featureMap_
(
0
,
fi
)
>=
0
)
{
...
...
@@ -53,13 +53,13 @@ public:
CvParams
();
virtual
~
CvParams
()
{}
// from|to file
virtual
void
write
(
FileStorage
&
fs
)
const
=
0
;
virtual
bool
read
(
const
FileNode
&
node
)
=
0
;
virtual
void
write
(
cv
::
FileStorage
&
fs
)
const
=
0
;
virtual
bool
read
(
const
cv
::
FileNode
&
node
)
=
0
;
// from|to screen
virtual
void
printDefaults
()
const
;
virtual
void
printAttrs
()
const
;
virtual
bool
scanAttr
(
const
String
prmName
,
const
S
tring
val
);
S
tring
name
;
virtual
bool
scanAttr
(
const
std
::
string
prmName
,
const
std
::
s
tring
val
);
std
::
s
tring
name
;
};
class
CvFeatureParams
:
public
CvParams
...
...
@@ -68,9 +68,9 @@ public:
enum
{
HAAR
=
0
,
LBP
=
1
,
HOG
=
2
};
CvFeatureParams
();
virtual
void
init
(
const
CvFeatureParams
&
fp
);
virtual
void
write
(
FileStorage
&
fs
)
const
;
virtual
bool
read
(
const
FileNode
&
node
);
static
Ptr
<
CvFeatureParams
>
create
(
int
featureType
);
virtual
void
write
(
cv
::
FileStorage
&
fs
)
const
;
virtual
bool
read
(
const
cv
::
FileNode
&
node
);
static
cv
::
Ptr
<
CvFeatureParams
>
create
(
int
featureType
);
int
maxCatCount
;
// 0 in case of numerical features
int
featSize
;
// 1 in case of simple features (HAAR, LBP) and N_BINS(9)*N_CELLS(4) in case of Dalal's HOG features
};
...
...
@@ -80,25 +80,25 @@ class CvFeatureEvaluator
public
:
virtual
~
CvFeatureEvaluator
()
{}
virtual
void
init
(
const
CvFeatureParams
*
_featureParams
,
int
_maxSampleCount
,
Size
_winSize
);
virtual
void
setImage
(
const
Mat
&
img
,
uchar
clsLabel
,
int
idx
);
virtual
void
writeFeatures
(
FileStorage
&
fs
,
const
Mat
&
featureMap
)
const
=
0
;
int
_maxSampleCount
,
cv
::
Size
_winSize
);
virtual
void
setImage
(
const
cv
::
Mat
&
img
,
uchar
clsLabel
,
int
idx
);
virtual
void
writeFeatures
(
cv
::
FileStorage
&
fs
,
const
cv
::
Mat
&
featureMap
)
const
=
0
;
virtual
float
operator
()(
int
featureIdx
,
int
sampleIdx
)
const
=
0
;
static
Ptr
<
CvFeatureEvaluator
>
create
(
int
type
);
static
cv
::
Ptr
<
CvFeatureEvaluator
>
create
(
int
type
);
int
getNumFeatures
()
const
{
return
numFeatures
;
}
int
getMaxCatCount
()
const
{
return
featureParams
->
maxCatCount
;
}
int
getFeatureSize
()
const
{
return
featureParams
->
featSize
;
}
const
Mat
&
getCls
()
const
{
return
cls
;
}
const
cv
::
Mat
&
getCls
()
const
{
return
cls
;
}
float
getCls
(
int
si
)
const
{
return
cls
.
at
<
float
>
(
si
,
0
);
}
protected
:
virtual
void
generateFeatures
()
=
0
;
int
npos
,
nneg
;
int
numFeatures
;
Size
winSize
;
cv
::
Size
winSize
;
CvFeatureParams
*
featureParams
;
Mat
cls
;
cv
::
Mat
cls
;
};
#endif
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