--- /dev/null
+'''\r
+Multiscale Turing Patterns generator.\r
+Inspired by http://www.jonathanmccabe.com/Cyclic_Symmetric_Multi-Scale_Turing_Patterns.pdf\r
+'''\r
+\r
+import numpy as np\r
+import cv2, cv\r
+from common import draw_str\r
+\r
+w, h = 512, 512\r
+\r
+a = np.zeros((h, w, 1), np.float32)\r
+cv2.randu(a, np.array([0]), np.array([1]))\r
+a.shape = (h, w)\r
+\r
+def process_scale(a_lods, lod):\r
+ d = a_lods[lod] - cv2.pyrUp(a_lods[lod+1])\r
+ for i in xrange(lod):\r
+ d = cv2.pyrUp(d)\r
+ v = cv2.GaussianBlur(d*d, (3, 3), 0)\r
+ return np.sign(d), v\r
+ \r
+print 'Generating AVI file. Press ESC to stop.'\r
+out = cv2.VideoWriter('turing.avi', cv.CV_FOURCC(*'DIB '), 30.0, (w, h), False)\r
+\r
+scale_num = 6\r
+frame_num = 1000\r
+for frame_i in xrange(frame_num):\r
+ a_lods = [a]\r
+ for i in xrange(scale_num):\r
+ a_lods.append(cv2.pyrDown(a_lods[-1])) \r
+ ms, vs = [], []\r
+ for i in xrange(1, scale_num):\r
+ m, v = process_scale(a_lods, i)\r
+ ms.append(m)\r
+ vs.append(v)\r
+ mi = np.argmin(vs, 0)\r
+ a += np.choose(mi, ms) * 0.025\r
+ a = (a-a.min()) / a.ptp()\r
+\r
+ out.write(a)\r
+ vis = a.copy()\r
+ draw_str(vis, (20, 20), 'frame %d / %d' % (frame_i+1, frame_num))\r
+ cv2.imshow('a', vis)\r
+ if cv2.waitKey(5) == 27:\r
+ break\r
+else:\r
+ print 'done'\r
+ cv2.waitKey()
\ No newline at end of file