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
0934344a
Commit
0934344a
authored
Sep 12, 2013
by
Mathieu Barnachon
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Update sample and code with external computation of HOG detector.
parent
2fe340bf
Show whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
49 additions
and
40 deletions
+49
-40
ml.hpp
modules/ml/include/opencv2/ml.hpp
+1
-2
svm.cpp
modules/ml/src/svm.cpp
+0
-32
train_HOG.cpp
samples/cpp/train_HOG.cpp
+48
-6
No files found.
modules/ml/include/opencv2/ml.hpp
View file @
0934344a
...
...
@@ -518,8 +518,7 @@ public:
virtual
CvSVMParams
get_params
()
const
{
return
params
;
};
CV_WRAP
virtual
void
clear
();
// return a single vector for HOG detector.
virtual
void
get_svm_detector
(
std
::
vector
<
float
>
&
detector
)
const
;
virtual
const
CvSVMDecisionFunc
*
get_decision_function
()
const
{
return
decision_func
;
}
static
CvParamGrid
get_default_grid
(
int
param_id
);
...
...
modules/ml/src/svm.cpp
View file @
0934344a
...
...
@@ -1245,38 +1245,6 @@ const float* CvSVM::get_support_vector(int i) const
return
sv
&&
(
unsigned
)
i
<
(
unsigned
)
sv_total
?
sv
[
i
]
:
0
;
}
void
CvSVM
::
get_svm_detector
(
std
::
vector
<
float
>
&
detector
)
const
{
CV_Assert
(
var_all
>
0
&&
sv_total
>
0
&&
sv
!=
0
&&
decision_func
!=
0
&&
decision_func
->
alpha
!=
0
&&
decision_func
->
sv_count
==
sv_total
);
float
svi
=
0.
f
;
detector
.
clear
();
//clear stuff in vector.
detector
.
reserve
(
var_all
+
1
);
//reserve place for memory efficiency.
/**
* detector^i = \sum_j support_vector_j^i * \alpha_j
* detector^dim = -\rho
*/
for
(
int
i
=
0
;
i
<
var_all
;
++
i
)
{
svi
=
0.
f
;
for
(
int
j
=
0
;
j
<
sv_total
;
++
j
)
{
if
(
decision_func
->
sv_index
!=
NULL
)
// sometime the sv_index isn't store on YML/XML.
svi
+=
(
float
)(
sv
[
decision_func
->
sv_index
[
j
]][
i
]
*
decision_func
->
alpha
[
j
]
);
else
svi
+=
(
float
)(
sv
[
j
][
i
]
*
decision_func
->
alpha
[
j
]
);
}
detector
.
push_back
(
svi
);
}
detector
.
push_back
(
(
float
)
-
decision_func
->
rho
);
}
bool
CvSVM
::
set_params
(
const
CvSVMParams
&
_params
)
{
bool
ok
=
false
;
...
...
samples/cpp/train_HOG.cpp
View file @
0934344a
...
...
@@ -11,11 +11,53 @@ using namespace cv;
using
namespace
std
;
/*
* Convert training/testing set to be used by OpenCV Machine Learning algorithms.
* TrainData is a matrix of size (#samples x max(#cols,#rows) per samples), in 32FC1.
* Transposition of samples are made if needed.
void
get_svm_detector
(
const
SVM
&
svm
,
vector
<
float
>
&
hog_detector
)
{
// get the number of variables
const
int
var_all
=
svm
.
get_var_count
();
// get the number of support vectors
const
int
sv_total
=
svm
.
get_support_vector_count
();
// get the decision function
const
CvSVMDecisionFunc
*
decision_func
=
svm
.
get_decision_function
();
// get the support vectors
const
float
**
sv
=
&
(
svm
.
get_support_vector
(
0
));
CV_Assert
(
var_all
>
0
&&
sv_total
>
0
&&
decision_func
!=
0
&&
decision_func
->
alpha
!=
0
&&
decision_func
->
sv_count
==
sv_total
);
float
svi
=
0.
f
;
hog_detector
.
clear
();
//clear stuff in vector.
hog_detector
.
reserve
(
var_all
+
1
);
//reserve place for memory efficiency.
/**
* hog_detector^i = \sum_j support_vector_j^i * \alpha_j
* hog_detector^dim = -\rho
*/
for
(
int
i
=
0
;
i
<
var_all
;
++
i
)
{
svi
=
0.
f
;
for
(
int
j
=
0
;
j
<
sv_total
;
++
j
)
{
if
(
decision_func
->
sv_index
!=
NULL
)
// sometime the sv_index isn't store on YML/XML.
svi
+=
(
float
)(
sv
[
decision_func
->
sv_index
[
j
]][
i
]
*
decision_func
->
alpha
[
j
]
);
else
svi
+=
(
float
)(
sv
[
j
][
i
]
*
decision_func
->
alpha
[
j
]
);
}
hog_detector
.
push_back
(
svi
);
}
hog_detector
.
push_back
(
(
float
)
-
decision_func
->
rho
);
}
/*
* Convert training/testing set to be used by OpenCV Machine Learning algorithms.
* TrainData is a matrix of size (#samples x max(#cols,#rows) per samples), in 32FC1.
* Transposition of samples are made if needed.
*/
void
convert_to_ml
(
const
std
::
vector
<
cv
::
Mat
>
&
train_samples
,
cv
::
Mat
&
trainData
)
{
//--Convert data
...
...
@@ -322,7 +364,7 @@ void test_it( const Size & size )
Scalar
reference
(
0
,
255
,
0
);
Scalar
trained
(
0
,
0
,
255
);
Mat
img
,
draw
;
SVM
svm
;
My
SVM
svm
;
HOGDescriptor
hog
;
HOGDescriptor
my_hog
;
my_hog
.
winSize
=
size
;
...
...
@@ -333,7 +375,7 @@ void test_it( const Size & size )
svm
.
load
(
"my_people_detector.yml"
);
// Set the trained svm to my_hog
vector
<
float
>
hog_detector
;
svm
.
get_svm_detector
(
hog_detector
);
get_svm_detector
(
svm
,
hog_detector
);
my_hog
.
setSVMDetector
(
hog_detector
);
// Set the people detector.
hog
.
setSVMDetector
(
hog
.
getDefaultPeopleDetector
()
);
...
...
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