В настоящее время я пытаюсь ускорить свой код на Python. Код должен рассчитать все возможные маршруты в лабиринте, созданном из черно-белого изображения. Лабиринт должен быть решен, начиная с 0, j пикселя изображения до каждой точки в нижней части изображения (лучше объяснено на прилагаемом изображении).
На данный момент мой код занимает 3 минуты для анализа изображения размером 240×120 пикселей. Я хотел бы проанализировать изображение размером 16000 x 1000, как вы думаете, это возможно?
Я попытался цитонизировать свой код, но мне это не удалось, поэтому я пытался решить свою проблему с помощью Python.
Скажите, если у вас есть идея ускорить мой код. Заранее спасибо!
Это пример Интерпретированного изображения (только с 0 и 1) и результатов вывода (с инкрементным числом от 1), где дополнительный шаг добавляется везде, где есть 0 в матрице (0 представляет проход, 1 — стена)
from matplotlib.image import imread
import numpy as np
from time import time
start_time = time()
image= imread("white_fibers2.jpg")
array_of_tuples = map(tuple, image)
image2 = tuple(array_of_tuples)
threshold= 170
image1 = np.zeros((len(image2),len(image2[0])))
for i in range(len(image2)):
for j in range(len(image2[i])):
if image2[i][j] >threshold:
image1[i][j]=0
else:
image1[i][j]=1 ### matrix AKA wall in the labyrinth
f = np.zeros(len(image2[1]))
##### from array image 1 to tuple image 3
# array_of_tuples1 = map(tuple, image1)
# image3 = tuple(array_of_tuples1)
image3=np.array(image1)
for w in range(len(image3[1])-1):
if image1[0,w]==0:
start = 0, w
end = len(image2)-1, 0
##### empty matrix that will store the possible moves
m = []
for i in range(len(image3)):
m.append([])
for j in range(len(image3[i])):
m[-1].append(0)
i,j = start
m[i][j] = 1
def make_step(k,m,image3):
for i in range(len(m)):
for j in range(len(m[i])):
if m[i][j] == k:
if i>0 and m[i-1][j] == 0 and image3[i-1][j] == 0: ##### if there is space in Maze (no space no way) and there is space in the path (to avoid that goes back)
m[i-1][j] = k + 1 ####### on top of the square i,j
if j>0 and m[i][j-1] == 0 and image3[i][j-1] == 0:
m[i][j-1] = k + 1 ####### on left of the square i,j
if i<len(m)-1 and m[i+1][j] == 0 and image3[i+1][j] == 0:
m[i+1][j] = k + 1 ####### on bottom of the square i,j
if j<len(m[i])-1 and m[i][j+1] == 0 and image3[i][j+1] == 0:
m[i][j+1] = k + 1 ####### on right of the square i,j
####### continue to perform step till the end point is reached + if there is a cul-de-sac it
k = 0
while m[end[0]][end[1]] == 0 :
if k<600:
k += 1
make_step(k,m, image3)
# make_step(k,m, image3)
print (k)
else:
break
path=np.array(m)
f = np.vstack((f,path[end[0]])) ###### f contain the final line of the final line of the paths
else: ######## if the starting block of the maze is a wall fill the row of "f" with zeros
p = np.zeros(len(image2[1]))
f = np.vstack((f,p))
########
f1=np.delete(f,np.where(~f.any(axis=1))[0], axis=0) ##### remove the row with only 0s
ns =[]
for i in range(len(f1)):
a= f1[i,:]
ns.append(np.min(a[np.nonzero(a)]))
rz=0
for i in range(len(ns)):
rz= rz+ 1/(ns[i])
Rzi = 1/rz
end_time = time()
print (end_time - start_time)

