2013-03-06 14:41:02 +08:00
|
|
|
#!/usr/bin/env python
|
2012-11-24 02:57:22 +08:00
|
|
|
|
2012-10-17 07:18:30 +08:00
|
|
|
'''
|
|
|
|
Multiscale Turing Patterns generator
|
|
|
|
====================================
|
|
|
|
|
|
|
|
Inspired by http://www.jonathanmccabe.com/Cyclic_Symmetric_Multi-Scale_Turing_Patterns.pdf
|
|
|
|
'''
|
|
|
|
|
2015-09-14 00:00:22 +08:00
|
|
|
# Python 2/3 compatibility
|
|
|
|
from __future__ import print_function
|
|
|
|
import sys
|
|
|
|
PY3 = sys.version_info[0] == 3
|
|
|
|
|
|
|
|
if PY3:
|
|
|
|
xrange = range
|
|
|
|
|
2012-10-17 07:18:30 +08:00
|
|
|
import numpy as np
|
2017-12-11 17:55:03 +08:00
|
|
|
import cv2 as cv
|
2012-10-17 07:18:30 +08:00
|
|
|
from common import draw_str
|
|
|
|
import getopt, sys
|
|
|
|
from itertools import count
|
|
|
|
|
|
|
|
help_message = '''
|
|
|
|
USAGE: turing.py [-o <output.avi>]
|
|
|
|
|
|
|
|
Press ESC to stop.
|
|
|
|
'''
|
|
|
|
|
2019-03-20 02:03:58 +08:00
|
|
|
def main():
|
2015-09-14 00:00:22 +08:00
|
|
|
print(help_message)
|
2012-10-17 07:18:30 +08:00
|
|
|
|
|
|
|
w, h = 512, 512
|
|
|
|
|
|
|
|
args, args_list = getopt.getopt(sys.argv[1:], 'o:', [])
|
|
|
|
args = dict(args)
|
|
|
|
out = None
|
|
|
|
if '-o' in args:
|
|
|
|
fn = args['-o']
|
2017-12-11 17:55:03 +08:00
|
|
|
out = cv.VideoWriter(args['-o'], cv.VideoWriter_fourcc(*'DIB '), 30.0, (w, h), False)
|
2015-09-14 00:00:22 +08:00
|
|
|
print('writing %s ...' % fn)
|
2012-10-17 07:18:30 +08:00
|
|
|
|
|
|
|
a = np.zeros((h, w), np.float32)
|
2017-12-11 17:55:03 +08:00
|
|
|
cv.randu(a, np.array([0]), np.array([1]))
|
2012-10-17 07:18:30 +08:00
|
|
|
|
|
|
|
def process_scale(a_lods, lod):
|
2017-12-11 17:55:03 +08:00
|
|
|
d = a_lods[lod] - cv.pyrUp(a_lods[lod+1])
|
2017-08-25 00:45:14 +08:00
|
|
|
for _i in xrange(lod):
|
2017-12-11 17:55:03 +08:00
|
|
|
d = cv.pyrUp(d)
|
|
|
|
v = cv.GaussianBlur(d*d, (3, 3), 0)
|
2012-10-17 07:18:30 +08:00
|
|
|
return np.sign(d), v
|
|
|
|
|
|
|
|
scale_num = 6
|
|
|
|
for frame_i in count():
|
|
|
|
a_lods = [a]
|
|
|
|
for i in xrange(scale_num):
|
2017-12-11 17:55:03 +08:00
|
|
|
a_lods.append(cv.pyrDown(a_lods[-1]))
|
2012-10-17 07:18:30 +08:00
|
|
|
ms, vs = [], []
|
|
|
|
for i in xrange(1, scale_num):
|
|
|
|
m, v = process_scale(a_lods, i)
|
|
|
|
ms.append(m)
|
|
|
|
vs.append(v)
|
|
|
|
mi = np.argmin(vs, 0)
|
|
|
|
a += np.choose(mi, ms) * 0.025
|
|
|
|
a = (a-a.min()) / a.ptp()
|
|
|
|
|
|
|
|
if out:
|
|
|
|
out.write(a)
|
|
|
|
vis = a.copy()
|
|
|
|
draw_str(vis, (20, 20), 'frame %d' % frame_i)
|
2017-12-11 17:55:03 +08:00
|
|
|
cv.imshow('a', vis)
|
|
|
|
if cv.waitKey(5) == 27:
|
2012-10-17 07:18:30 +08:00
|
|
|
break
|
2019-03-20 02:03:58 +08:00
|
|
|
|
|
|
|
print('Done')
|
|
|
|
|
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
|
print(__doc__)
|
|
|
|
main()
|
2017-12-11 17:55:03 +08:00
|
|
|
cv.destroyAllWindows()
|