如何从图像中读取迷宫并将其转换为 Python 中的二进制值

How to read a maze from an image and convert it to binary values in Python

我想要一些方法(在 Python 3 中)读取迷宫图像的像素,预期输出应该是二进制列表 [[1, 1, 0], [1, 1, 1] , [1, 0, 0]] (例如 1 代表白色,0 代表黑色)(应该看起来像这样,值是为了示例)我不想要解决迷宫的方法,所有我需要的是能够在给定图像的情况下实现迷宫内部表示的东西。我希望能够做出任何选择的算法来绘制它的执行和显示路径,也可能显示进度。 (我不需要算法,我只想要一种读取迷宫图像和编写新迷宫图像的方法,以显示所选路径/显示算法的进展,而不管算法是什么(Dijkstra,A *,广度优先搜索...)

我尝试使用 PIL 和 cv2,但由于我不是这方面的专家,所以我不知道该怎么做

以下是一些迷宫示例:

小迷宫:

大迷宫:

这里:

import matplotlib.pyplot as plt
import numpy as np
from scipy.ndimage import imread

im = imread("maze.jpg") # values from 0 to 255
im = im.astype(np.float) / 255. # 0 to 1
im[im>0.5] = 1.0 # round
im[im<=0.5] = 0.0

x = np.linspace(0, 1, im.shape[0])
y = np.linspace(0, 1, im.shape[1])

plt.contourf(x, y, im)
plt.colorbar()

这是我使用的测试图像:

在尝试对 jpeg 执行此任务之前,您可以尝试使用更简单的格式。

例如,您可以从 PGM 文件.

开始

PGM是灰度图(黑白图)。您可以使用 gimp 创建一个非常简单的 PGM 文件(导出为 -> PGM -> raw)。

例如,我画了这个非常简单的 4*4 图像:

*小心!我刚刚链接的图像是我的 4*4 pgm 图像的 jpeg 大尺寸版本!这不是我的真实文件!*

PGM 与所有类型的图像一样,是一种遵循规范

的格式

你可以找到标准here

最有趣的部分在这里:

Each PGM image consists of the following:

A "magic number" for identifying the file type. A pgm image's magic number is the two characters "P5".

Whitespace (blanks, TABs, CRs, LFs).

A width, formatted as ASCII characters in decimal.

Whitespace.

...

它描述了 PGM 文件的格式!

所以现在,根据这个规范,我们可以创建一个非常简单的python PGM 解析器!

# Opening my PGM file. Since this is a raw encoded file, img.read() will read
# bytes !
img = open('./maze_test.pgm', 'rb')

# This line means this is a PGM file. 
# It is encoded in ASCII. So, since every ASCII character is encoded with 1 byte,
# we have to read 2 bytes according to the norm
print(img.read(2))

# This is a blank line
print(img.readline())

# This line is a GIMP comment
print(img.readline())

# This line is an ASCII line. It contains the width, encoded in ASCII, then a 
# space, and then the height also encoded in ASCII
width_height = str(img.readline())
# Remove the python byte information
width_height = width_height[2:-3]

# We split this line in an list
width_height = width_height.split(' ')

# The first element represents the width
width = int(width_height[0])
# The second represents the height
height = int(width_height[1])

# The max_value encoded in ASCII
max_value = int(img.readline())

# Now, there is only byte data

pixel_map = []
for row in range(width):
    # We prepare the next line in our list
    pixel_map.append([])
    for column in range(height):
        # The value that we read is a byte. We simply use ord to convert it to int
        pixel_value = ord(img.read(1))

        # We normalize the value using the max_value
        pixel_value = pixel_value//max_value

        pixel_map[row].append(pixel_value)

# Here is the pixel map
print(pixel_map)

输出: [[0, 1, 0, 1], [1, 0, 0, 1], [1, 0, 0, 0], [1, 0, 1, 1]]

您可以像这样加载 PNG、GIF、TIF 或 JPEG 文件,然后确保它只有 01 值,并使用 PIL/Pillow 和 Numpy 处理像素。

我使用了你的迷你迷宫的这个编辑版本:

#!/usr/bin/env python3

from PIL import Image
import numpy as np

# Open the maze image and make greyscale, and get its dimensions
im = Image.open('maze.png').convert('L')
w, h = im.size

# Ensure all black pixels are 0 and all white pixels are 1
binary = im.point(lambda p: p > 128 and 1)

# Resize to half its height and width so we can fit on Stack Overflow, get new dimensions
binary = binary.resize((w//2,h//2),Image.NEAREST)
w, h = binary.size

# Convert to Numpy array - because that's how images are best stored and processed in Python
nim = np.array(binary)

# Print that puppy out 
for r in range(h):
    for c in range(w):
        print(nim[r,c],end='')
    print()

结果如下:

000000000000000000001111111111111111111000000000000000000000000000000000000000000000000000000000000
001111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111100
001111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111100
001111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111100
001111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111100
001111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111100
001111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111100
001111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111100
001111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111100
001111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111100
001111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111100
001111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111100
001111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111100
001111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111100
001111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111100
001111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111100
001111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111100
001111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111100
001111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111100
001111111111111111110000000000000000000000000000000000000000000000000000000000111111111111111111100
001111111111111111110000000000000000000000000000000000000000000000000000000000111111111111111111100
001111111111111111111111111111111111110011111111111111111111111111111111111111111111111111111111100
001111111111111111111111111111111111110011111111111111111111111111111111111111111111111111111111100
001111111111111111111111111111111111110011111111111111111111111111111111111111111111111111111111100
001111111111111111111111111111111111110011111111111111111111111111111111111111111111111111111111100
001111111111111111111111111111111111110011111111111111111111111111111111111111111111111111111111100
001111111111111111111111111111111111110011111111111111111111111111111111111111111111111111111111100
001111111111111111111111111111111111110011111111111111111111111111111111111111111111111111111111100
001111111111111111111111111111111111110011111111111111111111111111111111111111111111111111111111100
001111111111111111111111111111111111110011111111111111111111111111111111111111111111111111111111100
001111111111111111111111111111111111110011111111111111111111111111111111111111111111111111111111100
001111111111111111111111111111111111110011111111111111111111111111111111111111111111111111111111100
001111111111111111111111111111111111110011111111111111111111111111111111111111111111111111111111100
001111111111111111111111111111111111110011111111111111111111111111111111111111111111111111111111100
001111111111111111111111111111111111110011111111111111111111111111111111111111111111111111111111100
001111111111111111111111111111111111110011111111111111111111111111111111111111111111111111111111100
001111111111111111111111111111111111110011111111111111111111111111111111111111111111111111111111100
001111111111111111111111111111111111110011111111111111111111111111111111111111111111111111111111100
000000000000000000001111111111111111110011111111111111111110000000000000000000111111111111111111100
000000000000000000001111111111111111110011111111111111111100000000000000000000011111111111111111100
001111111111111111111111111111111111110011111111111111111100111111111111111110011111111111111111100
001111111111111111111111111111111111110011111111111111111100111111111111111110011111111111111111100
001111111111111111111111111111111111110011111111111111111100111111111111111110011111111111111111100
001111111111111111111111111111111111110011111111111111111100111111111111111110011111111111111111100
001111111111111111111111111111111111110011111111111111111100111111111111111110011111111111111111100
001111111111111111111111111111111111110011111111111111111100111111111111111110011111111111111111100
001111111111111111111111111111111111110011111111111111111100111111111111111110011111111111111111100
001111111111111111111111111111111111110011111111111111111100111111111111111110011111111111111111100
001111111111111111111111111111111111110011111111111111111100111111111111111110011111111111111111100
001111111111111111111111111111111111110011111111111111111100111111111111111110011111111111111111100
001111111111111111111111111111111111110011111111111111111100111111111111111110011111111111111111100
001111111111111111111111111111111111110011111111111111111100111111111111111110011111111111111111100
001111111111111111111111111111111111110011111111111111111100111111111111111110011111111111111111100
001111111111111111111111111111111111110011111111111111111100111111111111111110011111111111111111100
001111111111111111111111111111111111110011111111111111111100111111111111111110011111111111111111100
001111111111111111111111111111111111110011111111111111111100111111111111111110011111111111111111100
001111111111111111111111111111111111110011111111111111111100111111111111111110011111111111111111100
001111111111111111110111111111000000000001000000000000001100111111111111111110011111111111111111100
001111111111111111110000000000000000000000000000000000000000111111111111111110011111111111111111100
001111111111111111100000000000000000000000000000000000000000111111111111111110011111111111111111100
001111111111111111100111111111111111111111111111111111111100111111111111111110011111111111111111100
001111111111111111100111111111111111111111111111111111111100111111111111111110011111111111111111100
001111111111111111100111111111111111111111111111111111111100111111111111111110011111111111111111100
001111111111111111100111111111111111111111111111111111111100111111111111111110011111111111111111100
001111111111111111100111111111111111111111111111111111111100111111111111111110011111111111111111100
001111111111111111100111111111111111111111111111111111111100111111111111111110011111111111111111100
001111111111111111100111111111111111111111111111111111111100111111111111111110011111111111111111100
001111111111111111100111111111111111111111111111111111111100111111111111111110011111111111111111100
001111111111111111100111111111111111111111111111111111111100111111111111111110011111111111111111100
001111111111111111100111111111111111111111111111111111111100111111111111111110011111111111111111100
001111111111111111100111111111111111111111111111111111111100111111111111111110011111111111111111100
001111111111111111100111111111111111111111111111111111111100111111111111111110011111111111111111100
001111111111111111100111111111111111111111111111111111111100111111111111111110011111111111111111100
001111111111111111100111111111111111111111111111111111111100111111111111111110011111111111111111100
001111111111111111100111111111111111111111111111111111111100111111111111111110011111111111111111100
001111111111111111100111111111111111111111111111111111111100111111111111111110011111111111111111100
001111111111111111100111111111111111111111111111111111111100111111111111111110011111111111111111100
001111111111111111100111111111111111111111111111111111111100111111111111111110011111111111111111100
001111111111111111111111111111111111110011111111111111111111111111111111111110011111111111111111100
001111111111111111111111111111111111110011111111111111111111111111111111111110011111111111111111100
001111111111111111111111111111111111110011111111111111111111111111111111111110011111111111111111100
001111111111111111111111111111111111110011111111111111111111111111111111111110011111111111111111100
001111111111111111111111111111111111110011111111111111111111111111111111111110011111111111111111100
001111111111111111111111111111111111110011111111111111111111111111111111111110011111111111111111100
001111111111111111111111111111111111110011111111111111111111111111111111111110011111111111111111100
001111111111111111111111111111111111110011111111111111111111111111111111111110011111111111111111100
001111111111111111111111111111111111110011111111111111111111111111111111111110011111111111111111100
001111111111111111111111111111111111110011111111111111111111111111111111111110011111111111111111100
001111111111111111111111111111111111110011111111111111111111111111111111111110011111111111111111100
001111111111111111111111111111111111110011111111111111111111111111111111111110011111111111111111100
001111111111111111111111111111111111110011111111111111111111111111111111111110011111111111111111100
001111111111111111111111111111111111110011111111111111111111111111111111111110011111111111111111100
001111111111111111111111111111111111110011111111111111111111111111111111111110011111111111111111100
001111111111111111111111111111111111110011111111111111111111111111111111111110011111111111111111100
001111111111111111111111111111111111110011111111111111111111111111111111111110011111111111111111100
001111111111111111111111111111111111110011111111111111111111111111111111111110011111111111111111100
000000000000000000000000000000000000000011111111111111111110000000000000000000000000000000000000000
000000000000000000000000000000000000000011111111111111111110000000000000000000000000000000000000000