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
ae5dd1d7
Commit
ae5dd1d7
authored
Jul 16, 2011
by
Alexander Mordvintsev
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
math.pi -> np.pi
squares.py sample added
parent
570041fe
Hide whitespace changes
Inline
Side-by-side
Showing
5 changed files
with
46 additions
and
11 deletions
+46
-11
gaussian_mix.py
samples/python2/gaussian_mix.py
+1
-2
lk_track.py
samples/python2/lk_track.py
+2
-5
opt_flow.py
samples/python2/opt_flow.py
+2
-3
squares.py
samples/python2/squares.py
+40
-0
watershed.py
samples/python2/watershed.py
+1
-1
No files found.
samples/python2/gaussian_mix.py
View file @
ae5dd1d7
import
numpy
as
np
import
math
from
numpy
import
random
import
cv2
...
...
@@ -20,7 +19,7 @@ def make_gaussians(cluster_n, img_size):
def
draw_gaussain
(
img
,
mean
,
cov
,
color
):
x
,
y
=
np
.
int32
(
mean
)
w
,
u
,
vt
=
cv2
.
SVDecomp
(
cov
)
ang
=
np
.
arctan2
(
u
[
1
,
0
],
u
[
0
,
0
])
*
(
180
/
math
.
pi
)
ang
=
np
.
arctan2
(
u
[
1
,
0
],
u
[
0
,
0
])
*
(
180
/
np
.
pi
)
s1
,
s2
=
np
.
sqrt
(
w
)
*
3.0
cv2
.
ellipse
(
img
,
(
x
,
y
),
(
s1
,
s2
),
ang
,
0
,
360
,
color
,
1
,
cv2
.
CV_AA
)
...
...
samples/python2/lk_track.py
View file @
ae5dd1d7
...
...
@@ -13,9 +13,6 @@ Keys:
SPACE - reset features
'''
lk_params
=
dict
(
winSize
=
(
21
,
21
),
maxLevel
=
2
,
criteria
=
(
cv2
.
TERM_CRITERIA_EPS
|
cv2
.
TERM_CRITERIA_COUNT
,
10
,
0.03
),
...
...
@@ -35,7 +32,7 @@ def calc_flow_old(img0, img1, p0):
np
.
asarray
(
img1_cv
)[:]
=
img1
t
=
clock
()
features
,
status
,
error
=
cv
.
CalcOpticalFlowPyrLK
(
img0_cv
,
img1_cv
,
None
,
None
,
p0
,
lk_params
[
'winSize'
],
lk_params
[
'maxLevel'
],
(
cv
.
CV_TERMCRIT_EPS
|
cv
.
CV_TERMCRIT_ITER
,
10
,
0.03
)
,
0
,
p0
)
lk_params
[
'winSize'
],
lk_params
[
'maxLevel'
],
lk_params
[
'criteria'
]
,
0
,
p0
)
return
np
.
float32
(
features
),
status
,
error
,
clock
()
-
t
def
main
():
...
...
@@ -60,7 +57,7 @@ def main():
p1
,
st
,
err
,
dt
=
calc_flow_old
(
img0
,
img1
,
p0
)
else
:
t
=
clock
()
p1
,
st
,
err
=
cv2
.
calcOpticalFlowPyrLK
(
img0
,
img1
,
p0
,
**
lk_params
)
p1
,
st
,
err
=
cv2
.
calcOpticalFlowPyrLK
(
img0
,
img1
,
p0
,
None
,
**
lk_params
)
dt
=
clock
()
-
t
for
tr
,
(
x
,
y
)
in
zip
(
tracks
,
p1
.
reshape
(
-
1
,
2
)):
tr
.
append
((
x
,
y
))
...
...
samples/python2/opt_flow.py
View file @
ae5dd1d7
import
numpy
as
np
import
math
import
cv2
import
cv2.cv
as
cv
import
video
...
...
@@ -31,7 +30,7 @@ def draw_hsv(flow):
ang
=
np
.
arctan2
(
fy
,
fx
)
+
np
.
pi
v
=
np
.
sqrt
(
fx
*
fx
+
fy
*
fy
)
hsv
=
np
.
zeros
((
h
,
w
,
3
),
np
.
uint8
)
hsv
[
...
,
0
]
=
ang
*
(
180
/
math
.
pi
/
2
)
hsv
[
...
,
0
]
=
ang
*
(
180
/
np
.
pi
/
2
)
hsv
[
...
,
1
]
=
255
hsv
[
...
,
2
]
=
np
.
minimum
(
v
*
4
,
255
)
bgr
=
cv2
.
cvtColor
(
hsv
,
cv
.
CV_HSV2BGR
)
...
...
@@ -61,7 +60,7 @@ if __name__ == '__main__':
while
True
:
ret
,
img
=
cam
.
read
()
gray
=
cv2
.
cvtColor
(
img
,
cv
.
CV_BGR2GRAY
)
flow
=
cv2
.
calcOpticalFlowFarneback
(
prevgray
,
gray
,
0.5
,
3
,
15
,
3
,
5
,
1.2
,
0
)
flow
=
cv2
.
calcOpticalFlowFarneback
(
prevgray
,
gray
,
None
,
0.5
,
3
,
15
,
3
,
5
,
1.2
,
0
)
prevgray
=
gray
cv2
.
imshow
(
'flow'
,
draw_flow
(
gray
,
flow
))
...
...
samples/python2/squares.py
0 → 100644
View file @
ae5dd1d7
import
numpy
as
np
import
cv2
def
angle_cos
(
p0
,
p1
,
p2
):
d1
,
d2
=
p0
-
p1
,
p2
-
p1
return
abs
(
np
.
dot
(
d1
,
d2
)
/
np
.
sqrt
(
np
.
dot
(
d1
,
d1
)
*
np
.
dot
(
d2
,
d2
)
)
)
def
find_squares
(
img
):
img
=
cv2
.
GaussianBlur
(
img
,
(
5
,
5
),
0
)
squares
=
[]
for
gray
in
cv2
.
split
(
img
):
for
thrs
in
xrange
(
0
,
255
,
26
):
if
thrs
==
0
:
bin
=
cv2
.
Canny
(
gray
,
0
,
50
,
apertureSize
=
5
)
bin
=
cv2
.
dilate
(
bin
,
None
)
else
:
retval
,
bin
=
cv2
.
threshold
(
gray
,
thrs
,
255
,
cv2
.
THRESH_BINARY
)
contours
,
hierarchy
=
cv2
.
findContours
(
bin
,
cv2
.
RETR_LIST
,
cv2
.
CHAIN_APPROX_SIMPLE
)
for
cnt
in
contours
:
cnt_len
=
cv2
.
arcLength
(
cnt
,
True
)
cnt
=
cv2
.
approxPolyDP
(
cnt
,
0.02
*
cnt_len
,
True
)
if
len
(
cnt
)
==
4
and
cv2
.
contourArea
(
cnt
)
>
1000
and
cv2
.
isContourConvex
(
cnt
):
cnt
=
cnt
.
reshape
(
-
1
,
2
)
max_cos
=
np
.
max
([
angle_cos
(
cnt
[
i
],
cnt
[(
i
+
1
)
%
4
],
cnt
[(
i
+
2
)
%
4
]
)
for
i
in
xrange
(
4
)])
if
max_cos
<
0.3
:
squares
.
append
(
cnt
)
return
squares
if
__name__
==
'__main__'
:
from
glob
import
glob
for
fn
in
glob
(
'../cpp/pic*.png'
):
img
=
cv2
.
imread
(
fn
)
squares
=
find_squares
(
img
)
cv2
.
drawContours
(
img
,
squares
,
-
1
,
(
0
,
255
,
0
),
3
)
cv2
.
imshow
(
'squares'
,
img
)
ch
=
cv2
.
waitKey
()
if
ch
==
27
:
break
\ No newline at end of file
samples/python2/watershed.py
View file @
ae5dd1d7
...
...
@@ -52,7 +52,7 @@ class App:
print
'auto_update if'
,
[
'off'
,
'on'
][
self
.
auto_update
]
if
ch
in
[
ord
(
'r'
),
ord
(
'R'
)]:
self
.
markers
[:]
=
0
self
.
markers_vis
[:]
=
self
.
img
.
copy
()
self
.
markers_vis
[:]
=
self
.
img
self
.
sketch
.
show
()
...
...
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