1.问题描述:
迷宫创建算法和迷宫路径寻找算法
2.部分程序:
% Clean up.
clc;
close all;
clear all;
fontSize = 20; % Font size for image captions.% Change the current folder to the folder of this m-file.
% (The line of code below is from Brett Shoelson of The Mathworks.)
if(~isdeployed)
cd(fileparts(which(mfilename)));
end
% For convenience in browsing, set a starting folder from which to browse.
startingFolder = 'C:\Documents and Settings\tk2013\My Documents\My Pictures\MISC\Mazes';
if ~exist(startingFolder, 'dir')
% If that folder doesn't exist, just start in the current folder.
startingFolder = pwd;
endcontinueWithAnother = true;
promptMessage = sprintf('Please specify a maze image (in the next window).\nThis program will attempt to solve the maze.');
button = questdlg(promptMessage, 'maze_solution', 'OK', 'Cancel', 'OK');
if strcmpi(button, 'Cancel')
continueWithAnother = false;
endwhile continueWithAnother
% Get the name of the maze image file that the user wants to use.
defaultFileName = fullfile(startingFolder, '*.*');
[baseFileName, folder] = uigetfile(defaultFileName, 'Select maze image file');
if baseFileName == 0
% User hit cancel. Bail out.
return;
end
fullFileName = fullfile(folder, baseFileName); % Here are some hard coded file names for ease in developing and debugging,
% so we don't have to use uigetfile() all the time. % Gotten from http://www.erclc.org/StaffPages/David/Mazes/Maze1.gif
% This maze works fine.
% fullFileName = fullfile(folder, 'Maze1.gif'); % Gotten from http://www.mattneuman.com/maze.htm
% This is a test maze that won't work because it is not a "non-perfect maze."
% fullFileName = fullfile(folder, 'Maze of Sisyphus.gif'); % Open the maze image file.
originalImage = imread(fullFileName);
[rows cols numberOfColorBands] = size(originalImage); % Convert to monochrome for processing.
if numberOfColorBands > 1
% Convert to monochrome.
redPlane = originalImage(:, :, 1);
greenPlane = originalImage(:, :, 2);
bluePlane = originalImage(:, :, 3);
% Find the standard deviation of each color channel.
redStdDev = std(single(redPlane(:)));
greenStdDev = std(single(greenPlane(:)));
blueStdDev = std(single(bluePlane(:)));
% Take the color channel with the highest contrast.
% Transfer it into a monochrome image. This will be the one that we use.
if redStdDev >= greenStdDev && redStdDev >= blueStdDev
% Red has most contrast - use that channel.
monoImage = single(redPlane);
elseif greenStdDev >= redStdDev && greenStdDev >= blueStdDev
% Green has most contrast - use that channel.
monoImage = single(greenPlane);
else
% Blue has most contrast - use that channel.
monoImage = single(bluePlane);
end
else
monoImage = single(originalImage);
end
% Now we have a monochrome image that we can use to solve the maze.
% Display the results of this step.
close all; % Close any prior windows that are open from a prior run.
subplot(2, 2, 1);
imshow(monoImage, []);
title('Original Image', 'FontSize', fontSize);
set(gcf, 'Position', get(0,'Screensize')); % Maximize figure. % Scale image to 0-255.
maxValue = max(max(monoImage));
minValue = min(min(monoImage));
monoImage = uint8(255 * (single(monoImage) - minValue) / (maxValue - minValue));
% Threshold to get the walls. This will also sharpen up blurry, fuzzy wall edges.
thresholdValue = uint8((maxValue + minValue) / 2);
binaryImage = 255 * (monoImage < thresholdValue);
% Display the results of this step.
subplot(2, 2, 2);
imshow(binaryImage, []);
title('Binary Image - The walls are white here, instead of black', 'FontSize', fontSize); % Label the image to identify discrete, separate walls.
[labeledImage numberOfWalls] = bwlabel(binaryImage, 4); % Label each blob so we can make measurements of it
coloredLabels = label2rgb (labeledImage, 'hsv', 'k', 'shuffle'); % pseudo random color labels
% Display the results of this step.
subplot(2, 2, 3);
imshow(coloredLabels);
caption = sprintf('Labeled image of the %d walls, each a different color', numberOfWalls);
title(caption, 'FontSize', fontSize);
if numberOfWalls ~= 2
message = sprintf('This is not a "perfect maze" with just 2 walls.\nThis maze appears to have %d walls,\nso you may get unexpected results.', numberOfWalls);
uiwait(msgbox(message));
end
3.仿真结论:
C67