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
d2da8140
Commit
d2da8140
authored
Aug 13, 2011
by
Alexander Mordvintsev
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
work on camshift.py
parent
8c06a275
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
83 additions
and
0 deletions
+83
-0
camshift.py
samples/python2/camshift.py
+83
-0
No files found.
samples/python2/camshift.py
0 → 100644
View file @
d2da8140
import
numpy
as
np
import
cv2
import
video
class
App
(
object
):
def
__init__
(
self
,
video_src
):
self
.
cam
=
video
.
create_capture
(
video_src
)
ret
,
self
.
frame
=
self
.
cam
.
read
()
cv2
.
namedWindow
(
'camshift'
)
cv2
.
setMouseCallback
(
'camshift'
,
self
.
onmouse
)
self
.
selection
=
None
self
.
drag_start
=
None
self
.
tracking_state
=
0
def
onmouse
(
self
,
event
,
x
,
y
,
flags
,
param
):
x
,
y
=
np
.
int16
([
x
,
y
])
# BUG
if
event
==
cv2
.
EVENT_LBUTTONDOWN
:
self
.
drag_start
=
(
x
,
y
)
self
.
tracking_state
=
0
if
self
.
drag_start
:
if
flags
&
cv2
.
EVENT_FLAG_LBUTTON
:
h
,
w
=
self
.
frame
.
shape
[:
2
]
xo
,
yo
=
self
.
drag_start
x0
,
y0
=
np
.
maximum
(
0
,
np
.
minimum
([
xo
,
yo
],
[
x
,
y
]))
x1
,
y1
=
np
.
minimum
([
w
,
h
],
np
.
maximum
([
xo
,
yo
],
[
x
,
y
]))
self
.
selection
=
None
if
x1
-
x0
>
0
and
y1
-
y0
>
0
:
self
.
selection
=
(
x0
,
y0
,
x1
,
y1
)
else
:
self
.
drag_start
=
None
if
self
.
selection
is
not
None
:
self
.
tracking_state
=
1
def
show_hist
(
self
):
bin_count
=
self
.
hist
.
shape
[
0
]
bin_w
=
24
img
=
np
.
zeros
((
256
,
bin_count
*
bin_w
,
3
),
np
.
uint8
)
for
i
in
xrange
(
bin_count
):
h
=
int
(
self
.
hist
[
i
])
cv2
.
rectangle
(
img
,
(
i
*
bin_w
+
2
,
255
),
((
i
+
1
)
*
bin_w
-
2
,
255
-
h
),
(
int
(
180.0
*
i
/
bin_count
),
255
,
255
),
-
1
)
img
=
cv2
.
cvtColor
(
img
,
cv2
.
COLOR_HSV2BGR
)
cv2
.
imshow
(
'hist'
,
img
)
def
run
(
self
):
while
True
:
ret
,
self
.
frame
=
self
.
cam
.
read
()
self
.
frame
=
self
.
frame
.
copy
()
hsv
=
cv2
.
cvtColor
(
self
.
frame
,
cv2
.
COLOR_BGR2HSV
)
if
self
.
selection
:
x0
,
y0
,
x1
,
y1
=
self
.
selection
hsv_roi
=
hsv
[
y0
:
y1
,
x0
:
x1
]
hist
=
cv2
.
calcHist
(
[
hsv_roi
],
[
0
],
None
,
[
16
],
[
0
,
180
]
)
cv2
.
normalize
(
hist
,
hist
,
0
,
255
,
cv2
.
NORM_MINMAX
);
self
.
hist
=
hist
.
reshape
(
-
1
)
self
.
show_hist
()
roi
=
self
.
frame
[
y0
:
y1
,
x0
:
x1
]
cv2
.
bitwise_not
(
roi
,
roi
)
if
self
.
tracking_state
==
1
:
self
.
selection
=
None
prob
=
cv2
.
calcBackProject
([
hsv
],
[
0
],
self
.
hist
,
[
0
,
180
],
1
)
term_crit
=
(
cv2
.
TERM_CRITERIA_EPS
|
cv2
.
TERM_CRITERIA_COUNT
,
10
,
1
)
print
cv2
.
CamShift
(
prob
,
term_crit
)
#cv2.imshow('back', back)
cv2
.
imshow
(
'camshift'
,
self
.
frame
)
if
cv2
.
waitKey
(
5
)
==
27
:
break
if
__name__
==
'__main__'
:
import
sys
try
:
video_src
=
sys
.
argv
[
1
]
except
:
video_src
=
video
.
presets
[
'chess'
]
App
(
video_src
)
.
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