i need a matlab code for arnold cat map scrambling

i need a matlab code for arnold cat map scrambling

Answers (1)

See attached demo.

28 Comments

Awesome demo!!!
How can u save every iteration in a file?
You can use imwrite(). It should already be in the code.
% Save the image, if desired.
filename = sprintf('Arnold Cat Iteration %d.png', iteration);
% imwrite(currentScrambledImage, filename);
fprintf('Saved image file %s to disk.\n', filename);
Just uncomment the immwrite() line. In case it's not in there, I'm attaching my latest m-file.
clc; % Clear the command window.
close all; % Close all figures (except those of imtool.)
clear; % Erase all existing variables. Or clearvars if you want.
workspace; % Make sure the workspace panel is showing.
format long g;
format compact;
fontSize = 20;
myFolder = 'C:\Users\algo\Desktop';
%===============================================================================
% 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('a1.jpg')); % 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);
% Get the dimensions of the image.
% numberOfColorChannels should be = 1 for a gray scale image, and 3 for an RGB color image.
[rows, columns, numberOfColorChannels] = size(grayImage);
% Set a max size so the demo doesn't take too long.
maxAllowableSize = 401;
% Note: the number of iterations needed to return to the original image
% is very sensitive to the size of the image. For example if
% maxAllowableSize = 201 it takes 68 iterations, but if
% maxAllowableSize = 200 it takes 150 iterations.
% Make sure the image is square.
if rows ~= columns
% It's not yet square. Make it square and resize if needed.
N = max([rows, columns]);
if N > maxAllowableSize
N = maxAllowableSize;
end
grayImage = imresize(grayImage, [N, N]);
end
[rows, columns, numberOfColorChannels] = size(grayImage);
if rows > maxAllowableSize
% Make it smaller to speed up demo.
N = maxAllowableSize;
grayImage = imresize(grayImage, [N, N]);
end
% Update values.
[rows, columns, numberOfColorChannels] = size(grayImage);
% Display starting image.
%subplot(1, 2, 1);
imshow(grayImage);
axis on;
caption = sprintf('Original Image, %s', baseFileName);
title(caption, 'FontSize', fontSize);
% Set up figure properties:
% Enlarge figure to full screen.
set(gcf, 'Units', 'Normalized', 'OuterPosition', [0 0 1 1]);
% Get rid of tool bar and pulldown menus that are along top of figure.
% set(gcf, 'Toolbar', 'none', 'Menu', 'none');
% Give a name to the title bar.
set(gcf, 'Name', 'Arnolds Cat Map ', 'NumberTitle', 'Off')
startingTime = tic;
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
% Display the current image.
caption = sprintf('Arnolds Cat Map, Iteration #%d', iteration);
File = sprintf('arn%0.1f.png',iteration);
exportgraphics(figure,fullfile(myFolder,File));
fprintf('%s\n', caption);
%subplot(1, 2, 2);
imshow(currentScrambledImage);
axis on;
title(caption, 'FontSize', fontSize);
drawnow;
% Insert a delay if desired.
% pause(0.1);
% Save the image, if desired.
filename = sprintf('Arnold Cat Iteration %d.png', iteration);
% imwrite(currentScrambledImage, filename);
fprintf('Saved image file %s to disk.\n', filename);
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
elapsedTime = toc(startingTime);
fprintf('Elapsed time = %.1f seconds.\n', elapsedTime);
% 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.
yes what you are saying is corect but i want to save each iteration not only the last , i tried to modify your code this way but the pictures it saves for each iteration are empty for some reason.
But why did you not uncomment the imwrite() line of code???
i did but i got an error saying :
Error using imwrite (line 541)
Unable to open file "Arnold Cat Iteration 1.png" for writing. You might not have write permission.
Error in arnolds_cat_map (line 113)
imwrite(currentScrambledImage, filename);
thats why i tried the other way
What is the current folder name?
if you mean the folder where i have the code its C:\Users\alexk\Desktop\mas\cat
Try saving something simple there, like
moon = imread('moon.tif');
imwrite(moon, 'C:\Users\alexk\Desktop\mas\cat\moon.tif');
If that doesn't work then try just copying any file with File Explorer to your desktop and see if that works. It seems like it's an operating system permissions issue, not a MATLAB problem.
no, i can save something there without an error
i tried your code and it did save the pic moon.tif in the corect file
Then there is no reason that it should not work for the cat map images. In fact, I just uncommented the lines of code and ran it, and it saved them just fine.
HI image analyst! Do you know how can i save the path that one pixel is following while the iterations are done?
No, I'd have to figure it out just like you would. Good luck.
Construct an array that is
A = reshape(1:(rows*columns), rows, columns)
Apply your catmap algorithm to this array, producing new array C.
If you created a correct catmap algorithm, then C has all of the entries in A, just in a different order, and no other entries.
C is now a mapping matrix, and for any given intermediate matrix, you can apply the cat-map transform as
I = your_image;
for K = 1 : steps
I = I(C);
end
You can then modify this slightly to follow a particular pixel:
idx = sub2ind(size(your_image), row_of_interest, col_of_interest));
idxhistory = zeros(1, steps+1);
idxhistory(1) = idx;
I = your_image;
IA = A;
for K = 1 : steps
I = I(C);
IA = IA(C);
idxhistory(K+1) = find(IA == idx);
end
and if you want to see it in terms of rows and columns:
[rowhistory, colhistory] = ind2sub(size(your_image), idxhistory);
plot3(0:steps, rowhistory, colhistory)
xlabel('step #'); ylabel('row'); zlabel('column')
thank you so much for your answer and your help!!!
I wrote it like this:
clc;
clear all;
img = imread('1.png');
[rows, columns, numberOfColorChannels] = size(img);
A = reshape(1:(rows*columns), rows, columns)
iteration = 1;
% Initialize image.
oldScrambledImage = img;
% The number of iterations needed to restore the image can be shown never to exceed 3N.
N = rows;
%while iteration <= 3 * N
while iteration <= 5
% 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
% 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
%follow a particular pixel:
idx = sub2ind((size(img), row_of_interest, col_of_interest));
idxhistory = zeros(1, steps+1);
idxhistory(1) = idx;
I = img;
IA = A;
for K = 1 : steps
I = I(C);
IA = IA(C);
idxhistory(K+1) = find(IA == idx);
end
%[rowhistory, colhistory] = ind2sub(size(your_image), idxhistory);
%plot(0:steps, rowhistory, colhistory)
%xlabel('step #'); ylabel('row'); zlabel('column')
and i get this error :
Error: File: followpixelcatmap.m Line: 34 Column: 23
Invalid expression. When calling a function or indexing a variable, use parentheses. Otherwise, check for
mismatched delimiters.
for this line of code:
idx = sub2ind((size(img), row_of_interest, col_of_interest));
do you know why??
You're passing sub2ind() one argument - a 4 element row vector. You need to pass it three
idx = sub2ind(size(img), row_of_interest, col_of_interest);
You had too many parentheses in there that groups all your arguments together into one array.
Looks like I had an extra ) when I posted that line of code.
thank you so much for your time guys but i got a bit confused
idx = sub2ind(size(img), row_of_interest, col_of_interest);
idxhistory = zeros(1, steps+1);
idxhistory(1) = idx;
I = img;
IA = A;
for K = 1 : steps
I = I(C);
IA = IA(C);
idxhistory(K+1) = find(IA == idx);
end
is row_of_interest=rows of my image and col_of_interest=colums of myy image?
and is steps= iterations of the cat map?
and what is C equal to? from this code?
clc;
clear all;
img = imread('1.png');
[rows, columns, numberOfColorChannels] = size(img);
A = reshape(1:(rows*columns), rows, columns)
iteration = 1;
% Initialize image.
oldScrambledImage = img;
% The number of iterations needed to restore the image can be shown never to exceed 3N.
N = rows;
%while iteration <= 3 * N
while iteration <= 5
% 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
% 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
I wrote,
Construct an array that is
A = reshape(1:(rows*columns), rows, columns)
Apply your catmap algorithm to this array, producing new array C.
For example,
img = imread('1.png');
if size(img,3) > 1; img = rgb2gray(img); end
[rows, columns] = size(img);
A = reshape(1:(rows*columns), rows, columns);
C = arnold_catmap_step(A);
Now, you had written
Do you know how can i save the path that one pixel is following while the iterations are done?
so you need to pick a particular pixel to follow the path for.
row_of_interest = randi(rows)
col_of_interest = randi(columns)
idx = sub2ind(size(img), row_of_interest, col_of_interest);
N = rows;
maxsteps = 3*N;
idxhistory = zeros(1, steps+1);
idxhistory(1) = idx;
I = img;
IC = A;
for iteration = 1 : maxsteps
I = I(C);
IA = IA(C);
idxhistory(iteration+1) = find(IA == idx);
end
Here, function arnod_catmap_step would be a function that you would write that would take an input image and apply a single scrambling iteration to it. Mostly you would extract your code
% Scramble the image based on the old image.
and put it into a function.
You might notice that I do not have you call arnold_catmap_step() inside the for iterations loop. You could do that. However, it turns out that once you have created the C array by applying one scrambling step to the index array, A, that each further scrambling step can be done as a single lookup without having to do looping.
For example,
img = uint8([255 1 255 1; 240 128 240 128; 64 63 62 61; 238 0 127 92])
img = 4×4
255 1 255 1 240 128 240 128 64 63 62 61 238 0 127 92
image(img)
[r,c] = size(img)
r = 4
c = 4
C = reshape(randperm(r*c),r,c)
C = 4×4
6 16 15 8 10 4 11 14 2 12 13 7 9 1 3 5
IA = reshape(1:(r*c), r, c)
IA = 4×4
1 5 9 13 2 6 10 14 3 7 11 15 4 8 12 16
I = img
I = 4×4
255 1 255 1 240 128 240 128 64 63 62 61 238 0 127 92
for K = 1 : 4
I = I(C)
IA = IA(C)
figure(); image(I)
end
I = 4×4
128 92 61 0 240 238 62 128 240 127 1 63 255 255 64 1
IA = 4×4
6 16 15 8 10 4 11 14 2 12 13 7 9 1 3 5
I = 4×4
238 1 63 255 62 255 1 128 240 64 0 127 61 128 240 92
IA = 4×4
4 5 7 1 11 9 13 14 10 3 8 12 15 6 2 16
I = 4×4
255 92 127 128 1 61 0 128 62 240 255 64 63 238 240 1
IA = 4×4
9 16 12 6 13 15 8 14 11 2 1 3 7 4 10 5
I = 4×4
61 1 64 238 0 63 255 128 1 240 128 240 127 255 62 92
IA = 4×4
15 5 3 4 8 7 1 14 13 10 6 2 12 9 11 16
Here, I is the progressively scrambled image, and IA is the scrambling indices. C is acting as a permutation matrix.
Thank you so much for your help Walter!
So this is my function:
function currentScrambledImage = arnold_catmap_step(img)
[rows, columns, numberOfColorChannels] = size(img);
oldScrambledImage = img;
% The number of iterations needed to restore the image can be shown never to exceed 3N.
N = rows;
% 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
end
i think it does what you suggested.
But when i run this code:
clc;
clear all;
img = imread('1.png');
if size(img,3) > 1; img = rgb2gray(img); end
[rows, columns] = size(img);
A = reshape(1:(rows*columns), rows, columns);
C = arnold_catmap_step(A);
row_of_interest = randi(rows)
col_of_interest = randi(columns)
idx = sub2ind(size(img), row_of_interest, col_of_interest);
N = rows;
maxsteps = 3*N;
idxhistory = zeros(1, maxsteps+1);
idxhistory(1) = idx;
I = img;
IA = A;
for iteration = 1 : maxsteps
I = I(C);
IA = IA(C);
idxhistory(iteration+1) = find(IA == idx);
end
[rowhistory, colhistory] = ind2sub(size(img), idxhistory);
plot(0:maxsteps, rowhistory, colhistory)
xlabel('step #'); ylabel('row'); zlabel('column')
i get this error:
Error using plot
Data must be a single input of y-values or one or more pairs of x- and y-values.
Error in followpixelcatmap (line 26)
plot(0:maxsteps, rowhistory, colhistory)
is my function correct and do you know why i get that error?
Why do you need to know the path each pixel takes while being scrambled? Are you just curiosity, or is there a real use case?
no purely for optical reasons ,i dont think it can have any real practical scientific use, i was just curious, but if you think it has any i would be very intrested to hear your take on the matter
i think the error is because it wants plot3 not plot
Yes, plot3() not plot(), sorry.
hey! do you have the matlab code for image scrambling and unscrambling using arnold cat map? i am working on colored images.

Sign in to comment.

Asked:

on 22 Jan 2019

Commented:

on 2 Feb 2024

Community Treasure Hunt

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

Start Hunting!