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
58174f6a
Commit
58174f6a
authored
Mar 07, 2012
by
Alexander Mordvintsev
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Added GIL-release code into python wrappers
Added gabor_threads.py sample
parent
e7dda44a
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
79 additions
and
0 deletions
+79
-0
gen2.py
modules/python/src2/gen2.py
+2
-0
common.py
samples/python2/common.py
+10
-0
gabor_threads.py
samples/python2/gabor_threads.py
+67
-0
No files found.
modules/python/src2/gen2.py
View file @
58174f6a
...
@@ -19,7 +19,9 @@ gen_template_parse_args = Template("""const char* keywords[] = { $kw_list, NULL
...
@@ -19,7 +19,9 @@ gen_template_parse_args = Template("""const char* keywords[] = { $kw_list, NULL
gen_template_func_body
=
Template
(
"""$code_decl
gen_template_func_body
=
Template
(
"""$code_decl
$code_parse
$code_parse
{
{
Py_BEGIN_ALLOW_THREADS
$code_fcall;
$code_fcall;
Py_END_ALLOW_THREADS
$code_ret;
$code_ret;
}
}
"""
)
"""
)
...
...
samples/python2/common.py
View file @
58174f6a
import
numpy
as
np
import
numpy
as
np
import
cv2
import
cv2
import
os
import
os
from
contextlib
import
contextmanager
image_extensions
=
[
'.bmp'
,
'.jpg'
,
'.jpeg'
,
'.png'
,
'.tif'
,
'.tiff'
,
'.pbm'
,
'.pgm'
,
'.ppm'
]
image_extensions
=
[
'.bmp'
,
'.jpg'
,
'.jpeg'
,
'.png'
,
'.tif'
,
'.tiff'
,
'.pbm'
,
'.pgm'
,
'.ppm'
]
...
@@ -116,6 +117,15 @@ def nothing(*arg, **kw):
...
@@ -116,6 +117,15 @@ def nothing(*arg, **kw):
def
clock
():
def
clock
():
return
cv2
.
getTickCount
()
/
cv2
.
getTickFrequency
()
return
cv2
.
getTickCount
()
/
cv2
.
getTickFrequency
()
@contextmanager
def
Timer
(
msg
):
print
msg
,
'...'
,
start
=
clock
()
try
:
yield
finally
:
print
"
%.2
f ms"
%
((
clock
()
-
start
)
*
1000
)
class
RectSelector
:
class
RectSelector
:
def
__init__
(
self
,
win
,
callback
):
def
__init__
(
self
,
win
,
callback
):
self
.
win
=
win
self
.
win
=
win
...
...
samples/python2/gabor_threads.py
0 → 100644
View file @
58174f6a
'''
gabor_threads.py
=========
Sample demonstrates:
- use of multiple Gabor filter convolutions to get Fractalius-like image effect (http://www.redfieldplugins.com/filterFractalius.htm)
- use of python threading to accelerate the computation
Usage
-----
gabor_threads.py [image filename]
'''
import
numpy
as
np
import
cv2
from
threading
import
Lock
from
multiprocessing.pool
import
ThreadPool
def
build_filters
():
filters
=
[]
ksize
=
31
for
theta
in
np
.
arange
(
0
,
np
.
pi
,
np
.
pi
/
16
):
kern
=
cv2
.
getGaborKernel
((
ksize
,
ksize
),
4.0
,
theta
,
10.0
,
0.5
,
0
,
ktype
=
cv2
.
CV_32F
)
kern
/=
1.5
*
kern
.
sum
()
filters
.
append
(
kern
)
return
filters
def
process
(
img
,
filters
):
accum
=
np
.
zeros_like
(
img
)
for
kern
in
filters
:
fimg
=
cv2
.
filter2D
(
img
,
cv2
.
CV_8UC3
,
kern
)
np
.
maximum
(
accum
,
fimg
,
accum
)
return
accum
def
process_threaded
(
img
,
filters
,
threadn
=
8
):
accum
=
np
.
zeros_like
(
img
)
accum_lock
=
Lock
()
def
f
(
kern
):
fimg
=
cv2
.
filter2D
(
img
,
cv2
.
CV_8UC3
,
kern
)
with
accum_lock
:
np
.
maximum
(
accum
,
fimg
,
accum
)
pool
=
ThreadPool
(
processes
=
threadn
)
pool
.
map
(
f
,
filters
)
return
accum
if
__name__
==
'__main__'
:
import
sys
from
common
import
Timer
print
__doc__
try
:
img_fn
=
sys
.
argv
[
1
]
except
:
img_fn
=
'../cpp/baboon.jpg'
img
=
cv2
.
imread
(
img_fn
)
filters
=
build_filters
()
with
Timer
(
'running single-threaded'
):
res1
=
process
(
img
,
filters
)
with
Timer
(
'running multi-threaded'
):
res2
=
process_threaded
(
img
,
filters
)
print
'res1 == res2: '
,
(
res1
==
res2
)
.
all
()
cv2
.
imshow
(
'img'
,
img
)
cv2
.
imshow
(
'result'
,
res2
)
cv2
.
waitKey
()
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