Getting the maze solution to not wrap around the maze itself, and be centered off the walls.

1 view (last 30 days)
function C = solvemaze(A)
A = imread('maze09.png');
[rows cols numberOfColorBands] = size(A);
monoImage = single(A);
maxValue = max(max(monoImage));
minValue = min(min(monoImage));
monoImage = uint8(255 * (single(monoImage) - minValue) / (maxValue - minValue));
thresholdValue = uint8((maxValue + minValue) / 2);
binaryImage = 255 * (A < thresholdValue);
subplot(2, 2, 2);
imshow(binaryImage, []);
[labeledImage numberOfWalls] = bwlabel(binaryImage, 4);
coloredLabels = label2rgb (labeledImage, 'hsv', 'k', 'shuffle');
subplot(2, 2, 3);
imshow(coloredLabels);
binaryImage2 = (labeledImage == 1);
subplot(2, 2, 4);
imshow(binaryImage2, []);
dilationAmount = 7;
dilatedImage = imdilate(binaryImage2, ones(dilationAmount));
figure;
subplot(2, 2, 1);
imshow(dilatedImage, []);
filledImage = imfill(dilatedImage, 'holes');
subplot(2, 2, 2);
imshow(filledImage, []);
erodedImage = imerode(filledImage, ones(dilationAmount));
subplot(2, 2, 3);
imshow(erodedImage, []);
solutionA = filledImage;
solutionA(erodedImage) = 0;
subplot(2, 2, 4);
imshow(solutionA, []);
if numberOfColorBands == 1
redPlane = monoImage;
greenPlane = monoImage;
bluePlane = monoImage;
end
redPlane(solutionA) = 255;
greenPlane(solutionA) = 0;
bluePlane(solutionA) = 0;
solvedImage = cat(3, redPlane, greenPlane, bluePlane);
figure;
imshow(solvedImage);
C = solvedImage;
if logical(C) == 0
C = [];
else
C = C;
end
end
The solution path hugs the walls and I need it to not wrap around the entire maze and be centered. Using only morphological commands.

Answers (0)

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!