-
Notifications
You must be signed in to change notification settings - Fork 0
/
cv2_grayscott.py
185 lines (160 loc) · 5.95 KB
/
cv2_grayscott.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
# coding: utf-8
# Simulation of the gray scott reaction diffusion system.
# /
# | ∂ₜu(x,t) = Dᵤ ∇²u(x,t) - u(x,t) v²(x,t) + F(1- u(x,t))
# | ∂ₜv(x,t) = Dᵥ ∇²v(x,t) + u(x,t) v²(x,t) - (F + k) v(x,t)
# \
import cv2
import sys
import random
import numpy as np
import time
import scipy
import grayscott
# import libgrayscott
if(len(sys.argv) <= 1):
print("Usage : %s mode " % sys.argv[0])
print("With mode : ")
print(" 0 : spatial model with FFT convolution in python, forward euler") # 100 fps
print(" 1 : spatial model with ndimage.convolve in python, forward euler") # 165 fps
print(" 2 : spatial model with ndimage.laplace in python, forward euler") # 150 fps
print(" 3 : spatial model with fast laplacian in C++, forward euler") # 400 fps
print(" 4 : spectral model in python using ETDRK4")
sys.exit(-1)
print("\n Important : the windows is not always correctly resized,\
you might need to resize it manually \
so that the values are fully displayed\n")
print(" Press : ")
print(" s : start/pause")
print(" i : reinitialize the concentrations")
print(" q : quit")
print(" c : erase the reactant v in a randomly chosen box patch")
print(" m : mask the reactant with a randomly generated mask")
print(" p : save the current u potential")
print(" f : toggle fullscreen/normal screen")
try:
fullscreen_flag = cv2.WINDOW_FULLSCREEN
normal_flag = cv2.WINDOW_NORMAL
except:
fullscreen_flag = cv2.cv.CV_WINDOW_FULLSCREEN
normal_flag = cv2.cv.CV_WINDOW_NORMAL
cv2.namedWindow('u', cv2.WND_PROP_FULLSCREEN)
cv2.setWindowProperty("u", cv2.WND_PROP_FULLSCREEN, normal_flag)
key = 0
run = False
mode = int(sys.argv[1])
#
if(mode <= 3):
d = 1.5 # The width of the domain
height = 128 # The size of the lattice
width = 256
dt = 0.1 # the time step
else:
d = 3.
height = 100
width = 200
dt = 10
display_scaling_factor = 4
pattern = 'worms_solitons'
if(mode <= 2):
model = grayscott.Model(pattern,
width=width,
height=height,
mode=mode,
d=d, dt=dt)
elif mode == 3:
model = None # libgrayscott.GrayScott(pattern, width, height, d, dt)
else:
model = grayscott.SpectralModel(pattern,
width=width,
height=height,
d=d, dt=dt,
mode='ETDFD')
model = grayscott.ThreadedModel(model)
model.init()
model.start()
# Precompute the FFT of the kernel for speeding up the convolution
# For lightning
s_kernel = 11
kernel_light = np.ones((s_kernel, s_kernel), dtype=np.float)
kernel_light[:int(2./3 * s_kernel), :int(2./3 * s_kernel)] = -1
mask_light = np.zeros((height, width), np.float32)
mask_light[0:kernel_light.shape[0], 0:kernel_light.shape[1]] = kernel_light
mask_light = np.roll(np.roll(mask_light,
-(kernel_light.shape[1]//2-1), axis=1),
-(kernel_light.shape[0]//2-1), axis=0)
fft_mask_light = np.fft.rfft2(mask_light)
# For the blur, it is fastest to use uniform_filter
def make_effect(u_orig, scale):
res_height, res_width = scale * u_orig.shape[0], scale * u_orig.shape[1]
s_kernel = 11
kernel = np.ones((s_kernel, s_kernel), dtype=np.float)
# Light coming from top left
kernel[:int(2./3 * s_kernel), :int(2./3 * s_kernel)] = -1
# Light coming from left
# kernel[:int(2./3 * s_kernel), :] = -1
effect = scipy.signal.convolve2d(2. * (u_orig - 0.5), kernel, mode='same')
# HAND TUNED SCALING of the effect ...
# might need to be adapted if changing s_kernel
effect /= 30.
effect[effect >= 1.0] = 1.0
effect[effect <= 0.0] = 0.0
effect_hires = cv2.resize(effect,
(res_width, res_height),
interpolation=cv2.INTER_CUBIC)
u_hires = cv2.resize(u_orig,
(res_width, res_height),
interpolation=cv2.INTER_CUBIC)
u_hires[u_hires >= 0.5] = 1.
u_hires[u_hires < 0.5] = 0.
# Blur the image to get the shading
u_blur = scipy.ndimage.filters.uniform_filter(u_hires, size=5)
# Shift the shadding down right
u_blur = np.lib.pad(u_blur,
((2, 0), (2, 0)),
'constant',
constant_values=1)[:-2, :-2]
dst = 0.6 * u_hires + 0.4 * effect_hires
dst[u_hires >= 0.99] = u_blur[u_hires >= 0.99]
return dst
u = np.zeros((height, width))
epoch = 0
t0 = time.time()
frame_id = 0
while key != ord('q'):
# if(run):
# model.step()
# epoch += 1
# if(epoch % 100 == 0):
# t1 = time.time()
# print("FPS: %f fps" % (100 / (t1 - t0)))
# t0 = t1
u_img = make_effect(model.get_ut(), display_scaling_factor)
cv2.imshow('u', u_img)
key = cv2.waitKey(1) & 0xFF
if(key == ord('c')):
c = (random.randint(0, height-1), random.randint(0, width-1))
model.erase_reactant(c, height/8)
elif(key == ord('m')):
mask = 0.75 + 0.25*np.random.random((height, width))
model.mask_reactant(mask)
elif key == ord('s'):
model.trigger_pause()
print("Running ? : " + str(not model.is_paused()))
elif key == ord('i'):
model.init()
elif key == ord('p'):
print("Saving u-%05d.png" % frame_id)
cv2.imwrite("u-%05d.png" % frame_id,
(np.minimum(255*u_img, 255)).astype(np.uint8))
frame_id += 1
elif key == ord('f'):
screenmode = cv2.getWindowProperty("u", cv2.WND_PROP_FULLSCREEN)
if(screenmode == normal_flag):
cv2.setWindowProperty("u",
cv2.WND_PROP_FULLSCREEN,
fullscreen_flag)
else:
cv2.setWindowProperty("u", cv2.WND_PROP_FULLSCREEN, normal_flag)
model.stop()
model.join()