mirror of https://github.com/opencv/opencv.git
Open Source Computer Vision Library
https://opencv.org/
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
77 lines
1.8 KiB
77 lines
1.8 KiB
12 years ago
|
#!/usr/bin/env python
|
||
12 years ago
|
|
||
12 years ago
|
'''
|
||
|
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]
|
||
|
|
||
|
'''
|
||
|
|
||
9 years ago
|
# Python 2/3 compatibility
|
||
|
from __future__ import print_function
|
||
|
|
||
12 years ago
|
import numpy as np
|
||
|
import cv2
|
||
|
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)
|
||
|
def f(kern):
|
||
|
return cv2.filter2D(img, cv2.CV_8UC3, kern)
|
||
|
pool = ThreadPool(processes=threadn)
|
||
|
for fimg in pool.imap_unordered(f, filters):
|
||
|
np.maximum(accum, fimg, accum)
|
||
|
return accum
|
||
|
|
||
|
if __name__ == '__main__':
|
||
|
import sys
|
||
|
from common import Timer
|
||
|
|
||
9 years ago
|
print(__doc__)
|
||
12 years ago
|
try:
|
||
|
img_fn = sys.argv[1]
|
||
11 years ago
|
except:
|
||
10 years ago
|
img_fn = '../data/baboon.jpg'
|
||
12 years ago
|
|
||
|
img = cv2.imread(img_fn)
|
||
12 years ago
|
if img is None:
|
||
9 years ago
|
print('Failed to load image file:', img_fn)
|
||
12 years ago
|
sys.exit(1)
|
||
11 years ago
|
|
||
12 years ago
|
filters = build_filters()
|
||
|
|
||
|
with Timer('running single-threaded'):
|
||
|
res1 = process(img, filters)
|
||
|
with Timer('running multi-threaded'):
|
||
|
res2 = process_threaded(img, filters)
|
||
|
|
||
9 years ago
|
print('res1 == res2: ', (res1 == res2).all())
|
||
12 years ago
|
cv2.imshow('img', img)
|
||
|
cv2.imshow('result', res2)
|
||
|
cv2.waitKey()
|
||
|
cv2.destroyAllWindows()
|