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
aa695aba
Commit
aa695aba
authored
Jul 01, 2011
by
Alexander Mordvintsev
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
watershed.py sample added
parent
f8e1e88c
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
78 additions
and
0 deletions
+78
-0
watershed.py
samples/python2/watershed.py
+78
-0
No files found.
samples/python2/watershed.py
0 → 100644
View file @
aa695aba
import
numpy
as
np
import
cv2
,
cv
help_message
=
'''
USAGE: watershed.py [<image>]
Use keys 1 - 7 to switch marker color
SPACE - update segmentation
r - reset
a - switch autoupdate
ESC - exit
'''
class
App
:
def
__init__
(
self
,
fn
):
self
.
img
=
cv2
.
imread
(
fn
)
h
,
w
=
self
.
img
.
shape
[:
2
]
self
.
markers
=
np
.
zeros
((
h
,
w
),
np
.
int32
)
self
.
markers_vis
=
self
.
img
.
copy
()
self
.
cur_marker
=
1
self
.
colors
=
np
.
int32
(
list
(
np
.
ndindex
(
2
,
2
,
2
))
)
*
255
cv2
.
imshow
(
'img'
,
self
.
markers_vis
)
self
.
prev_pt
=
None
self
.
need_update
=
False
self
.
auto_update
=
True
cv2
.
setMouseCallback
(
'img'
,
self
.
onmouse
)
def
onmouse
(
self
,
event
,
x
,
y
,
flags
,
param
):
pt
=
(
x
,
y
)
if
event
==
cv
.
CV_EVENT_LBUTTONDOWN
:
self
.
prev_pt
=
pt
if
self
.
prev_pt
and
flags
&
cv
.
CV_EVENT_FLAG_LBUTTON
:
color
=
map
(
int
,
self
.
colors
[
self
.
cur_marker
])
cv
.
Line
(
self
.
markers
,
self
.
prev_pt
,
pt
,
self
.
cur_marker
,
5
)
cv
.
Line
(
self
.
markers_vis
,
self
.
prev_pt
,
pt
,
color
,
5
)
self
.
need_update
=
True
self
.
prev_pt
=
pt
cv2
.
imshow
(
'img'
,
self
.
markers_vis
)
else
:
self
.
prev_pt
=
None
def
watershed
(
self
):
m
=
self
.
markers
.
copy
()
cv2
.
watershed
(
self
.
img
,
m
)
vis
=
np
.
uint8
(
(
self
.
img
+
self
.
colors
[
np
.
maximum
(
m
,
0
)])
/
2
)
cv2
.
imshow
(
'watershed'
,
vis
)
self
.
need_update
=
False
def
run
(
self
):
while
True
:
ch
=
cv2
.
waitKey
(
10
)
if
ch
==
27
:
break
if
ch
>=
ord
(
'1'
)
and
ch
<=
ord
(
'7'
):
self
.
cur_marker
=
ch
-
ord
(
'0'
)
print
'marker: '
,
self
.
cur_marker
if
ch
==
ord
(
' '
)
or
(
self
.
need_update
and
self
.
auto_update
):
self
.
watershed
()
if
ch
in
[
ord
(
'a'
),
ord
(
'A'
)]:
self
.
auto_update
=
not
self
.
auto_update
print
'auto_update if'
,
[
'off'
,
'on'
][
self
.
auto_update
]
if
ch
in
[
ord
(
'r'
),
ord
(
'R'
)]:
self
.
markers
=
np
.
zeros
(
self
.
img
.
shape
[:
2
],
np
.
int32
)
self
.
markers_vis
=
self
.
img
.
copy
()
cv2
.
imshow
(
'img'
,
self
.
markers_vis
)
cv2
.
destroyWindow
(
'watershed'
)
if
__name__
==
'__main__'
:
import
sys
try
:
fn
=
sys
.
argv
[
1
]
except
:
fn
=
'../cpp/fruits.jpg'
print
help_message
App
(
fn
)
.
run
()
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