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
123ca7e1
Commit
123ca7e1
authored
Nov 27, 2014
by
Takahiro Poly Horikawa
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Parallize building kmeans index in flann
parent
ae4cb571
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
67 additions
and
18 deletions
+67
-18
kmeans_index.h
modules/flann/include/opencv2/flann/kmeans_index.h
+67
-18
No files found.
modules/flann/include/opencv2/flann/kmeans_index.h
View file @
123ca7e1
...
...
@@ -69,7 +69,6 @@ struct KMeansIndexParams : public IndexParams
}
};
/**
* Hierarchical kmeans index
*
...
...
@@ -271,6 +270,68 @@ public:
return
FLANN_INDEX_KMEANS
;
}
class
KMeansDistanceComputer
:
public
cv
::
ParallelLoopBody
{
public
:
KMeansDistanceComputer
(
Distance
_distance
,
const
Matrix
<
ElementType
>&
_dataset
,
const
int
_branching
,
const
int
*
_indices
,
const
Matrix
<
double
>&
_dcenters
,
const
int
_veclen
,
int
*
_count
,
int
*
_belongs_to
,
std
::
vector
<
DistanceType
>&
_radiuses
,
bool
*
_updated
)
:
distance
(
_distance
)
,
dataset
(
_dataset
)
,
branching
(
_branching
)
,
indices
(
_indices
)
,
dcenters
(
_dcenters
)
,
veclen
(
_veclen
)
,
count
(
_count
)
,
belongs_to
(
_belongs_to
)
,
radiuses
(
_radiuses
)
,
updated
(
_updated
)
{
}
void
operator
()(
const
cv
::
Range
&
range
)
const
{
const
int
begin
=
range
.
start
;
const
int
end
=
range
.
end
;
for
(
int
i
=
begin
;
i
<
end
;
++
i
)
{
DistanceType
sq_dist
=
distance
(
dataset
[
indices
[
i
]],
dcenters
[
0
],
veclen
);
int
new_centroid
=
0
;
for
(
int
j
=
1
;
j
<
branching
;
++
j
)
{
DistanceType
new_sq_dist
=
distance
(
dataset
[
indices
[
i
]],
dcenters
[
j
],
veclen
);
if
(
sq_dist
>
new_sq_dist
)
{
new_centroid
=
j
;
sq_dist
=
new_sq_dist
;
}
}
if
(
sq_dist
>
radiuses
[
new_centroid
])
{
radiuses
[
new_centroid
]
=
sq_dist
;
}
if
(
new_centroid
!=
belongs_to
[
i
])
{
count
[
belongs_to
[
i
]]
--
;
count
[
new_centroid
]
++
;
belongs_to
[
i
]
=
new_centroid
;
updated
[
i
]
=
true
;
}
else
{
updated
[
i
]
=
false
;
}
}
}
private
:
Distance
distance
;
const
Matrix
<
ElementType
>&
dataset
;
const
int
branching
;
const
int
*
indices
;
const
Matrix
<
double
>&
dcenters
;
int
veclen
;
int
*
count
;
int
*
belongs_to
;
std
::
vector
<
DistanceType
>&
radiuses
;
bool
*
updated
;
};
/**
* Index constructor
*
...
...
@@ -708,6 +769,7 @@ private:
bool
converged
=
false
;
int
iteration
=
0
;
bool
*
updated
=
new
bool
[
indices_length
];
while
(
!
converged
&&
iteration
<
iterations_
)
{
converged
=
true
;
iteration
++
;
...
...
@@ -732,25 +794,11 @@ private:
}
// reassign points to clusters
parallel_for_
(
cv
::
Range
(
0
,
indices_length
),
KMeansDistanceComputer
(
distance_
,
dataset_
,
branching
,
indices
,
dcenters
,
veclen_
,
count
,
belongs_to
,
radiuses
,
updated
));
for
(
int
i
=
0
;
i
<
indices_length
;
++
i
)
{
DistanceType
sq_dist
=
distance_
(
dataset_
[
indices
[
i
]],
dcenters
[
0
],
veclen_
);
int
new_centroid
=
0
;
for
(
int
j
=
1
;
j
<
branching
;
++
j
)
{
DistanceType
new_sq_dist
=
distance_
(
dataset_
[
indices
[
i
]],
dcenters
[
j
],
veclen_
);
if
(
sq_dist
>
new_sq_dist
)
{
new_centroid
=
j
;
sq_dist
=
new_sq_dist
;
}
}
if
(
sq_dist
>
radiuses
[
new_centroid
])
{
radiuses
[
new_centroid
]
=
sq_dist
;
}
if
(
new_centroid
!=
belongs_to
[
i
])
{
count
[
belongs_to
[
i
]]
--
;
count
[
new_centroid
]
++
;
belongs_to
[
i
]
=
new_centroid
;
if
(
updated
[
i
])
{
converged
=
false
;
break
;
}
}
...
...
@@ -828,6 +876,7 @@ private:
delete
[]
centers
;
delete
[]
count
;
delete
[]
belongs_to
;
delete
[]
updated
;
}
...
...
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