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
4c1c0b10
Commit
4c1c0b10
authored
Dec 14, 2015
by
Vadim Pisarevsky
Browse files
Options
Browse Files
Download
Plain Diff
Merge pull request #5790 from bastelflp:py3compatibility
parents
5cdf0e3e
4ed2d632
Hide whitespace changes
Inline
Side-by-side
Showing
23 changed files
with
218 additions
and
131 deletions
+218
-131
asift.py
samples/python2/asift.py
+13
-10
contours.py
samples/python2/contours.py
+12
-4
digits.py
samples/python2/digits.py
+16
-12
digits_adjust.py
samples/python2/digits_adjust.py
+24
-15
digits_video.py
samples/python2/digits_video.py
+4
-1
edge.py
samples/python2/edge.py
+2
-1
feature_homography.py
samples/python2/feature_homography.py
+4
-1
find_obj.py
samples/python2/find_obj.py
+16
-11
gaussian_mix.py
samples/python2/gaussian_mix.py
+18
-8
hist.py
samples/python2/hist.py
+16
-13
kmeans.py
samples/python2/kmeans.py
+7
-3
lappyr.py
samples/python2/lappyr.py
+1
-1
letter_recog.py
samples/python2/letter_recog.py
+23
-20
mouse_and_match.py
samples/python2/mouse_and_match.py
+7
-3
opt_flow.py
samples/python2/opt_flow.py
+7
-4
peopledetect.py
samples/python2/peopledetect.py
+3
-1
plane_ar.py
samples/python2/plane_ar.py
+4
-1
plane_tracker.py
samples/python2/plane_tracker.py
+13
-4
stereo_match.py
samples/python2/stereo_match.py
+10
-7
texture_flow.py
samples/python2/texture_flow.py
+5
-2
video.py
samples/python2/video.py
+1
-1
video_v4l2.py
samples/python2/video_v4l2.py
+6
-3
watershed.py
samples/python2/watershed.py
+6
-5
No files found.
samples/python2/asift.py
View file @
4c1c0b10
...
@@ -19,6 +19,9 @@ USAGE
...
@@ -19,6 +19,9 @@ USAGE
Press left mouse button on a feature point to see its matching point.
Press left mouse button on a feature point to see its matching point.
'''
'''
# Python 2/3 compatibility
from
__future__
import
print_function
import
numpy
as
np
import
numpy
as
np
import
cv2
import
cv2
...
@@ -96,15 +99,15 @@ def affine_detect(detector, img, mask=None, pool=None):
...
@@ -96,15 +99,15 @@ def affine_detect(detector, img, mask=None, pool=None):
ires
=
pool
.
imap
(
f
,
params
)
ires
=
pool
.
imap
(
f
,
params
)
for
i
,
(
k
,
d
)
in
enumerate
(
ires
):
for
i
,
(
k
,
d
)
in
enumerate
(
ires
):
print
'affine sampling:
%
d /
%
d
\r
'
%
(
i
+
1
,
len
(
params
)),
print
(
'affine sampling:
%
d /
%
d
\r
'
%
(
i
+
1
,
len
(
params
)),
end
=
''
)
keypoints
.
extend
(
k
)
keypoints
.
extend
(
k
)
descrs
.
extend
(
d
)
descrs
.
extend
(
d
)
print
print
()
return
keypoints
,
np
.
array
(
descrs
)
return
keypoints
,
np
.
array
(
descrs
)
if
__name__
==
'__main__'
:
if
__name__
==
'__main__'
:
print
__doc__
print
(
__doc__
)
import
sys
,
getopt
import
sys
,
getopt
opts
,
args
=
getopt
.
getopt
(
sys
.
argv
[
1
:],
''
,
[
'feature='
])
opts
,
args
=
getopt
.
getopt
(
sys
.
argv
[
1
:],
''
,
[
'feature='
])
...
@@ -121,23 +124,23 @@ if __name__ == '__main__':
...
@@ -121,23 +124,23 @@ if __name__ == '__main__':
detector
,
matcher
=
init_feature
(
feature_name
)
detector
,
matcher
=
init_feature
(
feature_name
)
if
img1
is
None
:
if
img1
is
None
:
print
'Failed to load fn1:'
,
fn1
print
(
'Failed to load fn1:'
,
fn1
)
sys
.
exit
(
1
)
sys
.
exit
(
1
)
if
img2
is
None
:
if
img2
is
None
:
print
'Failed to load fn2:'
,
fn2
print
(
'Failed to load fn2:'
,
fn2
)
sys
.
exit
(
1
)
sys
.
exit
(
1
)
if
detector
is
None
:
if
detector
is
None
:
print
'unknown feature:'
,
feature_name
print
(
'unknown feature:'
,
feature_name
)
sys
.
exit
(
1
)
sys
.
exit
(
1
)
print
'using'
,
feature_name
print
(
'using'
,
feature_name
)
pool
=
ThreadPool
(
processes
=
cv2
.
getNumberOfCPUs
())
pool
=
ThreadPool
(
processes
=
cv2
.
getNumberOfCPUs
())
kp1
,
desc1
=
affine_detect
(
detector
,
img1
,
pool
=
pool
)
kp1
,
desc1
=
affine_detect
(
detector
,
img1
,
pool
=
pool
)
kp2
,
desc2
=
affine_detect
(
detector
,
img2
,
pool
=
pool
)
kp2
,
desc2
=
affine_detect
(
detector
,
img2
,
pool
=
pool
)
print
'img1 -
%
d features, img2 -
%
d features'
%
(
len
(
kp1
),
len
(
kp2
))
print
(
'img1 -
%
d features, img2 -
%
d features'
%
(
len
(
kp1
),
len
(
kp2
)
))
def
match_and_draw
(
win
):
def
match_and_draw
(
win
):
with
Timer
(
'matching'
):
with
Timer
(
'matching'
):
...
@@ -145,12 +148,12 @@ if __name__ == '__main__':
...
@@ -145,12 +148,12 @@ if __name__ == '__main__':
p1
,
p2
,
kp_pairs
=
filter_matches
(
kp1
,
kp2
,
raw_matches
)
p1
,
p2
,
kp_pairs
=
filter_matches
(
kp1
,
kp2
,
raw_matches
)
if
len
(
p1
)
>=
4
:
if
len
(
p1
)
>=
4
:
H
,
status
=
cv2
.
findHomography
(
p1
,
p2
,
cv2
.
RANSAC
,
5.0
)
H
,
status
=
cv2
.
findHomography
(
p1
,
p2
,
cv2
.
RANSAC
,
5.0
)
print
'
%
d /
%
d inliers/matched'
%
(
np
.
sum
(
status
),
len
(
status
))
print
(
'
%
d /
%
d inliers/matched'
%
(
np
.
sum
(
status
),
len
(
status
)
))
# do not draw outliers (there will be a lot of them)
# do not draw outliers (there will be a lot of them)
kp_pairs
=
[
kpp
for
kpp
,
flag
in
zip
(
kp_pairs
,
status
)
if
flag
]
kp_pairs
=
[
kpp
for
kpp
,
flag
in
zip
(
kp_pairs
,
status
)
if
flag
]
else
:
else
:
H
,
status
=
None
,
None
H
,
status
=
None
,
None
print
'
%
d matches found, not enough for homography estimation'
%
len
(
p1
)
print
(
'
%
d matches found, not enough for homography estimation'
%
len
(
p1
)
)
vis
=
explore_match
(
win
,
img1
,
img2
,
kp_pairs
,
None
,
H
)
vis
=
explore_match
(
win
,
img1
,
img2
,
kp_pairs
,
None
,
H
)
...
...
samples/python2/contours.py
View file @
4c1c0b10
...
@@ -9,6 +9,14 @@ Usage:
...
@@ -9,6 +9,14 @@ Usage:
A trackbar is put up which controls the contour level from -3 to 3
A trackbar is put up which controls the contour level from -3 to 3
'''
'''
# Python 2/3 compatibility
from
__future__
import
print_function
import
sys
PY3
=
sys
.
version_info
[
0
]
==
3
if
PY3
:
xrange
=
range
import
numpy
as
np
import
numpy
as
np
import
cv2
import
cv2
...
@@ -16,8 +24,8 @@ def make_image():
...
@@ -16,8 +24,8 @@ def make_image():
img
=
np
.
zeros
((
500
,
500
),
np
.
uint8
)
img
=
np
.
zeros
((
500
,
500
),
np
.
uint8
)
black
,
white
=
0
,
255
black
,
white
=
0
,
255
for
i
in
xrange
(
6
):
for
i
in
xrange
(
6
):
dx
=
(
i
%
2
)
*
250
-
30
dx
=
int
((
i
%
2
)
*
250
-
30
)
dy
=
(
i
/
2
)
*
150
dy
=
int
((
i
/
2.
)
*
150
)
if
i
==
0
:
if
i
==
0
:
for
j
in
xrange
(
11
):
for
j
in
xrange
(
11
):
...
@@ -41,7 +49,7 @@ def make_image():
...
@@ -41,7 +49,7 @@ def make_image():
return
img
return
img
if
__name__
==
'__main__'
:
if
__name__
==
'__main__'
:
print
__doc__
print
(
__doc__
)
img
=
make_image
()
img
=
make_image
()
h
,
w
=
img
.
shape
[:
2
]
h
,
w
=
img
.
shape
[:
2
]
...
@@ -52,7 +60,7 @@ if __name__ == '__main__':
...
@@ -52,7 +60,7 @@ if __name__ == '__main__':
def
update
(
levels
):
def
update
(
levels
):
vis
=
np
.
zeros
((
h
,
w
,
3
),
np
.
uint8
)
vis
=
np
.
zeros
((
h
,
w
,
3
),
np
.
uint8
)
levels
=
levels
-
3
levels
=
levels
-
3
cv2
.
drawContours
(
vis
,
contours
,
(
-
1
,
3
)[
levels
<=
0
],
(
128
,
255
,
255
),
cv2
.
drawContours
(
vis
,
contours
,
(
-
1
,
2
)[
levels
<=
0
],
(
128
,
255
,
255
),
3
,
cv2
.
LINE_AA
,
hierarchy
,
abs
(
levels
)
)
3
,
cv2
.
LINE_AA
,
hierarchy
,
abs
(
levels
)
)
cv2
.
imshow
(
'contours'
,
vis
)
cv2
.
imshow
(
'contours'
,
vis
)
update
(
3
)
update
(
3
)
...
...
samples/python2/digits.py
View file @
4c1c0b10
...
@@ -23,6 +23,10 @@ Usage:
...
@@ -23,6 +23,10 @@ Usage:
digits.py
digits.py
'''
'''
# Python 2/3 compatibility
from
__future__
import
print_function
# built-in modules
# built-in modules
from
multiprocessing.pool
import
ThreadPool
from
multiprocessing.pool
import
ThreadPool
...
@@ -50,7 +54,7 @@ def split2d(img, cell_size, flatten=True):
...
@@ -50,7 +54,7 @@ def split2d(img, cell_size, flatten=True):
return
cells
return
cells
def
load_digits
(
fn
):
def
load_digits
(
fn
):
print
'loading "
%
s" ...'
%
fn
print
(
'loading "
%
s" ...'
%
fn
)
digits_img
=
cv2
.
imread
(
fn
,
0
)
digits_img
=
cv2
.
imread
(
fn
,
0
)
digits
=
split2d
(
digits_img
,
(
SZ
,
SZ
))
digits
=
split2d
(
digits_img
,
(
SZ
,
SZ
))
labels
=
np
.
repeat
(
np
.
arange
(
CLASS_N
),
len
(
digits
)
/
CLASS_N
)
labels
=
np
.
repeat
(
np
.
arange
(
CLASS_N
),
len
(
digits
)
/
CLASS_N
)
...
@@ -67,7 +71,7 @@ def deskew(img):
...
@@ -67,7 +71,7 @@ def deskew(img):
class
StatModel
(
object
):
class
StatModel
(
object
):
def
load
(
self
,
fn
):
def
load
(
self
,
fn
):
self
.
model
.
load
(
fn
)
self
.
model
.
load
(
fn
)
# Known bug: https://github.com/Itseez/opencv/issues/4969
def
save
(
self
,
fn
):
def
save
(
self
,
fn
):
self
.
model
.
save
(
fn
)
self
.
model
.
save
(
fn
)
...
@@ -101,14 +105,14 @@ class SVM(StatModel):
...
@@ -101,14 +105,14 @@ class SVM(StatModel):
def
evaluate_model
(
model
,
digits
,
samples
,
labels
):
def
evaluate_model
(
model
,
digits
,
samples
,
labels
):
resp
=
model
.
predict
(
samples
)
resp
=
model
.
predict
(
samples
)
err
=
(
labels
!=
resp
)
.
mean
()
err
=
(
labels
!=
resp
)
.
mean
()
print
'error:
%.2
f
%%
'
%
(
err
*
100
)
print
(
'error:
%.2
f
%%
'
%
(
err
*
100
)
)
confusion
=
np
.
zeros
((
10
,
10
),
np
.
int32
)
confusion
=
np
.
zeros
((
10
,
10
),
np
.
int32
)
for
i
,
j
in
zip
(
labels
,
resp
):
for
i
,
j
in
zip
(
labels
,
resp
):
confusion
[
i
,
j
]
+=
1
confusion
[
i
,
j
]
+=
1
print
'confusion matrix:'
print
(
'confusion matrix:'
)
print
confusion
print
(
confusion
)
print
print
()
vis
=
[]
vis
=
[]
for
img
,
flag
in
zip
(
digits
,
resp
==
labels
):
for
img
,
flag
in
zip
(
digits
,
resp
==
labels
):
...
@@ -145,17 +149,17 @@ def preprocess_hog(digits):
...
@@ -145,17 +149,17 @@ def preprocess_hog(digits):
if
__name__
==
'__main__'
:
if
__name__
==
'__main__'
:
print
__doc__
print
(
__doc__
)
digits
,
labels
=
load_digits
(
DIGITS_FN
)
digits
,
labels
=
load_digits
(
DIGITS_FN
)
print
'preprocessing...'
print
(
'preprocessing...'
)
# shuffle digits
# shuffle digits
rand
=
np
.
random
.
RandomState
(
321
)
rand
=
np
.
random
.
RandomState
(
321
)
shuffle
=
rand
.
permutation
(
len
(
digits
))
shuffle
=
rand
.
permutation
(
len
(
digits
))
digits
,
labels
=
digits
[
shuffle
],
labels
[
shuffle
]
digits
,
labels
=
digits
[
shuffle
],
labels
[
shuffle
]
digits2
=
map
(
deskew
,
digits
)
digits2
=
list
(
map
(
deskew
,
digits
)
)
samples
=
preprocess_hog
(
digits2
)
samples
=
preprocess_hog
(
digits2
)
train_n
=
int
(
0.9
*
len
(
samples
))
train_n
=
int
(
0.9
*
len
(
samples
))
...
@@ -165,18 +169,18 @@ if __name__ == '__main__':
...
@@ -165,18 +169,18 @@ if __name__ == '__main__':
labels_train
,
labels_test
=
np
.
split
(
labels
,
[
train_n
])
labels_train
,
labels_test
=
np
.
split
(
labels
,
[
train_n
])
print
'training KNearest...'
print
(
'training KNearest...'
)
model
=
KNearest
(
k
=
4
)
model
=
KNearest
(
k
=
4
)
model
.
train
(
samples_train
,
labels_train
)
model
.
train
(
samples_train
,
labels_train
)
vis
=
evaluate_model
(
model
,
digits_test
,
samples_test
,
labels_test
)
vis
=
evaluate_model
(
model
,
digits_test
,
samples_test
,
labels_test
)
cv2
.
imshow
(
'KNearest test'
,
vis
)
cv2
.
imshow
(
'KNearest test'
,
vis
)
print
'training SVM...'
print
(
'training SVM...'
)
model
=
SVM
(
C
=
2.67
,
gamma
=
5.383
)
model
=
SVM
(
C
=
2.67
,
gamma
=
5.383
)
model
.
train
(
samples_train
,
labels_train
)
model
.
train
(
samples_train
,
labels_train
)
vis
=
evaluate_model
(
model
,
digits_test
,
samples_test
,
labels_test
)
vis
=
evaluate_model
(
model
,
digits_test
,
samples_test
,
labels_test
)
cv2
.
imshow
(
'SVM test'
,
vis
)
cv2
.
imshow
(
'SVM test'
,
vis
)
print
'saving SVM as "digits_svm.dat"...'
print
(
'saving SVM as "digits_svm.dat"...'
)
model
.
save
(
'digits_svm.dat'
)
model
.
save
(
'digits_svm.dat'
)
cv2
.
waitKey
(
0
)
cv2
.
waitKey
(
0
)
samples/python2/digits_adjust.py
View file @
4c1c0b10
...
@@ -13,6 +13,14 @@ Usage:
...
@@ -13,6 +13,14 @@ Usage:
'''
'''
# Python 2/3 compatibility
from
__future__
import
print_function
import
sys
PY3
=
sys
.
version_info
[
0
]
==
3
if
PY3
:
xrange
=
range
import
numpy
as
np
import
numpy
as
np
import
cv2
import
cv2
from
multiprocessing.pool
import
ThreadPool
from
multiprocessing.pool
import
ThreadPool
...
@@ -33,10 +41,10 @@ def cross_validate(model_class, params, samples, labels, kfold = 3, pool = None)
...
@@ -33,10 +41,10 @@ def cross_validate(model_class, params, samples, labels, kfold = 3, pool = None)
model
.
train
(
train_samples
,
train_labels
)
model
.
train
(
train_samples
,
train_labels
)
resp
=
model
.
predict
(
test_samples
)
resp
=
model
.
predict
(
test_samples
)
score
=
(
resp
!=
test_labels
)
.
mean
()
score
=
(
resp
!=
test_labels
)
.
mean
()
print
"."
,
print
(
"."
,
end
=
''
)
return
score
return
score
if
pool
is
None
:
if
pool
is
None
:
scores
=
map
(
f
,
xrange
(
kfold
))
scores
=
list
(
map
(
f
,
xrange
(
kfold
)
))
else
:
else
:
scores
=
pool
.
map
(
f
,
xrange
(
kfold
))
scores
=
pool
.
map
(
f
,
xrange
(
kfold
))
return
np
.
mean
(
scores
)
return
np
.
mean
(
scores
)
...
@@ -50,7 +58,7 @@ class App(object):
...
@@ -50,7 +58,7 @@ class App(object):
digits
,
labels
=
load_digits
(
DIGITS_FN
)
digits
,
labels
=
load_digits
(
DIGITS_FN
)
shuffle
=
np
.
random
.
permutation
(
len
(
digits
))
shuffle
=
np
.
random
.
permutation
(
len
(
digits
))
digits
,
labels
=
digits
[
shuffle
],
labels
[
shuffle
]
digits
,
labels
=
digits
[
shuffle
],
labels
[
shuffle
]
digits2
=
map
(
deskew
,
digits
)
digits2
=
list
(
map
(
deskew
,
digits
)
)
samples
=
preprocess_hog
(
digits2
)
samples
=
preprocess_hog
(
digits2
)
return
samples
,
labels
return
samples
,
labels
...
@@ -68,7 +76,7 @@ class App(object):
...
@@ -68,7 +76,7 @@ class App(object):
scores
=
np
.
zeros
((
len
(
Cs
),
len
(
gammas
)))
scores
=
np
.
zeros
((
len
(
Cs
),
len
(
gammas
)))
scores
[:]
=
np
.
nan
scores
[:]
=
np
.
nan
print
'adjusting SVM (may take a long time) ...'
print
(
'adjusting SVM (may take a long time) ...'
)
def
f
(
job
):
def
f
(
job
):
i
,
j
=
job
i
,
j
=
job
samples
,
labels
=
self
.
get_dataset
()
samples
,
labels
=
self
.
get_dataset
()
...
@@ -79,20 +87,21 @@ class App(object):
...
@@ -79,20 +87,21 @@ class App(object):
ires
=
self
.
run_jobs
(
f
,
np
.
ndindex
(
*
scores
.
shape
))
ires
=
self
.
run_jobs
(
f
,
np
.
ndindex
(
*
scores
.
shape
))
for
count
,
(
i
,
j
,
score
)
in
enumerate
(
ires
):
for
count
,
(
i
,
j
,
score
)
in
enumerate
(
ires
):
scores
[
i
,
j
]
=
score
scores
[
i
,
j
]
=
score
print
'
%
d /
%
d (best error:
%.2
f
%%
, last:
%.2
f
%%
)'
%
(
count
+
1
,
scores
.
size
,
np
.
nanmin
(
scores
)
*
100
,
score
*
100
)
print
(
'
%
d /
%
d (best error:
%.2
f
%%
, last:
%.2
f
%%
)'
%
print
scores
(
count
+
1
,
scores
.
size
,
np
.
nanmin
(
scores
)
*
100
,
score
*
100
))
print
(
scores
)
print
'writing score table to "svm_scores.npz"'
print
(
'writing score table to "svm_scores.npz"'
)
np
.
savez
(
'svm_scores.npz'
,
scores
=
scores
,
Cs
=
Cs
,
gammas
=
gammas
)
np
.
savez
(
'svm_scores.npz'
,
scores
=
scores
,
Cs
=
Cs
,
gammas
=
gammas
)
i
,
j
=
np
.
unravel_index
(
scores
.
argmin
(),
scores
.
shape
)
i
,
j
=
np
.
unravel_index
(
scores
.
argmin
(),
scores
.
shape
)
best_params
=
dict
(
C
=
Cs
[
i
],
gamma
=
gammas
[
j
])
best_params
=
dict
(
C
=
Cs
[
i
],
gamma
=
gammas
[
j
])
print
'best params:'
,
best_params
print
(
'best params:'
,
best_params
)
print
'best error:
%.2
f
%%
'
%
(
scores
.
min
()
*
100
)
print
(
'best error:
%.2
f
%%
'
%
(
scores
.
min
()
*
100
)
)
return
best_params
return
best_params
def
adjust_KNearest
(
self
):
def
adjust_KNearest
(
self
):
print
'adjusting KNearest ...'
print
(
'adjusting KNearest ...'
)
def
f
(
k
):
def
f
(
k
):
samples
,
labels
=
self
.
get_dataset
()
samples
,
labels
=
self
.
get_dataset
()
err
=
cross_validate
(
KNearest
,
dict
(
k
=
k
),
samples
,
labels
)
err
=
cross_validate
(
KNearest
,
dict
(
k
=
k
),
samples
,
labels
)
...
@@ -101,9 +110,9 @@ class App(object):
...
@@ -101,9 +110,9 @@ class App(object):
for
k
,
err
in
self
.
run_jobs
(
f
,
xrange
(
1
,
9
)):
for
k
,
err
in
self
.
run_jobs
(
f
,
xrange
(
1
,
9
)):
if
err
<
best_err
:
if
err
<
best_err
:
best_err
,
best_k
=
err
,
k
best_err
,
best_k
=
err
,
k
print
'k =
%
d, error:
%.2
f
%%
'
%
(
k
,
err
*
100
)
print
(
'k =
%
d, error:
%.2
f
%%
'
%
(
k
,
err
*
100
)
)
best_params
=
dict
(
k
=
best_k
)
best_params
=
dict
(
k
=
best_k
)
print
'best params:'
,
best_params
,
'err:
%.2
f'
%
(
best_err
*
100
)
print
(
'best params:'
,
best_params
,
'err:
%.2
f'
%
(
best_err
*
100
)
)
return
best_params
return
best_params
...
@@ -111,14 +120,14 @@ if __name__ == '__main__':
...
@@ -111,14 +120,14 @@ if __name__ == '__main__':
import
getopt
import
getopt
import
sys
import
sys
print
__doc__
print
(
__doc__
)
args
,
_
=
getopt
.
getopt
(
sys
.
argv
[
1
:],
''
,
[
'model='
])
args
,
_
=
getopt
.
getopt
(
sys
.
argv
[
1
:],
''
,
[
'model='
])
args
=
dict
(
args
)
args
=
dict
(
args
)
args
.
setdefault
(
'--model'
,
'svm'
)
args
.
setdefault
(
'--model'
,
'svm'
)
args
.
setdefault
(
'--env'
,
''
)
args
.
setdefault
(
'--env'
,
''
)
if
args
[
'--model'
]
not
in
[
'svm'
,
'knearest'
]:
if
args
[
'--model'
]
not
in
[
'svm'
,
'knearest'
]:
print
'unknown model "
%
s"'
%
args
[
'--model'
]
print
(
'unknown model "
%
s"'
%
args
[
'--model'
])
sys
.
exit
(
1
)
sys
.
exit
(
1
)
t
=
clock
()
t
=
clock
()
...
@@ -127,4 +136,4 @@ if __name__ == '__main__':
...
@@ -127,4 +136,4 @@ if __name__ == '__main__':
app
.
adjust_KNearest
()
app
.
adjust_KNearest
()
else
:
else
:
app
.
adjust_SVM
()
app
.
adjust_SVM
()
print
'work time:
%
f s'
%
(
clock
()
-
t
)
print
(
'work time:
%
f s'
%
(
clock
()
-
t
)
)
samples/python2/digits_video.py
View file @
4c1c0b10
#!/usr/bin/env python
#!/usr/bin/env python
# Python 2/3 compatibility
from
__future__
import
print_function
import
numpy
as
np
import
numpy
as
np
import
cv2
import
cv2
...
@@ -22,7 +25,7 @@ def main():
...
@@ -22,7 +25,7 @@ def main():
classifier_fn
=
'digits_svm.dat'
classifier_fn
=
'digits_svm.dat'
if
not
os
.
path
.
exists
(
classifier_fn
):
if
not
os
.
path
.
exists
(
classifier_fn
):
print
'"
%
s" not found, run digits.py first'
%
classifier_fn
print
(
'"
%
s" not found, run digits.py first'
%
classifier_fn
)
return
return
model
=
SVM
()
model
=
SVM
()
model
.
load
(
classifier_fn
)
model
.
load
(
classifier_fn
)
...
...
samples/python2/edge.py
View file @
4c1c0b10
...
@@ -14,6 +14,7 @@ Usage:
...
@@ -14,6 +14,7 @@ Usage:
from
__future__
import
print_function
from
__future__
import
print_function
import
cv2
import
cv2
import
numpy
as
np
# relative module
# relative module
import
video
import
video
...
@@ -45,7 +46,7 @@ if __name__ == '__main__':
...
@@ -45,7 +46,7 @@ if __name__ == '__main__':
thrs2
=
cv2
.
getTrackbarPos
(
'thrs2'
,
'edge'
)
thrs2
=
cv2
.
getTrackbarPos
(
'thrs2'
,
'edge'
)
edge
=
cv2
.
Canny
(
gray
,
thrs1
,
thrs2
,
apertureSize
=
5
)
edge
=
cv2
.
Canny
(
gray
,
thrs1
,
thrs2
,
apertureSize
=
5
)
vis
=
img
.
copy
()
vis
=
img
.
copy
()
vis
/=
2
vis
=
np
.
uint8
(
vis
/
2.
)
vis
[
edge
!=
0
]
=
(
0
,
255
,
0
)
vis
[
edge
!=
0
]
=
(
0
,
255
,
0
)
cv2
.
imshow
(
'edge'
,
vis
)
cv2
.
imshow
(
'edge'
,
vis
)
ch
=
cv2
.
waitKey
(
5
)
&
0xFF
ch
=
cv2
.
waitKey
(
5
)
&
0xFF
...
...
samples/python2/feature_homography.py
View file @
4c1c0b10
...
@@ -22,6 +22,9 @@ Keys:
...
@@ -22,6 +22,9 @@ Keys:
Select a textured planar object to track by drawing a box with a mouse.
Select a textured planar object to track by drawing a box with a mouse.
'''
'''
# Python 2/3 compatibility
from
__future__
import
print_function
import
numpy
as
np
import
numpy
as
np
import
cv2
import
cv2
...
@@ -84,7 +87,7 @@ class App:
...
@@ -84,7 +87,7 @@ class App:
if
__name__
==
'__main__'
:
if
__name__
==
'__main__'
:
print
__doc__
print
(
__doc__
)
import
sys
import
sys
try
:
try
:
...
...
samples/python2/find_obj.py
View file @
4c1c0b10
...
@@ -14,6 +14,9 @@ USAGE
...
@@ -14,6 +14,9 @@ USAGE
Press left mouse button on a feature point to see its matching point.
Press left mouse button on a feature point to see its matching point.
'''
'''
# Python 2/3 compatibility
from
__future__
import
print_function
import
numpy
as
np
import
numpy
as
np
import
cv2
import
cv2
from
common
import
anorm
,
getsize
from
common
import
anorm
,
getsize
...
@@ -82,8 +85,10 @@ def explore_match(win, img1, img2, kp_pairs, status = None, H = None):
...
@@ -82,8 +85,10 @@ def explore_match(win, img1, img2, kp_pairs, status = None, H = None):
if
status
is
None
:
if
status
is
None
:
status
=
np
.
ones
(
len
(
kp_pairs
),
np
.
bool_
)
status
=
np
.
ones
(
len
(
kp_pairs
),
np
.
bool_
)
p1
=
np
.
int32
([
kpp
[
0
]
.
pt
for
kpp
in
kp_pairs
])
p1
,
p2
=
[],
[]
# python 2 / python 3 change of zip unpacking
p2
=
np
.
int32
([
kpp
[
1
]
.
pt
for
kpp
in
kp_pairs
])
+
(
w1
,
0
)
for
kpp
in
kp_pairs
:
p1
.
append
(
np
.
int32
(
kpp
[
0
]
.
pt
))
p2
.
append
(
np
.
int32
(
np
.
array
(
kpp
[
1
]
.
pt
)
+
[
w1
,
0
]))
green
=
(
0
,
255
,
0
)
green
=
(
0
,
255
,
0
)
red
=
(
0
,
0
,
255
)
red
=
(
0
,
0
,
255
)
...
@@ -133,7 +138,7 @@ def explore_match(win, img1, img2, kp_pairs, status = None, H = None):
...
@@ -133,7 +138,7 @@ def explore_match(win, img1, img2, kp_pairs, status = None, H = None):
if
__name__
==
'__main__'
:
if
__name__
==
'__main__'
:
print
__doc__
print
(
__doc__
)
import
sys
,
getopt
import
sys
,
getopt
opts
,
args
=
getopt
.
getopt
(
sys
.
argv
[
1
:],
''
,
[
'feature='
])
opts
,
args
=
getopt
.
getopt
(
sys
.
argv
[
1
:],
''
,
[
'feature='
])
...
@@ -150,33 +155,33 @@ if __name__ == '__main__':
...
@@ -150,33 +155,33 @@ if __name__ == '__main__':
detector
,
matcher
=
init_feature
(
feature_name
)
detector
,
matcher
=
init_feature
(
feature_name
)
if
img1
is
None
:
if
img1
is
None
:
print
'Failed to load fn1:'
,
fn1
print
(
'Failed to load fn1:'
,
fn1
)
sys
.
exit
(
1
)
sys
.
exit
(
1
)
if
img2
is
None
:
if
img2
is
None
:
print
'Failed to load fn2:'
,
fn2
print
(
'Failed to load fn2:'
,
fn2
)
sys
.
exit
(
1
)
sys
.
exit
(
1
)
if
detector
is
None
:
if
detector
is
None
:
print
'unknown feature:'
,
feature_name
print
(
'unknown feature:'
,
feature_name
)
sys
.
exit
(
1
)
sys
.
exit
(
1
)
print
'using'
,
feature_name
print
(
'using'
,
feature_name
)
kp1
,
desc1
=
detector
.
detectAndCompute
(
img1
,
None
)
kp1
,
desc1
=
detector
.
detectAndCompute
(
img1
,
None
)
kp2
,
desc2
=
detector
.
detectAndCompute
(
img2
,
None
)
kp2
,
desc2
=
detector
.
detectAndCompute
(
img2
,
None
)
print
'img1 -
%
d features, img2 -
%
d features'
%
(
len
(
kp1
),
len
(
kp2
))
print
(
'img1 -
%
d features, img2 -
%
d features'
%
(
len
(
kp1
),
len
(
kp2
)
))
def
match_and_draw
(
win
):
def
match_and_draw
(
win
):
print
'matching...'
print
(
'matching...'
)
raw_matches
=
matcher
.
knnMatch
(
desc1
,
trainDescriptors
=
desc2
,
k
=
2
)
#2
raw_matches
=
matcher
.
knnMatch
(
desc1
,
trainDescriptors
=
desc2
,
k
=
2
)
#2
p1
,
p2
,
kp_pairs
=
filter_matches
(
kp1
,
kp2
,
raw_matches
)
p1
,
p2
,
kp_pairs
=
filter_matches
(
kp1
,
kp2
,
raw_matches
)
if
len
(
p1
)
>=
4
:
if
len
(
p1
)
>=
4
:
H
,
status
=
cv2
.
findHomography
(
p1
,
p2
,
cv2
.
RANSAC
,
5.0
)
H
,
status
=
cv2
.
findHomography
(
p1
,
p2
,
cv2
.
RANSAC
,
5.0
)
print
'
%
d /
%
d inliers/matched'
%
(
np
.
sum
(
status
),
len
(
status
))
print
(
'
%
d /
%
d inliers/matched'
%
(
np
.
sum
(
status
),
len
(
status
)
))
else
:
else
:
H
,
status
=
None
,
None
H
,
status
=
None
,
None
print
'
%
d matches found, not enough for homography estimation'
%
len
(
p1
)
print
(
'
%
d matches found, not enough for homography estimation'
%
len
(
p1
)
)
vis
=
explore_match
(
win
,
img1
,
img2
,
kp_pairs
,
status
,
H
)
vis
=
explore_match
(
win
,
img1
,
img2
,
kp_pairs
,
status
,
H
)
...
...
samples/python2/gaussian_mix.py
View file @
4c1c0b10
#!/usr/bin/env python
#!/usr/bin/env python
# Python 2/3 compatibility
from
__future__
import
print_function
import
sys
PY3
=
sys
.
version_info
[
0
]
==
3
if
PY3
:
xrange
=
range
import
numpy
as
np
import
numpy
as
np
from
numpy
import
random
from
numpy
import
random
import
cv2
import
cv2
...
@@ -30,19 +38,21 @@ if __name__ == '__main__':
...
@@ -30,19 +38,21 @@ if __name__ == '__main__':
cluster_n
=
5
cluster_n
=
5
img_size
=
512
img_size
=
512
print
'press any key to update distributions, ESC - exit
\n
'
print
(
'press any key to update distributions, ESC - exit
\n
'
)
while
True
:
while
True
:
print
'sampling distributions...'
print
(
'sampling distributions...'
)
points
,
ref_distrs
=
make_gaussians
(
cluster_n
,
img_size
)
points
,
ref_distrs
=
make_gaussians
(
cluster_n
,
img_size
)
print
'EM (opencv) ...'
print
(
'EM (opencv) ...'
)
em
=
cv2
.
EM
(
cluster_n
,
cv2
.
EM_COV_MAT_GENERIC
)
em
=
cv2
.
ml
.
EM_create
()
em
.
train
(
points
)
em
.
setClustersNumber
(
cluster_n
)
means
=
em
.
getMat
(
'means'
)
em
.
setCovarianceMatrixType
(
cv2
.
ml
.
EM_COV_MAT_GENERIC
)
covs
=
em
.
getMatVector
(
'covs'
)
em
.
trainEM
(
points
)
means
=
em
.
getMeans
()
covs
=
em
.
getCovs
()
# Known bug: https://github.com/Itseez/opencv/pull/4232
found_distrs
=
zip
(
means
,
covs
)
found_distrs
=
zip
(
means
,
covs
)
print
'ready!
\n
'
print
(
'ready!
\n
'
)
img
=
np
.
zeros
((
img_size
,
img_size
,
3
),
np
.
uint8
)
img
=
np
.
zeros
((
img_size
,
img_size
,
3
),
np
.
uint8
)
for
x
,
y
in
np
.
int32
(
points
):
for
x
,
y
in
np
.
int32
(
points
):
...
...
samples/python2/hist.py
View file @
4c1c0b10
...
@@ -15,6 +15,9 @@ Usage : python hist.py <image_file>
...
@@ -15,6 +15,9 @@ Usage : python hist.py <image_file>
Abid Rahman 3/14/12 debug Gary Bradski
Abid Rahman 3/14/12 debug Gary Bradski
'''
'''
# Python 2/3 compatibility
from
__future__
import
print_function
import
cv2
import
cv2
import
numpy
as
np
import
numpy
as
np
...
@@ -38,8 +41,8 @@ def hist_curve(im):
...
@@ -38,8 +41,8 @@ def hist_curve(im):
def
hist_lines
(
im
):
def
hist_lines
(
im
):
h
=
np
.
zeros
((
300
,
256
,
3
))
h
=
np
.
zeros
((
300
,
256
,
3
))
if
len
(
im
.
shape
)
!=
2
:
if
len
(
im
.
shape
)
!=
2
:
print
"hist_lines applicable only for grayscale images"
print
(
"hist_lines applicable only for grayscale images"
)
#print
"so converting image to grayscale for representation"
#print
(
"so converting image to grayscale for representation"
im
=
cv2
.
cvtColor
(
im
,
cv2
.
COLOR_BGR2GRAY
)
im
=
cv2
.
cvtColor
(
im
,
cv2
.
COLOR_BGR2GRAY
)
hist_item
=
cv2
.
calcHist
([
im
],[
0
],
None
,[
256
],[
0
,
256
])
hist_item
=
cv2
.
calcHist
([
im
],[
0
],
None
,[
256
],[
0
,
256
])
cv2
.
normalize
(
hist_item
,
hist_item
,
0
,
255
,
cv2
.
NORM_MINMAX
)
cv2
.
normalize
(
hist_item
,
hist_item
,
0
,
255
,
cv2
.
NORM_MINMAX
)
...
@@ -58,18 +61,18 @@ if __name__ == '__main__':
...
@@ -58,18 +61,18 @@ if __name__ == '__main__':
fname
=
sys
.
argv
[
1
]
fname
=
sys
.
argv
[
1
]
else
:
else
:
fname
=
'../data/lena.jpg'
fname
=
'../data/lena.jpg'
print
"usage : python hist.py <image_file>"
print
(
"usage : python hist.py <image_file>"
)
im
=
cv2
.
imread
(
fname
)
im
=
cv2
.
imread
(
fname
)
if
im
is
None
:
if
im
is
None
:
print
'Failed to load image file:'
,
fname
print
(
'Failed to load image file:'
,
fname
)
sys
.
exit
(
1
)
sys
.
exit
(
1
)
gray
=
cv2
.
cvtColor
(
im
,
cv2
.
COLOR_BGR2GRAY
)
gray
=
cv2
.
cvtColor
(
im
,
cv2
.
COLOR_BGR2GRAY
)
print
''' Histogram plotting
\n
print
(
''' Histogram plotting
\n
Keymap :
\n
Keymap :
\n
a - show histogram for color image in curve mode
\n
a - show histogram for color image in curve mode
\n
b - show histogram in bin mode
\n
b - show histogram in bin mode
\n
...
@@ -77,7 +80,7 @@ if __name__ == '__main__':
...
@@ -77,7 +80,7 @@ if __name__ == '__main__':
d - show histogram for color image in curve mode
\n
d - show histogram for color image in curve mode
\n
e - show histogram for a normalized image in curve mode
\n
e - show histogram for a normalized image in curve mode
\n
Esc - exit
\n
Esc - exit
\n
'''
'''
)
cv2
.
imshow
(
'image'
,
im
)
cv2
.
imshow
(
'image'
,
im
)
while
True
:
while
True
:
...
@@ -86,31 +89,31 @@ if __name__ == '__main__':
...
@@ -86,31 +89,31 @@ if __name__ == '__main__':
curve
=
hist_curve
(
im
)
curve
=
hist_curve
(
im
)
cv2
.
imshow
(
'histogram'
,
curve
)
cv2
.
imshow
(
'histogram'
,
curve
)
cv2
.
imshow
(
'image'
,
im
)
cv2
.
imshow
(
'image'
,
im
)
print
'a'
print
(
'a'
)
elif
k
==
ord
(
'b'
):
elif
k
==
ord
(
'b'
):
print
'b'
print
(
'b'
)
lines
=
hist_lines
(
im
)
lines
=
hist_lines
(
im
)
cv2
.
imshow
(
'histogram'
,
lines
)
cv2
.
imshow
(
'histogram'
,
lines
)
cv2
.
imshow
(
'image'
,
gray
)
cv2
.
imshow
(
'image'
,
gray
)
elif
k
==
ord
(
'c'
):
elif
k
==
ord
(
'c'
):
print
'c'
print
(
'c'
)
equ
=
cv2
.
equalizeHist
(
gray
)
equ
=
cv2
.
equalizeHist
(
gray
)
lines
=
hist_lines
(
equ
)
lines
=
hist_lines
(
equ
)
cv2
.
imshow
(
'histogram'
,
lines
)
cv2
.
imshow
(
'histogram'
,
lines
)
cv2
.
imshow
(
'image'
,
equ
)
cv2
.
imshow
(
'image'
,
equ
)
elif
k
==
ord
(
'd'
):
elif
k
==
ord
(
'd'
):
print
'd'
print
(
'd'
)
curve
=
hist_curve
(
gray
)
curve
=
hist_curve
(
gray
)
cv2
.
imshow
(
'histogram'
,
curve
)
cv2
.
imshow
(
'histogram'
,
curve
)
cv2
.
imshow
(
'image'
,
gray
)
cv2
.
imshow
(
'image'
,
gray
)
elif
k
==
ord
(
'e'
):
elif
k
==
ord
(
'e'
):
print
'e'
print
(
'e'
)
norm
=
cv2
.
normalize
(
gray
,
alpha
=
0
,
beta
=
255
,
norm_type
=
cv2
.
NORM_MINMAX
)
norm
=
cv2
.
normalize
(
gray
,
gray
,
alpha
=
0
,
beta
=
255
,
norm_type
=
cv2
.
NORM_MINMAX
)
lines
=
hist_lines
(
norm
)
lines
=
hist_lines
(
norm
)
cv2
.
imshow
(
'histogram'
,
lines
)
cv2
.
imshow
(
'histogram'
,
lines
)
cv2
.
imshow
(
'image'
,
norm
)
cv2
.
imshow
(
'image'
,
norm
)
elif
k
==
27
:
elif
k
==
27
:
print
'ESC'
print
(
'ESC'
)
cv2
.
destroyAllWindows
()
cv2
.
destroyAllWindows
()
break
break
cv2
.
destroyAllWindows
()
cv2
.
destroyAllWindows
()
samples/python2/kmeans.py
View file @
4c1c0b10
...
@@ -10,6 +10,9 @@ Keyboard shortcuts:
...
@@ -10,6 +10,9 @@ Keyboard shortcuts:
space - generate new distribution
space - generate new distribution
'''
'''
# Python 2/3 compatibility
from
__future__
import
print_function
import
numpy
as
np
import
numpy
as
np
import
cv2
import
cv2
...
@@ -19,7 +22,7 @@ if __name__ == '__main__':
...
@@ -19,7 +22,7 @@ if __name__ == '__main__':
cluster_n
=
5
cluster_n
=
5
img_size
=
512
img_size
=
512
print
__doc__
print
(
__doc__
)
# generating bright palette
# generating bright palette
colors
=
np
.
zeros
((
1
,
cluster_n
,
3
),
np
.
uint8
)
colors
=
np
.
zeros
((
1
,
cluster_n
,
3
),
np
.
uint8
)
...
@@ -28,7 +31,7 @@ if __name__ == '__main__':
...
@@ -28,7 +31,7 @@ if __name__ == '__main__':
colors
=
cv2
.
cvtColor
(
colors
,
cv2
.
COLOR_HSV2BGR
)[
0
]
colors
=
cv2
.
cvtColor
(
colors
,
cv2
.
COLOR_HSV2BGR
)[
0
]
while
True
:
while
True
:
print
'sampling distributions...'
print
(
'sampling distributions...'
)
points
,
_
=
make_gaussians
(
cluster_n
,
img_size
)
points
,
_
=
make_gaussians
(
cluster_n
,
img_size
)
term_crit
=
(
cv2
.
TERM_CRITERIA_EPS
,
30
,
0.1
)
term_crit
=
(
cv2
.
TERM_CRITERIA_EPS
,
30
,
0.1
)
...
@@ -36,7 +39,8 @@ if __name__ == '__main__':
...
@@ -36,7 +39,8 @@ if __name__ == '__main__':
img
=
np
.
zeros
((
img_size
,
img_size
,
3
),
np
.
uint8
)
img
=
np
.
zeros
((
img_size
,
img_size
,
3
),
np
.
uint8
)
for
(
x
,
y
),
label
in
zip
(
np
.
int32
(
points
),
labels
.
ravel
()):
for
(
x
,
y
),
label
in
zip
(
np
.
int32
(
points
),
labels
.
ravel
()):
c
=
map
(
int
,
colors
[
label
])
c
=
list
(
map
(
int
,
colors
[
label
]))
cv2
.
circle
(
img
,
(
x
,
y
),
1
,
c
,
-
1
)
cv2
.
circle
(
img
,
(
x
,
y
),
1
,
c
,
-
1
)
cv2
.
imshow
(
'gaussian mixture'
,
img
)
cv2
.
imshow
(
'gaussian mixture'
,
img
)
...
...
samples/python2/lappyr.py
View file @
4c1c0b10
...
@@ -64,7 +64,7 @@ if __name__ == '__main__':
...
@@ -64,7 +64,7 @@ if __name__ == '__main__':
pyr
=
build_lappyr
(
frame
,
leveln
)
pyr
=
build_lappyr
(
frame
,
leveln
)
for
i
in
xrange
(
leveln
):
for
i
in
xrange
(
leveln
):
v
=
cv2
.
getTrackbarPos
(
'
%
d'
%
i
,
'level control'
)
/
5
v
=
int
(
cv2
.
getTrackbarPos
(
'
%
d'
%
i
,
'level control'
)
/
5
)
pyr
[
i
]
*=
v
pyr
[
i
]
*=
v
res
=
merge_lappyr
(
pyr
)
res
=
merge_lappyr
(
pyr
)
...
...
samples/python2/letter_recog.py
View file @
4c1c0b10
...
@@ -25,6 +25,9 @@ USAGE:
...
@@ -25,6 +25,9 @@ USAGE:
Models: RTrees, KNearest, Boost, SVM, MLP
Models: RTrees, KNearest, Boost, SVM, MLP
'''
'''
# Python 2/3 compatibility
from
__future__
import
print_function
import
numpy
as
np
import
numpy
as
np
import
cv2
import
cv2
...
@@ -58,22 +61,22 @@ class LetterStatModel(object):
...
@@ -58,22 +61,22 @@ class LetterStatModel(object):
class
RTrees
(
LetterStatModel
):
class
RTrees
(
LetterStatModel
):
def
__init__
(
self
):
def
__init__
(
self
):
self
.
model
=
cv2
.
RTrees
()
self
.
model
=
cv2
.
ml
.
RTrees_create
()
def
train
(
self
,
samples
,
responses
):
def
train
(
self
,
samples
,
responses
):
sample_n
,
var_n
=
samples
.
shape
sample_n
,
var_n
=
samples
.
shape
var_types
=
np
.
array
([
cv2
.
CV_VAR_NUMERICAL
]
*
var_n
+
[
cv2
.
CV_
VAR_CATEGORICAL
],
np
.
uint8
)
var_types
=
np
.
array
([
cv2
.
ml
.
VAR_NUMERICAL
]
*
var_n
+
[
cv2
.
ml
.
VAR_CATEGORICAL
],
np
.
uint8
)
#CvRTParams(10,10,0,false,15,0,true,4,100,0.01f,CV_TERMCRIT_ITER));
#CvRTParams(10,10,0,false,15,0,true,4,100,0.01f,CV_TERMCRIT_ITER));
params
=
dict
(
max_depth
=
10
)
params
=
dict
(
max_depth
=
10
)
self
.
model
.
train
(
samples
,
cv2
.
CV_
ROW_SAMPLE
,
responses
,
varType
=
var_types
,
params
=
params
)
self
.
model
.
train
(
samples
,
cv2
.
ml
.
ROW_SAMPLE
,
responses
,
varType
=
var_types
,
params
=
params
)
def
predict
(
self
,
samples
):
def
predict
(
self
,
samples
):
return
np
.
float32
(
[
self
.
model
.
predict
(
s
)
for
s
in
samples
]
)
return
[
self
.
model
.
predict
(
s
)
for
s
in
samples
]
class
KNearest
(
LetterStatModel
):
class
KNearest
(
LetterStatModel
):
def
__init__
(
self
):
def
__init__
(
self
):
self
.
model
=
cv2
.
KNearest
()
self
.
model
=
cv2
.
ml
.
KNearest_create
()
def
train
(
self
,
samples
,
responses
):
def
train
(
self
,
samples
,
responses
):
self
.
model
.
train
(
samples
,
responses
)
self
.
model
.
train
(
samples
,
responses
)
...
@@ -85,16 +88,16 @@ class KNearest(LetterStatModel):
...
@@ -85,16 +88,16 @@ class KNearest(LetterStatModel):
class
Boost
(
LetterStatModel
):
class
Boost
(
LetterStatModel
):
def
__init__
(
self
):
def
__init__
(
self
):
self
.
model
=
cv2
.
Boost
()
self
.
model
=
cv2
.
ml
.
Boost_create
()
def
train
(
self
,
samples
,
responses
):
def
train
(
self
,
samples
,
responses
):
sample_n
,
var_n
=
samples
.
shape
sample_n
,
var_n
=
samples
.
shape
new_samples
=
self
.
unroll_samples
(
samples
)
new_samples
=
self
.
unroll_samples
(
samples
)
new_responses
=
self
.
unroll_responses
(
responses
)
new_responses
=
self
.
unroll_responses
(
responses
)
var_types
=
np
.
array
([
cv2
.
CV_VAR_NUMERICAL
]
*
var_n
+
[
cv2
.
CV_VAR_CATEGORICAL
,
cv2
.
CV_
VAR_CATEGORICAL
],
np
.
uint8
)
var_types
=
np
.
array
([
cv2
.
ml
.
VAR_NUMERICAL
]
*
var_n
+
[
cv2
.
ml
.
VAR_CATEGORICAL
,
cv2
.
ml
.
VAR_CATEGORICAL
],
np
.
uint8
)
#CvBoostParams(CvBoost::REAL, 100, 0.95, 5, false, 0 )
#CvBoostParams(CvBoost::REAL, 100, 0.95, 5, false, 0 )
params
=
dict
(
max_depth
=
5
)
#, use_surrogates=False)
params
=
dict
(
max_depth
=
5
)
#, use_surrogates=False)
self
.
model
.
train
(
new_samples
,
cv2
.
CV_
ROW_SAMPLE
,
new_responses
,
varType
=
var_types
,
params
=
params
)
self
.
model
.
train
(
new_samples
,
cv2
.
ml
.
ROW_SAMPLE
,
new_responses
,
varType
=
var_types
,
params
=
params
)
def
predict
(
self
,
samples
):
def
predict
(
self
,
samples
):
new_samples
=
self
.
unroll_samples
(
samples
)
new_samples
=
self
.
unroll_samples
(
samples
)
...
@@ -105,11 +108,11 @@ class Boost(LetterStatModel):
...
@@ -105,11 +108,11 @@ class Boost(LetterStatModel):
class
SVM
(
LetterStatModel
):
class
SVM
(
LetterStatModel
):
def
__init__
(
self
):
def
__init__
(
self
):
self
.
model
=
cv2
.
SVM
()
self
.
model
=
cv2
.
ml
.
SVM_create
()
def
train
(
self
,
samples
,
responses
):
def
train
(
self
,
samples
,
responses
):
params
=
dict
(
kernel_type
=
cv2
.
SVM_LINEAR
,
params
=
dict
(
kernel_type
=
cv2
.
ml
.
SVM_LINEAR
,
svm_type
=
cv2
.
SVM_C_SVC
,
svm_type
=
cv2
.
ml
.
SVM_C_SVC
,
C
=
1
)
C
=
1
)
self
.
model
.
train
(
samples
,
responses
,
params
=
params
)
self
.
model
.
train
(
samples
,
responses
,
params
=
params
)
...
@@ -119,7 +122,7 @@ class SVM(LetterStatModel):
...
@@ -119,7 +122,7 @@ class SVM(LetterStatModel):
class
MLP
(
LetterStatModel
):
class
MLP
(
LetterStatModel
):
def
__init__
(
self
):
def
__init__
(
self
):
self
.
model
=
cv2
.
ANN_MLP
()
self
.
model
=
cv2
.
ml
.
ANN_MLP_create
()
def
train
(
self
,
samples
,
responses
):
def
train
(
self
,
samples
,
responses
):
sample_n
,
var_n
=
samples
.
shape
sample_n
,
var_n
=
samples
.
shape
...
@@ -130,7 +133,7 @@ class MLP(LetterStatModel):
...
@@ -130,7 +133,7 @@ class MLP(LetterStatModel):
# CvANN_MLP_TrainParams::BACKPROP,0.001
# CvANN_MLP_TrainParams::BACKPROP,0.001
params
=
dict
(
term_crit
=
(
cv2
.
TERM_CRITERIA_COUNT
,
300
,
0.01
),
params
=
dict
(
term_crit
=
(
cv2
.
TERM_CRITERIA_COUNT
,
300
,
0.01
),
train_method
=
cv2
.
ANN_MLP_TRAIN_PARAMS_BACKPROP
,
train_method
=
cv2
.
ml
.
ANN_MLP_TRAIN_PARAMS_BACKPROP
,
bp_dw_scale
=
0.001
,
bp_dw_scale
=
0.001
,
bp_moment_scale
=
0.0
)
bp_moment_scale
=
0.0
)
self
.
model
.
train
(
samples
,
np
.
float32
(
new_responses
),
None
,
params
=
params
)
self
.
model
.
train
(
samples
,
np
.
float32
(
new_responses
),
None
,
params
=
params
)
...
@@ -144,7 +147,7 @@ if __name__ == '__main__':
...
@@ -144,7 +147,7 @@ if __name__ == '__main__':
import
getopt
import
getopt
import
sys
import
sys
print
__doc__
print
(
__doc__
)
models
=
[
RTrees
,
KNearest
,
Boost
,
SVM
,
MLP
]
# NBayes
models
=
[
RTrees
,
KNearest
,
Boost
,
SVM
,
MLP
]
# NBayes
models
=
dict
(
[(
cls
.
__name__
.
lower
(),
cls
)
for
cls
in
models
]
)
models
=
dict
(
[(
cls
.
__name__
.
lower
(),
cls
)
for
cls
in
models
]
)
...
@@ -155,7 +158,7 @@ if __name__ == '__main__':
...
@@ -155,7 +158,7 @@ if __name__ == '__main__':
args
.
setdefault
(
'--model'
,
'rtrees'
)
args
.
setdefault
(
'--model'
,
'rtrees'
)
args
.
setdefault
(
'--data'
,
'../data/letter-recognition.data'
)
args
.
setdefault
(
'--data'
,
'../data/letter-recognition.data'
)
print
'loading data
%
s ...'
%
args
[
'--data'
]
print
(
'loading data
%
s ...'
%
args
[
'--data'
])
samples
,
responses
=
load_base
(
args
[
'--data'
])
samples
,
responses
=
load_base
(
args
[
'--data'
])
Model
=
models
[
args
[
'--model'
]]
Model
=
models
[
args
[
'--model'
]]
model
=
Model
()
model
=
Model
()
...
@@ -163,20 +166,20 @@ if __name__ == '__main__':
...
@@ -163,20 +166,20 @@ if __name__ == '__main__':
train_n
=
int
(
len
(
samples
)
*
model
.
train_ratio
)
train_n
=
int
(
len
(
samples
)
*
model
.
train_ratio
)
if
'--load'
in
args
:
if
'--load'
in
args
:
fn
=
args
[
'--load'
]
fn
=
args
[
'--load'
]
print
'loading model from
%
s ...'
%
fn
print
(
'loading model from
%
s ...'
%
fn
)
model
.
load
(
fn
)
model
.
load
(
fn
)
else
:
else
:
print
'training
%
s ...'
%
Model
.
__name__
print
(
'training
%
s ...'
%
Model
.
__name__
)
model
.
train
(
samples
[:
train_n
],
responses
[:
train_n
])
model
.
train
(
samples
[:
train_n
],
responses
[:
train_n
])
print
'testing...'
print
(
'testing...'
)
train_rate
=
np
.
mean
(
model
.
predict
(
samples
[:
train_n
])
==
responses
[:
train_n
])
train_rate
=
np
.
mean
(
model
.
predict
(
samples
[:
train_n
])
==
responses
[:
train_n
])
test_rate
=
np
.
mean
(
model
.
predict
(
samples
[
train_n
:])
==
responses
[
train_n
:])
test_rate
=
np
.
mean
(
model
.
predict
(
samples
[
train_n
:])
==
responses
[
train_n
:])
print
'train rate:
%
f test rate:
%
f'
%
(
train_rate
*
100
,
test_rate
*
100
)
print
(
'train rate:
%
f test rate:
%
f'
%
(
train_rate
*
100
,
test_rate
*
100
)
)
if
'--save'
in
args
:
if
'--save'
in
args
:
fn
=
args
[
'--save'
]
fn
=
args
[
'--save'
]
print
'saving model to
%
s ...'
%
fn
print
(
'saving model to
%
s ...'
%
fn
)
model
.
save
(
fn
)
model
.
save
(
fn
)
cv2
.
destroyAllWindows
()
cv2
.
destroyAllWindows
()
samples/python2/mouse_and_match.py
View file @
4c1c0b10
#!/usr/bin/env python
#!/usr/bin/env python
'''
'''
mouse_and_match.py [-i path | --input path: default ./]
mouse_and_match.py [-i path | --input path: default .
./data
/]
Demonstrate using a mouse to interact with an image:
Demonstrate using a mouse to interact with an image:
Read in the images in a directory one by one
Read in the images in a directory one by one
Allow the user to select parts of an image with a mouse
Allow the user to select parts of an image with a mouse
When they let go of the mouse, it correlates (using matchTemplate) that patch with the image.
When they let go of the mouse, it correlates (using matchTemplate) that patch with the image.
SPACE for next image
ESC to exit
ESC to exit
'''
'''
...
@@ -54,8 +56,10 @@ def onmouse(event, x, y, flags, param):
...
@@ -54,8 +56,10 @@ def onmouse(event, x, y, flags, param):
drag_start
=
None
drag_start
=
None
if
__name__
==
'__main__'
:
if
__name__
==
'__main__'
:
print
(
__doc__
)
parser
=
argparse
.
ArgumentParser
(
description
=
'Demonstrate mouse interaction with images'
)
parser
=
argparse
.
ArgumentParser
(
description
=
'Demonstrate mouse interaction with images'
)
parser
.
add_argument
(
"-i"
,
"--input"
,
default
=
'./'
,
help
=
"Input directory."
)
parser
.
add_argument
(
"-i"
,
"--input"
,
default
=
'.
./data
/'
,
help
=
"Input directory."
)
args
=
parser
.
parse_args
()
args
=
parser
.
parse_args
()
path
=
args
.
input
path
=
args
.
input
...
@@ -68,7 +72,7 @@ if __name__ == '__main__':
...
@@ -68,7 +72,7 @@ if __name__ == '__main__':
print
(
infile
)
print
(
infile
)
img
=
cv2
.
imread
(
infile
,
1
)
img
=
cv2
.
imread
(
infile
,
1
)
if
img
==
None
:
if
img
is
None
:
continue
continue
sel
=
(
0
,
0
,
0
,
0
)
sel
=
(
0
,
0
,
0
,
0
)
drag_start
=
None
drag_start
=
None
...
...
samples/python2/opt_flow.py
View file @
4c1c0b10
#!/usr/bin/env python
#!/usr/bin/env python
# Python 2/3 compatibility
from
__future__
import
print_function
import
numpy
as
np
import
numpy
as
np
import
cv2
import
cv2
import
video
import
video
...
@@ -15,7 +18,7 @@ Keys:
...
@@ -15,7 +18,7 @@ Keys:
def
draw_flow
(
img
,
flow
,
step
=
16
):
def
draw_flow
(
img
,
flow
,
step
=
16
):
h
,
w
=
img
.
shape
[:
2
]
h
,
w
=
img
.
shape
[:
2
]
y
,
x
=
np
.
mgrid
[
step
/
2
:
h
:
step
,
step
/
2
:
w
:
step
]
.
reshape
(
2
,
-
1
)
y
,
x
=
np
.
mgrid
[
step
/
2
:
h
:
step
,
step
/
2
:
w
:
step
]
.
reshape
(
2
,
-
1
)
.
astype
(
int
)
fx
,
fy
=
flow
[
y
,
x
]
.
T
fx
,
fy
=
flow
[
y
,
x
]
.
T
lines
=
np
.
vstack
([
x
,
y
,
x
+
fx
,
y
+
fy
])
.
T
.
reshape
(
-
1
,
2
,
2
)
lines
=
np
.
vstack
([
x
,
y
,
x
+
fx
,
y
+
fy
])
.
T
.
reshape
(
-
1
,
2
,
2
)
lines
=
np
.
int32
(
lines
+
0.5
)
lines
=
np
.
int32
(
lines
+
0.5
)
...
@@ -47,7 +50,7 @@ def warp_flow(img, flow):
...
@@ -47,7 +50,7 @@ def warp_flow(img, flow):
if
__name__
==
'__main__'
:
if
__name__
==
'__main__'
:
import
sys
import
sys
print
help_message
print
(
help_message
)
try
:
try
:
fn
=
sys
.
argv
[
1
]
fn
=
sys
.
argv
[
1
]
except
:
except
:
...
@@ -78,10 +81,10 @@ if __name__ == '__main__':
...
@@ -78,10 +81,10 @@ if __name__ == '__main__':
break
break
if
ch
==
ord
(
'1'
):
if
ch
==
ord
(
'1'
):
show_hsv
=
not
show_hsv
show_hsv
=
not
show_hsv
print
'HSV flow visualization is'
,
[
'off'
,
'on'
][
show_hsv
]
print
(
'HSV flow visualization is'
,
[
'off'
,
'on'
][
show_hsv
])
if
ch
==
ord
(
'2'
):
if
ch
==
ord
(
'2'
):
show_glitch
=
not
show_glitch
show_glitch
=
not
show_glitch
if
show_glitch
:
if
show_glitch
:
cur_glitch
=
img
.
copy
()
cur_glitch
=
img
.
copy
()
print
'glitch is'
,
[
'off'
,
'on'
][
show_glitch
]
print
(
'glitch is'
,
[
'off'
,
'on'
][
show_glitch
])
cv2
.
destroyAllWindows
()
cv2
.
destroyAllWindows
()
samples/python2/peopledetect.py
View file @
4c1c0b10
...
@@ -35,7 +35,9 @@ if __name__ == '__main__':
...
@@ -35,7 +35,9 @@ if __name__ == '__main__':
hog
=
cv2
.
HOGDescriptor
()
hog
=
cv2
.
HOGDescriptor
()
hog
.
setSVMDetector
(
cv2
.
HOGDescriptor_getDefaultPeopleDetector
()
)
hog
.
setSVMDetector
(
cv2
.
HOGDescriptor_getDefaultPeopleDetector
()
)
for
fn
in
it
.
chain
(
*
map
(
glob
,
sys
.
argv
[
1
:])):
default
=
[
'../data/basketball2.png '
]
if
len
(
sys
.
argv
[
1
:])
==
0
else
[]
for
fn
in
it
.
chain
(
*
map
(
glob
,
default
+
sys
.
argv
[
1
:])):
print
(
fn
,
' - '
,)
print
(
fn
,
' - '
,)
try
:
try
:
img
=
cv2
.
imread
(
fn
)
img
=
cv2
.
imread
(
fn
)
...
...
samples/python2/plane_ar.py
View file @
4c1c0b10
...
@@ -22,6 +22,9 @@ Select a textured planar object to track by drawing a box with a mouse.
...
@@ -22,6 +22,9 @@ Select a textured planar object to track by drawing a box with a mouse.
Use 'focal' slider to adjust to camera focal length for proper video augmentation.
Use 'focal' slider to adjust to camera focal length for proper video augmentation.
'''
'''
# Python 2/3 compatibility
from
__future__
import
print_function
import
numpy
as
np
import
numpy
as
np
import
cv2
import
cv2
import
video
import
video
...
@@ -97,7 +100,7 @@ class App:
...
@@ -97,7 +100,7 @@ class App:
if
__name__
==
'__main__'
:
if
__name__
==
'__main__'
:
print
__doc__
print
(
__doc__
)
import
sys
import
sys
try
:
try
:
...
...
samples/python2/plane_tracker.py
View file @
4c1c0b10
...
@@ -21,6 +21,14 @@ Keys:
...
@@ -21,6 +21,14 @@ Keys:
Select a textured planar object to track by drawing a box with a mouse.
Select a textured planar object to track by drawing a box with a mouse.
'''
'''
# Python 2/3 compatibility
from
__future__
import
print_function
import
sys
PY3
=
sys
.
version_info
[
0
]
==
3
if
PY3
:
xrange
=
range
import
numpy
as
np
import
numpy
as
np
import
cv2
import
cv2
...
@@ -64,6 +72,7 @@ class PlaneTracker:
...
@@ -64,6 +72,7 @@ class PlaneTracker:
self
.
detector
=
cv2
.
ORB_create
(
nfeatures
=
1000
)
self
.
detector
=
cv2
.
ORB_create
(
nfeatures
=
1000
)
self
.
matcher
=
cv2
.
FlannBasedMatcher
(
flann_params
,
{})
# bug : need to pass empty dict (#1329)
self
.
matcher
=
cv2
.
FlannBasedMatcher
(
flann_params
,
{})
# bug : need to pass empty dict (#1329)
self
.
targets
=
[]
self
.
targets
=
[]
self
.
frame_points
=
[]
def
add_target
(
self
,
image
,
rect
,
data
=
None
):
def
add_target
(
self
,
image
,
rect
,
data
=
None
):
'''Add a new tracking target.'''
'''Add a new tracking target.'''
...
@@ -87,8 +96,8 @@ class PlaneTracker:
...
@@ -87,8 +96,8 @@ class PlaneTracker:
def
track
(
self
,
frame
):
def
track
(
self
,
frame
):
'''Returns a list of detected TrackedTarget objects'''
'''Returns a list of detected TrackedTarget objects'''
frame_points
,
frame_descrs
=
self
.
detect_features
(
frame
)
self
.
frame_points
,
frame_descrs
=
self
.
detect_features
(
frame
)
if
len
(
frame_points
)
<
MIN_MATCH_COUNT
:
if
len
(
self
.
frame_points
)
<
MIN_MATCH_COUNT
:
return
[]
return
[]
matches
=
self
.
matcher
.
knnMatch
(
frame_descrs
,
k
=
2
)
matches
=
self
.
matcher
.
knnMatch
(
frame_descrs
,
k
=
2
)
matches
=
[
m
[
0
]
for
m
in
matches
if
len
(
m
)
==
2
and
m
[
0
]
.
distance
<
m
[
1
]
.
distance
*
0.75
]
matches
=
[
m
[
0
]
for
m
in
matches
if
len
(
m
)
==
2
and
m
[
0
]
.
distance
<
m
[
1
]
.
distance
*
0.75
]
...
@@ -103,7 +112,7 @@ class PlaneTracker:
...
@@ -103,7 +112,7 @@ class PlaneTracker:
continue
continue
target
=
self
.
targets
[
imgIdx
]
target
=
self
.
targets
[
imgIdx
]
p0
=
[
target
.
keypoints
[
m
.
trainIdx
]
.
pt
for
m
in
matches
]
p0
=
[
target
.
keypoints
[
m
.
trainIdx
]
.
pt
for
m
in
matches
]
p1
=
[
frame_points
[
m
.
queryIdx
]
.
pt
for
m
in
matches
]
p1
=
[
self
.
frame_points
[
m
.
queryIdx
]
.
pt
for
m
in
matches
]
p0
,
p1
=
np
.
float32
((
p0
,
p1
))
p0
,
p1
=
np
.
float32
((
p0
,
p1
))
H
,
status
=
cv2
.
findHomography
(
p0
,
p1
,
cv2
.
RANSAC
,
3.0
)
H
,
status
=
cv2
.
findHomography
(
p0
,
p1
,
cv2
.
RANSAC
,
3.0
)
status
=
status
.
ravel
()
!=
0
status
=
status
.
ravel
()
!=
0
...
@@ -169,7 +178,7 @@ class App:
...
@@ -169,7 +178,7 @@ class App:
break
break
if
__name__
==
'__main__'
:
if
__name__
==
'__main__'
:
print
__doc__
print
(
__doc__
)
import
sys
import
sys
try
:
try
:
...
...
samples/python2/stereo_match.py
View file @
4c1c0b10
...
@@ -6,6 +6,9 @@ Simple example of stereo image matching and point cloud generation.
...
@@ -6,6 +6,9 @@ Simple example of stereo image matching and point cloud generation.
Resulting .ply file cam be easily viewed using MeshLab ( http://meshlab.sourceforge.net/ )
Resulting .ply file cam be easily viewed using MeshLab ( http://meshlab.sourceforge.net/ )
'''
'''
# Python 2/3 compatibility
from
__future__
import
print_function
import
numpy
as
np
import
numpy
as
np
import
cv2
import
cv2
...
@@ -25,13 +28,13 @@ def write_ply(fn, verts, colors):
...
@@ -25,13 +28,13 @@ def write_ply(fn, verts, colors):
verts
=
verts
.
reshape
(
-
1
,
3
)
verts
=
verts
.
reshape
(
-
1
,
3
)
colors
=
colors
.
reshape
(
-
1
,
3
)
colors
=
colors
.
reshape
(
-
1
,
3
)
verts
=
np
.
hstack
([
verts
,
colors
])
verts
=
np
.
hstack
([
verts
,
colors
])
with
open
(
fn
,
'w'
)
as
f
:
with
open
(
fn
,
'w
b
'
)
as
f
:
f
.
write
(
ply_header
%
dict
(
vert_num
=
len
(
verts
)
))
f
.
write
(
(
ply_header
%
dict
(
vert_num
=
len
(
verts
)))
.
encode
(
'utf-8'
))
np
.
savetxt
(
f
,
verts
,
'
%
f
%
f
%
f
%
d
%
d
%
d
'
)
np
.
savetxt
(
f
,
verts
,
fmt
=
'
%
f
%
f
%
f
%
d
%
d
%
d
'
)
if
__name__
==
'__main__'
:
if
__name__
==
'__main__'
:
print
'loading images...'
print
(
'loading images...'
)
imgL
=
cv2
.
pyrDown
(
cv2
.
imread
(
'../data/aloeL.jpg'
)
)
# downscale images for faster processing
imgL
=
cv2
.
pyrDown
(
cv2
.
imread
(
'../data/aloeL.jpg'
)
)
# downscale images for faster processing
imgR
=
cv2
.
pyrDown
(
cv2
.
imread
(
'../data/aloeR.jpg'
)
)
imgR
=
cv2
.
pyrDown
(
cv2
.
imread
(
'../data/aloeR.jpg'
)
)
...
@@ -50,10 +53,10 @@ if __name__ == '__main__':
...
@@ -50,10 +53,10 @@ if __name__ == '__main__':
speckleRange
=
32
speckleRange
=
32
)
)
print
'computing disparity...'
print
(
'computing disparity...'
)
disp
=
stereo
.
compute
(
imgL
,
imgR
)
.
astype
(
np
.
float32
)
/
16.0
disp
=
stereo
.
compute
(
imgL
,
imgR
)
.
astype
(
np
.
float32
)
/
16.0
print
'generating 3d point cloud...'
,
print
(
'generating 3d point cloud...'
,)
h
,
w
=
imgL
.
shape
[:
2
]
h
,
w
=
imgL
.
shape
[:
2
]
f
=
0.8
*
w
# guess for focal length
f
=
0.8
*
w
# guess for focal length
Q
=
np
.
float32
([[
1
,
0
,
0
,
-
0.5
*
w
],
Q
=
np
.
float32
([[
1
,
0
,
0
,
-
0.5
*
w
],
...
@@ -67,7 +70,7 @@ if __name__ == '__main__':
...
@@ -67,7 +70,7 @@ if __name__ == '__main__':
out_colors
=
colors
[
mask
]
out_colors
=
colors
[
mask
]
out_fn
=
'out.ply'
out_fn
=
'out.ply'
write_ply
(
'out.ply'
,
out_points
,
out_colors
)
write_ply
(
'out.ply'
,
out_points
,
out_colors
)
print
'
%
s saved'
%
'out.ply'
print
(
'
%
s saved'
%
'out.ply'
)
cv2
.
imshow
(
'left'
,
imgL
)
cv2
.
imshow
(
'left'
,
imgL
)
cv2
.
imshow
(
'disparity'
,
(
disp
-
min_disp
)
/
num_disp
)
cv2
.
imshow
(
'disparity'
,
(
disp
-
min_disp
)
/
num_disp
)
...
...
samples/python2/texture_flow.py
View file @
4c1c0b10
...
@@ -10,6 +10,9 @@ Usage:
...
@@ -10,6 +10,9 @@ Usage:
texture_flow.py [<image>]
texture_flow.py [<image>]
'''
'''
# Python 2/3 compatibility
from
__future__
import
print_function
import
numpy
as
np
import
numpy
as
np
import
cv2
import
cv2
...
@@ -22,7 +25,7 @@ if __name__ == '__main__':
...
@@ -22,7 +25,7 @@ if __name__ == '__main__':
img
=
cv2
.
imread
(
fn
)
img
=
cv2
.
imread
(
fn
)
if
img
is
None
:
if
img
is
None
:
print
'Failed to load image file:'
,
fn
print
(
'Failed to load image file:'
,
fn
)
sys
.
exit
(
1
)
sys
.
exit
(
1
)
gray
=
cv2
.
cvtColor
(
img
,
cv2
.
COLOR_BGR2GRAY
)
gray
=
cv2
.
cvtColor
(
img
,
cv2
.
COLOR_BGR2GRAY
)
...
@@ -36,7 +39,7 @@ if __name__ == '__main__':
...
@@ -36,7 +39,7 @@ if __name__ == '__main__':
vis
[:]
=
(
192
+
np
.
uint32
(
vis
))
/
2
vis
[:]
=
(
192
+
np
.
uint32
(
vis
))
/
2
d
=
12
d
=
12
points
=
np
.
dstack
(
np
.
mgrid
[
d
/
2
:
w
:
d
,
d
/
2
:
h
:
d
]
)
.
reshape
(
-
1
,
2
)
points
=
np
.
dstack
(
np
.
mgrid
[
d
/
2
:
w
:
d
,
d
/
2
:
h
:
d
]
)
.
reshape
(
-
1
,
2
)
for
x
,
y
in
points
:
for
x
,
y
in
np
.
int32
(
points
)
:
vx
,
vy
=
np
.
int32
(
flow
[
y
,
x
]
*
d
)
vx
,
vy
=
np
.
int32
(
flow
[
y
,
x
]
*
d
)
cv2
.
line
(
vis
,
(
x
-
vx
,
y
-
vy
),
(
x
+
vx
,
y
+
vy
),
(
0
,
0
,
0
),
1
,
cv2
.
LINE_AA
)
cv2
.
line
(
vis
,
(
x
-
vx
,
y
-
vy
),
(
x
+
vx
,
y
+
vy
),
(
0
,
0
,
0
),
1
,
cv2
.
LINE_AA
)
cv2
.
imshow
(
'input'
,
img
)
cv2
.
imshow
(
'input'
,
img
)
...
...
samples/python2/video.py
View file @
4c1c0b10
...
@@ -182,7 +182,7 @@ if __name__ == '__main__':
...
@@ -182,7 +182,7 @@ if __name__ == '__main__':
if
len
(
sources
)
==
0
:
if
len
(
sources
)
==
0
:
sources
=
[
0
]
sources
=
[
0
]
caps
=
map
(
create_capture
,
sources
)
caps
=
list
(
map
(
create_capture
,
sources
)
)
shot_idx
=
0
shot_idx
=
0
while
True
:
while
True
:
imgs
=
[]
imgs
=
[]
...
...
samples/python2/video_v4l2.py
View file @
4c1c0b10
...
@@ -14,6 +14,9 @@ Keys:
...
@@ -14,6 +14,9 @@ Keys:
'''
'''
# Python 2/3 compatibility
from
__future__
import
print_function
import
cv2
import
cv2
def
decode_fourcc
(
v
):
def
decode_fourcc
(
v
):
...
@@ -24,13 +27,13 @@ font = cv2.FONT_HERSHEY_SIMPLEX
...
@@ -24,13 +27,13 @@ font = cv2.FONT_HERSHEY_SIMPLEX
color
=
(
0
,
255
,
0
)
color
=
(
0
,
255
,
0
)
cap
=
cv2
.
VideoCapture
(
0
)
cap
=
cv2
.
VideoCapture
(
0
)
cap
.
set
(
cv2
.
CAP_PROP_AUTOFOCUS
,
False
)
cap
.
set
(
cv2
.
CAP_PROP_AUTOFOCUS
,
False
)
# Known bug: https://github.com/Itseez/opencv/pull/5474
cv2
.
namedWindow
(
"Video"
)
cv2
.
namedWindow
(
"Video"
)
convert_rgb
=
True
convert_rgb
=
True
fps
=
int
(
cap
.
get
(
cv2
.
CAP_PROP_FPS
))
fps
=
int
(
cap
.
get
(
cv2
.
CAP_PROP_FPS
))
focus
=
int
(
cap
.
get
(
cv2
.
CAP_PROP_FOCUS
))
*
100
focus
=
int
(
min
(
cap
.
get
(
cv2
.
CAP_PROP_FOCUS
)
*
100
,
2
**
31
-
1
))
# ceil focus to C_LONG as Python3 int can go to +inf
cv2
.
createTrackbar
(
"FPS"
,
"Video"
,
fps
,
30
,
lambda
v
:
cap
.
set
(
cv2
.
CAP_PROP_FPS
,
v
))
cv2
.
createTrackbar
(
"FPS"
,
"Video"
,
fps
,
30
,
lambda
v
:
cap
.
set
(
cv2
.
CAP_PROP_FPS
,
v
))
cv2
.
createTrackbar
(
"Focus"
,
"Video"
,
focus
,
100
,
lambda
v
:
cap
.
set
(
cv2
.
CAP_PROP_FOCUS
,
v
/
100
))
cv2
.
createTrackbar
(
"Focus"
,
"Video"
,
focus
,
100
,
lambda
v
:
cap
.
set
(
cv2
.
CAP_PROP_FOCUS
,
v
/
100
))
...
@@ -55,7 +58,7 @@ while True:
...
@@ -55,7 +58,7 @@ while True:
cv2
.
putText
(
img
,
"FPS: {}"
.
format
(
fps
),
(
15
,
80
),
font
,
1.0
,
color
)
cv2
.
putText
(
img
,
"FPS: {}"
.
format
(
fps
),
(
15
,
80
),
font
,
1.0
,
color
)
cv2
.
imshow
(
"Video"
,
img
)
cv2
.
imshow
(
"Video"
,
img
)
k
=
cv2
.
waitKey
(
1
)
k
=
0xFF
&
cv2
.
waitKey
(
1
)
if
k
==
27
:
if
k
==
27
:
break
break
...
...
samples/python2/watershed.py
View file @
4c1c0b10
...
@@ -22,7 +22,8 @@ Keys
...
@@ -22,7 +22,8 @@ Keys
'''
'''
# Python 2/3 compatibility
from
__future__
import
print_function
import
numpy
as
np
import
numpy
as
np
import
cv2
import
cv2
...
@@ -44,7 +45,7 @@ class App:
...
@@ -44,7 +45,7 @@ class App:
self
.
sketch
=
Sketcher
(
'img'
,
[
self
.
markers_vis
,
self
.
markers
],
self
.
get_colors
)
self
.
sketch
=
Sketcher
(
'img'
,
[
self
.
markers_vis
,
self
.
markers
],
self
.
get_colors
)
def
get_colors
(
self
):
def
get_colors
(
self
):
return
map
(
int
,
self
.
colors
[
self
.
cur_marker
]
),
self
.
cur_marker
return
list
(
map
(
int
,
self
.
colors
[
self
.
cur_marker
])
),
self
.
cur_marker
def
watershed
(
self
):
def
watershed
(
self
):
m
=
self
.
markers
.
copy
()
m
=
self
.
markers
.
copy
()
...
@@ -60,13 +61,13 @@ class App:
...
@@ -60,13 +61,13 @@ class App:
break
break
if
ch
>=
ord
(
'1'
)
and
ch
<=
ord
(
'7'
):
if
ch
>=
ord
(
'1'
)
and
ch
<=
ord
(
'7'
):
self
.
cur_marker
=
ch
-
ord
(
'0'
)
self
.
cur_marker
=
ch
-
ord
(
'0'
)
print
'marker: '
,
self
.
cur_marker
print
(
'marker: '
,
self
.
cur_marker
)
if
ch
==
ord
(
' '
)
or
(
self
.
sketch
.
dirty
and
self
.
auto_update
):
if
ch
==
ord
(
' '
)
or
(
self
.
sketch
.
dirty
and
self
.
auto_update
):
self
.
watershed
()
self
.
watershed
()
self
.
sketch
.
dirty
=
False
self
.
sketch
.
dirty
=
False
if
ch
in
[
ord
(
'a'
),
ord
(
'A'
)]:
if
ch
in
[
ord
(
'a'
),
ord
(
'A'
)]:
self
.
auto_update
=
not
self
.
auto_update
self
.
auto_update
=
not
self
.
auto_update
print
'auto_update if'
,
[
'off'
,
'on'
][
self
.
auto_update
]
print
(
'auto_update if'
,
[
'off'
,
'on'
][
self
.
auto_update
])
if
ch
in
[
ord
(
'r'
),
ord
(
'R'
)]:
if
ch
in
[
ord
(
'r'
),
ord
(
'R'
)]:
self
.
markers
[:]
=
0
self
.
markers
[:]
=
0
self
.
markers_vis
[:]
=
self
.
img
self
.
markers_vis
[:]
=
self
.
img
...
@@ -80,5 +81,5 @@ if __name__ == '__main__':
...
@@ -80,5 +81,5 @@ if __name__ == '__main__':
fn
=
sys
.
argv
[
1
]
fn
=
sys
.
argv
[
1
]
except
:
except
:
fn
=
'../data/fruits.jpg'
fn
=
'../data/fruits.jpg'
print
__doc__
print
(
__doc__
)
App
(
fn
)
.
run
()
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