turing.py 1.77 KB
Newer Older
1
#!/usr/bin/env python
2

3 4 5 6 7 8 9
'''
Multiscale Turing Patterns generator
====================================

Inspired by http://www.jonathanmccabe.com/Cyclic_Symmetric_Multi-Scale_Turing_Patterns.pdf
'''

10 11 12 13 14 15 16 17
# Python 2/3 compatibility
from __future__ import print_function
import sys
PY3 = sys.version_info[0] == 3

if PY3:
    xrange = range

18
import numpy as np
19
import cv2 as cv
20 21 22 23 24 25 26 27 28 29 30
from common import draw_str
import getopt, sys
from itertools import count

help_message = '''
USAGE: turing.py [-o <output.avi>]

Press ESC to stop.
'''

if __name__ == '__main__':
31
    print(help_message)
32 33 34 35 36 37 38 39

    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']
40
        out = cv.VideoWriter(args['-o'], cv.VideoWriter_fourcc(*'DIB '), 30.0, (w, h), False)
41
        print('writing %s ...' % fn)
42 43

    a = np.zeros((h, w), np.float32)
44
    cv.randu(a, np.array([0]), np.array([1]))
45 46

    def process_scale(a_lods, lod):
47
        d = a_lods[lod] - cv.pyrUp(a_lods[lod+1])
tribta's avatar
tribta committed
48
        for _i in xrange(lod):
49 50
            d = cv.pyrUp(d)
        v = cv.GaussianBlur(d*d, (3, 3), 0)
51 52 53 54 55 56
        return np.sign(d), v

    scale_num = 6
    for frame_i in count():
        a_lods = [a]
        for i in xrange(scale_num):
57
            a_lods.append(cv.pyrDown(a_lods[-1]))
58 59 60 61 62 63 64 65 66 67 68 69 70
        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)
71 72
        cv.imshow('a', vis)
        if cv.waitKey(5) == 27:
73
            break
74
    cv.destroyAllWindows()