from __future__ import print_function
import sys
import numpy as np
from scipy import signal
import cv2
mask = np.ones((3, 3), dtype=int)
def init_state(width, height, init_alive_prob=0.5):
N = width*height
v = np.array(np.random.rand(N) + init_alive_prob, dtype=int)
return v.reshape(height, width)
def count_neighbor(F):
return signal.correlate2d(F, mask, mode="same", boundary="wrap")
def next_generation(F):
N = count_neighbor(F)
G = (N == 3) + F * (N == 4)
return G
def to_image(F, scale=3.0):
img = np.array(F, dtype=np.uint8)*255
W = int(F.shape[1]*scale)
H = int(F.shape[0]*scale)
img = cv2.resize(img, (W, H), interpolation=cv2.INTER_NEAREST)
return img
def main():
p = 0.08
F = init_state(300, 300, init_alive_prob=p)
ret = 0
wait = 1
while True:
img = to_image(F, scale=5.0/3)
cv2.imshow("test", img)
ret = cv2.waitKey(wait)
F = next_generation(F)
if ret == ord('r'):
F = init_state(300, 300, init_alive_prob=p)
if ret == ord('q'):
break
cv2.destroyAllWindows()
if __name__ == "__main__":
main()