Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
Tetragramm
GitHub Repository: Tetragramm/opencv
Path: blob/master/samples/python/gabor_threads.py
16337 views
1
#!/usr/bin/env python
2
3
'''
4
gabor_threads.py
5
=========
6
7
Sample demonstrates:
8
- use of multiple Gabor filter convolutions to get Fractalius-like image effect (http://www.redfieldplugins.com/filterFractalius.htm)
9
- use of python threading to accelerate the computation
10
11
Usage
12
-----
13
gabor_threads.py [image filename]
14
15
'''
16
17
# Python 2/3 compatibility
18
from __future__ import print_function
19
20
import numpy as np
21
import cv2 as cv
22
from multiprocessing.pool import ThreadPool
23
24
25
def build_filters():
26
filters = []
27
ksize = 31
28
for theta in np.arange(0, np.pi, np.pi / 16):
29
kern = cv.getGaborKernel((ksize, ksize), 4.0, theta, 10.0, 0.5, 0, ktype=cv.CV_32F)
30
kern /= 1.5*kern.sum()
31
filters.append(kern)
32
return filters
33
34
def process(img, filters):
35
accum = np.zeros_like(img)
36
for kern in filters:
37
fimg = cv.filter2D(img, cv.CV_8UC3, kern)
38
np.maximum(accum, fimg, accum)
39
return accum
40
41
def process_threaded(img, filters, threadn = 8):
42
accum = np.zeros_like(img)
43
def f(kern):
44
return cv.filter2D(img, cv.CV_8UC3, kern)
45
pool = ThreadPool(processes=threadn)
46
for fimg in pool.imap_unordered(f, filters):
47
np.maximum(accum, fimg, accum)
48
return accum
49
50
if __name__ == '__main__':
51
import sys
52
from common import Timer
53
54
print(__doc__)
55
try:
56
img_fn = sys.argv[1]
57
except:
58
img_fn = '../data/baboon.jpg'
59
60
img = cv.imread(img_fn)
61
if img is None:
62
print('Failed to load image file:', img_fn)
63
sys.exit(1)
64
65
filters = build_filters()
66
67
with Timer('running single-threaded'):
68
res1 = process(img, filters)
69
with Timer('running multi-threaded'):
70
res2 = process_threaded(img, filters)
71
72
print('res1 == res2: ', (res1 == res2).all())
73
cv.imshow('img', img)
74
cv.imshow('result', res2)
75
cv.waitKey()
76
cv.destroyAllWindows()
77
78