 # Problem about maze path finder

Dear friends;

I want to developed maze path finder I found one of tutorial in matlab, I revised this in python but I have not get final solution, I want to share these two codes,

MATLAB code

%% Clean the Workspace and command window
clear all;
clc;
close all;
%% Read the image of maze
disp(size(Maze))
figure,imshow(Maze);title(‘Original Maze image’);
%% Convert to binary image
if size(Maze,3) ==3
Maze = rgb2gray(Maze);
end
Maze_Binary = imbinarize(Maze,graythresh(Maze)-0.1); % Make sure to have black walls and white path
% There should not be any broken walls, walls should be seperate rom boundary of images
disp('maze binary: ')
disp(size(Maze_Binary))
figure,imshow(Maze_Binary);title(‘Binary Maze’);
%% Start Solving
%Use Watershed transform to find catchment basins
%Some other methods also can be used
Maze_Watershed = watershed(Maze_Binary);
C1 = (Maze_Watershed == 2);%Obtain First Part of Watershed
Maze_watershed1 = C1.*Maze_Binary;
figure,imshow(Maze_watershed1);title(‘One of the Part of catchment basins’);
C2 = watershed(Maze_watershed1);
%Using watershed transform once again so the image obtained will be
%shrinked along path of maze, imshow pir is used to observe this.
figure,imshowpair(Maze_Watershed,C2);title(‘Shrinked Path’)
%So now we can easily Take difference of both images to get the path.
P1 = Maze_Watershed == 2;
P2 = C2 == 2;
path = P1 - P2;
Solved = imoverlay(Maze_Binary, path, [0.25 0.25 1]);
figure,imshow(Solved);title(‘Solved Maze’)

Python Code

import numpy as np
import cv2
from matplotlib import pyplot as plt

b,g,r = cv2.split(img)
rgb_img = cv2.merge([r,g,b])

gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
ret, thresh = cv2.threshold(gray, 50, 20, cv2.THRESH_BINARY_INV + cv2.THRESH_OTSU)

kernel = np.ones((2,2),np.uint8)
opening = cv2.morphologyEx(thresh,cv2.MORPH_OPEN,kernel, iterations = 2)
closing = cv2.morphologyEx(thresh, cv2.MORPH_CLOSE, kernel, iterations = 1)

sure_bg = cv2.dilate(closing, kernel, iterations=8)
dist_transform = cv2.distanceTransform(sure_bg, cv2.DIST_L2, 3)
ret, sure_fg = cv2.threshold(dist_transform, 0.1 * dist_transform.max(), 255, 0)

sure_fg = np.uint8(sure_fg)
unknown = cv2.subtract(sure_bg,sure_fg)

ret, markers = cv2.connectedComponents(sure_fg)

markers = markers + 1
c1 = 2
markers[unknown==255] = 2

markers = cv2.watershed(img, markers)
img[markers == 2] = [255,0,0]

cv2.imshow(“result from watershed”, img)

cv2.waitKey(0)
cv2.destroyAllWindows()