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
3b55e0d4
Commit
3b55e0d4
authored
Jun 18, 2012
by
Alexander Mordvintsev
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
added kmeans.py sample
parent
b4dafa6b
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
44 additions
and
0 deletions
+44
-0
kmeans.py
samples/python2/kmeans.py
+44
-0
No files found.
samples/python2/kmeans.py
0 → 100644
View file @
3b55e0d4
'''
K-means clusterization sample.
Usage:
kmeans.py
Keyboard shortcuts:
ESC - exit
space - generate new distribution
'''
import
numpy
as
np
import
cv2
from
gaussian_mix
import
make_gaussians
if
__name__
==
'__main__'
:
cluster_n
=
5
img_size
=
512
print
__doc__
# generating bright palette
colors
=
np
.
zeros
((
1
,
cluster_n
,
3
),
np
.
uint8
)
colors
[
0
,:]
=
255
colors
[
0
,:,
0
]
=
np
.
arange
(
0
,
180
,
180.0
/
cluster_n
)
colors
=
cv2
.
cvtColor
(
colors
,
cv2
.
COLOR_HSV2BGR
)[
0
]
while
True
:
print
'sampling distributions...'
points
,
_
=
make_gaussians
(
cluster_n
,
img_size
)
term_crit
=
(
cv2
.
TERM_CRITERIA_EPS
,
30
,
0.1
)
ret
,
labels
,
centers
=
cv2
.
kmeans
(
points
,
cluster_n
,
term_crit
,
10
,
0
)
img
=
np
.
zeros
((
img_size
,
img_size
,
3
),
np
.
uint8
)
for
(
x
,
y
),
label
in
zip
(
np
.
int32
(
points
),
labels
.
ravel
()):
c
=
map
(
int
,
colors
[
label
])
cv2
.
circle
(
img
,
(
x
,
y
),
1
,
c
,
-
1
)
cv2
.
imshow
(
'gaussian mixture'
,
img
)
ch
=
0xFF
&
cv2
.
waitKey
(
0
)
if
ch
==
27
:
break
cv2
.
destroyAllWindows
()
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