【发布时间】:2020-11-08 22:19:24
【问题描述】:
我在使用我的自动机 - Game of Life 时遇到问题。好吧,我有一个图像,我将其转换为 2d 矩阵。在这张图片的中间有一个振荡器。正如规则所说,这个函数的输出应该是另一个振荡器,但旋转了 90 度。不幸的是我的输出是错误的。您可以在下面的图片中看到。代码有什么问题?你能帮我解决这个问题吗?
def sim():
tab = cv2.imread(jpg_path, 0)
tab_x = tab.shape[0]
tab_y = tab.shape[1]
new_tab = np.zeros([tab_x, tab_y])
for x in range(0, tab_x):
for y in range(0, tab_y):
if tab[x, y] == 255:
tab[x, y] = 0
else:
tab[x, y] = 1
#new_tab = tab.copy()
for x in range(tab_x):
for y in range(tab_y):
summary = (tab[x, (y-1) % tab_x] + tab[x, (y+1) % tab_x] +
tab[(x-1) % tab_x, y] + tab[(x+1) % tab_x, y] +
tab[(x-1) % tab_x, (y-1) % tab_x] + tab[(x-1) % tab_x, (y+1) % tab_x] +
tab[(x+1) % tab_x, (y-1) % tab_x] + tab[(x+1) % tab_x, (y+1) % tab_x])
if tab[x, y] == 1:
if (summary < 2) or (summary > 3):
new_tab[x, y] = 0
else:
if summary == 3:
new_tab[x, y] = 1
for y in range(0, tab_y):
for x in range(0, tab_x):
if new_tab[y, x] == 1:
new_tab[y, x] = 0
else:
new_tab[y, x] = 255
cv2.imwrite('anMD.jpg', new_tab)
编辑:我正在添加更多功能,也许有问题。现在我的代码如下所示:
def osc(tab, x, y):
tab[x-1,y-1] = 0
tab[x-1,y] = 1
tab[x-1,y+1] = 0
tab[x,y-1] = 0
tab[x,y] = 1
tab[x,y+1] = 0
tab[x+1,y-1] = 0
tab[x+1,y] = 1
tab[x+1,y+1] = 0
return tab
def gol():
tab_x = 50
tab_y = 50
x = 25
y = 25
tab = np.zeros([tab_x, tab_y])
new_tab = osc(tab, x, y)
for y in range(0, tab_y):
for x in range(0, tab_x):
if new_tab[y, x] == 1:
new_tab[y, x] = 0
else:
new_tab[y, x] = 255
cv2.imwrite('anMD.jpg', new_tab)
canvas.delete("all")
cv2.imwrite('show.jpg', new_tab)
image = Image.open('show.jpg')
new_image = image.resize((500, 500))
new_image.save('show.jpg')
dimg = ImageTk.PhotoImage(Image.open('show.jpg'))
canvas.create_image(0, 0, anchor='nw', image=dimg)
canvas.image = dimg
def sim():
tab = cv2.imread(jpg_path, 0)
tab_x = tab.shape[0]
tab_y = tab.shape[1]
new_tab = np.zeros_like(tab)
for x in range(0, tab_x):
for y in range(0, tab_y):
if tab[x, y] == 255:
tab[x, y] = 0
else:
tab[x, y] = 1
for x in range(tab_x):
for y in range(tab_y):
"""summary = 0
summary += tab[x - 1, y - 1]
summary += tab[x - 1, y]
summary += tab[x - 1, y + 1]
summary += tab[x, y - 1]
summary += tab[x, y + 1]
summary += tab[x + 1, y - 1]
summary += tab[x + 1, y]
summary += tab[x + 1, y + 1]"""
summary = (tab[x, (y-1) % tab_y] + tab[x, (y+1) % tab_y] +
tab[(x-1) % tab_x, y] + tab[(x+1) % tab_x, y] +
tab[(x-1) % tab_x, (y-1) % tab_y] + tab[(x-1) % tab_x, (y+1) % tab_y] +
tab[(x+1) % tab_x, (y-1) % tab_y] + tab[(x+1) % tab_x, (y+1) % tab_y])
if tab[x, y] == 1:
if (summary < 2) or (summary > 3):
new_tab[x, y] = 0
else:
new_tab[x, y] = 1
else:
if summary == 3:
new_tab[x, y] = 1
for y in range(0, tab_y):
for x in range(0, tab_x):
if new_tab[y, x] == 1:
new_tab[y, x] = 0
else:
new_tab[y, x] = 255
for x in range (tab_x):
for y in range (tab_y):
tab[x, y] = new_tab[x, y]
cv2.imwrite('anMD.jpg', new_tab)
cv2.imwrite('show.jpg', new_tab)
image = Image.open('show.jpg')
new_image = image.resize((500,500))
new_image.save('show.jpg')
canvas.delete("all")
dimg = ImageTk.PhotoImage(Image.open('show.jpg'))
canvas.create_image(0, 0, anchor='nw', image=dimg)
canvas.image = dimg
这是原始的 50x50 输入和输出。 original input original output
【问题讨论】:
-
是的,我想了很久,但我不知道如何不混淆现在和未来。矩阵保存有问题,但是什么?
标签: python arrays conways-game-of-life cellular-automata