%===============================================================================
% Get the name of the demo image the user wants to use.
% Let's let the user select from a list of all the demo images that ship with the
Image Processing Toolbox.
folder = fileparts(which('cameraman.tif')); % Determine where demo folder is (works
with all versions).
% Demo images have extensions of TIF, PNG, and JPG. Get a list of all of them.
imageFiles = [dir(fullfile(folder,'*.TIF')); dir(fullfile(folder,'*.PNG'));
dir(fullfile(folder,'*.jpg'))];
for k = 1 : length(imageFiles)
% fprintf('%d: %s\n', k, files(k).name);
[~, baseFileName, extension] = fileparts(imageFiles(k).name);
ca{k} = [baseFileName, extension];
end
% Sort the base file names alphabetically.
[ca, sortOrder] = sort(ca);
imageFiles = imageFiles(sortOrder);
button = menu('Use which gray scale demo image?', ca); % Display all image file
names in a popup menu.
% Get the base filename.
baseFileName = imageFiles(button).name; % Assign the one on the button that they
clicked on.
% Get the full filename, with path prepended.
fullFileName = fullfile(folder, baseFileName);
% Read in an image.
grayImage = imread(fullFileName);
iteration = 1;
% Initialize image.
oldScrambledImage = grayImage;
% The number of iterations needed to restore the image can be shown never to exceed
3N.
N = rows;
while iteration <= 3 * N
% Scramble the image based on the old image.
for row = 1 : rows % y
for col = 1 : columns % x
c = mod((2 * col) + row, N) + 1; % x coordinate
r = mod(col + row, N) + 1; % y coordinate
% Move the pixel. Note indexes are (row, column) = (y, x) NOT
(x, y)!
currentScrambledImage(row, col, :) = oldScrambledImage(r, c, :);
end
end
if immse(currentScrambledImage, grayImage) == 0
caption = sprintf('Back to Original after %d Iterations.', iteration);
fprintf('%s\n', caption);
title(caption, 'FontSize', fontSize);
break;
end
% Make the current image the prior/old one so we'll operate on that the next
iteration.
oldScrambledImage = currentScrambledImage;
% Update the iteration counter.
iteration = iteration+1;
end
% Note: an interesting experiment might be to put the above loop in a loop where
you're
% changing N and see how iteration (the number of iterations to return to the
original)
% changes depending on N.