从图像中读取网格 Python

Read a grid from image Python

我需要关于为迷宫生成目的读取网格的建议,我不需要生成迷宫的代码,我需要的只是一种从图像中读取 m x n 网格并能够遍历单元格和 link/ unlink 那些单元格。我已经编写了一些使用 PIL 生成网格的代码,我将编写代码使用不同的算法生成迷宫。

示例:

给定一个看起来像这样的网格,我需要例如通过移除墙将单元格 0、0 和单元格 0、1 link 合并在一起来修改它们 |它们之间。关于如何能够在可能执行以下操作的方法中修改单元格之间的墙壁的任何建议:

def link_cells(cell1, cell2, grid):
    """Link 2 cells in a given image.
    cell1: a tuple (row, column)
    cell2: a tuple (row, column)
    grid: an image object (full grid)
    """
    # do ...

注意:我不需要用于生成迷宫的算法,只需要启用图像处理部分的算法,我将从那里开始工作。

这是一种可能的方法。将图像中所有行的均值计算为单列宽向量,如右图所示。同样将所有列的平均值计算为单行高矢量,如图中底部所示:

现在对这两个向量设置阈值,然后寻找从白到黑以及从黑到白的过渡。这将为您提供图像中所有黑线的开始和结束行和列。您现在可以使用它们将要擦除​​的单元格边界涂成白色。我涂上了青色和洋红色,所以你可以看到我在做什么。

#!/usr/bin/env python3

import numpy as np
from PIL import Image

# Open image and make greyscale and Numpy versions
pimRGB  = Image.open('grid.jpg')
pimgrey = pim.convert('L')
nimRGB  = np.array(pimRGB)
nimgrey = np.array(pimgrey)

# Work out where the horizontal lines are
rowmeans  = np.mean(nimgrey,axis=1)
rowthresh = np.where(rowmeans>128,255,0)
# Difference each element with its neighbour...
diffs = rowthresh[:-1] - rowthresh[1:]
rowStarts, rowEnds = [], []
for i,v in enumerate(diffs):
    if v>0:
        rowStarts.append(i)
    if v<0:
        rowEnds.append(i)

# Work out where the vertical lines are
colmeans  = np.mean(nimgrey,axis=0)
colthresh = np.where(colmeans>128,255,0)
# Difference each element with its neighbour...
diffs = colthresh[:-1] - colthresh[1:]
colStarts, colEnds = [], []
for i,v in enumerate(diffs):
    if v>0:
        colStarts.append(i)
    if v<0:
        colEnds.append(i)

# Now all our initialisation is finished

# Colour in cyan the 2nd black row starting after the 3rd black col
r, c = 2, 3
nimRGB[rowStarts[r]:rowEnds[r],colEnds[c]:colStarts[c+1]] = [0,255,255] 

# Colour in magenta the 8th black column starting after the 5th black row
r, c = 5, 8
nimRGB[rowEnds[r]:rowStarts[r+1],colStarts[c]:colEnds[c]] = [255,0,255] 

# Convert Numpy array back to PIL Image and save
Image.fromarray(nimRGB).save('result.png')


作为参考,colthresh 看起来像这样:

array([255, 255, 255, 255, 255, 255, 255, 255, 255,   0,   0,   0,   0,
       255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
       255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
       255, 255, 255, 255, 255, 255, 255, 255, 255, 255,   0,   0, 255,
       255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
       255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
       255, 255, 255, 255, 255,   0,   0, 255, 255, 255, 255, 255, 255,
       255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
       255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
       255, 255,   0,   0, 255, 255, 255, 255, 255, 255, 255, 255, 255,
       255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
       255, 255, 255, 255, 255, 255, 255, 255, 255, 255,   0,   0, 255,
       255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
       255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
       255, 255, 255, 255, 255, 255,   0,   0, 255, 255, 255, 255, 255,
       255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
       255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
       255, 255,   0,   0, 255, 255, 255, 255, 255, 255, 255, 255, 255,
       255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
       255, 255, 255, 255, 255, 255, 255, 255, 255, 255,   0,   0, 255,
       255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
       255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
       255, 255, 255, 255, 255, 255, 255,   0, 255, 255, 255, 255, 255,
       255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
       255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
       255, 255, 255, 255,   0,   0,   0,   0, 255, 255, 255, 255, 255,
       255, 255, 255, 255])

diffs看起来像这样:

array([   0,    0,    0,    0,    0,    0,    0,    0,  255,    0,    0,
          0, -255,    0,    0,    0,    0,    0,    0,    0,    0,    0,
          0,    0,    0,    0,    0,    0,    0,    0,    0,    0,    0,
          0,    0,    0,    0,    0,    0,    0,    0,    0,    0,    0,
          0,    0,    0,    0,  255,    0, -255,    0,    0,    0,    0,
          0,    0,    0,    0,    0,    0,    0,    0,    0,    0,    0,
          0,    0,    0,    0,    0,    0,    0,    0,    0,    0,    0,
          0,    0,    0,    0,    0,  255,    0, -255,    0,    0,    0,
          0,    0,    0,    0,    0,    0,    0,    0,    0,    0,    0,
          0,    0,    0,    0,    0,    0,    0,    0,    0,    0,    0,
          0,    0,    0,    0,    0,    0,    0,    0,  255,    0, -255,
          0,    0,    0,    0,    0,    0,    0,    0,    0,    0,    0,
          0,    0,    0,    0,    0,    0,    0,    0,    0,    0,    0,
          0,    0,    0,    0,    0,    0,    0,    0,    0,  255,    0,
       -255,    0,    0,    0,    0,    0,    0,    0,    0,    0,    0,
          0,    0,    0,    0,    0,    0,    0,    0,    0,    0,    0,
          0,    0,    0,    0,    0,    0,    0,    0,    0,    0,    0,
        255,    0, -255,    0,    0,    0,    0,    0,    0,    0,    0,
          0,    0,    0,    0,    0,    0,    0,    0,    0,    0,    0,
          0,    0,    0,    0,    0,    0,    0,    0,    0,    0,    0,
          0,    0,  255,    0, -255,    0,    0,    0,    0,    0,    0,
          0,    0,    0,    0,    0,    0,    0,    0,    0,    0,    0,
          0,    0,    0,    0,    0,    0,    0,    0,    0,    0,    0,
          0,    0,    0,  255,    0, -255,    0,    0,    0,    0,    0,
          0,    0,    0,    0,    0,    0,    0,    0,    0,    0,    0,
          0,    0,    0,    0,    0,    0,    0,    0,    0,    0,    0,
          0,    0,    0,    0,    0,    0,  255, -255,    0,    0,    0,
          0,    0,    0,    0,    0,    0,    0,    0,    0,    0,    0,
          0,    0,    0,    0,    0,    0,    0,    0,    0,    0,    0,
          0,    0,    0,    0,    0,    0,    0,    0,    0,  255,    0,
          0,    0, -255,    0,    0,    0,    0,    0,    0,    0,    0]

colStarts 看起来像这样:

[8, 48, 82, 118, 152, 187, 222, 256, 292, 328]

colEnds 看起来像这样:

[12, 50, 84, 120, 154, 189, 224, 258, 293, 332]