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
241e2d23
Commit
241e2d23
authored
Jul 10, 2013
by
Roman Donchenko
Committed by
OpenCV Buildbot
Jul 10, 2013
Browse files
Options
Browse Files
Download
Plain Diff
Merge pull request #1077 from bitwangyaoyao:2.4_kmeans
parents
894b30b3
88ed74a7
Expand all
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
258 additions
and
0 deletions
+258
-0
ocl.hpp
modules/ocl/include/opencv2/ocl/ocl.hpp
+12
-0
kmeans.cpp
modules/ocl/src/kmeans.cpp
+0
-0
kmeans_kernel.cl
modules/ocl/src/opencl/kmeans_kernel.cl
+84
-0
test_kmeans.cpp
modules/ocl/test/test_kmeans.cpp
+162
-0
No files found.
modules/ocl/include/opencv2/ocl/ocl.hpp
View file @
241e2d23
...
...
@@ -834,6 +834,18 @@ namespace cv
CV_EXPORTS
void
cornerMinEigenVal_dxdy
(
const
oclMat
&
src
,
oclMat
&
dst
,
oclMat
&
Dx
,
oclMat
&
Dy
,
int
blockSize
,
int
ksize
,
int
bordertype
=
cv
::
BORDER_DEFAULT
);
/////////////////////////////////// ML ///////////////////////////////////////////
//! Compute closest centers for each lines in source and lable it after center's index
// supports CV_32FC1/CV_32FC2/CV_32FC4 data type
CV_EXPORTS
void
distanceToCenters
(
oclMat
&
dists
,
oclMat
&
labels
,
const
oclMat
&
src
,
const
oclMat
&
centers
);
//!Does k-means procedure on GPU
// supports CV_32FC1/CV_32FC2/CV_32FC4 data type
CV_EXPORTS
double
kmeans
(
const
oclMat
&
src
,
int
K
,
oclMat
&
bestLabels
,
TermCriteria
criteria
,
int
attemps
,
int
flags
,
oclMat
&
centers
);
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////CascadeClassifier//////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
...
...
modules/ocl/src/kmeans.cpp
0 → 100644
View file @
241e2d23
This diff is collapsed.
Click to expand it.
modules/ocl/src/opencl/kmeans_kernel.cl
0 → 100644
View file @
241e2d23
/*M///////////////////////////////////////////////////////////////////////////////////////
//
//
IMPORTANT:
READ
BEFORE
DOWNLOADING,
COPYING,
INSTALLING
OR
USING.
//
//
By
downloading,
copying,
installing
or
using
the
software
you
agree
to
this
license.
//
If
you
do
not
agree
to
this
license,
do
not
download,
install,
//
copy
or
use
the
software.
//
//
//
License
Agreement
//
For
Open
Source
Computer
Vision
Library
//
//
Copyright
(
C
)
2010-2012,
Multicoreware,
Inc.,
all
rights
reserved.
//
Copyright
(
C
)
2010-2012,
Advanced
Micro
Devices,
Inc.,
all
rights
reserved.
//
Third
party
copyrights
are
property
of
their
respective
owners.
//
//
@Authors
//
Xiaopeng
Fu,
fuxiaopeng2222@163.com
//
//
Redistribution
and
use
in
source
and
binary
forms,
with
or
without
modification,
//
are
permitted
provided
that
the
following
conditions
are
met:
//
//
*
Redistribution
's
of
source
code
must
retain
the
above
copyright
notice,
//
this
list
of
conditions
and
the
following
disclaimer.
//
//
*
Redistribution
's
in
binary
form
must
reproduce
the
above
copyright
notice,
//
this
list
of
conditions
and
the
following
disclaimer
in
the
documentation
//
and/or
other
GpuMaterials
provided
with
the
distribution.
//
//
*
The
name
of
the
copyright
holders
may
not
be
used
to
endorse
or
promote
products
//
derived
from
this
software
without
specific
prior
written
permission.
//
//
This
software
is
provided
by
the
copyright
holders
and
contributors
as
is
and
//
any
express
or
implied
warranties,
including,
but
not
limited
to,
the
implied
//
warranties
of
merchantability
and
fitness
for
a
particular
purpose
are
disclaimed.
//
In
no
event
shall
the
Intel
Corporation
or
contributors
be
liable
for
any
direct,
//
indirect,
incidental,
special,
exemplary,
or
consequential
damages
//
(
including,
but
not
limited
to,
procurement
of
substitute
goods
or
services
;
//
loss
of
use,
data,
or
profits
; or business interruption) however caused
//
and
on
any
theory
of
liability,
whether
in
contract,
strict
liability,
//
or
tort
(
including
negligence
or
otherwise
)
arising
in
any
way
out
of
//
the
use
of
this
software,
even
if
advised
of
the
possibility
of
such
damage.
//
//M*/
__kernel
void
distanceToCenters
(
int
label_step,
int
K,
__global
float
*src,
__global
int
*labels,
int
dims,
int
rows,
__global
float
*centers,
__global
float
*dists
)
{
int
gid
=
get_global_id
(
1
)
;
float
dist,
euDist,
min
;
int
minCentroid
;
if
(
gid
>=
rows
)
return
;
for
(
int
i
=
0
; i < K; i++)
{
euDist
=
0
;
for
(
int
j
=
0
; j < dims; j++)
{
dist
=
(
src[j
+
gid
*
dims]
-
centers[j
+
i
*
dims]
)
;
euDist
+=
dist
*
dist
;
}
if
(
i
==
0
)
{
min
=
euDist
;
minCentroid
=
0
;
}
else
if
(
euDist
<
min
)
{
min
=
euDist
;
minCentroid
=
i
;
}
}
dists[gid]
=
min
;
labels[label_step
*
gid]
=
minCentroid
;
}
modules/ocl/test/test_kmeans.cpp
0 → 100644
View file @
241e2d23
/*M///////////////////////////////////////////////////////////////////////////////////////
//
// IMPORTANT: READ BEFORE DOWNLOADING, COPYING, INSTALLING OR USING.
//
// By downloading, copying, installing or using the software you agree to this license.
// If you do not agree to this license, do not download, install,
// copy or use the software.
//
//
// License Agreement
// For Open Source Computer Vision Library
//
// Copyright (C) 2010-2012, Multicoreware, Inc., all rights reserved.
// Copyright (C) 2010-2012, Advanced Micro Devices, Inc., all rights reserved.
// Third party copyrights are property of their respective owners.
//
// @Authors
// Erping Pang, pang_er_ping@163.com
// Xiaopeng Fu, fuxiaopeng2222@163.com
//
// Redistribution and use in source and binary forms, with or without modification,
// are permitted provided that the following conditions are met:
//
// * Redistribution's of source code must retain the above copyright notice,
// this list of conditions and the following disclaimer.
//
// * Redistribution's in binary form must reproduce the above copyright notice,
// this list of conditions and the following disclaimer in the documentation
// and/or other oclMaterials provided with the distribution.
//
// * The name of the copyright holders may not be used to endorse or promote products
// derived from this software without specific prior written permission.
//
// This software is provided by the copyright holders and contributors "as is" and
// any express or implied warranties, including, but not limited to, the implied
// warranties of merchantability and fitness for a particular purpose are disclaimed.
// In no event shall the Intel Corporation or contributors be liable for any direct,
// indirect, incidental, special, exemplary, or consequential damages
// (including, but not limited to, procurement of substitute goods or services;
// loss of use, data, or profits; or business interruption) however caused
// and on any theory of liability, whether in contract, strict liability,
// or tort (including negligence or otherwise) arising in any way out of
// the use of this software, even if advised of the possibility of such damage.
//
//M*/
#include "precomp.hpp"
#ifdef HAVE_OPENCL
using
namespace
cvtest
;
using
namespace
testing
;
using
namespace
std
;
using
namespace
cv
;
#define OCL_KMEANS_USE_INITIAL_LABELS 1
#define OCL_KMEANS_PP_CENTERS 2
PARAM_TEST_CASE
(
Kmeans
,
int
,
int
,
int
)
{
int
type
;
int
K
;
int
flags
;
cv
::
Mat
src
;
ocl
::
oclMat
d_src
,
d_dists
;
Mat
labels
,
centers
;
ocl
::
oclMat
d_labels
,
d_centers
;
cv
::
RNG
rng
;
virtual
void
SetUp
(){
K
=
GET_PARAM
(
0
);
type
=
GET_PARAM
(
1
);
flags
=
GET_PARAM
(
2
);
rng
=
TS
::
ptr
()
->
get_rng
();
// MWIDTH=256, MHEIGHT=256. defined in utility.hpp
cv
::
Size
size
=
cv
::
Size
(
MWIDTH
,
MHEIGHT
);
src
.
create
(
size
,
type
);
int
row_idx
=
0
;
const
int
max_neighbour
=
MHEIGHT
/
K
-
1
;
CV_Assert
(
K
<=
MWIDTH
);
for
(
int
i
=
0
;
i
<
K
;
i
++
)
{
Mat
center_row_header
=
src
.
row
(
row_idx
);
center_row_header
.
setTo
(
0
);
int
nchannel
=
center_row_header
.
channels
();
for
(
int
j
=
0
;
j
<
nchannel
;
j
++
)
center_row_header
.
at
<
float
>
(
0
,
i
*
nchannel
+
j
)
=
50000.0
;
for
(
int
j
=
0
;
(
j
<
max_neighbour
)
||
(
i
==
K
-
1
&&
j
<
max_neighbour
+
MHEIGHT
%
K
);
j
++
)
{
Mat
cur_row_header
=
src
.
row
(
row_idx
+
1
+
j
);
center_row_header
.
copyTo
(
cur_row_header
);
Mat
tmpmat
=
randomMat
(
rng
,
cur_row_header
.
size
(),
cur_row_header
.
type
(),
-
200
,
200
,
false
);
cur_row_header
+=
tmpmat
;
}
row_idx
+=
1
+
max_neighbour
;
}
}
};
TEST_P
(
Kmeans
,
Mat
){
if
(
flags
&
KMEANS_USE_INITIAL_LABELS
)
{
// inital a given labels
labels
.
create
(
src
.
rows
,
1
,
CV_32S
);
int
*
label
=
labels
.
ptr
<
int
>
();
for
(
int
i
=
0
;
i
<
src
.
rows
;
i
++
)
label
[
i
]
=
rng
.
uniform
(
0
,
K
);
d_labels
.
upload
(
labels
);
}
d_src
.
upload
(
src
);
for
(
int
j
=
0
;
j
<
LOOP_TIMES
;
j
++
)
{
kmeans
(
src
,
K
,
labels
,
TermCriteria
(
CV_TERMCRIT_EPS
+
CV_TERMCRIT_ITER
,
100
,
0
),
1
,
flags
,
centers
);
ocl
::
kmeans
(
d_src
,
K
,
d_labels
,
TermCriteria
(
CV_TERMCRIT_EPS
+
CV_TERMCRIT_ITER
,
100
,
0
),
1
,
flags
,
d_centers
);
Mat
dd_labels
(
d_labels
);
Mat
dd_centers
(
d_centers
);
if
(
flags
&
KMEANS_USE_INITIAL_LABELS
)
{
EXPECT_MAT_NEAR
(
labels
,
dd_labels
,
0
);
EXPECT_MAT_NEAR
(
centers
,
dd_centers
,
1e-3
);
}
else
{
int
row_idx
=
0
;
for
(
int
i
=
0
;
i
<
K
;
i
++
)
{
// verify lables with ground truth resutls
int
label
=
labels
.
at
<
int
>
(
row_idx
);
int
header_label
=
dd_labels
.
at
<
int
>
(
row_idx
);
for
(
int
j
=
0
;
(
j
<
MHEIGHT
/
K
)
||
(
i
==
K
-
1
&&
j
<
MHEIGHT
/
K
+
MHEIGHT
%
K
);
j
++
)
{
ASSERT_NEAR
(
labels
.
at
<
int
>
(
row_idx
+
j
),
label
,
0
);
ASSERT_NEAR
(
dd_labels
.
at
<
int
>
(
row_idx
+
j
),
header_label
,
0
);
}
// verify centers
float
*
center
=
centers
.
ptr
<
float
>
(
label
);
float
*
header_center
=
dd_centers
.
ptr
<
float
>
(
header_label
);
for
(
int
t
=
0
;
t
<
centers
.
cols
;
t
++
)
ASSERT_NEAR
(
center
[
t
],
header_center
[
t
],
1e-3
);
row_idx
+=
MHEIGHT
/
K
;
}
}
}
}
INSTANTIATE_TEST_CASE_P
(
OCL_ML
,
Kmeans
,
Combine
(
Values
(
3
,
5
,
8
),
Values
(
CV_32FC1
,
CV_32FC2
,
CV_32FC4
),
Values
(
OCL_KMEANS_USE_INITIAL_LABELS
/*, OCL_KMEANS_PP_CENTERS*/
)));
#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