i need a matlab code for arnold cat map scrambling
古いコメントを表示
i need a matlab code for arnold cat map scrambling
4 件のコメント
amreena najeeb
2019 年 9 月 2 日
how arnold's cat map work?? on which principle????
Walter Roberson
2019 年 9 月 2 日
編集済み: Walter Roberson
2019 年 9 月 2 日
mamoona
2024 年 1 月 31 日
hey! do you have the matlab code for image scrambling and unscrambling using arnold cat map? i am working on colored images.
回答 (1 件)
Image Analyst
2019 年 9 月 2 日
1 投票
See attached demo.
28 件のコメント
lena kappa
2021 年 3 月 22 日
Awesome demo!!!
How can u save every iteration in a file?
Image Analyst
2021 年 3 月 23 日
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.
lena kappa
2021 年 3 月 23 日
編集済み: lena kappa
2021 年 7 月 6 日
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.
Image Analyst
2021 年 3 月 24 日
But why did you not uncomment the imwrite() line of code???
lena kappa
2021 年 3 月 24 日
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
Image Analyst
2021 年 3 月 24 日
What is the current folder name?
lena kappa
2021 年 3 月 24 日
if you mean the folder where i have the code its C:\Users\alexk\Desktop\mas\cat
Image Analyst
2021 年 3 月 24 日
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.
lena kappa
2021 年 3 月 25 日
no, i can save something there without an error
lena kappa
2021 年 3 月 25 日
i tried your code and it did save the pic moon.tif in the corect file
Image Analyst
2021 年 3 月 26 日
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.
lena kappa
2021 年 6 月 9 日
HI image analyst! Do you know how can i save the path that one pixel is following while the iterations are done?
Image Analyst
2021 年 6 月 10 日
No, I'd have to figure it out just like you would. Good luck.
Walter Roberson
2021 年 6 月 10 日
編集済み: Walter Roberson
2021 年 7 月 6 日
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')
lena kappa
2021 年 6 月 12 日
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??
Image Analyst
2021 年 6 月 12 日
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.
Walter Roberson
2021 年 6 月 12 日
Looks like I had an extra ) when I posted that line of code.
lena kappa
2021 年 6 月 13 日
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
Walter Roberson
2021 年 6 月 13 日
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])
image(img)
[r,c] = size(img)
C = reshape(randperm(r*c),r,c)
IA = reshape(1:(r*c), r, c)
I = img
for K = 1 : 4
I = I(C)
IA = IA(C)
figure(); image(I)
end
Here, I is the progressively scrambled image, and IA is the scrambling indices. C is acting as a permutation matrix.
lena kappa
2021 年 6 月 13 日
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?
Image Analyst
2021 年 6 月 13 日
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?
lena kappa
2021 年 6 月 13 日
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
lena kappa
2021 年 6 月 13 日
i think the error is because it wants plot3 not plot
Walter Roberson
2021 年 6 月 13 日
Yes, plot3() not plot(), sorry.
mamoona
2024 年 1 月 31 日
hey! do you have the matlab code for image scrambling and unscrambling using arnold cat map? i am working on colored images.
Walter Roberson
2024 年 1 月 31 日
The code is posted above.
mamoona
2024 年 2 月 2 日
thank u got it !!
カテゴリ
ヘルプ センター および File Exchange で MATLAB Mobile Fundamentals についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!




