Python Forum
problem about maze path finder
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
problem about maze path finder
#1
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,

can anybody help about this python code please?

MATLAB code
***********
%% Clean the Workspace and command window
clear all;
clc;
close all;
%% Read the image of maze
Maze = imread('maze02.png'); % Read your image here.
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

img = cv2.imread('maze02.png')
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()
Reply
#2
What is the problem and would you please wrap code in python tags so the indentation is preserved.
Reply
#3
in the Matlab there I got the picture one (https://drive.google.com/file/d/1uF-_daD...sp=sharing) but in python I could not get that effect, I got the picture 2 (https://drive.google.com/file/d/17sd_w2H...sp=sharing)

after all watershed effect the maze is solved (https://drive.google.com/file/d/13XLq0ei...sp=sharing)

the watershed function in the python is working effectively
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  WebDriverException: Message: 'PATH TO CHROME DRIVER' executable needs to be in PATH Led_Zeppelin 1 2,149 Sep-09-2021, 01:25 PM
Last Post: Yoriz
Exclamation Path sacn problem lucky511 10 3,783 Jun-24-2021, 12:09 PM
Last Post: Axel_Erfurt
  Help in designing a timestamp finder for chapter-less TV shows Daring_T 1 1,819 Oct-26-2020, 09:30 PM
Last Post: Daring_T
  ISS Location Finder problem birdwatcher 7 3,561 Jan-20-2020, 07:41 PM
Last Post: birdwatcher
  maze solution jenya56 5 3,200 Mar-27-2019, 05:11 PM
Last Post: ichabod801
  Problem with reading a path gkiller007 30 22,677 Jan-05-2019, 10:09 PM
Last Post: snippsat
  Maze Mapping with Image Processing furkankuyumcu 0 2,159 Dec-16-2018, 02:45 PM
Last Post: furkankuyumcu
  Anaconda problem about path Leloup 1 8,051 Nov-23-2018, 05:14 PM
Last Post: Larz60+
  path.exists() problem CAHinton 2 2,867 Jul-24-2018, 05:47 PM
Last Post: CAHinton
  .pth file does not show up in sys.path when configuring path. arjunsingh2908 2 5,670 Jul-03-2018, 11:16 AM
Last Post: arjunsingh2908

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020